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

Potential Issue with 'AtmStrategyClose(string atmStrategyId)' Command in Ninjascript

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

    Potential Issue with 'AtmStrategyClose(string atmStrategyId)' Command in Ninjascript

    Hello Support and fellow traders,

    I wanted to bring to your attention a potential problem I encountered while programming a strategy in Ninjascript and utilizing the 'AtmStrategyClose(string atmStrategyId)' command. I believe this issue could be crucial for those who rely on this command in their trading strategies.

    The problem arises when trying to read the profit and loss using the command 'GetAtmStrategyRealizedProfitLoss(string atmStrategyId)' after having first called the 'AtmStrategyClose(string atmStrategyId)' command.

    In my testing, I have observed cases where the 'AtmStrategyClose' command successfully closes the strategy, but in many instances, the subsequent call to 'GetAtmStrategyRealizedProfitLoss' returns 0 even though the ATM strategy might have produced a positive/negative result.

    I have tried putting a delay (skipping a few OnBarUpdate events) before calling the 'GetAtmStrategyRealizedProfitLoss(...)' after having called the AtmStrategyClose(...) but it did not help.

    My workaround was to create a method to close any given ATM strategy and moving the Limit/target orders below/above the current price close depending on the trade direction Long/Short. If i proceed this way then the call to 'GetAtmStrategyRealizedProfitLoss' always returns the correct amount.

    Do you think it is due to the particular way the ATM strategy close behavior proceeds ?

    *see this link for further details:

    Thank you for your attention and valuable input.
    Last edited by trendisyourfriend; 05-16-2023, 12:10 PM.

    #2
    Hello trendisyourfriend,

    What you are seeing would be expected, once the ATM has been filled or closed the status methods won't be able to look up information for that ATM because its not active.

    To do what you are trying you would need to get the status of the ATM before calling the close method and then save that information to a variable so you can use it for whatever the original intended goal was.

    This is lightly noted in the help guide page for GetAtmStrategyStopTargetOrderStatus,
    Gets the current order state(s) of the specified stop or target order of a still-active ATM strategy.




    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by trendisyourfriend View Post
      My workaround was to create a method to close any given ATM strategy and moving the Limit/target orders below/above the current price close depending on the trade direction Long/Short. If i proceed this way then the call to 'GetAtmStrategyRealizedProfitLoss' always returns the correct amount.

      Do you think it is due to the particular way the ATM strategy close behavior proceeds ?

      *see this link for further details:
      Your workaround is very interesting.

      Why?
      Because on NT7, the Close button on Chart Trader would adjust
      the target limit orders to immediately fill
      -- apparently this was
      changed in NT8(?) but the NT8 help page reads like an incomplete
      rehash from NT7, resulting in a contradictory and confusing
      explanation.

      How so?
      The NT8 help page implies all stop/target orders are cancelled,
      then a Sell Market order is submitted to close a Long position,
      or a Buy Market order is submitted to close a Short position.
      What else could step 4 be except a Market Buy/Sell order?
      Right?

      Ok, so right after these steps are outlined in NT8, where step
      step 4 clearly implies a market order, the section that follows is
      titled "Why not just submit a Market Order?"

      WTF? The NT8 help page is contradicting itself.

      That NT8 help page is poorly written and needs a massive
      rewrite. In fact, it looks a like hasty update to the NT7 page
      and then that hasty/incomplete page got inserted into the
      NT8 help guide.

      The NT7 help page is well written, and very consistent.

      In contrast,
      The NT8 help page on the same topic is a poor attempt
      at an 'update' of the NT7 page, resulting in inconsistent
      information and conflicting explanations. The NT8 help
      page is contradictory, and needs a massive overhaul.

      Just my 2˘.

      Comment


        #4
        Thanks guys for your explanation. However, i am not sure Jesse understood my point.

        Just to be sure i'll reformat my question like this:

        If yo have an ATM strategy running which is positive (have some profits) and you call AtmStrategyClose(string atmStrategyId) then you can use this code afterwards :

        if (GetAtmStrategyMarketPosition(thisTrade.atmStrateg yId) == Cbi.MarketPosition.Flat)
        double ​tradePnL = GetAtmStrategyRealizedProfitLoss(thisTrade.atmStra tegyId);


        And many times the output will be correct but at times for some reasons probably due to the specific way the close bahavior works, the ouput will be zero even if the ATM strategy produces a positive or negative result.

        Comment


          #5
          Originally posted by NinjaTrader_Jesse View Post
          What you are seeing would be expected, once the ATM has been filled or closed the status methods won't be able to look up information for that ATM because its not active.

          To do what you are trying you would need to get the status of the ATM before calling the close method and then save that information to a variable so you can use it for whatever the original intended goal was.

          This is lightly noted in the help guide page for GetAtmStrategyStopTargetOrderStatus,
          The OP never asked about GetAtmStrategyStopTargetOrderStatus.

          He asked about GetAtmStrategyRealizedProfitLoss and AtmStrategyClose.

          Neither of the help pages for above two APIs contain the 'lightly noted' comment
          you mention.

          Isn't 'RealizedProfitLoss' something that is only available after a position closes,
          because until the position closes, the profit/loss is 'unrealized', right?

          Well, then what is the correct calling sequence to get the realized profit/loss of
          a closed position that entered/exited via an Atm strategy method?

          I mean, what's the point of GetAtmStrategyRealizedProfitLoss if not to precisely
          do what the OP is doing? The help page says nothing about this API only working
          with an active Atm strategy.

          Comment


            #6
            Originally posted by trendisyourfriend View Post
            However, i am not sure Jesse understood my point.
            Totally agreed. Jesse's answer was crap, er, I mean, very off-target.



            Comment


              #7
              Hello trendisyourfriend,

              I understand what you are saying and the reason I mentioned the GetAtmStrategyStopTargetOrderStatus help guide page is becuase that is the only place the help guide mentions "Active ATM strategy", the other status method pages do not mention that which is an important item to keep in mind.

              If you need the GetAtmStrategyRealizedProfitLoss to drive your logic you should access that value before closing the ATM and save that value to a variable. That would ensure you have a value before the ATM is finished. If the ATM did not make any actual Realized PNL before calling close then you would want to instead use GetAtmStrategyUnrealizedProfitLoss before closing the ATM as well to ensure you have some value to drive your later logic.

              In general what you are seeing is expected due to the order of operations happening. You can avoid that by calling the status and making sure you have a value before closing the ATM or also if the workaround you use works you can do that as well.
              Last edited by NinjaTrader_Jesse; 05-16-2023, 01:34 PM.
              JesseNinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_Jesse View Post
                I understand what you are saying and the reason I mentioned the GetAtmStrategyStopTargetOrderStatus help guide page is becuase that is the only place the help guide mentions "Active ATM strategy", the other status method pages do not mention that which is an important item to keep in mind.

                If you need the GetAtmStrategyRealizedProfitLoss to drive your logic you should access that value before closing the ATM and save that value to a variable. That would ensure you have a value before the ATM is finished. If the ATM did not make any actual Realized PNL before calling close then you would want to instead use GetAtmStrategyUnrealizedProfitLoss before closing the ATM as well to ensure you have some value to drive your later logic.​

                In general what you are seeing is expected due to the order of operations happening. You can avoid that by calling the status and making sure you have a value before closing the ATM or also if the workaround you use works you can do that as well.
                Jesse, your answer is still full of crap.

                Please take a good close look at the reference sample here.

                The code in that reference sample shows what OP is doing is correct.

                -=o=-

                The second red highlighted area is what the OP is asking about.

                Why does the method used to close an active Atm Strategy position
                affect the returned value from GetAtmStrategyRealizedProfitLoss?

                Your own reference sample clearly shows this API being called after
                the Atm Strategy position is flat.

                How is what OP reports in his first post expected behavior? Why does
                the AtmStrategyClose method used to achieve the 'flatness' affect the
                realized pnl? Why does the workaround of adjusting the target limit
                order to achieve 'flatness' fix his problem of retrieving the realized pnl?

                Last edited by bltdavid; 05-16-2023, 02:36 PM.

                Comment


                  #9
                  Clearly, if what OP is reporting is correct, there is a problem.

                  Comment


                    #10
                    Originally posted by trendisyourfriend View Post
                    The problem arises when trying to read the profit and loss using the command 'GetAtmStrategyRealizedProfitLoss(string atmStrategyId)' after having first called the 'AtmStrategyClose(string atmStrategyId)' command.

                    In my testing, I have observed cases where the 'AtmStrategyClose' command successfully closes the strategy, but in many instances, the subsequent call to 'GetAtmStrategyRealizedProfitLoss' returns 0 even though the ATM strategy might have produced a positive/negative result.

                    I have tried putting a delay (skipping a few OnBarUpdate events) before calling the 'GetAtmStrategyRealizedProfitLoss(...)' after having called the AtmStrategyClose(...) but it did not help.

                    My workaround was to create a method to close any given ATM strategy and moving the Limit/target orders below/above the current price close depending on the trade direction Long/Short. If i proceed this way then the call to 'GetAtmStrategyRealizedProfitLoss' always returns the correct amount.
                    Do you have a test script that illustrates the problem?
                    Could you please attach it?

                    Who is your broker?
                    Are you testing with a live acct or with a Sim101 acct?

                    Which version of NT8 are you running?

                    Comment


                      #11
                      Also, Jesse, you have not addressed any of the contradictory
                      statements made in the NT8 help page referenced by the OP
                      and pointed out to you by me.

                      Step 4 says this,

                      "4.After receiving confirmation for all cancellation requests, send
                      an offset order to close any open position (For example, a Buy
                      order for 3 contracts would be sent to close an open short
                      position of 3 contracts)​"

                      What step 4 is clearly doing is sending a Market order, despite
                      the attempt at obfuscation with 'offset order' and the failure to
                      say 'a Buy Market order for 3 contracts would be sent'.

                      If step 4 is using a Market order, why is there a section that
                      immediately follows step 4 titled, 'Why not just submit a market
                      order?'

                      I'll tell you why.
                      Because this NT8 help page is a crappy update to the NT7 help
                      page
                      on the same topic.

                      The NT8 help page is contradictory, and needs to be corrected.

                      Do you agree?

                      -=o=-

                      In particular, does NT8 Chart Trader Close button employ a different
                      sequence of steps to close the position when compared to the steps
                      used by the Close button in NT7?


                      Comment


                        #12
                        Thanks for your reply,

                        Unfortunately, you need to call the 'GetAtmStrategyRealizedProfitLoss' once the strategy is flat not before as it does not make sense since when i execute the 'AtmStrategyClose(string atmStrategyId)' command then i need to track the process until the ATM strategy gets flat to read the final profit or loss via the command GetAtmStrategyUnrealizedProfitLoss(). Doing it before calling AtmStrategyClose() would not report the realized PnL. Anyway, my workaround does deliever what i need. I appreciate your help and effort to answer my question.

                        Originally posted by NinjaTrader_Jesse View Post
                        Hello trendisyourfriend,

                        I understand what you are saying and the reason I mentioned the GetAtmStrategyStopTargetOrderStatus help guide page is becuase that is the only place the help guide mentions "Active ATM strategy", the other status method pages do not mention that which is an important item to keep in mind.

                        If you need the GetAtmStrategyRealizedProfitLoss to drive your logic you should access that value before closing the ATM and save that value to a variable. That would ensure you have a value before the ATM is finished. If the ATM did not make any actual Realized PNL before calling close then you would want to instead use GetAtmStrategyUnrealizedProfitLoss before closing the ATM as well to ensure you have some value to drive your later logic.

                        In general what you are seeing is expected due to the order of operations happening. You can avoid that by calling the status and making sure you have a value before closing the ATM or also if the workaround you use works you can do that as well.

                        Comment


                          #13
                          Originally posted by trendisyourfriend View Post
                          Unfortunately, you need to call the 'GetAtmStrategyRealizedProfitLoss' once the strategy is flat not before
                          Absolutely. I totally agree with you.

                          Would you be so kind as to address post 10?

                          The answers there would be very helpful.

                          Many thanks!

                          Comment


                            #14
                            Originally posted by bltdavid View Post

                            Do you have a test script that illustrates the problem?
                            Could you please attach it?

                            Who is your broker?
                            Are you testing with a live acct or with a Sim101 acct?

                            Which version of NT8 are you running?
                            I am still using version 8.0.27.1. Developing my tool on a Sim101 acct. I do not have a prototype to share yet but as i wrote using my workaround it's OK, it does the job.

                            This is my code snippet if you want to see how i did it:

                            Last edited by trendisyourfriend; 05-17-2023, 05:19 PM.

                            Comment


                              #15
                              Hello trendisyourfriend,

                              I just wanted to try and help clarify that this is expected that a 0 value is seen in this use case. This is tracked internally under the ID NTEIGHT-13996. This is not something listed in the change logs because it is expected and has not been changed, that is developments internal tracking id for this issue. This is the specific internal note that we have about using AtmStrategyClose and then trying to use GetAtmStrategyRealizedProfitLoss.

                              ATMStrategyClose will cancel SL and PT and submit a closing order.

                              In NT7, calling ATMStrategyClose would modify the Profit Target down to fill, thus allowing you to call GetAtmStrategyRealizedProfitLoss on future bars.

                              Calling GetAtmStrategyRealizedProfitLoss after calling ATMStrategyClose will return zero in NT8 on future bars.​
                              It would be expected that a 0 is returned. GetAtmStrategyRealizedProfitLoss can be used before calling ATMStrategyClose if the ATM has any existing realized PnL, for example itt has multiple quantity and one of the targets filled, that will return a non 0 value. After closing the strategy with ATMStrategyClose a 0 would be returned. If you need to get the realized Pnl your workaround would work based on the old way in NT7 that the close happened.

                              JesseNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by AaronKoRn, Today, 09:49 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post AaronKoRn  
                              Started by carnitron, Today, 08:42 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post carnitron  
                              Started by strategist007, Today, 07:51 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post strategist007  
                              Started by StockTrader88, 03-06-2021, 08:58 AM
                              44 responses
                              3,980 views
                              3 likes
                              Last Post jhudas88  
                              Started by rbeckmann05, Today, 06:48 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post rbeckmann05  
                              Working...
                              X