Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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,

    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you I will give this a try!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by futtrader, 04-21-2024, 01:50 AM
      4 responses
      41 views
      0 likes
      Last Post futtrader  
      Started by Option Whisperer, Today, 09:55 AM
      1 response
      11 views
      0 likes
      Last Post bltdavid  
      Started by port119, Today, 02:43 PM
      0 responses
      7 views
      0 likes
      Last Post port119
      by port119
       
      Started by Philippe56140, Today, 02:35 PM
      0 responses
      7 views
      0 likes
      Last Post Philippe56140  
      Started by 00nevest, Today, 02:27 PM
      0 responses
      7 views
      0 likes
      Last Post 00nevest  
      Working...
      X