Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

RealTime Trades are recorded as all Longs or Shorts for a reversal strategy

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

    RealTime Trades are recorded as all Longs or Shorts for a reversal strategy

    I'm testing an unmanaged order manager framework with a simple reversal strategy. From flat I enter by hitting either a long or short stopmarket entry.

    Then in OnExecutionUpdate I reverse from long to flat by doubling the number of contracts.

    In OnBarUpdate I adjust the opposing GTC entry order to follow the market, which eventually gets hit, with another opposing order placed etc. This works OK. When reviewing the trade history, historical trades are correctly categorized as long and short trades, when transitioning into real-time, all subsequent real-time trades are either long or short, not a mix.

    Do I need to split the reversal order into two orders, i.e. for a long position of 2 contracts, place both OrderType.StopMarket Order.Sell of 2 and OrderType.StopMarket Order.SellShort of 2 at the same Stop Price as opposed to a single OrderType.StopMarket Order.SellShort 4 in order to get the trades properly categorized in real-time or is there something else I can do?
    Last edited by lavalampmj; 03-11-2020, 10:27 AM.

    #2
    Hello lavalampmj,

    For the pairing to work for trades in the strategy performance or trade performance, there would need to be an exit order to exit the existing position and a new order as the entry for the new position.
    This would be instead of placing a single order with double the quantity to both exit and reverse the position, which would not make pairable trades.
    The exit would be similar to the 'Close position' NinjaTrader submits to exit a position when reversing using the managed approach.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      "Close position' NinjaTrader submits to exit a position when reversing using the managed approach". I don't see such method exposed. Please confirm. Is there any reference code that would show how to correctly code this up in an unmanaged situation. I'm assuming I'd need to place an appropriate OrderType.Market, OrderAction.Buy or OrderAction.BuyToCover for closing long and short respectively in OnExecutionUpdate as the opposite EntryOrder triggers sometime intrabar , as opposed to placing a paired GTC exit order and managing it until being hit?

      Its complicated...
      Last edited by lavalampmj; 03-11-2020, 10:58 AM.

      Comment


        #4
        Hello lavalampmj,

        Run the Sample MA Crossover and wait until the position reverses in real-time. Then view the orders.

        When calling EnterLong() in a short position, NinjaTrader automatically manages the position and will automatically submit an order with the name Close position, and then will send a second order for the enter long.

        The method call for this would be EnterLong() when in a short position.

        For your unmanaged script, you would just submit an order with SubmitOrderUnmanaged() to exit the existing position, and then submit another order with SubmitOrderUnmanaged() to enter the new position.

        I wasn't able to find any existing examples with this custom logic. However, it would really just mean the extra call to SubmitOrderUnmanaged() to exit the existing position before calling SubmitOrderUnmanaged() for the entry, to give the trades proper entries and exits for pairing in the performance reports.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks. I now understand that in the case of managed EnterXXXX orders actually issues two orders in the case of reversal system.

          My first attempt of fixing the trade pairing issue was to issue a second intrabar exit order (such as StopMarket exit) at the same price and time as I issued a StopMarket Entry). With Position.Quantity for the exit order and the desired number of contracts for the entry order.

          Then changing the order prices for both orders in OnBarUpdate through their life This approach proved to be rather unrobust, encountering many corner case issues, for example, when one or other order was rejected, or one filled and the other not on a specific bar etc.

          I'll build the equivalent of EnterLong, by simply by sending a market order when the OnExecution fires for an existing GTC entry order to close out previous entry.

          Anyone reading this contemplating building a robust unmanaged strategy, I would say really really really consider whether its worth going this route. I only ventured into this area because I wanted to build a breakout system, where I wanted to place opposing entry orders (which is something the managed approach does not allow). In retrospect, I could have implemented this desired functionality in a managed system by monitoring where the market is relative to the desired entry points, canceling one order while placing the other as the market moved towards the desired entry price point. I still might go that route as the managed approach does a lot of very complicated stuff on your behalf...

          Comment


            #6
            Hello lavalampmj,

            I have an example of an unmanaged bracket strategy. However this does not demonstrate an immediate reversal by sending two orders, this only shows how to bracket with OCO.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Chelsea, OCO was up next after I have some stability! There is also some cleaner code in there than mine, also interesting to see a working system with no code in OnBarUpdate. It was useful, thanks!

              The managed order manager already has some smarts to cancel and reintroduce orders on either side of the market, so long as they are in the same direction, all in the name in avoiding overfills. It would be a wonderful enhancement to support opposing entry orders too. It sure would be safer to rely on code that has been battle tested than anything I can write!
              Last edited by lavalampmj; 03-12-2020, 08:20 PM.

              Comment


                #8
                I'm still struggling with this problem. I've had 3 attempts to solve it when using intra bar GTC entry orders on the opposing side for a reversal system.

                1. On the first attempt I'd issue an intrabar exit order to match the intrabar entry order (with the exit order being to close the existing open position), both being issued at the same moment. Then as we needed to change the entry order price I'd simultaneously change the exit order price to match. I found this approach to be not very stable as many corner cases crept in, e.g. not all fills on the same bar for example, one order being occasionally rejected and not the other etc.

                2. Attempt number two. When an entry order is hit on OnExecutionUpdate I'd place a market exit order for the open position. This seems to be robust but the trade matching is not correct still.

                3. Finally, I adjusted the 2nd approach, by adding the same signal name for all longs and all shorts, hoping to match entries with exits. This also did not solve the problem either.

                I have not yet created a convenience wrapper around intrabar GTC entry orders, such as is done in the managed approach, because of the issues I encountered in my first attempt.

                Would it be possible to email the code to someone to review and provide some pointers how to get correct matching? Thanks for your help on this.
                Last edited by lavalampmj; 03-18-2020, 02:17 PM.

                Comment


                  #9
                  Hello lavalampmj,

                  Unfortunately, in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. This is so that we can maintain a high level of service for all of our clients as well as our partners.

                  This thread will remain open for any community members that would like to assist with creating custom logic.

                  You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. I will have a member of the Ecosystem team follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.


                  My advice is have an exit for every entry. When the exit order state is Order.Filled, the entry in the opposite direction can be placed and an exit placed for that entry.

                  Use prints and provide the output from TraceOrders and the order object in OnOrderUpdate() with your posts to help understand the behavior.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10

                    Hello lavalampmj,

                    This is Chris following up on behalf of Chelsea.

                    You can search our extensive library of NinjaScript consultants through the link below. Simply enter a consultant name or search by using our filter categories. Once you have identified your consultants of choice, please visit each consultant's site for more information or contact them directly to learn more:You can locate the contact information for the consultants on their direct websites for any additional questions you may have. Since these consultants are third-party services for NinjaTrader, all pricing and support information will need to be obtained through the consultant.

                    The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The companies and services listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem, LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.

                    Let me know if I may be of further assistance.

                    Comment


                      #11
                      "When the exit order state is Order.Filled, the entry in the opposite direction can be placed and an exit placed for that entry"

                      That insight probably is enough to solve my sequencing problem. I was keying actions of off entry code, not exit fills... Thanks.

                      Comment


                        #12
                        Hello lavalampmj,

                        Below is a link to an example that shows triggering orders based on fills of other orders. You will want to look at the unmanaged example.
                        https://ninjatrader.com/support/foru...269#post802269
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks Chelsea for the reference code (ProfitChaseStopTrail unmanaged example). BTW: I think there might be a minor bug in the example (line 215) where the stop trail price adjustment is picking up the ProfitTargetDistance ticks property not the StopLossDistance ticks property) Cut and paste causes way to many bugs in my code too! ;-)

                          I have rewritten my code to ensure that other than from flat, where I place two entry opposing entry StopMarket orders in OnBarUpdate (when one or other of these are hit the other order is canceled in OnExcutionUpdate). I am certain that every entry is matched with a StopMarket Exit order. During the Exit order's life I adjust its price, if the market moves in our favor. When the Exit is hit in OnExcuteUpdate, a reverse Market entry order is placed, with that order's OnExcutionUpdate Order.Filled fires we, place a covering StopMarket Exit order in OnExecutionUpdate and so on.

                          I've carefully reviewed trace orders and extensive print statements. All seems to be correct. I then wondered whether something is getting lost in transition to realtime. I can confirm that when the system makes the transition with "Immediately Submit" or Immediately Submit, Synchronize Account". The trade accounting error occurs, whereby all trades are either long or short, even though they are correctly drawn on the chart and also correct in the historical trade history. If the system is set to "Wait until flat" or Wait Until Flat, sync account" then all is correctly accounted for.

                          I have checked that the position state, account state are correctly reported and orders transition correctly in OnStateChange. At this point I am not able to further isolate, it appears to be something going astray during the realtime transition when we immediately sync. Open to suggestions?
                          Last edited by lavalampmj; 03-19-2020, 03:10 PM.

                          Comment


                            #14
                            Hello lavalampmj,

                            This script is tested and working.

                            What is the bug you are referring to?
                            Is not placing the stop and limit orders when the entry fills as expected?

                            What is the issue you are having you want assistance with?
                            Are exit orders not being submitted when the entry order fills?
                            Do you have print and TraceOrders information to provide about the order in question?
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Chelsea. This thread is about an unmanaged reversal system whereby historical trades are correctly accounted for in the trade performance history but realtime, the trades are not correctly accounted for in the performance history. They are all noted as either all shorts or longs in realtime. In reality the system is reversing long to short continuously.

                              Further, realtime trades are being correctly being displayed on the chart.

                              I believe that my code is working correctly as stated above. All orders are issued and executed correctly. I have further isolated that problem such that it is only encountered when the system is set to "Immediately Submit" or Immediately Submit, Synchronize Account" , not "Wait until flat" or Wait Until Flat, sync account". I believe my code is exposing a Ninjatrader bug in the way it handles position state / order transition from historical to realtime for reversal unmanaged systems.

                              I can simply set my system to only trade in realtime or use "Wait until flat" or "Wait Until Flat, sync account" to avoid this problem. This is not a big issue to me as it does not impede my progress.

                              To assist NinjaTrader in determining whether there is a bug or whether the error is in my code I would be happy to send my code and instructions how to reproduce the problem.

                              Please advise how NinjaTrader wishes to proceed.




                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Yesterday, 05:17 AM
                              0 responses
                              81 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              149 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              79 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              52 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              59 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X