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

Manipulating StopLoss

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

    Manipulating StopLoss

    The following code will set the stop loss to break-even, but it will not adjust the stop loss to a profit of 10 when it is up 25, nor a profit of 25 when it is up 50! Can anyone tell me why??


    // 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
    elseif (Position.MarketPosition == MarketPosition.Long)
    {
    // Once the price is greater than entry price+10 ticks, set stop loss to breakeven
    if (Close[0] > Position.AvgPrice + 10 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice);
    Print("Code has entered into a break-even situation");
    }
    }

    // If a long position is open, once the price is greater than entry price+25 ticks, set stop loss profit of 10
    elseif (Position.MarketPosition == MarketPosition.Long)
    {

    if (Close[0] > Position.AvgPrice + 25 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice +
    10);
    Print(
    "Code has entered into a minimum 10 PIP profit situation");
    }
    }

    // If a long position is open, // Once the price is greater than entry price+50 ticks, set stop loss profit of 25
    elseif (Position.MarketPosition == MarketPosition.Long)
    {

    if (Close[0] > Position.AvgPrice + 50 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice +
    25);
    Print(
    "Code has entered into a minimum 25 PIP profit situation");
    }

    #2
    Please provide more information. Is your code even getting inside the if-statements? Are those print statements triggering?

    I suspect not. You are using else-ifs. The way those work is if one of them is true the rest do not evaluate.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      In the output window, it does print "Code has entered into a break-even situation", but that is as far as it goes. Should I get rid of the else if?

      Comment


        #4
        Yup. Once one of them is evaluated to true the rest are essentially skipped.

        Right now you are doing this in english:

        If ____ do ____, or else if ____ do _____.

        "or else" does not happen unless the first "if" is false. If the "if" is true there is no "or else" to follow.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Is there a better way to adjust to a tighter stoploss as the profits increase?

          Comment


            #6
            You need to just submit the modifying orders when the proper conditions arise.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Would eliminating all the "else if"'s take care of that? (and just use "if"?)

              Comment


                #8
                Sure thing. If you remove the elses this is what it sounds like in english:

                if ____, do ____. if _____, do _____. It will just keep following the code sequentially.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks Josh!

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by AttiM, 02-14-2024, 05:20 PM
                  12 responses
                  211 views
                  0 likes
                  Last Post DrakeiJosh  
                  Started by cre8able, 02-11-2023, 05:43 PM
                  3 responses
                  236 views
                  0 likes
                  Last Post rhubear
                  by rhubear
                   
                  Started by frslvr, 04-11-2024, 07:26 AM
                  8 responses
                  114 views
                  1 like
                  Last Post NinjaTrader_BrandonH  
                  Started by stafe, 04-15-2024, 08:34 PM
                  10 responses
                  47 views
                  0 likes
                  Last Post stafe
                  by stafe
                   
                  Started by rocketman7, Today, 09:41 AM
                  3 responses
                  11 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Working...
                  X