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 Mindset, 04-21-2026, 06:46 AM
    0 responses
    87 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    132 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    68 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    118 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    67 views
    0 likes
    Last Post PaulMohn  
    Working...
    X