Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Move stoplosses if trigger hits based on realtime price

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

    Move stoplosses if trigger hits based on realtime price

    I am developing a strategy that has entry/exit criteria based on values at the close of bar. For reference, the the code is something like the below below:

    protected override void OnBarUpdate()
    {
    if (CurrentBar < 1)
    return;

    double emaHigh = EMA(High, EmaPeriod)[0];
    double emaLow = EMA(Low, EmaPeriod)[0];

    if (Close[0] > emaHigh)
    { //entry code here...}
    else if (Close[0] < emaLow)
    { //entry code here...}

    }



    Under Set.defaults i am using Calculate = Calculate.OnBarClose

    I also have two other functions:

    1) CalculateUnrealizedPL()
    2) ModifyStopLoss() - This moves the stoploss to BE once the price hits a trigger value which is a certain amount into the green

    Both these functions are called in onMarketUpdate()

    protected override void OnMarketData(MarketDataEventArgs e)
    {
    if (e.MarketDataType != MarketDataType.Last || e.Instrument != Instrument)
    return;

    // Loop through positions and calculate unrealized P/L
    if (!stopLossesModified)
    {
    foreach (var position in Positions)
    {
    double unrealizedPL = CalculateUnrealizedPL(position);

    if (unrealizedPL >= BreakevenThreshold)
    {
    ModifyStopLosses(position);
    }
    }
    }
    }​


    The issue i am having is that it is only moving the stoploss if the candle closes beyond the trigger value, but i want it to move the stoploss immediately when it hits the trigger value. I know that if i cange calculate to OnEachTick it would do that, but then the entry/exit critera are affected when i change that. Is there a way that i can kep the onbarupdate code using calculate.onBarClose while using OnEachTick for the onmarket data conditions? Or any other way to achieve movement of stoploss based on real time touch of the trigger price instead of only after a bar close beyond it?​

    #2
    Hello sofortune

    From this snippet it would be impossible to see how the logic is working when you run the script, you would likely need to use a print here to see when your condition is becoming true. To move a stoploss in realtime you would need to call the order method again or use the ChangeOrder method depending on how you developed your script.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    39 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    124 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    64 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    41 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    46 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X