Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy doesn't transition from historical to live

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

    Strategy doesn't transition from historical to live

    Hi, I have a simple swing trading strategy that is always in the market, either long or short. When the strategy is long, it looks for the sell signal to go short and vice versa. I am able to backtest and optimize this strategy without any problems. However, it fails when I go live.

    The problem:

    When I attach the strategy to a chart, after it calculates all the historical trades, it closes the current position and takes the market position flat before going live. This causes my strategy to fail as it “can never be flat.”

    Why is the position being taken flat before going live? Is there another way to write this so it works for live trading?

    Thanks,

    CS


    PS. I am attaching the strategy code to give you an idea of what I’m trying to do.


    Code:
     
    // User defined variables (add any user defined variables below)
    private IOrder buyOrder = null; // buy order
    private IOrder sellOrder = null; // sell order
    privatedouble buyStop = 0; // buy stop price
    privatedouble sellStop = 0; // sell stop price
    privateint startBar = 0; // bar to start strategy
     
     
    protectedoverridevoid Initialize()
    {
    CalculateOnBarClose = true;
    TimeInForce = Cbi.TimeInForce.Gtc;
    BarsRequired = 0;
    ExitOnClose = false;
    }
     
    protectedoverridevoid OnBarUpdate()
    {
    // check for minumum bars
    if (!Signal().BStop.ContainsValue(0))
    {
    startBar = CurrentBar + 20;
    return;
    }
    if (CurrentBar < startBar) return;
     
    // start strategy
    buyStop = Signal().BStop[1];
    sellStop = Signal().SStop[1];
     
    if (Position.MarketPosition == MarketPosition.Flat) // enter initial buy/sell order 
    {
    if (High[0] >= buyStop) buyOrder = EnterLong(1, "Buy");
     
    elseif (Low[0] <= sellStop) sellOrder = EnterShort(1, "Sell");
    }
     
    elseif (Position.MarketPosition == MarketPosition.Short) // enter buy stop order 
    {
    buyOrder = EnterLongStop(1, buyStop, "BuyStop");
    }
     
    elseif (Position.MarketPosition == MarketPosition.Long) // enter sell stop order 
    {
    sellOrder = EnterShortStop(1, sellStop, "SellStop");
    }
    }

    #2
    Cshep, have you tried the setting "Immediately submit live working historical orders" under Tools -> Options -> Strategies -> NinjaScript?

    The default behavior for NinjaScripts is to wait until flat before going live. The strategy will create a "virtual position" until the strategy decides it isn't flat anymore. From that point on, it should submit real orders. In addition, here is a link to the difference between strategy position and account position.

    In NinjaTrader 7, this is being addressed:
    Position synchronization
    You can optionally have your strategy synchronize your live account to the strategy's position when a strategy is enabled. If your strategy starts long 1, a market order for 1 is immediately sent to your account.
    AustinNinjaTrader Customer Service

    Comment


      #3
      Yes, "Immediately submit live working historical orders" is set. This is one of the first things I checked.

      When I set the "Wait until flat before executing live" option, it actually maintains my position properly but it will never execute the strategy as the position will never be flat.

      I've actually been trying to troubleshoot this problem for three days now. I've gone through all the help and forums without finding a solution. I'm out of ideas and completely baffled as to why this is happening.

      Also, I'm running in the Sim101 account and it is the strategy position that's causing the problem not the account position.

      Please help.

      Thanks,

      CS

      Comment


        #4
        CS, there is a way to make sure the strategy is flat upon start, and this should make your strategy work. In OnBarUpdate:
        Code:
        OnBarUpdate()
        {
            // make sure the following is the first thing in OnBarUpdate
            if (Historical)
                return;
        }
        The downside of this is you can't backtest the strategy without taking out that code.
        AustinNinjaTrader Customer Service

        Comment


          #5
          Thanks for getting back to me Austin.

          Unfortunately this will not work. As I mentioned, my strategy is never flat, it's only long or short. My original problem is a flat position upon start. This is what I’m trying to prevent, not what I want to achieve.

          When I begin trading this for real, if the strategy starts flat, it will always be out of sync with my account. This just won’t work.

          The real question still is, why is the position being taken flat before going live? I haven’t coded this. NT just does it on it’s own for some reason.

          Thanks,

          CS

          Comment


            #6
            CS, did you check with TraceOrders = true why your position gets exited as you take the strategy live?



            Are you sure the issue is with your strategy position not being correct? If not, proper synching would most likely help.
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Thanks Bertrand, TraceOrders is a very useful tool.

              After adding that to my code and re-launching the strategy on the instrument ES 09-09, I get the following results:

              On historical data, the strategy position is long.

              On live data, the strategy position is flat. No “Close position” orders show up in the trace. On the chart, there is a tag that says “Close position 1 @ 934.25”. Again, this tag is on the chart and doesn’t show up in the trace.

              Given that my strategy position is long on historical data and flat on live data, I have to assume that the problem is that my strategy position is not correct. I don’t know how NT works internally but it is clear that my strategy position should be long. Again, it should never be flat.

              Any help would be greatly appreciated.

              Thanks,

              CS

              Comment


                #8
                cshep, to get on the same page as you, and to get an better idea of what is going on with your strategy, could you please post the code? If you'd rather not post it on the forum, you can mail it to support at ninjatrader dot com Attn Austin.

                In the meantime, could you try submitting the orders with liveUntilCancelled = true?
                AustinNinjaTrader Customer Service

                Comment


                  #9
                  Okay, I've written a simple standalone strategy that demonstrates the problem. You should see the strategy position go flat when it makes the transition from historical to live data.

                  Thanks again for all your time and effort.

                  CS
                  Attached Files

                  Comment


                    #10
                    Thanks CS, we'll give this a run and get back to you shortly.
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      Just tested this on a 20 tick ES chart and it worked as expected when using the 'Wait until Flat' option for me, as you start it up you see the virtual 0 strategy position and it's taking the next developing trade then. You defnitely get some errors in the TradeOrders for invalid StopOrders (BuyStops equal or less than the current close price), so you might want to debug this further to not run into 'ignored' orders.
                      BertrandNinjaTrader Customer Service

                      Comment


                        #12
                        Hi Bertrand, it looks like your test demonstrated the problem. The strategy goes flat when transitioning from historical to live data. This is not correct. There is no code that tells the strategy to go flat. Why is it doing this? By the logic of the code, if you have the "Wait until flat" option selected, then it should never trade live. The strategy should never be flat and therefore, would never trade.

                        If we can't figure this out, I'm going to have to write a kludge that would probably triple the amount of code. But that would not be very reassuring.

                        Also, the trade errors you're getting are a function of testing on a 20 tick chart. It works fine on longer time frames (eg. range charts) but that's not the issue right now.

                        Thanks,

                        CS

                        Comment


                          #13
                          CS, I'm not sure I follow - setting this to 'Wait until flat' means it would take the next realtime generated entry signal and just not place any historically working orders like stops and targets for example. It would register a flat condition even for a reversal strategy like yours, you see first 'close position' and then the order in the reversing direction, this is the flat state - http://www.ninjatrader-support.com/H...tegiesTab.html
                          BertrandNinjaTrader Customer Service

                          Comment


                            #14
                            Flat is not an option. The strategy is always in the market, either long or short. It can NEVER BE FLAT. The option "Immediately submit live working historical orders" must be set.

                            Eventually, when I start executing trades on a real account, my account is going to start out either long or short. NEVER FLAT. If the strategy starts out ALWAYS FLAT then then the strategy will ALWAYS BE OUT OF SYNC with my account. This will cause a world of problems. Therefore, I would have to write a major kludge to "ignore" the strategy position.

                            I want and need all the historical trades to get the proper strategy position. For example, from your help guide...



                            "An extremely common scenario is starting a NinjaScript strategy in the middle of a trading session, such as one hour after the session has begun.. The NinjaScript strategy is run on each historical bar for the 1st hour of the session (it will actually run on all historical data loaded in a chart) to determine the current position state it would be in if it had been running live since the start of the session. This position state then becomes the Strategy Position for your strategy."

                            My strategy is not behaving the way you describe in the help guide. "This position state then becomes the Strategy Position for your strategy." In my strategy it runs all historical data and then closes the strategy position before it starts live. My position state is always flat every time.

                            Why is it doing this????

                            I can't make myself any clearer. If you still don't get what I'm saying or if there is no answer, no soltution, please let me know. That way I can start working on my kludge and forget about trying to trouble shoot this.

                            CS

                            Comment


                              #15
                              CS, strategy is behaving as expected - I'm not sure why you would need the first part of your code entering positions if the strategy is flat, just implement the channel break and sync up the strategy as needed if you want to use the 'Immediately Submit' option -

                              Code:
                               
                              if (High[0] >= highestHigh) buyOrder = EnterLong(1, "Buy");
                              else if (Low[0] <= lowestLow) sellOrder = EnterShort(1, "Sell");
                              Now matter how seamless the whole trading operation is planned and laid out, there'll be most likely times when you would for sure need to sync up the strategy vs the account positions. NinjaTrader 7 will offer more options in this regard and also addresses strategy persistence between sessions -

                              Last edited by NinjaTrader_Bertrand; 07-17-2009, 08:27 AM.
                              BertrandNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by ChartTourist, Today, 08:22 AM
                              0 responses
                              0 views
                              0 likes
                              Last Post ChartTourist  
                              Started by LiamTwine, Today, 08:10 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post LiamTwine  
                              Started by Balage0922, Today, 07:38 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post Balage0922  
                              Started by JoMoon2024, Today, 06:56 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post JoMoon2024  
                              Started by Haiasi, 04-25-2024, 06:53 PM
                              2 responses
                              19 views
                              0 likes
                              Last Post Massinisa  
                              Working...
                              X