mirror of https://github.com/01-edu/Branch-AI.git
Badr Ghazlane
3 years ago
3 changed files with 48 additions and 1 deletions
@ -0,0 +1,16 @@ |
|||||||
|
# Exercise 2 |
||||||
|
|
||||||
|
The goal of this exercise is to learn to use Pandas on Time Series an on Financial data. |
||||||
|
|
||||||
|
The data we will use is Apple stock. |
||||||
|
|
||||||
|
1. Using `Plotly` plot a Candlestick |
||||||
|
|
||||||
|
2. Aggregate the data to **last business day of each month**. The aggregation should consider the meaning of the variables. How many months are in the considered period ? |
||||||
|
|
||||||
|
3. When comparing many stocks between them the metric which is frequently used is the return of the price. The price is not a convenient metric as the prices evolve in different ranges. The return at time t is defined as |
||||||
|
|
||||||
|
- (Price(t) - Price(t-1))/ Price(t-1) |
||||||
|
|
||||||
|
Using the open price compute the **daily return**. Propose two different ways **without for loop**. |
||||||
|
|
@ -0,0 +1,31 @@ |
|||||||
|
# Exercise 3 Multi asset returns |
||||||
|
|
||||||
|
The goal of this exercise is to learn to compute daily returns on a DataFrame that contains many assets (multi-assets). |
||||||
|
|
||||||
|
```python |
||||||
|
business_dates = pd.bdate_range('2021-01-01', '2021-12-31') |
||||||
|
|
||||||
|
#generate tickers |
||||||
|
tickers = ['AAPL', 'FB', 'GE', 'AMZN', 'DAI'] |
||||||
|
|
||||||
|
#create indexs |
||||||
|
index = pd.MultiIndex.from_product([business_dates, tickers], names=['Date', 'Ticker']) |
||||||
|
|
||||||
|
# create DFs |
||||||
|
market_data = pd.DataFrame(index=index, |
||||||
|
data=np.random.randn(len(index), 1), |
||||||
|
columns=['Price']) |
||||||
|
``` |
||||||
|
|
||||||
|
1. **Without using a for loop**, compute the daily returns (return(d) = (price(d)-price(d-1))/price(d-1)) for all the companies and returns a DataFrame as: |
||||||
|
|
||||||
|
| Date | ('Price', 'AAPL') | ('Price', 'AMZN') | ('Price', 'DAI') | ('Price', 'FB') | ('Price', 'GE') | |
||||||
|
|:--------------------|--------------------:|--------------------:|-------------------:|------------------:|------------------:| |
||||||
|
| 2021-01-01 00:00:00 | nan | nan | nan | nan | nan | |
||||||
|
| 2021-01-04 00:00:00 | 1.01793 | 0.0512955 | 3.84709 | -0.503488 | 0.33529 | |
||||||
|
| 2021-01-05 00:00:00 | -0.222884 | -1.64623 | -0.71817 | -5.5036 | -4.15882 | |
||||||
|
|
||||||
|
Note: The data is generated randomly, the values you may have a different results. But, this shows the expected DataFrame structure. |
||||||
|
|
||||||
|
`Hint use groupby` |
||||||
|
|
Loading…
Reference in new issue