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, 05-11-2026, 05:56 AM
      0 responses
      68 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 05-10-2026, 08:12 PM
      0 responses
      43 views
      0 likes
      Last Post CarlTrading  
      Started by Hwop38, 05-04-2026, 07:02 PM
      0 responses
      202 views
      0 likes
      Last Post Hwop38
      by Hwop38
       
      Started by CaptainJack, 04-24-2026, 11:07 PM
      0 responses
      366 views
      0 likes
      Last Post CaptainJack  
      Started by Mindset, 04-21-2026, 06:46 AM
      0 responses
      285 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Working...
      X