Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnBarUpdate order execution

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    OnBarUpdate order execution

    Dear team,

    I just notice that when I use an Indicator declared inside a strategy, every time I try to make a simulation with Strategy Analyzer, I see this behavior:

    1. first all the calls to OnBarUpdate() for Strategy class for the period considered
    2. then calls to the OnBarUpdate() for the indicator.

    But for my has no sense as Strategy would make decisions based on Indicator status, I mean, both OnBarUpdate() calls should be called for both within the same bar (actually I'd prefer to call to all indicator before call Strategy to have fresh data.

    What I missing?

    I 'd tried to instantiate indicator on Configure, DataLoaded, Historical, with the same results.

    Thanks in advance
    --
    Asterio

    #2
    Hello asterio,
    Thanks for your post.

    You are correct, the strategies OnBarUpdate() would be called first and then the indicators OnBarUpdate() would be called after that. If you require the indicators value to be updated before processing any logic that references that indicators value you can use Update(). Something similar to the following snippet would instantiate an SMA indicator and then force it to update before processing further logic in the strategies OnBarUpdate().

    Code:
    private SMA mySMA;
    protected override void OnStateChange()
    {
         if (State == State.DataLoaded)
         {
               mySMA = SMA(100);
         }
    }
    
    protected override void OnBarUpdate()
    {
    	mySMA.Update();
    	if (mySMA[0] < Close[0])
    	{
    		//do something
    	}
    }
    I am including a link to the help guide documentation for Update(), for your convenience.

    Update()
    https://ninjatrader.com/support/help...us/?update.htm

    Please let me know if you have any questions.
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      Thats works perfect!

      I also notice that indicators are called for OnStateChange: Historical before calling OnBarUpdate().
      That's nice.

      Here is a output working with 1 strategy and 2 indicators using your suggestion.

      >> Strategy is running a regular backtest
      [vT2] : OnStateChange: SetDefaults
      [vT2] : OnStateChange: Configure
      [vOB] : OnStateChange: SetDefaults
      [vOB] : OnStateChange: Configure
      13/03/2018 23:01:00: [vStrategy] : OnStateChange: DataLoaded | Backtest
      13/03/2018 23:01:00: [vT2] : OnStateChange: DataLoaded
      13/03/2018 23:01:00: [vOB] : OnStateChange: DataLoaded
      13/03/2018 21:57:00: [vStrategy] : OnStateChange: Historical | Backtest
      14/03/2018 15:30:00: [vStrategy] : OnBarUpdate from vStrategy
      13/03/2018 21:57:00: [vT2] : OnStateChange: Historical
      14/03/2018 15:30:00: [vT2] : OnBarUpdate from vT2
      13/03/2018 21:57:00: [vOB] : OnStateChange: Historical
      14/03/2018 15:30:00: [vOB] : OnBarUpdate from vOB
      14/03/2018 15:31:00: [vStrategy] : OnBarUpdate from vStrategy
      14/03/2018 15:31:00: [vT2] : OnBarUpdate from vT2
      14/03/2018 15:31:00: [vOB] : OnBarUpdate from vOB
      14/03/2018 15:32:00: [vStrategy] : OnBarUpdate from vStrategy
      14/03/2018 15:32:00: [vT2] : OnBarUpdate from vT2
      14/03/2018 15:32:00: [vOB] : OnBarUpdate from vOB
      14/03/2018 15:33:00: [vStrategy] : OnBarUpdate from vStrategy
      14/03/2018 15:33:00: [vT2] : OnBarUpdate from vT2
      14/03/2018 15:33:00: [vOB] : OnBarUpdate from vOB
      14/03/2018 15:34:00: [vStrategy] : OnBarUpdate from vStrategy
      14/03/2018 15:34:00: [vT2] : OnBarUpdate from vT2
      14/03/2018 15:34:00: [vOB] : OnBarUpdate from vOB
      [vStrategy] : OnStateChange: SetDefaults | NinjaScript
      [vStrategy] : OnStateChange: Terminated | Backtest
      13/03/2018 23:01:00: [vStrategy] : OnStateChange: Terminated | NinjaScript
      13/03/2018 23:01:00: [vT2] : OnStateChange: Terminated
      13/03/2018 23:01:00: [vOB] : OnStateChange: Terminated


      where you can notice how both indicators are update in the same bar as strategy.

      Thanks JoshG!!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by jeronymite, Today, 08:43 PM
      0 responses
      2 views
      0 likes
      Last Post jeronymite  
      Started by jeronymite, Today, 07:01 PM
      0 responses
      7 views
      0 likes
      Last Post jeronymite  
      Started by LeitoGTR, Today, 06:41 PM
      0 responses
      7 views
      0 likes
      Last Post LeitoGTR  
      Started by 2inthebush, Today, 03:29 PM
      2 responses
      13 views
      0 likes
      Last Post 2inthebush  
      Started by forrestang, Today, 02:17 PM
      5 responses
      25 views
      0 likes
      Last Post NinjaTrader_ChrisL  
      Working...
      X