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

Moving multiple stop Loss Orders

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

    Moving multiple stop Loss Orders

    Hi there. Trying to move multiple stop losses in a strategy (effectively using them as a trailing stop). The problem is I seem to get looping. Moving the first one to breakeven works ok but then the next ones are looping so basically in the log you see the stops being moved continuously as orders. So I modified the example here with just a single order and the same thing happens. The first one to BE works when the market moves up 30 ticks but when the market moves up 40 or 60 ticks the stop moves but in a continuous loop. Any ideas what is causing this would be appreciated. Thanks
    Attached Files
    Last edited by djkiwi; 11-23-2020, 07:43 PM.

    #2
    Here is the example code for the multiple orders. Both behave in this looping fashion.

    {
    EnterLongLimit(Convert.ToInt32(DefaultQuantity), GetCurrentBid(), @"Long1");
    EnterLongLimit(Convert.ToInt32(DefaultQuantity), GetCurrentBid(), @"Long2");
    EnterLongLimit(Convert.ToInt32(DefaultQuantity), GetCurrentBid(), @"Long3");
    }

    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Ticks, Stoploss1);
    SetStopLoss(CalculationMode.Ticks, Stoploss2);
    SetStopLoss(CalculationMode.Ticks, Stoploss3);
    }


    else if


    ((Position.MarketPosition == MarketPosition.Long)
    && (Close[0] > Position.AveragePrice+10 * TickSize))

    {
    SetStopLoss(@"Long1",CalculationMode.Price, Position.AveragePrice,false);
    SetStopLoss(@"Long2",CalculationMode.Price, Position.AveragePrice,false);
    SetStopLoss(@"Long3",CalculationMode.Price, Position.AveragePrice,false);
    Print(Time[0].ToString() + " not first tick of bar Long");
    }

    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Ticks, Stoploss1);
    SetStopLoss(CalculationMode.Ticks, Stoploss2);
    SetStopLoss(CalculationMode.Ticks, Stoploss3);
    }

    else if ((Position.MarketPosition == MarketPosition.Long)
    && (Close[0] > Position.AveragePrice+30 * TickSize))

    {
    //SetTrailStop(@"Long1", CalculationMode.Ticks, 12,false);
    //SetTrailStop(@"Long1", CalculationMode.Ticks, 12,false);
    //SetTrailStop(@"Long1", CalculationMode.Ticks, 12,false);

    SetStopLoss(@"Long1",CalculationMode.Price, Position.AveragePrice+10*TickSize,false);
    SetStopLoss(@"Long2",CalculationMode.Price, Position.AveragePrice+10*TickSize,false);
    SetStopLoss(@"Long3",CalculationMode.Price, Position.AveragePrice+10*TickSize,false);
    Print(Time[0].ToString() + " second");
    }



    // If a long position is open, allow for stop loss modification to breakeven


    //2. Shorts

    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(@"Long1", CalculationMode.Ticks, Stoploss1, false);
    SetStopLoss(@"Long2", CalculationMode.Ticks, Stoploss2, false);
    SetStopLoss(@"Long3", CalculationMode.Ticks, Stoploss3, false);
    SetStopLoss(@"Short1", CalculationMode.Ticks, Stoploss1, false);
    SetStopLoss(@"Short2", CalculationMode.Ticks, Stoploss2, false);
    SetStopLoss(@"Short3", CalculationMode.Ticks, Stoploss3, false);
    }

    Comment


      #3
      Hello djkiwi,

      Thank you for the post.

      When you say that the stop moves in a loop what do you mean? Are you seeing that its moved multiple times? Are you expecting that it only happens one time?

      The code as you have it should all happen at once when the price increases, you are not using any else statements with your if conditions. If the price is + 60 * TickSize you will also be setting the stop for the 40 and 60 because those are true as well at that point.

      You do have one else statement but its only going to apply to a single one of the if statements below it, you are not using any curly braces to contain the code you want the else to apply to. If and else statements will only apply to the following 1 line if you don't use { }


      I look forward to being of further assistance.
      JesseNinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by chbruno, Today, 04:10 PM
      0 responses
      3 views
      0 likes
      Last Post chbruno
      by chbruno
       
      Started by josh18955, 03-25-2023, 11:16 AM
      6 responses
      436 views
      0 likes
      Last Post Delerium  
      Started by FAQtrader, Today, 03:35 PM
      0 responses
      6 views
      0 likes
      Last Post FAQtrader  
      Started by rocketman7, Today, 09:41 AM
      5 responses
      19 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by frslvr, 04-11-2024, 07:26 AM
      9 responses
      127 views
      1 like
      Last Post caryc123  
      Working...
      X