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

Hedging Longs and Shorts

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

    Hedging Longs and Shorts

    I have been attempting to create a strategy that opens Longs and Shorts depending on closing prices, and then sets Limit exits per unique position. I have had quite a few issues trying to get the strategy to perate correctly and have been told that Ninjascript does not support this sort of action.

    So, my question is, can i create one strategy that opens my long positions (with the related Limit orders to close) and then create a second that opens my short positions (with the related Limit orders to close) and run both simultaneously on one chart? Also, if I can do this, how can I use the optimizer for both strategies combined?

    Secondly, I have attempted this already on the Sim101 account and it appears that when i start the strategies the system tries to create positions based on all past data, and not just start the strategy with the newest incoming bar. This has generated initial orders of many multiple positions instead of just one.

    #2
    three faced devil,

    You cannot hedge from within one single strategy. You will need to take your approach of using two separate strategies. Unfortunately cross-strategy communication is not supported and as such, backtesting such a hedging system would not be supported either.

    To prevent orders generated off of the past you have a few options available to you.
    1. You can add this to the top of your OnBarUpdate() on both strategies:
    Code:
    if (Historical)
         return;
    2. You can go into Tools->Options->Strategies->NinjaScript and you can try selecting "Wait until flat before executing live".
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Ok, since I don't know how to program the ninjascript itself I have only been using the wizard, and I did notice the "Wait until flat before executing live" option. No, i do not have it selected, as I guess I misunderstood it's purpose.

      Can you please explain exactly what this selection does.

      Comment


        #4
        The option means that your strategy will not make any real-time trades until the simulated strategy position (based off of historical data) has closed itself.

        So say you start up your strategy and according to your strategy's calculations you would theoretically have a long position. With this option checked, the strategy will continue to pretend it has a long position in simulation. It will then simulate the closing of this position based on your strategy logic and then it will finally be ready to make real-time trades. Now the next time your strategy wants to go long it will place the order into your Sim101 account instead of only having it virtually.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Ok, that makes sense, but it's not what I expected to happen. I just want the position to be flat from the the start and for the strategy to begin making trades based off the current incoming data at the moment I start it..... I only need one past bar to begin the trades, so I have no need to have the strategy calculate what it's theoretical position would be based off of days of data, in fact, doing that generates many multiple "theoretical" positions and, as such, I would have to wait quite a while before the currency becomes flat. I just want it to start flat when I hit "Go".

          Is this just an issue with the Sim101 account, or will this also happen if I try to execute on a live account?

          Comment


            #6
            The behavior will be the same regardless of which account you use. For what you want to do the only way to achieve this is for you to use option 1 I suggested.

            Finish up everything you wanted to do in the Strategy Wizard then press "Unlock code". You will see a section called OnBarUpdate(). You will need to add the two lines of code I typed out inside that method at the top. So it should look something like this:
            Code:
            protected override void OnBarUpdate()
            {
                 if (Historical)
                      return;
                 ....... // all your other wizard generated code goes below
            }
            Unfortunately after you do this you will not be able to go back into the Wizard though. I suggest you first backup your Wizard strategy before unlocking the code so you can keep reimporting it whenever you want to change the strategy. Then unlock the code and add the necessary code lines.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Thank you, I'll give that a shot....

              Comment


                #8
                Okay...so why?

                Same account; If I can go long in one strategy and short in another...why can't I do that from the same strategy?

                Is it by design that NT just ignores, without notice, a long order when a short position exists? Please explain.

                Thanks!

                Comment


                  #9
                  There are numerous order handling issues that arise. Take for instance a simple EnterLong() and EnterShort(). When you EnterShort() from a long position it will automatically close out the long position and then acquire you your short position. Vice versa for EnterLong(). Now you take that forward into EnterLongLimit() and EnterShortLimit(). If you submit both at the same time they can technically both be filled at the same time in fast moving markets. The long will fill then the short might have already filled, but now what happens with the auto close logic? It may have kicked in if the short filled after the long already acquired a position, or it may not have kicked in because the short was filled before the long acquired a position. Your ending position would then be impossible to determine and as such this is not allowed.

                  NinjaTrader does notify you of these ignorals. You need to have TraceOrders = true and look for it in the Output Window. As well as looking in the Control Center for the error that tells you you broke an Internal Order Handling Rule.
                  Josh P.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by jclose, Today, 09:37 PM
                  0 responses
                  4 views
                  0 likes
                  Last Post jclose
                  by jclose
                   
                  Started by WeyldFalcon, 08-07-2020, 06:13 AM
                  10 responses
                  1,413 views
                  0 likes
                  Last Post Traderontheroad  
                  Started by firefoxforum12, Today, 08:53 PM
                  0 responses
                  10 views
                  0 likes
                  Last Post firefoxforum12  
                  Started by stafe, Today, 08:34 PM
                  0 responses
                  10 views
                  0 likes
                  Last Post stafe
                  by stafe
                   
                  Started by sastrades, 01-31-2024, 10:19 PM
                  11 responses
                  169 views
                  0 likes
                  Last Post NinjaTrader_Manfred  
                  Working...
                  X