Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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()
    {
    	[B]mySMA.Update();[/B]
    	if (mySMA[0] < Close[0])
    	{
    		[COLOR="SeaGreen"]//do something[/COLOR]
    	}
    }
    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 elderan, Today, 08:03 PM
      0 responses
      2 views
      0 likes
      Last Post elderan
      by elderan
       
      Started by algospoke, Today, 06:40 PM
      0 responses
      10 views
      0 likes
      Last Post algospoke  
      Started by maybeimnotrader, Today, 05:46 PM
      0 responses
      8 views
      0 likes
      Last Post maybeimnotrader  
      Started by quantismo, Today, 05:13 PM
      0 responses
      7 views
      0 likes
      Last Post quantismo  
      Started by AttiM, 02-14-2024, 05:20 PM
      8 responses
      169 views
      0 likes
      Last Post jeronymite  
      Working...
      X