Let’s assume I’m using 1m timeframe and 5m informative timeframe. Before I start, the timestamp
that the exchanges gives are the open time of each candles. So if you have a candle with timestamp 00:00, that means that candle opens at 00:00 UTC. When it closes will depend on the timeframe used. Now I have this 1m candles’ data
Timestamp | Close value |
23:59 | 4 |
00:00 | 5 |
00:01 | 4 |
00:02 | 6 |
00:03 | 7 |
00:04 | 6 |
00:05 | 3 |
00:06 | 4 |
00:07 | 3 |
00:08 | 5 |
00:09 | 2 |
00:10 | 4 |
And this is my 5m informative candles’ data
Timestamp | Close value |
23:55 | 4 |
00:00 | 6 |
00:05 | 2 |
00:10 | 4 |
And this is how it would looks like in your dataframe after you merged them
Timestamp | close | close_5m |
00:00 | 5 | 4 |
00:01 | 4 | 4 |
00:02 | 6 | 4 |
00:03 | 7 | 4 |
00:04 | 6 | 6 |
00:05 | 3 | 6 |
00:06 | 4 | 6 |
00:07 | 3 | 6 |
00:08 | 5 | 6 |
00:09 | 2 | 2 |
00:10 | 4 | 2 |
Now, what if at 00:10 UTC, I want to know the average close value of the latest 3 candles? The correct way is to do it inside informative function, like this
@informative('5m')
def populate_indicators_5m(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
dataframe['avg_close_3'] = dataframe['close'].rolling(3).mean()
return dataframe
It will correctly use data from table 2 above, which will give you the latest value avg_close_3_5m of 4. But if you do the calculation inside any main populate functions, for example inside populate_indicators like this
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
dataframe['avg_close_3_5m'] = dataframe['close_5m'].rolling(3).mean()
return dataframe
It would use data from table 3, which means the avg_close_3_5m value calculated would be 4.666667, which is wrong.
That’s why, if you want to do any calculation using informative data, it’s better to do it inside each of the respective informative functions. Hopefully this article helps.
[…] How dataframe looked like after merging informative timeframe(s) […]