Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multiple Instrument exits

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

    Multiple Instrument exits

    i have a pairs strategy that i have entering on 2 different instruments and i cant seem to get it to trigger the exits on the secondary instrument

    here is the code : private bool ManageExit()
    {
    double currentSpread = CalculateSpread();
    double spreadMean = spreadMeanIndicator[0];
    double spreadStdDev = spreadStdDevIndicator[0];
    double currentCorrelation = CalculateCurrentCorrelation();

    Print("Checking Exit Conditions:");
    Print("Current Spread: " + currentSpread);
    Print("Spread Mean: " + spreadMean);
    Print("Spread StdDev: " + spreadStdDev);
    Print("Spread Deviation from Mean for Exit: " + Math.Abs(currentSpread - spreadMean));
    Print("Required Deviation (StdDev) for Exit: " + spreadStdDev);

    // Calculate the ATR-based profit target with multiplier
    double atrProfitTargetPrimary = atrIndicatorPrimary[0] * atrMultiplier;
    double atrProfitTargetSecondary = atrIndicatorSecondary[0] * atrMultiplier;


    Print("Checking Exit Conditions based on ATR targets:");
    Print("ATR Profit Target Primary: " + atrProfitTargetPrimary);
    Print("ATR Profit Target Secondary: " + atrProfitTargetSecondary);



    // Check if both positions are in profit and meet ATR target
    bool primaryProfitTargetMet = Positions[0].MarketPosition == MarketPosition.Long && Close[0] - Positions[0].AveragePrice >= atrProfitTargetPrimary ||
    Positions[0].MarketPosition == MarketPosition.Short && Positions[0].AveragePrice - Close[0] >= atrProfitTargetPrimary;

    bool secondaryProfitTargetMet = Positions[1].MarketPosition == MarketPosition.Long && Closes[1][0] - Positions[1].AveragePrice >= atrProfitTargetSecondary ||
    Positions[1].MarketPosition == MarketPosition.Short && Positions[1].AveragePrice - Closes[1][0] >= atrProfitTargetSecondary;


    // Apply the exit logic for each instrument based on the profit target met condition
    if (primaryProfitTargetMet)
    {
    Print("Exiting primary position based on ATR target.");
    if (Position.MarketPosition == MarketPosition.Long) ExitLong("ExitATRLongNQ", "NQ_F_Long");
    else if (Position.MarketPosition == MarketPosition.Short) ExitShort("ExitATRShortNQ", "NQ_F_Short");
    }

    if (secondaryProfitTargetMet)
    {
    Print("Exiting secondary position based on ATR target.");
    if (Position.MarketPosition == MarketPosition.Long) ExitLong("ExitATRLongES", "ES_F_Long");
    else if (Position.MarketPosition == MarketPosition.Short) ExitShort("ExitATRShortES", "ES_F_Short");
    }



    // Check if the spread-based exit condition is met
    if (Math.Abs(CalculateSpread() - spreadMeanIndicator[0]) <= spreadStdDevIndicator[0])
    {
    Print("Exiting all positions based on spread condition.");
    ExitLong("ExitSPRDLongNQ", "NQ_F_Long");
    ExitShort("ExitSPRDShortNQ", "NQ_F_Short");
    ExitLong("ExitSPRDLongES", "ES_F_Long");
    ExitShort("ExitSPRDShortES", "ES_F_Short");
    }


    return primaryProfitTargetMet || secondaryProfitTargetMet; // Return true if any exit was executed
    }


    ​please help , i cant seem to figure this one out

    #2
    Hello thehammer,

    For this type of situation it would generally be suggested to use prints to confirm that your condition is becoming true. I see that you are only using Position.MarketPosition which is the primary position. For multiple instruments you need to use Positions plural to be able to access the position of the second instrument. You can see an example here:

    JesseNinjaTrader Customer Service

    Comment


      #3
      so i made the changes to the plural with the positions and it is printing that the exits are being made on the secondary positions

      but the exits on the secondary positions are still not exiting properly and the exit names are not showing on the chart like they are on the primary

      but the position entrance names on both primary and secondary positions are correct

      so i don’t know what I’m doing wrong with this ?

      here is a portion of the output

      OnBarUpdate called - Bar #9933 - Spread: 12204.5
      Spread Mean: 12183.2925
      Spread StdDev: 9.57092308766507
      Series NQ 03-24 - Bar Count: 9933, Last Close: 17022.75, Timestamp: 12/19/2023 1:27:00 PM
      Series ES 03-24 - Bar Count: 9933, Last Close: 4818.25, Timestamp: 12/19/2023 1:27:00 PM
      Series ^VIX - Bar Count: 5328, Last Close: 12.52, Timestamp: 12/19/2023 1:15:00 PM
      Correlation between NQ 03-24 and ES 03-24 is: 0.91
      Entering Short NQ and Long ES
      NQ position is Short
      ES positions is Long
      Exiting secondary position based on ATR target.
      OnBarUpdate called - Bar #9933 - Spread: 12204.5
      Spread Mean: 12183.2925
      Spread StdDev: 9.57092308766507
      Series NQ 03-24 - Bar Count: 9933, Last Close: 17022.75, Timestamp: 12/19/2023 1:27:00 PM
      Series ES 03-24 - Bar Count: 9933, Last Close: 4818.25, Timestamp: 12/19/2023 1:27:00 PM
      Series ^VIX - Bar Count: 5328, Last Close: 12.52, Timestamp: 12/19/2023 1:15:00 PM
      OnBarUpdate called - Bar - Spread: 12205.5
      Spread Mean: 12183.5125
      Spread StdDev: 9.82270170319755
      Series NQ 03-24 - Bar Count: 9934, Last Close: 17023.5, Timestamp: 12/19/2023 1:30:00 PM
      Series ES 03-24 - Bar Count: 9934, Last Close: 4818, Timestamp: 12/19/2023 1:30:00 PM
      Series ^VIX - Bar Count: 5328, Last Close: 12.52, Timestamp: 12/19/2023 1:15:00 PM
      Correlation between NQ 03-24 and ES 03-24 is: 0.91
      Entering Short NQ and Long ES
      NQ position is Short
      ES positions is Long
      Exiting secondary position based on ATR target.

      and here is the updated code

      private bool ManageExit()
      {
      double currentSpread = CalculateSpread();
      double spreadMean = spreadMeanIndicator[0];
      double spreadStdDev = spreadStdDevIndicator[0];
      double currentCorrelation = CalculateCurrentCorrelation();

      /*Print("Checking Exit Conditions:");
      Print("Current Spread: " + currentSpread);
      Print("Spread Mean: " + spreadMean);
      Print("Spread StdDev: " + spreadStdDev);
      Print("Spread Deviation from Mean for Exit: " + Math.Abs(currentSpread - spreadMean));
      Print("Required Deviation (StdDev) for Exit: " + spreadStdDev);*/

      // Calculate the ATR-based profit target with multiplier
      double atrProfitTargetPrimary = atrIndicatorPrimary[0] * atrMultiplier;
      double atrProfitTargetSecondary = atrIndicatorSecondary[0] * atrMultiplier;


      /*Print("Checking Exit Conditions based on ATR targets:");
      Print("ATR Profit Target Primary: " + atrProfitTargetPrimary);
      Print("ATR Profit Target Secondary: " + atrProfitTargetSecondary);*/

      Print("NQ position is " + Positions[0].MarketPosition);
      Print("ES positions is " + Positions[1].MarketPosition);


      // Check if both positions are in profit and meet ATR target
      bool primaryProfitTargetMet = Positions[0].MarketPosition == MarketPosition.Long && Close[0] - Positions[0].AveragePrice >= atrProfitTargetPrimary ||
      Positions[0].MarketPosition == MarketPosition.Short && Positions[0].AveragePrice - Close[0] >= atrProfitTargetPrimary;

      bool secondaryProfitTargetMet = Positions[1].MarketPosition == MarketPosition.Long && Closes[1][0] - Positions[1].AveragePrice >= atrProfitTargetSecondary ||
      Positions[1].MarketPosition == MarketPosition.Short && Positions[1].AveragePrice - Closes[1][0] >= atrProfitTargetSecondary;


      // Apply the exit logic for each instrument based on the profit target met condition
      if (primaryProfitTargetMet)
      {

      if (Positions[0].MarketPosition == MarketPosition.Long) ExitLong("ExitATRLongNQ", "NQ_F_Long");
      else if (Positions[0].MarketPosition == MarketPosition.Short) ExitShort("ExitATRShortNQ", "NQ_F_Short");
      Print("Exiting primary position based on ATR target.");
      }


      if (secondaryProfitTargetMet)
      {

      if (Positions[1].MarketPosition == MarketPosition.Long) ExitLong("ExitATRLongES", "ES_F_Long");
      else if (Positions[1].MarketPosition == MarketPosition.Short) ExitShort("ExitATRShortES", "ES_F_Short");
      Print("Exiting secondary position based on ATR target.");
      }



      // Check if the spread-based exit condition is met
      if (Math.Abs(CalculateSpread() - spreadMeanIndicator[0]) <= spreadStdDevIndicator[0])
      {

      if (Positions[0].MarketPosition == MarketPosition.Long) ExitLong("ExitSPRDLongNQ", "NQ_F_Long");
      else if (Positions[0].MarketPosition == MarketPosition.Short) ExitShort("ExitSPRDShortNQ", "NQ_F_Short");
      Print("Exiting NQ positions based on spread condition.");


      if (Positions[1].MarketPosition == MarketPosition.Long) ExitLong("ExitSPRDLongES", "ES_F_Long");
      else if (Positions[1].MarketPosition == MarketPosition.Short) ExitShort("ExitSPRDShortES", "ES_F_Short");
      Print("Exiting ES positions based on spread condition.");
      }


      return primaryProfitTargetMet || secondaryProfitTargetMet; // Return true if any exit was executed
      }​

      Comment


        #4
        Hello thehammer,

        When you say the exits are not happening properly what do you mean?

        Do you see the exits happening in the strategies performance report?

        JesseNinjaTrader Customer Service

        Comment


          #5
          so this is a picture it is entering and exiting on the secondary position but exits is happening because of the new entry direction not because it is properly exiting

          Click image for larger version

