1. The strategy codes have 2 methods under it: OnStateChange() and OnBarUpdate(). From reading the help guides, it seems that everything in OnStateChange is run only once when strategy is started. I notice that indicator instances are coded under State.DataLoaded, where it's updated only once after data series are loaded. I don't understand how does the strategy run real-time calculations on the indicators, since they are not updated with bar updates?
I need to understand the executing order of when OnStateChange() and OnBarUpdate(), for below reason:
a) I need to move stop loss value with current price change. SetStopLoss() is under State.Dataloaded by default. I'm not sure if I need to move the code to be under OnBarUpdate(), since my strategy needs to make real-time decision to update stop loss value when price goes up & down. Not sure if SetStopLoss() is under State.Dataloaded will work.
b) I need to calculate real-time moving average difference for different data series. For example, I have 2 instances: EMA1 = EMA(Close, 21); EMA2 = EMA(Close, 60); I need to create a new variable Diff: Diff = EMA1 - EMA2; This will be used in my conditions. I'm not sure where I should put it. In OnBarUpdate() or State.Dataloaded?
2. Question about ATR indicator. My strategy runs on 1 minute data series. I added addtional 30 minute data series. When I call ATR on 30 minute, what should the input be? For example, I know when I call EMA, it's EMA(Closes[1], 60), since EMA calculates based on close price. But ATR is based on several price series for calculation, so what should the method input parameter be?
I know this is a long question. I really appreciate it if you can elaborate in details, so that I can have a good idea of how everything works.
Thank you very much!
Comment