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 Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        666 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        376 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        110 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        575 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        580 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X