Bar data N days back
-
I want to access the data N days ago. I have written the following code
def next(self): print('next:: current period:', len(self)) print('date %s, current price %.2f, previous price %.2f' % (self.datas[0].datetime.datetime(), self.data.close[0], self.data.close[-1]))
the dataset I use consists of 3 lines
2018-01-01,50.1,... 2018-01-02,50.2,... 2018-01-03,50.3,...
and the output is
next:: current period: 1 date 2018-01-01 23:59:59.999989, current price 50.10, previous price 50.30 next:: current period: 2 date 2018-01-02 23:59:59.999989, current price 50.20, previous price 50.10 next:: current period: 3 date 2018-01-03 23:59:59.999989, current price 50.30, previous price 50.20
How can day 1 previous price be 50.30? Why it takes the value of the last day and doesn't throws an error?
-
Because the data is preloaded and the magic of array arithmetic in Python does the rest ...
@sspy said in Bar data N days back:
I want to access the data N days ago. I have written the following code
You may want to do it better: Docs - Platform Concepts - See: Lines - Delayed Indexing
-
@backtrader said in Bar data N days back:
Because the data is preloaded
Can this behavior be disabled? It really confuses me.
-
@sspy said in Bar data N days back:
Can this behavior be disabled? It really confuses me.
You are simply doing things wrong. But you can disable preloading: Docs - Cerebro
-
@backtrader said in Bar data N days back:
You are simply doing things wrong. But you can disable preloading: Docs - Cerebro
Thank you for your prompt reply. Finally I was able to get it right.
The only moment by disabling preloading I was able to get
next:: current period: 1 date 2018-01-01 23:59:59.999989, current price 50.10, previous price 50.10
it's certainly better, but still not how it should
-
I'm completely confused. Is there a simple way to get data N days back?
-
It would seem you are unaware of the Python arithmetic with arrays.
As quoted above ..
@backtrader said in Bar data N days back:
You may want to do it better: Docs - Platform Concepts - See: Lines - Delayed Indexing
Using a delayed index provides the proper automatic constraint to the platform.
You really want to read the document linked above and also Docs - Operating the Platform
-
@backtrader said in Bar data N days back:
It would seem you are unaware of the Python arithmetic with arrays.
I understand the python arrays but this logic is unacceptable here, because we can't get future data, so the -1 has to be the previous day, -2 the day before it and so on and if no such day is available to throw an exception, but not start everything in a circle.
@backtrader said in Bar data N days back:
Using a delayed index provides the proper automatic constraint to the platform.
Can you show me an example of proper way to get data for 2 days back or exeption if no such data is available?
-
I was able to find a solution,
self.data.close.get(size=1, ago=-1)[0]
works right as I want