Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Open Multiple Long Positions with Different TPs

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

    Open Multiple Long Positions with Different TPs

    The title sums it up. I have been trying to create a very basic strategy that opens two positions. Both with the same stop and with two different profit targets. When the first take profit is hit, I would like to update the stop loss to breakeven and adjust the second profit target. I have spent hours combing through the forums and had something that almost worked with EnterLong and ExitLongLimit / ExitLongStopLimit but was hoping to get an easier solution with SetStopLoss and SetProfitTarget.

    Below is my current code and a screenshot. As can be seen in the screenshot, only the first EnterLong (P1) is triggered. When looking in the logs, I can't find any mention on the second order "P2".

    Please let me know if I am missing something.

    Thank you


    Code:
    //This namespace holds Strategies in this folder and is required. Do not change it.
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class MultipleOrderStrategy : Strategy
    
    {
    
    protected override void OnStateChange()
    
    {
    
    if (State == State.SetDefaults)
    
    {
    
    Description = @"Enter the description for your new custom Strategy here.";
    
    Name = “Multiple”OrderStrategy;
    
    Calculate = Calculate.OnBarClose;
    
    EntriesPerDirection = 1;
    
    EntryHandling = EntryHandling.UniqueEntries;
    
    IsExitOnSessionCloseStrategy = true;
    
    ExitOnSessionCloseSeconds = 30;
    
    IsFillLimitOnTouch = false;
    
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    
    OrderFillResolution = OrderFillResolution.Standard;
    
    Slippage = 0;
    
    StartBehavior = StartBehavior.WaitUntilFlat;
    
    TimeInForce = TimeInForce.Gtc;
    
    TraceOrders = false;
    
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    
    BarsRequiredToTrade = 30;
    
    IsInstantiatedOnEachOptimizationIteration = true;
    
    }
    
    }
    
    
    
    private bool inLongTrade;
    
    private bool isTargetMoved;
    
    private float entryPrice;
    
    
    
    
    protected override void OnBarUpdate() {
    
    if (CurrentBar < BarsRequiredToTrade) {
    
    return;
    
    }
    
    Print(string.Format("Market Position: {0}",Position.MarketPosition));
    
    //if (Position.MarketPosition == MarketPosition.Flat && Close[2] > Close[1]) {
    
    if(Close[2] > Close[1]) {
    
    Print("Entering a Long Position");
    
    SetStopLoss(CalculationMode.Ticks, 40);
    
    entryPrice = (float) Close[0];
    
    SetProfitTarget("P1", CalculationMode.Ticks, 40);
    
    SetProfitTarget("P2", CalculationMode.Ticks, 80);
    
    SetStopLoss(CalculationMode.Ticks, 40);
    
    EnterLong("P1");
    
    EnterLong("P2");
    
    isTargetMoved = false;
    
    }
    }
    
    
    
    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    
    {
    
    if (execution.Order.OrderState == OrderState.Filled) {
    
    if (execution.Order.Name == "P1" && !isTargetMoved) {
    
    SetStopLoss(CalculationMode.Price, entryPrice);
    
    SetProfitTarget("P2", CalculationMode.Price, entryPrice + 100);
    
    isTargetMoved = true;
    
    }
    
    }
    
    }
    }
    
    }
    Attached Files

    #2
    Hello TheSpartan,

    It looks like you have EntriesPerDirection set to 1so the second entry and its targets will get blocked. You need to use 2 for that value.

    Comment


      #3
      Jesse,

      Thanks! I should have also mentioned that I wanted to be able to drag and adjust the stop loss / take profit in chart trader once the trade is open.

      I have sorted this out and have working code. I'll attache here for anyone else that would benefit.

      Thanks

      Code:
      
      //This namespace holds Strategies in this folder and is required. Do not change it.
      
      namespace NinjaTrader.NinjaScript.Strategies
      
      {
      
          public class MultipleOrderStrategy : Strategy
      
          {
      
      
      
      
              protected override void OnStateChange()
      
              {
      
                  if (State == State.SetDefaults)
      
                  {
      
                      Description = @"Enter the description for your new custom Strategy here.";
      
                      Name = “MultipleOrderStrategy";
      
                      Calculate = Calculate.OnBarClose;
      
                      // Need to update this when we expand past one position
      
                      EntriesPerDirection = 1;
      
                      EntryHandling = EntryHandling.AllEntries;
      
                      IsExitOnSessionCloseStrategy = true;
      
                      ExitOnSessionCloseSeconds = 30;
      
                      IsFillLimitOnTouch = false;
      
                      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
      
                      OrderFillResolution = OrderFillResolution.Standard;
      
                      Slippage = 0;
      
                      StartBehavior = StartBehavior.WaitUntilFlat;
      
                      TimeInForce = TimeInForce.Gtc;
      
                      TraceOrders = false;
      
                      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
      
      StopTargetHandling = StopTargetHandling.ByStrategyPosition;
      
                      BarsRequiredToTrade = 30;
      
      
      
      
                      // Disable this property for performance gains in Strategy Analyzer optimizations
      
                      // See the Help Guide for additional information
      
                      IsInstantiatedOnEachOptimizationIteration = true;
      
                  }
      
              }
      
      
      
      
      
      
      private bool isTargetMoved;
      
      private float entryPrice;
      
      private float exePrice;
      
      protected override void OnBarUpdate() {
      
      if (CurrentBar < BarsRequiredToTrade) {
      
      inLongTrade = false;
      
      return;
      
      }
      
      if(Position.MarketPosition == MarketPosition.Flat) {
      
      entryPrice = (float)Low[1];
      
      EnterLongLimit(0, true, 2, entryPrice, "Long");
      
      isTargetMoved = false;
      
      }
      
      
      
      
      }
      
      
      
      protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
      
              {
      
      if (execution.Order.OrderState == OrderState.Filled) {
      
      if (execution.Order.Name == "Target1" && !isTargetMoved) {
      
      ExitLongStopLimit(0,true,1,0,exePrice, "LongTradeStop", "Long");
      
      ExitLongLimit(0, true, 1, exePrice + 50.0,"Target2", "Long");
      
      isTargetMoved = true;
      
      }
      
      if (execution.Order.Name == "Long") {
      
      exePrice = (float)execution.Price;
      
      ExitLongStopLimit(0,true,2,0,execution.Price - 20.0, "LongTradeStop", "Long");
      
      ExitLongLimit(0, true, 1, execution.Price + 20.0,"Target1", "Long");
      
      ExitLongLimit(0, true, 1, execution.Price + 40.0,"Target2", "Long");
      
      }
      
      }
      
      }
      ​

      Comment


        #4
        Hello TheSpartan,

        You won't be able to manually manage the targets from this kind of strategy. You would have to submit live until cancel orders and not use the set methods for targets to do that.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        43 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