Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Breakeven after X ticks code

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

    Breakeven after X ticks code

    My long code is working. But when a short is executed, it immediately errors out saying that I can't put a stop loss above the market or something like that...

    Can someone check this code... does this look right?


    // Resets the stop loss to the original value when all positions are closed
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Ticks, 50);
    }

    // If a long position is open, allow for stop loss modification to breakeven
    else if (Position.MarketPosition == MarketPosition.Long)
    {
    // Once the price is greater than entry price by 8 ticks, set stop loss to BE + 2
    if (GetCurrentBid() > Position.AvgPrice + 8 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice + 2 * TickSize);
    }
    }

    else if (Position.MarketPosition == MarketPosition.Short)
    {
    // Once the price is less than entry price by 8 ticks, set stop loss to BE + 2
    if (GetCurrentBid() < Position.AvgPrice - 8 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice - 2 * TickSize);
    }
    }

    #2
    trend747, it looks like it should be working but perhaps the market is moving too fast. The only thing I can see that would be different is that you use the current bid price for both instead of bid for one and ask for the other.
    AustinNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Austin View Post
      trend747, it looks like it should be working but perhaps the market is moving too fast. The only thing I can see that would be different is that you use the current bid price for both instead of bid for one and ask for the other.
      I think you're right. It seems to occur whenever I have market replay turned up a lot.

      Comment


        #4
        Hello,

        There should be a reproducible time and date where this occurs where it shouldnt. Can you give me and example of this along with all settings your using to run the strategy so I can test this on my side.

        I will also need full code to support at ninjatrader dot com and reference this forum post and attach your full strategy so I can duplicate this on my side as I would not expect this to occur.

        I look forward to assisting you further.
        BrettNinjaTrader Product Management

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by charlesugo_1, 05-26-2026, 05:03 PM
        0 responses
        59 views
        0 likes
        Last Post charlesugo_1  
        Started by DannyP96, 05-18-2026, 02:38 PM
        1 response
        143 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 05-11-2026, 05:56 AM
        0 responses
        161 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 05-10-2026, 08:12 PM
        0 responses
        97 views
        0 likes
        Last Post CarlTrading  
        Started by Hwop38, 05-04-2026, 07:02 PM
        0 responses
        276 views
        0 likes
        Last Post Hwop38
        by Hwop38
         
        Working...
        X