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 NullPointStrategies, Today, 05:17 AM
      0 responses
      46 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      126 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      66 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      42 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      46 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X