Name:	exits.jpg
Views:	92
Size:	158.4 KB
ID:	1282885Click image for larger version

Name:	exits.jpg
Views:	71
Size:	158.4 KB
ID:	1282886

          Comment


            #6
            Hello thehammer,

            If you are seeing a reversal that means your entry logic is becoming true while you are in a position. You need to disable the entry condition while in a position to avoid a reversal.

            JesseNinjaTrader Customer Service

            Comment


              #7
              actually i got it to work k properly with this solution: EnterLong(int barsInProgressIndex, int quantity, string signalName)

              thanks for the help

              Comment


                #8
                and this ExitLong(int barsInProgressIndex, int quantity, string signalName, string fromEntrySignal)

                Comment


                  #9
                  Hi Jesse and thehammer,
                  I have had similar issue with it. In order to call ExitLong() for 2nd data series, the difficulty is how to obtain remaining position quantity.
                  BTW, for #5 thread above, how did you get the entry/exit signal on the chart , along with the red/green dotted line ? is there a place to configure the dotted line ?

                  thanks in advance

                  Comment


                    #10
                    You would need to get the Positions quantity.



                    Gaby V.NinjaTrader Customer Service

                    Comment


                      #11
                      thanks Gaby.
                      you didn't reply my question regarding how to generate the red dotted line which connects entry order and exit order. do I need configure it somewhere from strategy or data series?
                      thanks

                      ​​​

                      Comment


                        #12
                        Hello judysamnt7,

                        To have the lines between orders you would need to apply the strategy directly to a chart and make sure in the data series menu the plot executions option is set to text and markers.
                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Jesse,
                          what do you mean by applying strategy directly to the chart? I always enable the strategy and turn on execution plot to text and marker. Just curious why I am not seeing the line between orders.

                          Comment


                            #14
                            Click image for larger version

Name:	image.png
Views:	14
Size:	46.8 KB
ID:	1326488
                            this is a screenshot for post #13​

                            Comment


                              #15
                              Hello judysamnt7,

                              You can also apply a strategy directly to the control center, in that situation no strategy information is shown in the charts. You would only see executions but no connecting lines. That may also relate to the chart trader, try and close the chart trader to see if that helps.
                              JesseNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by natngk, 02-21-2019, 06:27 AM
                              21 responses
                              1,291 views
                              1 like
                              Last Post FMtrader  
                              Started by richt3, Yesterday, 10:28 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post richt3
                              by richt3
                               
                              Started by Atychiphobia66, Yesterday, 10:22 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post Atychiphobia66  
                              Started by Scalper1969, Yesterday, 09:24 PM
                              0 responses
                              13 views
                              0 likes
                              Last Post Scalper1969  
                              Started by RISKYBUSINEZZ, 12-12-2023, 05:11 PM
                              3 responses
                              127 views
                              0 likes
                              Last Post judysamnt7  
                              Working...
                              X