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

Back Test On Bar Update

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

    Back Test On Bar Update

    Hello,

    I am backtesting a strategy that is based on the weekly chart.

    Since it is such a wide time frame, I am afraid my back test results may be a bit skewed since the the entries and exits are done on the next bar after the trigger.

    Is there anyway to back test the strategy where it will use the exact entry and exit price so I can get a more accurate back test result?

    #2
    Hello.

    Thanks for the post.

    You can set a higher fill resolution in the Strategy Analyzer to make your fill estimation more accurate. Under the configuration of the strategy you are backtesting, in the "Historical fill processing" section, change Order fill resolution to High, then select a data series to use.

    NinjaTrader uses a special three pass process to determine if you should be filled historically or not, for more information on this algorithm, please see the link below.


    Please let us know if you have any questions.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      It says that I need to program it directly into my code since I have a multi-series strategy.

      How would I go about doing that?

      Comment


        #4
        Hello.

        Thanks for the reply.

        You will need to add, for instance, a 1 tick series to your script to fill orders on.

        Code:
        if (State == State.Configure)
            {
                AddDataSeries(BarsPeriodType.Tick, 1);
                ...
        Then fill your orders on that series in OnBarUpdate:

        Code:
        protected override void OnBarUpdate()
        {
            ...
            //This example assumes you already have some other secondary series
            if(condition)
            {
                EnterLong(2, 1, "EntryFromTickSeries");
            }
            ...
        }
        Please see the help guide section on advanced order handling:


        Also, see this reference sample for a full example:
        You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by


        Please let us know if you have any questions.
        Last edited by NinjaTrader_ChrisL; 03-16-2018, 06:43 AM.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Thanks!

          I currently have my exit strategy set to ExitLong();

          Do I need to add the data series as well to that or it will automatically do the same?

          Comment


            #6
            Hello.

            Thanks for the reply.

            You should use the similar ExitLong() method in tandem with EnterLong. This will make sure your historical exit orders will be filled on the 1 tick series as well.

            ExitLong(int barsInProgressIndex, int quantity, string signalName, string fromEntrySignal)

            https://ninjatrader.com/support/help.../?exitlong.htm - ExitLong()

            Please let us know if we may be of any further assistance.
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Thanks!

              Im getting 0 results (0 trades taken) once I make this adjustment.

              In the strategy analyzer, I normally use the Weekly chart as Type for Data Series. Do I now need to switch that to Tick Type as well or leave it on Week?

              TIA

              Comment


                #8
                Hello.

                Thanks for the reply.

                You can keep the series at the same weekly interval. Does changing the series as you suggest make the strategy take trades? Without the code, I can't say why the strategy is not entering. You can use Print statements to debug your entry statement, you can also turn on trace orders for output on orders taken by the strategy.

                https://ninjatrader.com/support/help...-us/?print.htm - Print()
                https://ninjatrader.com/support/help...raceorders.htm -Trace orders.

                Please let us know if we may be of any further assistance.
                Chris L.NinjaTrader Customer Service

                Comment


                  #9
                  Hey Thanks for the response.

                  Here is my code. Not sure what I am doing wrong. Was trying a few different things but cant seem to get it to work.

                  Code:
                  {
                  	public class WTFStratTickTest : Strategy
                  	{
                  		private SMA	SMAmonth;
                  		private Bollinger midbb;
                  		private double holdValue = 0;
                  		private double holdValueShort = 0;
                  		private DM DMI;
                  		private ATR ATRdaily;
                  		private ADX ADXweek;
                  		private Stochastics StoConfirmWeek;
                  		private Stochastics StoConfirmMonth;
                  		private RSI RSIweek;
                  		private RSI RSIdaily;
                  		
                  		
                  	
                  		
                  		
                  		protected override void OnStateChange()
                  		{
                  			if (State == State.SetDefaults)
                  			{
                  				Description									= @"Enter the description for your new custom Strategy here.";
                  				Name										= "WTFStratTickTest";
                  				Calculate									= Calculate.OnEachTick;
                  				EntriesPerDirection							= 1;
                  				EntryHandling								= EntryHandling.AllEntries;
                  				IsExitOnSessionCloseStrategy				= true;
                  				ExitOnSessionCloseSeconds					= 30;
                  				IsFillLimitOnTouch							= false;
                  				MaximumBarsLookBack							= MaximumBarsLookBack.TwoHundredFiftySix;
                  				OrderFillResolution							= OrderFillResolution.Standard;
                  				Slippage									= 0;
                  				StartBehavior								= StartBehavior.WaitUntilFlat;
                  				TimeInForce									= TimeInForce.Gtc;
                  				TraceOrders									= false;
                  				RealtimeErrorHandling						= RealtimeErrorHandling.StopCancelClose;
                  				StopTargetHandling							= StopTargetHandling.PerEntryExecution;
                  				BarsRequiredToTrade							= 20;
                  				// Disable this property for performance gains in Strategy Analyzer optimizations
                  				// See the Help Guide for additional information
                  				IsInstantiatedOnEachOptimizationIteration	= true;
                  				
                  				AddPlot(Brushes.Orange, "PLOTA");
                  				AddPlot(Brushes.Green, "PLOTB");
                  			}
                  			else if (State == State.Configure)
                  			{
                  				//SetProfitTarget(CalculationMode.Currency, 5000);
                  				//SetStopLoss(CalculationMode.Currency, 1500);
                  				AddDataSeries(BarsPeriodType.Month, 1);
                  				AddDataSeries(BarsPeriodType.Day, 1);
                  				AddDataSeries(BarsPeriodType.Tick, 1);
                  			}
                  			else if (State == State.DataLoaded)
                  			{
                  				midbb = Bollinger(Close, 2, 20);
                  				SMAmonth = SMA(BarsArray[1], 5);
                  				DMI = DM(Close, 14);
                  				ATRdaily = ATR(Close, 14);
                  				ADXweek = ADX(BarsArray[2], 14);
                  				StoConfirmWeek = Stochastics(5, 15, 3);
                  				StoConfirmMonth = Stochastics(BarsArray[1], 5, 15, 3);
                  				RSIweek = RSI(Close, 5, 3);
                  				RSIdaily = RSI(BarsArray[2], 5, 3);
                  				
                  				
                  			}
                  		}
                  
                  		protected override void OnBarUpdate()
                  		{
                  			if (CurrentBars[0] < 5 || CurrentBars[1] < 5)
                  				return;
                  			
                  			Values[0][0] = midbb.Middle[0];
                  			Values[1][0] = SMAmonth[0];
                  			
                  			if (holdValue == 0
                  			&& (Highs[0][4] < Highs[0][3])
                  			&& (Lows[0][4] > midbb.Middle[4])
                  			&& (Lows[0][3] > midbb.Middle[3])
                  			&& (Lows[0][2] > midbb.Middle[2])
                  			&& (Lows[0][1] > midbb.Middle[1])
                  			&& (Highs[0][3] > Highs[0][2])
                  			&& (Highs[0][3] > Highs[0][1]))
                  			{
                  				holdValue = Highs[0][3];
                  			}
                  			
                  	
                  			
                  			if (holdValue != 0 && Lows[0][0] < midbb.Middle[0])
                  			{
                  				holdValue = 0;
                  				Print("Reset of holdValue " + holdValue);
                  			}
                  			
                  		
                  			
                  			if (holdValue != 0)
                  			{
                  				Print("High price at last condition was " + holdValue);
                  			}
                  			
                  			if ((holdValue > midbb.Middle[0])
                  			&& (Lows[1][2] >= SMAmonth[2])
                  			&& (Lows[1][1] >= SMAmonth[1])
                  			&& (Lows[1][0] >= SMAmonth[0])
                  			&& (holdValue != 0)
                  			//&& (ATRdaily[0] >= .75)
                  			&& (RSIweek[0] >= 75)
                  			//&& (RSIdaily[0] >= 75)
                  			&& (GetCurrentAsk(0) >= 40)
                  			//&& (Slope(DMI.DiPlus, 1, 0) > 1)
                  			//&& (Slope(DMI.DiMinus, 1, 0) < -1)
                  			//&& (DMI.DiPlus[0] >= 50)
                  			//&& (StoConfirmWeek.K[0] > StoConfirmWeek.D[0])
                  			//&& (StoConfirmMonth.K[0] > StoConfirmMonth.D[0])
                  			&& (Highs[0][0] > holdValue))
                  			{
                  				int myQuantity = (int) ((1200)/(holdValue - midbb.Middle[0]));
                  				EnterLong(3, myQuantity, @"");
                  				holdValue = 0;
                  			} else if (holdValue != 0 && Highs[0][0] > holdValue + .02)
                  			{
                  				holdValue = 0;
                  				Print("Reset of holdValue" + holdValue);
                  			}
                  			
                  			if (Lows[0][0] < SMAmonth[0])
                  			{
                  				ExitLong();
                  			}
                  			
                  			if (holdValueShort == 0
                  			&& (Lows[0][4] > Lows[0][3])
                  			&& (Highs[0][4] < midbb.Middle[4])
                  			&& (Highs[0][3] < midbb.Middle[3])
                  			&& (Highs[0][2] < midbb.Middle[2])
                  			&& (Highs[0][1] < midbb.Middle[1])
                  			&& (Lows[0][3] < Lows[0][2])
                  			&& (Lows[0][3] < Lows[0][1]))
                  			{
                  				holdValueShort = Lows[0][3];
                  			}
                  			
                  			if (holdValueShort != 0 && Highs[0][0] > midbb.Middle[0])
                  			{
                  				holdValueShort = 0;
                  				Print("Reset of holdValueShort " + holdValueShort);
                  			}
                  			
                  			
                  			if (holdValueShort != 0)
                  			{
                  				Print("Low price at last condition was " + holdValueShort);
                  			}
                  			
                  			if ((holdValueShort < midbb.Middle[0])
                  			&& (Highs[1][2] <= SMAmonth[2])
                  			&& (Highs[1][1] <= SMAmonth[1])
                  			&& (Highs[1][0] <= SMAmonth[0])
                  			&& (holdValueShort != 0)
                  			//&& (ATRdaily[0] >= .75)
                  			&& (RSIweek[0] <= 25)
                  			//&& (RSIdaily[0] <= 25)
                  		      && (GetCurrentAsk(0) >= 40)
                  			//&& (Slope(DMI.DiPlus, 1, 0) < -1)
                  			//&& (Slope(DMI.DiMinus, 1, 0) > 1)
                  			//&& (DMI.DiMinus[0] >= 50)
                  			//&& (StoConfirmWeek.K[0] < StoConfirmWeek.D[0])
                  			//&& (StoConfirmMonth.K[0] < StoConfirmMonth.D[0])
                  			&& (Lows[0][0] < holdValueShort))
                  			{
                  				int myQuantity = (int) ((1200)/(midbb.Middle[0]) - holdValue);
                  				EnterShort(3, myQuantity, @"");
                  				holdValueShort = 0;
                  			} else if (holdValueShort != 0 && Lows[0][0] < holdValueShort - .02)
                  			{
                  				holdValueShort = 0;
                  				Print("Reset of holdValueShort" + holdValueShort);
                  			}
                  			
                  			if (Highs[0][0] > SMAmonth[0])
                  			{
                  				ExitShort();
                  			}
                  		}
                  	}
                  }

                  Comment


                    #10
                    Hello.

                    Thanks for the reply.

                    I added some extra prints on every condition in your script. I noticed most of them were being hit but the condition for EnterLong and EnterShort were never triggered. So that we can maintain a high level of service for all of our clients, I can not debug or change this code any further. The script will need to have these crucial entry conditions analyzed further. We offer the NinjaTrader Ecosystem, a member of the NinjaTrader Ecosystem team can help you find a NinjaScript consultant to help with this code.

                    Code:
                    		protected override void OnBarUpdate()
                    		{
                    			//Print(CurrentBars[0]);
                    			//Print(CurrentBars[1]);
                    			if (CurrentBars[0] < 5 || CurrentBars[1] < 5)
                    			{
                    				//Print("C0 True");
                    				return;
                    			}
                    			
                    			Values[0][0] = midbb.Middle[0];
                    			Values[1][0] = SMAmonth[0];
                    			//Print(Highs[0][4].ToString());
                    			//Print(Highs[0][3].ToString());
                    			if (holdValue == 0
                    			&& (Highs[0][4] < Highs[0][3])
                    			&& (Lows[0][4] > midbb.Middle[4])
                    			&& (Lows[0][3] > midbb.Middle[3])
                    			&& (Lows[0][2] > midbb.Middle[2])
                    			&& (Lows[0][1] > midbb.Middle[1])
                    			&& (Highs[0][3] > Highs[0][2])
                    			&& (Highs[0][3] > Highs[0][1]))
                    			{
                    				//Print("C1 True");
                    				holdValue = Highs[0][3];
                    			}
                    			
                    	
                    			
                    			if (holdValue != 0 && Lows[0][0] < midbb.Middle[0])
                    			{
                    				//Print("C2 True");
                    				holdValue = 0;
                    				//Print("Reset of holdValue " + holdValue);
                    			}
                    			
                    		
                    			
                    			if (holdValue != 0)
                    			{
                    				//Print("C3 True");
                    				//Print("High price at last condition was " + holdValue);
                    			}
                    			
                    			
                    			if ((holdValue > midbb.Middle[0])
                    			&& (Lows[1][2] >= SMAmonth[2])
                    			&& (Lows[1][1] >= SMAmonth[1])
                    			&& (Lows[1][0] >= SMAmonth[0])
                    			&& (holdValue != 0)
                    			//&& (ATRdaily[0] >= .75)
                    			&& (RSIweek[0] >= 75)
                    			//&& (RSIdaily[0] >= 75)
                    			&& (GetCurrentAsk(0) >= 40)
                    			//&& (Slope(DMI.DiPlus, 1, 0) > 1)
                    			//&& (Slope(DMI.DiMinus, 1, 0) < -1)
                    			//&& (DMI.DiPlus[0] >= 50)
                    			//&& (StoConfirmWeek.K[0] > StoConfirmWeek.D[0])
                    			//&& (StoConfirmMonth.K[0] > StoConfirmMonth.D[0])
                    			&& (Highs[0][0] > holdValue))
                    			{
                    				int myQuantity = (int) ((1200)/(holdValue - midbb.Middle[0]));
                    				EnterLong(3, myQuantity, @"SN");
                    				holdValue = 0;
                    				Print("C4 True");
                    			} else if (holdValue != 0 && Highs[0][0] > holdValue + .02)
                    			{
                    				//Print("C5 True");
                    				holdValue = 0;
                    				//Print("Reset of holdValue" + holdValue);
                    			}
                    			
                    			if (Lows[0][0] < SMAmonth[0])
                    			{
                    				int myQuantity = (int) ((1200)/(holdValue - midbb.Middle[0]));
                    				//Print("C6 True");
                    				ExitLong(3,myQuantity,"EE", @"SN");
                    				
                    			}
                    			
                    			if (holdValueShort == 0
                    			&& (Lows[0][4] > Lows[0][3])
                    			&& (Highs[0][4] < midbb.Middle[4])
                    			&& (Highs[0][3] < midbb.Middle[3])
                    			&& (Highs[0][2] < midbb.Middle[2])
                    			&& (Highs[0][1] < midbb.Middle[1])
                    			&& (Lows[0][3] < Lows[0][2])
                    			&& (Lows[0][3] < Lows[0][1]))
                    			{
                    				//Print("C7 True");
                    				holdValueShort = Lows[0][3];
                    			}
                    			
                    			if (holdValueShort != 0 && Highs[0][0] > midbb.Middle[0])
                    			{
                    				//Print("C8 True");
                    				holdValueShort = 0;
                    				//Print("Reset of holdValueShort " + holdValueShort);
                    			}
                    			
                    			
                    			if (holdValueShort != 0)
                    			{
                    				//Print("C9 True");
                    				//Print("Low price at last condition was " + holdValueShort);
                    			}
                    			
                    			if ((holdValueShort < midbb.Middle[0])
                    			&& (Highs[1][2] <= SMAmonth[2])
                    			&& (Highs[1][1] <= SMAmonth[1])
                    			&& (Highs[1][0] <= SMAmonth[0])
                    			&& (holdValueShort != 0)
                    			//&& (ATRdaily[0] >= .75)
                    			&& (RSIweek[0] <= 25)
                    			//&& (RSIdaily[0] <= 25)
                    		      && (GetCurrentAsk(0) >= 40)
                    			//&& (Slope(DMI.DiPlus, 1, 0) < -1)
                    			//&& (Slope(DMI.DiMinus, 1, 0) > 1)
                    			//&& (DMI.DiMinus[0] >= 50)
                    			//&& (StoConfirmWeek.K[0] < StoConfirmWeek.D[0])
                    			//&& (StoConfirmMonth.K[0] < StoConfirmMonth.D[0])
                    			&& (Lows[0][0] < holdValueShort))
                    			{
                    				Print("C10 True");
                    				int myQuantity = (int) ((1200)/(midbb.Middle[0]) - holdValue);
                    				EnterShort(3, myQuantity, @"");
                    				holdValueShort = 0;
                    			} else if (holdValueShort != 0 && Lows[0][0] < holdValueShort - .02)
                    			{
                    				//Print("C11 True");
                    				holdValueShort = 0;
                    				//Print("Reset of holdValueShort" + holdValueShort);
                    			}
                    			
                    			if (Highs[0][0] > SMAmonth[0])
                    			{
                    				//Print("C12 True");
                    				ExitShort();
                    			}
                    		}
                    Please let me know if I may be of any further assistance.
                    Chris L.NinjaTrader Customer Service

                    Comment


                      #11
                      Thanks...

                      For some reason it is still not taking any trades off the tick data.

                      Can you think of a reason why?

                      Comment


                        #12
                        Hello.

                        Thanks for the reply.

                        Have you made any changes to the script to ensure that your entry conditions are being hit? The reason the strategy was not making trades on the tick series is that the conditions were never being reached at all. The logic of the script must be evaluated to find out why it is not making trades. Have you made any adjustments to the script and confirmed that the entry condition is indeed being reached?

                        I look forward to your reply.
                        Chris L.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Lumbeezl, 01-11-2022, 06:50 PM
                        31 responses
                        816 views
                        1 like
                        Last Post NinjaTrader_Adrian  
                        Started by xiinteractive, 04-09-2024, 08:08 AM
                        5 responses
                        13 views
                        0 likes
                        Last Post NinjaTrader_Erick  
                        Started by swestendorf, Today, 11:14 AM
                        2 responses
                        6 views
                        0 likes
                        Last Post NinjaTrader_Kimberly  
                        Started by Mupulen, Today, 11:26 AM
                        0 responses
                        6 views
                        0 likes
                        Last Post Mupulen
                        by Mupulen
                         
                        Started by Sparkyboy, Today, 10:57 AM
                        1 response
                        6 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Working...
                        X