I am new to python/coding so please bare with me. I would like to analyse a forex pair at various time periods and then combine columns back into the one dataframe based on date. For example id like to analyse 00:00- 07:00 as a time period using 7 hourly bars and look at different indicators/stats , then just say 07:00-09:00 as another period and the overall day bar and 3rd period. Each of these 3 periods would have there own column and stats but have the same date. Ive done afew courses on qauntra so far but I am not sure how to create this. Perhaps have 3 different dataframes and then concat?
Any advise from the delevopers or folk on here be very appreicated.
Hi Stephen,
You can start with fetching a high frequency data (like minute or hour) and then aggregate the values for the desired time interval.
One way, like you said, can be storing the values in different dataframes and then joining them. A more efficient way would be to just create one dataframe with the dates as index and then simply add columns to this dataframe with the aggreated value.
Hope this helps.
Satyapriya can you show can example? upload a code , I am new to python and this would be a great help
Stephen, This is an interesting question and will help to analyze how the price is changing in different time-frame. I'll give you the framework below with sample codes that you can modify based on your requirements.
Get the data: I have used a free source (yahoo finance) to get minute level data. If you need the data for a longer time range then you need to get it from some paid source. The interval selected is 5 minutes which can be changed as required.
import yfinance as yf
bars5m = yf.download('AAPL',auto_adjust=True,interval='5m',period='60')
bars5m.head()
Resample data: Here you resample the 5 minutes data to different frequency such as 1-hour, and 2-hour.
ohlcv_dict = {
'Open': 'first',
'High': 'max',
'Low': 'min',
'Close': 'last',
'Volume': 'sum'
}
bars1h = bars5m.resample('1H', closed='right', label='right').agg(ohlcv_dict).dropna()
bars2h = bars5m.resample('2H', closed='right', label='right').agg(ohlcv_dict).dropna()
Get the Last N timebars:
You need to specify the time and get past N time bars which can be used for calculation. For example, you can specify the time as 9-Jun-2021 15:00 and the time bar as 3. You will get 3 candles from bars5m, bars1h and bars2h
def get_last_N_timebars(bars5m, bars1h, bars2h, curr_time, lkbk):
"""---creating the timebars based on the lookback---"""
last5m = bars5m[:curr_time].iloc[-lkbk:]
last1h = bars1h[:curr_time].iloc[-lkbk:]
last2h = bars2h[:curr_time].iloc[-lkbk:]
'''---Making sure that window lengths agree with lookback---'''
try:
assert(len(last5m) == lkbk)
assert(len(last1h) == lkbk)
assert(len(last2h) == lkbk)
except Exception as e:
print('****Window length too short****')
return last5m, last1h, last2h</code></pre>
Compute Tech Indicator or Stats
Once you receive the required number of candles for 5m, 1h and 2h duration, you can compute technical indicator such as RSI.
curr_time = datetime.datetime(2021, 6, 9, 15, 0, 0)
lkbk = 3
last5m, last1h, last2h = get_last_N_timebars(bars5m, bars1h, bars2h, curr_time, lkbk)
RSI5m = ta.RSI(last5m.Close,lkbk-1)
RSI1h = ta.RSI(last1h.Close,lkbk-1)
RSI2h = ta.RSI(last2h.Close,lkbk-1)
Instead of storing the RSI values in different variables, you can store these values in pandas DataFrame with curr_time as the index. You can repeat this exercise for different times by changing the value in curr_time. At the end, you will have a dataframe with tech indicator or other stats (RSI in example) for different frequency in columns and time where they are valid as an index.
I hope this helps.