Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Break Even Stop after 1st profit target for 2nd entry + Trailing Stop for Runner

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

    Break Even Stop after 1st profit target for 2nd entry + Trailing Stop for Runner

    Hello everyone,

    So I've spent the whole day yesterday trying to figure this out, watching videos (not a lot that address this though), and browsing through forums. So far, no luck. Hopefully someone here can help me. I'm not good with code, so I've been just using the builder thus far.

    So, I have a strategy with 3 entries after it crosses a specific area (SSL Channel).

    LongEntry1: ProfitTarget1 - 2 Points, StopLoss1 - 2 Points.
    LongEntry2: ProfitTarget2- 5 points, StopLoss2 - 4 Points
    LongEntry3 (Runner): ProfitTarget3 - 20 Points, StopLoss3 - 4 Points

    How would I go about doing the following:

    Once the price crosses the channel, all 3 positions enter LONG or SHORT. Once ProfitTarget1 is reached, LongEntry 2 and 3 automatically adjust their Stop Loss to 7 ticks behind ProfitTarget1 (1 tick above Entry price).

    Once ProfitTarget2 is reached at 5 Points, all that remains is the Runner. I'd like for the Runner to have a trailing stop that adjusts after x amount of ticks or something like that.

    Is this hard to do? Please let me know if you can provide the general steps. I'm currently testing this strategy in Playback Mode.

    Thanks a lot!

    #2
    Hello ujin765,

    Thank you for your note.

    While this would be possible with the Strategy Builder, something like this is going to be pretty complex to achieve, and would be considerably simpler to manually code.

    If you're wanting stops and targets to be able to be modified, you would need to use Exit orders in the Conditions and Actions screen instead of the options on the Stops and Targets screen, as those can't be changed once placed using the Strategy Builder.

    I'm attaching a pair of relatively simple examples that will either move a stop to breakeven or create a trailing action that you can use as a starting point.

    You'll also want to give your entry orders names so you can link them to specific exit orders, and use Unique Entries for your Entry Handling setting with Entries Per Direction set to 1, so it will enter once for each uniquely named order.

    If you do decide to go the manual coding route, you would be able to use SetStopLoss and SetProfitTarget instead of Exit methods. This page of our help guide has an example of how you can modify a stop or target when manually coding:



    Please let us know if we may be of further assistance to you.

    Attached Files

    Comment


      #3
      Hey Kate,

      Thanks for responding and providing those files! So, I was able to almost completely build the strategy as I intended, with one small issue remaining. The trailing stop doesn't seem to trail for some reason. If you could glance at the code, and tell me what I may be missing, I'd greatly appreciate it.

      For some reason it gives me an error when trying to upload the strategy file, so I'll just copy paste it below.

      So, all the entries meet their respective profit targets, including the runner with TP4 (if it hits it). The TrailTriggerPrice gets activated once price hits TP3, with a TrailStopLoss at -20ticks. But as the price moves up, that code must be faulty cause the TrailStopPrice doesn't readjust again after that. So, either the Runner hits TP4, or it comes back down and gets stopped out at TP3 -20ticks. The runner code starts at Set 8 of the builder.

      BreakEvenTrigger = 16;
      InitialStopDistance = -15;
      InitialStopDistance2 = -20;
      TP1 = 16;
      TP2 = 20;
      TP3 = 40;
      TP4 = 80;
      TrailStopFrequency = 5;
      TrailStopDistance = -20;

      StopPrice = 0;
      TriggerPrice = 0;
      TriggerState = 0;
      SavedBar = 1;
      StopPrice2 = 0;
      TrailTriggerPrice = 0;
      TrailStopPrice = 0;
      }
      else if (State == State.Configure)
      {
      }
      else if (State == State.DataLoaded)
      {
      ChannelSSL1 = ChannelSSL(Close, 200);
      EMA1 = EMA(Close, 100);
      ChannelSSL2 = ChannelSSL(Close, 200);
      MACD1 = MACD(Close, 12, 26, 9);
      ChannelSSL1.Plots[0].Brush = Brushes.SpringGreen;
      ChannelSSL1.Plots[1].Brush = Brushes.Red;
      AddChartIndicator(ChannelSSL1);
      }
      }


      protected override void OnBarUpdate()
      {
      if (BarsInProgress != 0)
      return;

      // Set 1
      if ((TriggerState >= 2)
      && (Position.MarketPosition == MarketPosition.Flat))
      {
      TriggerState = 0;
      TrailStopPrice = 0;
      }

      if (CurrentBars[0] < 1)
      return;

      // Set 2
      if ((CrossAbove(High, ChannelSSL1.SslDown, 1))
      && (CurrentBars[0] != SavedBar)
      && (EMA1[0] < ChannelSSL2.SslUp[0])
      && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0))
      && (Times[0][0].TimeOfDay < new TimeSpan(16, 0, 0))
      && (EMA1[0] < ChannelSSL2.SslDown[0])
      && (MACD1.Diff[0] >= 0))
      {
      TriggerState = 1;
      EnterLong(2, @"Long1");
      SavedBar = Convert.ToInt32(CurrentBars[0]);
      EnterLong(2, @"Long2");
      EnterLong(2, @"Long3");
      EnterLong(1, runner);
      }

      // Set 3
      if ((TriggerState == 1)
      && (Position.MarketPosition == MarketPosition.Long))
      {
      TriggerState = 2;
      StopPrice = (Position.AveragePrice + (InitialStopDistance * TickSize)) ;
      TriggerPrice = (Position.AveragePrice + (BreakEvenTrigger * TickSize)) ;
      StopPrice2 = (Position.AveragePrice + (InitialStopDistance2 * TickSize)) ;
      }

      // Set 4
      if ((TriggerState == 2)
      && (Close[0] >= TriggerPrice))
      {
      TriggerState = 3;
      StopPrice = (Position.AveragePrice + (1 * TickSize)) ;
      StopPrice2 = (Position.AveragePrice + (1 * TickSize)) ;
      }

      // Set 5
      if (TriggerState >= 2)
      {
      ExitLongStopMarket(2, StopPrice, @"exitLong1", @"Long1");
      ExitLongStopMarket(2, StopPrice2, @"exitLong2", @"Long2");
      ExitLongStopMarket(2, StopPrice2, @"exitLong3", @"Long3");
      ExitLongStopMarket(1, StopPrice2, @"exitRunner", runner);
      }

      // Set 6
      if (Position.GetUnrealizedProfitLoss(PerformanceUnit. Ticks, Close[0]) >= TP1)
      {
      ExitLong(2, @"exitLong1", @"Long1");
      }

      // Set 7
      if (Position.GetUnrealizedProfitLoss(PerformanceUnit. Ticks, Close[0]) >= TP2)
      {
      ExitLong(2, @"exitLong2", @"Long2");
      }

      // Set 8
      if ((Position.GetUnrealizedProfitLoss(PerformanceUnit .Ticks, Close[0]) >= TP3)
      && (Position.MarketPosition == MarketPosition.Long))
      {
      ExitLong(2, @"exitLong3", @"Long3");
      TrailTriggerPrice = (Close[0] + (TrailStopFrequency * TickSize)) ;
      TrailStopPrice = Convert.ToInt32((Close[0] + (TrailStopDistance * TickSize)) );
      }

      // Set 9
      if ((Close[0] >= TrailTriggerPrice)
      && (Position.MarketPosition == MarketPosition.Long))
      {
      TrailTriggerPrice = (Close[0] + (TrailStopFrequency * TickSize)) ;
      TrailStopPrice = Convert.ToInt32((Close[0] + (TrailStopDistance * TickSize)) );
      }

      // Set 10
      if ((TrailStopPrice != 0)
      && (Position.MarketPosition == MarketPosition.Long))
      {
      ExitLongStopMarket(1, TrailStopPrice, @"StopRunner", runner);
      }

      // Set 11
      if (Position.GetUnrealizedProfitLoss(PerformanceUnit. Ticks, Close[0]) >= TP4)
      {
      ExitLong(1, @"exitRunner", runner);
      }

      }​


      I used examples of different already built strategies, and used some other advice I found on the forum cause I was running into other issues. Maybe some combination of code is not allowing the final part of it to execute correctly? Any help would be appreciated!

      Thanks a lot!

      Comment


        #4
        Hello ujin765,

        Thanks for your note.

        I see in the code you shared that you have not added debugging prints to the strategy yet to determine how the strategy is behaving.

        To understand why the script is behaving as it is, such as placing orders or not placing orders when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

        In the strategy add prints that print the values of every variable used in every condition that places an order along with the time of that bar. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

        Also, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.​

        Below is a link to a forum post that demonstrates how to use prints to understand behavior.

        https://ninjatrader.com/support/foru...121#post791121

        Let us know if we may assist further.​
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        44 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        124 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        65 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