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

Limit Order Discrepancy LiveTrading ag. BackTesting

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

    Limit Order Discrepancy LiveTrading ag. BackTesting

    Hi Guys,

    I am trading the following CrossAbove/Below Strategy that enters into position via LimitOrder, below the code:

    Code:
    protected override void OnBarUpdate()
     {//Enter short
    	if (Position.MarketPosition == MarketPosition.Flat
    	&& entryOrder == null && CrossBelow(...))
    		{
    entryOrder = EnterShortLimit(0, true, DefaultQuantity, Close[0] + 0.35 * (High[0] - Low[0]),"Short");
    barNumberOfOrder = CurrentBar;
    		}
    				
    //Cancel limit order if not done within lookback period			
    	if(CurrentBar > barNumberOfOrder +1 && entryOrder != null)
    				{
    					CancelOrder(entryOrder);
    				}
    protected override void OnOrderUpdate(IOrder order)
    {
    if (entryOrder != null && entryOrder == order)
    	{
    		if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
    		{
    			entryOrder = null;
    		}
    	}
    }
    		        
    protected override void OnExecution(IExecution execution)
    {
    	if (entryOrder != null && entryOrder == execution.Order)
    		{
    			if (execution.Order.OrderState != OrderState.PartFilled)
    			{
    				entryOrder 	= null;
    			}
    		}
    }
    Basically when a CrossBelow occurs I enter short with the following formula:
    Close[0] + 0.35 * (High[0] - Low[0]

    It seems very simple, It works great in back testing, as you can see from the attached, however in real time trading the limit orders often are completely different from what I would expect them to be looking at the high/low/close of the previous bar session. As an example see attached the log of my real time trading, the same code/strategy that in back testing is leaving a EnterShortLimit at 131.55 in real time trading is leaving the order at 131.65. Any idea why this happens?

    Please help, this is driving me crazy
    Attached Files

    #2
    I'm not seeing anything obvious sticking out to me.

    You'll want to make sure COBC = true in order for your realtime calculations to match the backtest. If it is set to true could you attached a complete sample script so that I may test for the issue on my end?
    LanceNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Lance View Post
      I'm not seeing anything obvious sticking out to me.

      You'll want to make sure COBC = true in order for your realtime calculations to match the backtest. If it is set to true could you attached a complete sample script so that I may test for the issue on my end?
      Lance, I am using COBC = true;

      However in live trading I am running the strategy on a 1 min chart, whilst calculation is done on a different time frame. could this be causing the issue? In back testing both the MTF and non-MTF strategy convey to the same result, de facto calculation in done on the higher time frame.

      If you need to see the full code, can I send it via private massage to you?

      Comment


        #4
        You can send the simplified version of the script that replicates the error to support [at] ninjatrader [dot] com if you'd prefer not to post it here.
        LanceNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Lance View Post
          You can send the simplified version of the script that replicates the error to support [at] ninjatrader [dot] com if you'd prefer not to post it here.
          I've just sent you an email , if you need further details or have any suggestion on how to fix this issue, please let me know. Thanks for your help, appreciated

          Comment


            #6
            I suspect...

            What are you using as a data feed in back testing? I suggest you may get bogus bid/ask data from your data provider. Have you logged your bid and ask during live and historic data?

            GetCurrentBid();

            GetCurrentAsk();
            Last edited by molecool; 05-31-2013, 01:22 AM.

            Comment


              #7
              Originally posted by sburtt View Post
              Lance, I am using COBC = true;

              However in live trading I am running the strategy on a 1 min chart, whilst calculation is done on a different time frame. could this be causing the issue? In back testing both the MTF and non-MTF strategy convey to the same result, de facto calculation in done on the higher time frame.

              If you need to see the full code, can I send it via private massage to you?
              I suspect that you may be suffering from the same problem that I'm facing (see this open thread for more details).

              I take it you are using a higher period series for your strategy's pattern parsing and a lower series (e.g. minutes, seconds, or ticks) to actually handle the order management?

              If so then what happens is this. Let's assume you have a 1-hr period with a 1-sec period underneath. In historic mode at 18:01 the Closes[0][0] candle is at 18:00 while the Closes[1[0] candle is at 18:01. In LIVE mode the Closes[0][0] candle is at 19:00 and as such your entries are being calculated differently.

              I have attempted to address this problem vIa the Historic flag as such:

              Code:
              int index = Historic ? 0 : 1;
              However there's a problem with that - if you jump into an existing open candle then it'll be labeled as historic until the next candle rolls over. In other words you apparently need to have the strategy running until a candle roll over until NT labels the candle as running live. What's making matters worse is that the behavior appears to be intermittent - sometimes I suddenly get a live flag after reloading (at the middle of a candle) and most commonly I get a historic one. If you load a new symbol on your chart then you will get the historic flag until a roll over. If you let it run for a few hours and then reload the chart (F5) then you will get a live flag.

              I have been banging my head against the wall with this issue for weeks now and have yet to receive a solution for this. I'm currently attempting to fix this via time stamps but have not gotten far. I have also attempted to explain this to NT support and unfortunately have confused them more than anything else ;-)
              Last edited by molecool; 05-31-2013, 01:33 AM.

              Comment


                #8
                Originally posted by molecool View Post
                What are you using as a data feed in back testing? I suggest you may get bogus bid/ask data from your data provider. Have you logged your bid and ask during live and historic data?

                GetCurrentBid();

                GetCurrentAsk();
                good point. no i haven't, hence I assume (correct me if wrong) that NT should refer to last data, rather than bid/ask data

                Comment


                  #9
                  Originally posted by molecool View Post
                  I suspect that you may be suffering from the same problem that I'm facing (see this open thread for more details).

                  I take it you are using a higher period series for your strategy's pattern parsing and a lower series (e.g. minutes, seconds, or ticks) to actually handle the order management?

                  If so then what happens is this. Let's assume you have a 1-hr period with a 1-sec period underneath. In historic mode at 18:01 the Closes[0][0] candle is at 18:00 while the Closes[1[0] candle is at 18:01. In LIVE mode the Closes[0][0] candle is at 19:00 and as such your entries are being calculated differently.

                  I have attempted to address this problem vIa the Historic flag as such:

                  Code:
                  int index = Historic ? 0 : 1;
                  However there's a problem with that - if you jump into an existing open candle then it'll be labeled as historic until the next candle rolls over. In other words you apparently need to have the strategy running until a candle roll over until NT labels the candle as running live. What's making matters worse is that the behavior appears to be intermittent - sometimes I suddenly get a live flag after reloading (at the middle of a candle) and most commonly I get a historic one. If you load a new symbol on your chart then you will get the historic flag until a roll over. If you let it run for a few hours and then reload the chart (F5) then you will get a live flag.

                  I have been banging my head against the wall with this issue for weeks now and have yet to receive a solution for this. I'm currently attempting to fix this via time stamps but have not gotten far. I have also attempted to explain this to NT support and unfortunately have confused them more than anything else ;-)
                  I take it you are using a higher period series for your strategy's pattern parsing and a lower series (e.g. minutes, seconds, or ticks) to actually handle the order management?

                  YES this is exactly what I am doing.

                  If so then what happens is this. Let's assume you have a 1-hr period with a 1-sec period underneath. In historic mode at 18:01 the Closes[0][0] candle is at 18:00 while the Closes[1[0] candle is at 18:01. In LIVE mode the Closes[0][0] candle is at 19:00 and as such your entries are being calculated differently.

                  I re-edited my response, initially I was very happy, as this sounded like the solution, but rethinking about this I don't think this is what is causing my problem, let me further elaborate on this: I have 2 identical strategies, the first runs on 60min the second runs on 1 min time frame, but calculates entry signals on 60min, hence the 1min time frame is simply to provide extra granularity. Both strategies return identical results in back testing (besides for the entry time, but this is irrelevant),they also provide the same results in live trading, however occasionally the limit order used to enter long/short is +/-5-10pips from what I would expect looking at the back test results and the high/low/close used to determine the limit order. I would like to stress that this only happens once every n trades, that is a real head scratch for me.
                  Last edited by sburtt; 06-04-2013, 03:45 PM.

                  Comment


                    #10
                    Originally posted by sburtt View Post
                    [B]
                    I re-edited my response, initially I was very happy, as this sounded like the solution, but rethinking about this I don't think this is what is causing my problem, let me further elaborate on this: I have 2 identical strategies, the first runs on 60min the second runs on 1 min time frame, but calculates entry signals on 60min, hence the 1min time frame is simply to provide extra granularity. Both strategies return identical results in back testing (besides for the entry time, but this is irrelevant),they also provide the same results in live trading, however occasionally the limit order used to enter long/short is +/-5-10pips from what I would expect looking at the back test results and the high/low/close used to determine the limit order. I would like to stress that this only happens once every n trades, that is a real head scratch for me.
                    What's happening to you is the bid/ask spread is different in live vs. historical mode. What feed are you using for testing? In my case for instance I'm using Kinetick and it gives me simulated b/a spreads which are much wider than what I'm seeing on IB for instance (e.g. 2 pips vs. 0.5 pips). It also depends on the time of day (i.e. intermittent) for obvious reasons - not sure what instruments you're trading. If you're trading FX look at your pip settings as well.

                    On the entry time issue - well, I'm surprised that you are not having problems. I take it you are taking entries at the END/BEGINNING of candles? Because my current work takes intra-candle entries and, trust me, that complicates things considerably. If your 'current candle' is one step behind in historic mode it's very difficult to properly take an entry in the very same fashion as it's happening in live mode. I think my current implementation is doing it (almost) correctly but I'm still running into issues with getting stopped out with outside candles in historic mode. Probably due to the entry candle index but I don't want to confuse you.

                    I hope this helps you if not drop me a line and perhaps share a chart with an example so i can try to help.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by samish18, Today, 11:26 AM
                    0 responses
                    1 view
                    0 likes
                    Last Post samish18  
                    Started by Trader146, 03-29-2024, 01:22 PM
                    2 responses
                    14 views
                    0 likes
                    Last Post Trader146  
                    Started by tsantospinto, 04-12-2024, 07:04 PM
                    7 responses
                    127 views
                    0 likes
                    Last Post aligator  
                    Started by futtrader, 04-21-2024, 01:50 AM
                    5 responses
                    56 views
                    0 likes
                    Last Post NinjaTrader_Eduardo  
                    Started by PeakTry, Today, 10:49 AM
                    0 responses
                    2 views
                    0 likes
                    Last Post PeakTry
                    by PeakTry
                     
                    Working...
                    X