Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Closing ATM Strategy and Exiting Trade for Specific Instrument in Market Analyzer

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

    Closing ATM Strategy and Exiting Trade for Specific Instrument in Market Analyzer

    Hello NinjaTrader Community,

    I have an indicator attached to a Market Analyzer, which submits orders with an attached 'ATM1' strategy. The Market Analyzer contains two instruments: 'A' and 'B'. The 'ATM1' strategy is already active for both instruments, and an order has been placed for each instrument.

    I want to modify the indicator so that it closes the 'ATM1' strategy and exits the trade using a market order when a specific condition is met. My question is:
    1. Can I use the following code snippet to exit the trade if the condition is true for Instrument A but not for Instrument B?
    2. If the condition is True for Instrument A but false for Instrument B, will the below code exit the 'ATM1' strategy and close the position for Instrument A only, or will it close the positions for both instruments?​
    If the provided code snippet doesn't work for my specific scenario, I would greatly appreciate an example indicator that addresses my requirements. Thank you for your guidance,

    Code:
    protected override void OnBarUpdate()
    {
    
    if (Condition == True)
    {
         foreach (StrategyBase strat in myAccount.Strategies)
              {
                   strat.CloseStrategy("Close Strategy");
              }
    }
    
    }

    #2
    Hello MSerag,

    "Can I use the following code snippet to exit the trade if the condition is true for Instrument A but not for Instrument B?"

    Yes, you could compare the strat.Instruments[0].FullName to the Instrument (of the indicator instance) and if equal then call CloseStrategy().


    "If the condition is True for Instrument A but false for Instrument B, will the below code exit the 'ATM1' strategy and close the position for Instrument A only, or will it close the positions for both instruments?​"

    Your current code will close all Atm strategies on all instruments.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you for yoru reply. Is there any way to close the Atm strategy on the instrument that achieves (Condition == true) only given the other instrument is running the same ATM strategy but its (Condition == false)?
      Thanks again.

      Comment


        #4
        Hello MSerag,

        This is what I was advising in post # 2.

        May I confirm you are comparing the instrument in a condition and in the action block you are are calling strat.CloseStrategy?

        This would make the that instance of the script only close positions on the instrument for that instance.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I appreciate the clarification. One more question. If more than one strategy is active for the same instrument, how can we close one particular strategy and leave the others? For example, ATM1 and ATM2 are active for Instrument B. How do you close ATM1 and leave ATM2 active? If I understood the code correctly, it looped through all active strategies and closed them individually.

          Comment


            #6
            Hello MSerag,

            You are looping through all strategies in myAccount.Strategies.

            You can choose which of the strategies you want to close. I've suggested closing the ones that match the instrument. The ones you close are up to you.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              My queston is about the syntax
              strat.CloseStrategy("Close Strategy")
              Is "Close Strategy" a placeholder for the name of the atm strategy ( i.e. strat.CloseStrategy("myATMstrategy") )? If not, what about if there are two ATM strategies operating on the same insturment, which one will be closed?

              Here is the syntax I am using but it seems that it is not working

              Code:
              // Exit trade at specific timing
              if (date.Hour == 15 && date.Minute >= 50)
              {
              Print(Instrument.FullName +" "+date+" "+ "Trade exited");
              foreach (StrategyBase strat in myAccount.Strategies)
              {
              if (strat.Instruments[0].FullName == Instrument.FullName)
              {
              strat.CloseStrategy("myATM");
              }
              }
              }

              Thaks again.

              Comment


                #8
                Hello MSerag,

                The "Close Strategy" string supplied to the CloseStrategy() method call is the signal name of the order used to close the strategy.


                In the loop, the strat variable is holding the instance of the strategy for this loop iteration.

                With this logic all Atms on the instrument would be closed.

                If you want further logic in the if conditions to match for a specific strat.Id or strat.DisplayName you could do so.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks a lot. Can you guide me to the resource on the logic in the if conditions to match for a specific strat.Id or strat.DisplayName?

                  Comment


                    #10
                    Hello MSerag,

                    if (strat.Name == "AtmTemplateNameHere")
                    if (strat.Id == "StrategyInstaceIDHere")

                    You will need to work out some logic on how you want to identify a specific Atm strategy instance.
                    Which one do you want to close?
                    The first one?

                    You may find this indicator helpful.
                    This indicator serves to provide labels for Atm strategies that are present on your chart. Each new Atm strategy will rotate through the colors defined in your Brush Collection. Simply add to a chart, select the data series you want to have labels added to, choose your font and add all the brushes/colors you want […]
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi again,
                      I have been trying to manage the ATM strategy but no clue to make it working. I used the below code while the ATM strategy "test" is working and got the below printouts.


                      Code:
                      if (date.Hour == 15 && date.Minute >= 30 && exiT == 0)
                      {
                      
                      exiT = 1;
                      
                      Print(myAccount.Strategies[0]);
                      
                      foreach (StrategyBase strat in myAccount.Strategies)
                      {
                      
                      Print(strat.Name);
                      
                      if (strat.Instruments[0].FullName == Instrument.FullName && strat.Name == "test")
                      {
                      
                      Print("Entered closing");
                      
                      strat.CloseStrategy("Close Strategy");
                      }
                      }
                      }
                      name='AtmStrategy' id=313252767
                      AtmStrategy​

                      However, if I remove
                      Code:
                      && strat.Name == "test"
                      The strategy is exited with printing

                      name='AtmStrategy' id=313252767
                      AtmStrategy
                      Entered closing

                      Although the name of the working ATM strategy is "test", I got it printed as AtmStrategy. I tried to find ways to get the ATM id but it seems that there is no way to get the id as it is pooled from a random number.
                      Any insight?​

                      Comment


                        #12
                        Hello MSerag,

                        Apologies, that should be the AtmStrategy object .DisplayName.

                        AtmStrategy atm = strat.GetOwnerStrategy() as AtmStrategy;

                        Print(atm.DisplayName);

                        See lines 191 to 197 of the AtmStrategyIdentifier script I have provided you a link to.

                        Below is a link toa video testing the code.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Appreciation NinjaTrader_ChelseaB!

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Harvard, 10-17-2022, 07:23 PM
                          17 responses
                          184 views
                          0 likes
                          Last Post NinjaTrader_Jason  
                          Started by TradeForge, Yesterday, 05:51 AM
                          2 responses
                          20 views
                          0 likes
                          Last Post TradeForge  
                          Started by Alaina19, Yesterday, 10:55 PM
                          0 responses
                          5 views
                          0 likes
                          Last Post Alaina19  
                          Started by cmv727, 04-12-2024, 10:38 AM
                          9 responses
                          122 views
                          0 likes
                          Last Post mandrake88  
                          Started by BadA$$B3ast, 10-06-2024, 09:15 PM
                          2 responses
                          14 views
                          0 likes
                          Last Post BadA$$B3ast  
                          Working...
                          X