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

Indicator Values Incorrect when using Multiple Timeframes

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

    Indicator Values Incorrect when using Multiple Timeframes

    Hello,

    I have created a simple MTF strategy that has 3 timeframes (i.e. BarsArray0, BarsArray[1], and BarsArray[2]). An SMA() has been applied to each and the values reported when printing based on BarsInProgress is incorrect for both BarsInProgress == 1 and BarsInProgress == 2 (i.e longer timeframe bars). I am testing this from the Strategy Analyzer and I was using the following:
    Contract = ES
    BarsArray[0] = 10 Min Bars
    BarsArray[1] = 60 Min Bars
    BarsArray[2] = 240 Min Bars

    The strat is set to CalculateBarsOnClose = true.

    Additionally, I have tested this with both EMA and SMA as examples and both are giving incorrect indicator values for BarsArray[1] and BarsArray[2].

    I have attached the strat for your review.

    Please let me know why this is not working as expected.

    Thanks,
    Attached Files

    #2
    Hi tornadoatc, thanks for your note.

    Unfortunately, I can not profile or review any custom code. I attached a sample that works ok on my end. I would suggest reducing the code down until the bug is found.

    Best regards,

    -ChrisL

    Attached Files
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hello ChrisL,

      I am not sure why you would not be able to review a simple strategy that I have provided? There is nothing proprietary about it.

      Additionally the code you provided is based on an Indicator. Mine is coded as a Strategy.

      I am providing the code here:
      Code:
      namespace NinjaTrader.NinjaScript.Strategies
      {
          public class IbdTestMTFStratEMA : Strategy  
          {
              private bool debug = true;
              //private NinjaTrader.NinjaScript.Indicators.LizardIndicators.amaSuperTrendU11 
              //    lo_EMA_LTC, lo_EMA_MTC, lo_EMA_STC;
              private SMA lo_EMA_LTC, lo_EMA_MTC, lo_EMA_STC;
      
      
      
              // parameters for Adding Additional DataSeries
              private readonly int MTCPeriodType = 4;         // 0=Tick, 1=Volume, 2=Range, 3=Second, 4=Minute, 5=Day, 6=Week
              private readonly int MTCPeriodValue = 60;
              private readonly int LTCPeriodType = 4;
              private readonly int LTCPeriodValue = 240;
      
              public DateTime dt;
      
              /***********************************************************************************************************************/
              /**************************************  OnStateChange  ****************************************************************/
              /***********************************************************************************************************************/
      
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description = @"System Discovery Script";
                      Name = "IbdTestMTFStratEMA";
                      Calculate = Calculate.OnBarClose;
                      EntriesPerDirection = 1;
                      EntryHandling = EntryHandling.UniqueEntries;  // for Unique named entries ... VS AllEntries???
                      IsExitOnSessionCloseStrategy = true;
                      ExitOnSessionCloseSeconds = 30;
                      IsFillLimitOnTouch = false;
                      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
                      OrderFillResolution = OrderFillResolution.Standard;
                      Slippage = 0;
                      StartBehavior = StartBehavior.WaitUntilFlat;
                      TimeInForce = TimeInForce.Gtc;
                      TraceOrders = false;
                      IncludeTradeHistoryInBacktest = false; // this is needed to use SystemPerformance.AllTrades.Count and others
                      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
                      StopTargetHandling = StopTargetHandling.ByStrategyPosition;   //VS PerEntryExecution
                      //This parameter will default to ByStrategy Position with IB and TD Brokers.   May result in stacked orders  
                      IsInstantiatedOnEachOptimizationIteration = false;
      
                  }
                  //********************************************************/
                  //*******************   State Configure ******************/
                  //********************************************************/
                  else if (State == State.Configure)
                  {
                      BarsRequiredToTrade = 50;
      
                      AddDataSeries(BarsPeriodType.Minute, 60);
                      AddDataSeries(BarsPeriodType.Minute, 240);
      
                  }
      
                  //********************************************************/
                  //*******************   State Data Loaded ****************/
                  //********************************************************/
                  else if (State == State.DataLoaded)
                  {
                      if (debug)
                          Print("OnStateChange -> State.DataLoaded");
      
                      // STC Indicator 
                      if (debug)
                          Print("STC Signal Chart = " + BarsArray[0].Instrument.FullName + " " + BarsArray[0].BarsPeriod); // Signal Chart
      
                      //lo_EMA_STC = amaSuperTrendU11(Inputs[0], amaSuperTrendU11BaseType.Median, amaSuperTrendU11VolaType.True_Range, amaSuperTrendU11OffsetType.Wilder, false, 8, 15, 2.5);
                      lo_EMA_STC = SMA(20); 
                      if (lo_EMA_STC == null)
                      {
                          Print("Instance of lo_EMA_STC not correctly created in State.DataLoaded");
                          //SetState(State.Terminated);
                      }
                  }
              }
              /***********************************************************************************************************************/
              /**************************************  OnBarUpdate  ******************************************************************/
              /***********************************************************************************************************************/
              protected override void OnBarUpdate()    
              {
                  // All logic Requires that MTC = BIP0 && STC = BIP1 && LTC = BIP2     Possible problem when not all three charts loaded??
                  #region Return when Number of Bars < BarsRequiredToTrade
                  if (BarsInProgress == 0 && CurrentBars[0] <= BarsRequiredToTrade)
                  {
                      return;
                  }
                  else if (BarsInProgress == 1 && CurrentBars.Length >= 2)
                  {
                      if (CurrentBars[1] <= BarsRequiredToTrade) return;
                  }
                  else if (BarsInProgress == 2 && CurrentBars.Length == 3)
                  {
                      if (CurrentBars[2] <= BarsRequiredToTrade) return;
                  }
                  #endregion
      
                  DateTime dt = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, Time[0].Hour, Time[0].Minute, Time[0].Second);
      
                  #region Setting MultiTimeframe Indicators 
                  if (lo_EMA_MTC == null)
                      lo_EMA_MTC = SMA(BarsArray[1], 20);
      
                  if (LTCPeriodValue != 0 && lo_EMA_LTC == null) // then LTC Bars Loaded
                      lo_EMA_LTC = SMA(BarsArray[2], 20); 
                  #endregion Setting MultiTimeframe Indicators 
      
                  if (BarsInProgress == 0)  // Medium Term Chart 
                  {
                      //Print(dt);
                      //Print("STCBar EMA(20) = " + lo_EMA_STC[0]); 
                  }
      
                  if (BarsInProgress == 1)  // Medium Term Chart 
                  {
                      // Print MTC Values used for Logic 
                      Print(dt);
                      Print("MTCBar EMA(20) = " + lo_EMA_MTC[0]);
                  }
      
                  if (BarsInProgress == 2) // Long Term Chart 
                  {
                      // Print LTC Values used for Logic 
                      Print(dt);
                      Print("LTCBar EMA(20) = " + lo_EMA_LTC[0]);
      
                  }
      
                  if (lo_EMA_LTC[0] > lo_EMA_LTC[1] && lo_EMA_MTC[0] > lo_EMA_MTC[1] && lo_EMA_STC[0] > lo_EMA_STC[1] && lo_EMA_STC[0] > lo_EMA_STC[1])
                      EnterLong();
                  if (Position.MarketPosition == MarketPosition.Long && lo_EMA_STC[0] < lo_EMA_STC[5])
                      ExitLong();
      
                  if (lo_EMA_LTC[0] < lo_EMA_LTC[1] && lo_EMA_MTC[0] < lo_EMA_MTC[1] && lo_EMA_STC[0] < lo_EMA_STC[1] && lo_EMA_STC[0] < lo_EMA_STC[1])
                      EnterShort();
                  if (Position.MarketPosition == MarketPosition.Short && lo_EMA_STC[0] > lo_EMA_STC[5])
                      ExitShort();
              }
          }
      }
      Why does this simple strategy print different values for the indicators than what is shown when the same indicator is applied to a chart for the 60 min and 240 min periods ?

      Comment


        #4
        The attached contains no references to "custom" code.
        Attached Files

        Comment


          #5
          Hello tornadoatc,

          Thank for your replies.

          Regarding, "I am not sure why you would not be able to review a simple strategy that I have provided? There is nothing proprietary about it." To clarify, custom code would be any code that you write. As we are a small team we do not provide debugging or code writing services.

          It looks like you are already using print statements and that would be the best way to debug your code.

          Can you provide an example chart that shows the indicators that have the values that you are expecting to see in your code along with a screenshot of the Ninjascript output window with the related time points of the chart example? I would like to see what you are seeing that shows incorrect values. In the chart, please be sure to expand the bars to help show the time base along with the price scale.
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Disregard ... I think this is user error ..
            Last edited by tornadoatc; 01-27-2020, 12:04 PM.

            Comment


              #7
              Hello tornadoatc,

              Thanks for your reply.

              I replicated your set-up and using standard prints I don't see any issues. I've attached a screenshot that shows the same data (I am on mountain time so your 10:00 AM bar is my 8:00 AM bar). I've placed a green rectangle around the bar, the data box and the NS output for the same date and time showing that all three line up.

              I reworked your code in terms of the Print statements and would ask that you modify your code as follows to verify that you do see the same.

              if (BarsInProgress == 0) // Medium Term Chart
              {
              Print(Times[0][0]+" SMA20 on 15m bars: " + lo_EMA_STC[0]);
              }

              if (BarsInProgress == 1) // Medium Term Chart
              {
              Print(Times[1][0]+" SMA20 on 60m bars = " + lo_EMA_MTC[0]);
              }

              if (BarsInProgress == 2) // Long Term Chart
              {
              Print(Times[2][0]+" SMA20 on 240m bars = " + lo_EMA_LTC[0]);
              }


              Click image for larger version

Name:	tornadoatc-1.PNG
Views:	322
Size:	174.4 KB
ID:	1085244
              Paul H.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by xiinteractive, 04-09-2024, 08:08 AM
              4 responses
              12 views
              0 likes
              Last Post xiinteractive  
              Started by Mupulen, Today, 11:26 AM
              0 responses
              1 view
              0 likes
              Last Post Mupulen
              by Mupulen
               
              Started by Sparkyboy, Today, 10:57 AM
              1 response
              5 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by swestendorf, Today, 11:14 AM
              2 responses
              4 views
              0 likes
              Last Post NinjaTrader_Kimberly  
              Started by TheMarlin801, 10-13-2020, 01:40 AM
              21 responses
              3,917 views
              0 likes
              Last Post Bidder
              by Bidder
               
              Working...
              X