Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trying to backtest using tick data with 5min chart.

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

    Trying to backtest using tick data with 5min chart.

    Maybe I am missing something here, I have looked for hours for the solution but can't seem to find it in this forum. I am sorry if it has been done before.

    I want to buy/sell once the FastMA is above/below SlowMA when there is a minimum spread between them. However I want to buy/sell once the price comes back to touch the slow MA.

    Below is my code:

    protected override void OnBarUpdate()
    {
    CalculateOnBarClose = false;

    // Condition set 1
    if (EMA(8)[0] > EMA(LinReg(80), 32)[0]
    && (EMA(8)[0] - EMA(LinReg(80), 32)[0]) >= 0.00015)
    {
    if (CrossBelow (GetCurrentAsk(), EMA(LinReg(80), 32),1)) //need to see tick data but stay on 5 min chart!!
    {
    EnterLong(1, "Long");
    }
    }

    // Condition set 2
    if (EMA(8)[0] < EMA(LinReg(80), 32)[0]
    && (EMA(LinReg(80), 32)[0]) - EMA(8)[0] >= 0.00015)
    {
    if (CrossAbove (GetCurrentAsk(), EMA(LinReg(80), 32),1))
    {
    EnterShort(1, "Short");
    }
    }
    }

    However when using GetCurrentAsk() or Bid() in backtesting it uses the Close().

    For example my problem is that when the price closes below the SlowMA i get long, but when it only touches the SlowMA and closes above, I do not get a signal to go long. This is the problem. I have turned the CalculateOnBarClose() = false so that it uses tick data, but because I am using a 5min chart, in the OnBarUpdate() it is still waiting for the close of the 5min candle.

    It is driving me crazy. Anyway to set the buy once it touches the slow MA etc.?
    Attached Files

    #2
    Hello twitch,

    Thank you for your post.

    Backtesting in the Strategy Analyzer is always CalculateOnBarClose = True, no matter what you have set this to in the NinjaScript code.

    To simulate an intra-bar fill you will need to add another period type such as a tick interval to your strategy. For information and a reference sample on this item please visit the following link: http://www.ninjatrader.com/support/f...ead.php?t=6652

    Please let me know if you have any questions.

    Comment


      #3
      Hopefully small problem.

      Hi,

      I have taken your advice and modified the code. It now looks like:

      protected override void Initialize()
      {
      TraceOrders = true;

      /* Add a secondary bar series.
      Very Important: This secondary bar series needs to be smaller than the primary bar series.

      Note: The primary bar series is whatever you choose for the strategy at startup. In this example I will
      reference the primary as a 5min bars series. */

      Add(PeriodType.Tick, 89); //BarsInProgress Index = 1

      Add(EMA(8));
      Add(EMA(LinReg(80), 32));
      Add(LinReg(80));
      Add(ADX(14));
      Add(RSI(14, 3));
      Add(RSI(14, 3));
      Add(EMA(8));
      Add(EMA(LinReg(80), 32));
      Add(LinReg(80));
      Add(ADX(14));
      Add(RSI(14, 3));
      Add(RSI(14, 3));
      SetProfitTarget("", CalculationMode.Ticks, 10);
      SetStopLoss("", CalculationMode.Ticks, 8, false);

      CalculateOnBarClose = false;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      /* When working with multiple bar series objects it is important to understand the sequential order in which the
      OnBarUpdate() method is triggered. The bars will always run with the primary first followed by the secondary and
      so on.

      Important: Primary bars will always execute before the secondary bar series.
      If a bar is timestamped as 12:00PM on the 5min bar series, the call order between the equally timestamped 12:00PM
      bar on the 1min bar series is like this:
      12:00PM 5min
      12:00PM 1min
      12:01PM 1min
      12:02PM 1min
      12:03PM 1min
      12:04PM 1min
      12:05PM 5min
      12:05PM 1min */

      if (BarsInProgress == 1) //primary bar series, do the following. 0 = primary bars, 1 = secondary bars, 2 = tertiary bars
      {
      //Condition set 1
      if (EMA(BarsArray[0],8)[0] > EMA(BarsArray[0]LinReg(80), 32)[0])
      {
      if (GetCurrentAsk() == EMA(BarsArray[0]LinReg(80), 32)[0])
      {
      // The entry condition is triggered on the primary bar series, but the order is sent and filled on the secondary bar series.
      EnterLong(89, 1, "Long: Tick Chart");
      }
      }

      // Condition set 2
      if (EMA(BarsArray[0],8)[0] < EMA(BarsArray[0]LinReg(80), 32)[0])

      {
      if (GetCurrentBid() == EMA(BarsArray[0]LinReg(80), 32)[0])
      {
      EnterShort(89, 1, "Short: Tick Chart");
      }
      }
      }

      if (BarsInProgress == 0)
      {
      return;
      }

      else
      {
      return;
      }
      }

      What I am trying to do here, is have 5min primary and 89 tick secondary bars.

      If the 5min EMA(8) > 5min EMA(LinReg(8),32) and the price comes back to the slow EMA i want to go long. I am trying to say on every 89 tick barUpdate() when the price == 5min slow EMA then go long.

      Please advise.

      Thanks

      Comment


        #4
        Hello twitch,

        Thank you for your response.

        The OnBarUpdate() method is called on each update of a bar, so in this case on each 89 tick bar update.

        However, the first condition if returned true will look to the second condition within the { } and if the condition is false it does nothing. Let's say the second condition is true but the first one is not, then we have the same case where nothing happens yet again.

        So to clarify on your conditions here are we looking for the if (EMA(BarsArray[0],8)[0] > EMA(BarsArray[0]LinReg(80), 32)[0]) and the if (GetCurrentAsk() == EMA(BarsArray[0]LinReg(80), 32)[0]) conditions within the same bar update? Or the first condition on one bar update and then the second condition on any following bar updates?

        I look forward to your response.

        Comment


          #5
          Hi Patrick,

          The first condition should be if (EMA(BarsArray[0],8)[0] > EMA(BarsArray[0]LinReg(80), 32)[0]) and it should check for this on each bar update.

          If this is true, then it should check the second condition if (GetCurrentAsk() == EMA(BarsArray[0]LinReg(80), 32)[0]) to enter the long off the 89tick chart.

          So each bar update it is checking the 5min chart for the correct conditions. Once the coniditions are met it is asking if the 89 tick chart price == the slow MA on 5 min chart within the same bar update. this is where it should go long.

          I do this because if I wait for the 5min to update it could take along time and price would have moved. This way once the conditions are met on 5min, I am entering as soon as possible on the 89tick chart.

          Thanks

          Comment


            #6
            Hello twitch,

            Thank you for your response.

            So is your strategy working as expected? Or are you seeing cases where the strategy is not submitting the orders?

            If the strategy is not working as expected we recommend debugging your NinjaScript strategy. For information on debugging your NinjaScript strategy please visit the following link: http://www.ninjatrader.com/support/f...ead.php?t=3418

            Please let me know if I may be of further assistance.

            Comment


              #7
              Hi Patrick,

              I have modified the code with the flags, but I am still not executing any trades.

              Please have a look and tell me if you see any errors why this could be?

              protected override void OnBarUpdate()
              {
              //Long Condition
              if (EMA(8)[0] > EMA(LinReg(80), 32)[0]
              && (EMA(8)[0] - EMA(LinReg(80), 32)[0]) >= 0.0005)
              {
              if (Swing(5).SwingLow[1] == Swing(5).SwingLow[0])
              {
              LongFlag = true;

              if (LongFlag == true && BarsSinceExit() > 5)
              {
              EnterLongLimit(1, Swing(5).SwingLow[0] * TickSize, "LongSwing");
              Variable1 = Swing(5).SwingHigh[0] + -1 * TickSize;
              SetProfitTarget(CalculationMode.Price, Variable1);
              }
              }

              else
              {
              LongFlag = false;
              }
              }

              //Short Condition
              if( EMA(8)[0] < EMA(LinReg(80), 32)[0]
              && (EMA(LinReg(80), 32)[0]) - EMA(8)[0] >= 0.0005)
              {
              if (Swing(5).SwingHigh[1] == Swing(5).SwingHigh[0])
              {
              ShortFlag = true;

              if (ShortFlag == true && BarsSinceExit() > 5)
              {
              EnterShortLimit(1, Swing(5).SwingHigh[0] * TickSize, "ShortSwing");
              Variable2 = Swing(5).SwingLow[0] + 1 * TickSize;
              SetProfitTarget(CalculationMode.Price, Variable2);
              }
              }

              else
              {
              ShortFlag = false;
              }
              }
              }

              Thanks.

              Comment


                #8
                Hello twitch,

                Thank you for your update.

                Please enable TraceOrders for your strategy and pepper your code with Print() statements after conditions to see if the conditions are returning true.

                For information on TraceOrders please visit the following link: http://www.ninjatrader.com/support/f...ead.php?t=3627

                Please let me know if you have any questions.

                Comment


                  #9
                  Hi Patrick,

                  I have now completed what I set out to acheive. Thank you.

                  I now want to set a rule whereby there will be no more signals IF the daily ATR has been reached. I was trying max/min with bars array but can't seem to find out how to do it?

                  Thanks

                  Comment


                    #10
                    Hello twitch,

                    Thank you for your post.

                    Please advise the complete condition you are using here to determine if the Daily ATR is reached.

                    I look forward to your response.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Shansen, 08-30-2019, 10:18 PM
                    25 responses
                    949 views
                    0 likes
                    Last Post NinjaTrader_BrandonH  
                    Started by JonesJoker, 04-22-2024, 12:23 PM
                    8 responses
                    41 views
                    0 likes
                    Last Post JonesJoker  
                    Started by timko, Today, 06:45 AM
                    0 responses
                    3 views
                    0 likes
                    Last Post timko
                    by timko
                     
                    Started by Waxavi, 04-19-2024, 02:10 AM
                    2 responses
                    39 views
                    0 likes
                    Last Post poeds
                    by poeds
                     
                    Started by chbruno, Yesterday, 04:10 PM
                    1 response
                    44 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Working...
                    X