How does the code run in bt.strategy
-
I am quite new to programming and backtrader, what I know is code runs from top to the bottom, I have very basic understanding of "calling" classes, loops and ifs conditions.
My question is down below
I have a strategy class that contains:
1. def log(self, txt, dt=None): #Logging Function
2. def next(self):
- Self-coded ATR function to define self.atr - ``` self.log('Close: %.2f , ATR:%.2f ,Trailing:%.2f'% (self.dataclose[0],(self.atr*1), (self.dataclose[-1]-(self.atr*1)))) ```
3. def notify_order(self,order):
self.log("LONG BUY EXECUTED,Price: %.2f, Quantity: %.2f, Cost: %.2f, Comm%.2f, ATR: %.2f" % (order.executed.price, (order.executed.value/ order.executed.price), order.executed.value, order.executed.comm, self.atr*1))
4. def notify_trade(self, trade): #to log the pnl of the most recent trade completed
.
.
Here is the structure:
My problem here is
why does "3. def notify_order(self,order):" gets executed first before "2. def next(self):", I have a self-coded atr function that calculates the ATR and when there is an ongoing order, it will print out the previous ATR.As you can see here the ATR on 2020-02-10 is 1.62,
an order was created on that day and the order was filled in on the next day whic h is 2020-02-11. But the "LONG BUY EXECU....." prints out the ATR of 02-10, instead of 02-11.
And you can also see that the actual atr of 02-11 was printed after the notify_order function which is 1.61.
-
Please read backtrader docs from the very beginning bro the very end. All your questions are answered their.
-
@ab_trader said in How does the code run in bt.strategy:
Please read backtrader docs from the very beginning bro the very end. All your questions are answered their.
I tried to a few times, but a lot of things I can't really understand.
I've been learning through the quickstart guide and follow-along videos on youtube so I can learn by doing it.Can you point me out on which part of the docs will help with my answer?
-
@Gleetche said in How does the code run in bt.strategy:
Can you point me out on which part of the docs will help with my answer?
At this stage you need to invest the time to learn the software. Start here. https://www.backtrader.com/docu/concepts/ And walk your way forward using the built examples.
Backtrader assumes a certain level of programming ability and doesn't attempt to teach you that. So you are going to have to grind it out. Be prepared for some stuff not to work, but once you've got the basic elements under your belt, come back and we'll get you going.
Good luck!
-
@Gleetche said in How does the code run in bt.strategy:
Can you point me out on which part of the docs will help with my answer?
Also Quickstart Guide gives full understanding of the
bt
workflow steps.Answering your question about ATR -
notify_order()
prints the last delivered ATR value, which is the value from the 02-10. -
thank you @ab_trader @run-out