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

Executing OnBarUpdate conditions only once

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

    Executing OnBarUpdate conditions only once

    1. I have a calculation in my code that gets me a value , let's say : If cross above then take current price minus 10 and store this value in a variable. Put a buy limit order at this value.
    The first time when is crossed above my code makes the calculation and put's a buy limit at the specified value. (current minus 10).
    On the next bar (or tick) current price is different and my buy limit order has a different value now. ( the new current price minus 10) .
    But i dont want that. I want to make the calculation first time when conditions are met , put my buy order at that value and keep it there with the same value until i say otherwise.

    protected override void OnBarUpdate()
    {
    if(State == State.Historical)
    {
    return;
    }
    //Add your custom strategy logic here.

    if( Position.MarketPosition == MarketPosition.Flat
    && EMA(12)[1] > EMA(26)[1])

    {
    LimitPrice = EMA(26)[0]; // or LimitPrice = Close[0] - 10
    EnterLongLimit(LimitPrice,"long");
    }

    Let's take the code above as an example. When the conditions are met and the code executes for the first time the LimitPrice has a value given by the slow EMA. I want to keep this value , the first value that goes into LimitPrice until i say otherwise (until ema fast is below ema slow let's say).

    ​​

    #2
    Hello cosmin1ke,

    Thank you for your post.

    To make sure that the value of LimitPrice is only saved once (for the first time) you could set up a bool variable. The bool could default to false, then once the Entry is submitted it could be set to true. Next, once the position becomes flat again you could reset the bool back to false to track the LimitPrice for the next entry condition. In order to keep the EnterLongLimit order alive, you have to separate the conditions a bit. Here is a modification of your snippet that demonstrates this idea:


    Code:
    // declared at class level
    private double LimitPrice;
    private bool limitSaved;
    
    protected override void OnStateChange()
    {
    else if (State == State.SetDefaults)
    {​​
    // set default value for limitSaved to false
    limitSaved = false;
    }
    
    protected override void OnBarUpdate()
    {
    if(State == State.Historical)
    {
    return;
    }
    //Add your custom strategy logic here.
    
    // when the desired EMA conditions are hit and there is not currently a limit saved, save the LimitPrice and set limitSaved to true so this will only occur once
    if(EMA(12)[1] > EMA(26)[1] && limitSaved == false)
    {
    LimitPrice = EMA(26)[0]; // or LimitPrice = Close[0] - 10
    limitSaved = true;
    }​
    
    // if the position is flat and the desired EMA entry conditions are true, EnterLongLimit with the saved LimitPrice
    if(Position.MarketPosition == MarketPosition.Flat)
    && EMA(12)[1] > EMA(26)[1])
    {
    EnterLongLimit(LimitPrice,"long");
    }​
    
    // reset limitSaved to false when the position becomes flat again so you can save the new LimitPrice when the condition is hit again
    ​if (Position.MarketPosition == MarketPosition.Flat && limitSaved == true)
    {
    limitSaved = false;
    }
    Please let us know if we may be of further assistance.​​
    Emily C.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by ageeholdings, Today, 07:43 AM
    0 responses
    7 views
    0 likes
    Last Post ageeholdings  
    Started by pibrew, Today, 06:37 AM
    0 responses
    4 views
    0 likes
    Last Post pibrew
    by pibrew
     
    Started by rbeckmann05, Yesterday, 06:48 PM
    1 response
    14 views
    0 likes
    Last Post bltdavid  
    Started by llanqui, Today, 03:53 AM
    0 responses
    6 views
    0 likes
    Last Post llanqui
    by llanqui
     
    Started by burtoninlondon, Today, 12:38 AM
    0 responses
    12 views
    0 likes
    Last Post burtoninlondon  
    Working...
    X