Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Questions about moving stop to break even and plotting stop loss

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

    Questions about moving stop to break even and plotting stop loss

    Hello,

    New ninja dev here. I'm having some issues with what should be very basic functionality. First, I'd like to be able to move my stop to either break even or profit after going into profit by x number of ticks. Second, I'd like to plot the stop loss. The code I'm using is as follows in the onBarUpdate() method.

    Code:
               if (Position.MarketPosition == MarketPosition.Short) {
                    OrderCoord = Math.Round(Position.AveragePrice - (LongStopTicks * TickSize),2);
                    Draw.Line(this, "LPT-"+CurrentBar, true, CurrentBar - OrderSBar, OrderCoord, -1, OrderCoord, Brushes.Red, DashStyleHelper.Solid, 1);
                    Print("Short stop loss should be " + OrderCoord);
                } else if (Position.MarketPosition == MarketPosition.Long) {
                    OrderLCoord = Math.Round(Position.AveragePrice - (LongStopTicks * TickSize),2);
                    Print("Long stop loss should be " + OrderLCoord);
                    Draw.Line(this, "LPT-"+CurrentBar, true, CurrentBar - OrderLBar, OrderLCoord, -1, OrderLCoord, Brushes.Red, DashStyleHelper.Solid, 1);
                }
    
                if (Position.MarketPosition == MarketPosition.Long)
                    if (Close[0] >= Position.AveragePrice + BreakEvenAfter * TickSize) // BreakEvenAfter is set to 10
                        ExitLongStopMarket(Position.Quantity, Position.AveragePrice);
                        OrderLCoord = Position.AveragePrice;
    
                if  (Position.MarketPosition == MarketPosition.Short)
                    if (Close[0] <= Position.AveragePrice - BreakEvenAfter * TickSize)
                        ExitShortStopMarket(Position.Quantity, Position.AveragePrice);
                        OrderCoord = Position.AveragePrice;​
    Click image for larger version  Name:	ninjaquestions.png Views:	0 Size:	42.0 KB ID:	1224752
    As you can see from the screenshot above, the stop is not being moved to BE after going 10 ticks into profit and the stop loss is never in the correct spot.

    Thanks.
    Last edited by sonnygrapples; 11-22-2022, 08:00 PM.

    #2
    Hello sonnygrapples,

    One problem here is that you have structured your if conditions incorrectly. You are not using curly braces so only the first action is going to apply toward the condition.

    This:

    Code:
    if (Position.MarketPosition == MarketPosition.Long)
       if (Close[0] >= Position.AveragePrice + BreakEvenAfter * TickSize) // BreakEvenAfter is set to 10
          ExitLongStopMarket(Position.Quantity, Position.AveragePrice);
    OrderLCoord = Position.AveragePrice;


    Should be like this:

    Code:
    if (Position.MarketPosition == MarketPosition.Long)
    {
        if (Close[0] >= Position.AveragePrice + BreakEvenAfter * TickSize) // BreakEvenAfter is set to 10
       {
          ExitLongStopMarket(Position.Quantity, Position.AveragePrice);
          OrderLCoord = Position.AveragePrice;
       }
    }
    When using if statements you need to add the curly braces if the if statement has more than 1 action so that all code that should apply toward that statement is executed at the right time.

    You can find a simple sample of creating a break even in the following link,

    Comment


      #3
      Thank you I will give this a try!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      45 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      21 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      31 views
      1 like
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      50 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      42 views
      0 likes
      Last Post CarlTrading  
      Working...
      X