Announcement

Collapse
No announcement yet.

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.​​

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    63 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    139 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    75 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    45 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    50 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X