Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy Trades One Candle Late

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

    Strategy Trades One Candle Late

    I built an indicator that has characteristics of a strategy in that buy-sell situations are created. I ported the "indicator" relevant trading code to a strategy. It's identical code with the draw and print code removed. The strategy code enters trades one candle after the indicator code enters trades. A synopsis of the code is below:

    Indicator code:
    protected override void OnBarUpdate()
    {

    if (FirstTickOfBar)
    {
    Coded Logic

    //Trying to enter an UP trade
    if (X=Y) //redundant
    if (X=Y)
    if (X=Y)
    if ((X=Y) | (X=Y))
    {
    upentry = true; //for the indicator
    EnterLong(); //for the strategy
    }
    }

    }

    If the indicator calls the trade, say, on the first tick of bar 100, the strategy enters the trade on the first tick of bar 101. If it makes a difference, I'm using 15 minute candles.

    The is a lot of logic based on bar number like this:

    if ((Close[1] - Open[1]) < 0.0)
    {lastred =
    true;}
    else
    {lastred = false;}


    volatility = (High[2] + High[3] + High[4] - Low[2] - Low[3] - Low[4])/3.0;

    Can anyone tell me what to do? This variance makes a hugh difference as one might suspect.

    Thanks!
    Last edited by epcompany; 02-06-2009, 09:57 AM.

    #2
    Hi epcompany, welcome to the NinjaTrader support forums!

    If you run the strategy on CalculateOnBarClose = true then you will wait for the 15 min bar to close until it can place an order to be executed on the next bar, unfortunately this is the nature of things when backtesting.

    For more finetuning, you can use this approach - http://www.ninjatrader-support2.com/...ead.php?t=6652

    Comment


      #3
      Thanks for the response!

      Unfortunately, this is the code used:

      protectedoverridevoid Initialize()
      {
      CalculateOnBarClose =
      false;
      ClearOutputWindow();}

      protectedoverridevoid OnBarUpdate()
      {
      if (CurrentBar <40) return;

      if (FirstTickOfBar)
      {

      place order

      etc....
      }

      So, refering to the first post, everything I'm doing to get into a trade is done off the first tick, or so I think. My indicator gets into the trade state one bar and the strategy waits until the first tick of the next bar, or so it looks.

      Thanks again!

      Comment


        #4
        Try checking the condition of the last bar at FirstTickOfBar.

        if(FirstTickOfBar)
        if (orderId1.Length == 0 && atmStrategyId1.Length == 0 && ShortSignal[1] == 1)
        {Do your thing;}

        It looks like some your logic is doing this already:
        if ((Close[1] - Open[1]) < 0.0)
        {lastred =
        true;}
        else
        {lastred = false;}

        but basically all of your logic has to reference the last bar since there is really nothing to check on the current bar at first tick.
        Last edited by eDanny; 02-06-2009, 01:24 PM.
        eDanny
        NinjaTrader Ecosystem Vendor - Integrity Traders

        Comment


          #5
          Are you using any embedded indicators with CalculateOnBarClose set to true? If yes, remove those references and set none in your indicators and control this behavior from the strategy.

          Comment


            #6
            eDanny and Bertrand,
            Thanks very much for the interest! I appreciate it.

            eDanny: I will implement your test this weekend. Thanks!

            Bertrand: The only reference to CalculateOnBarClose is the " = false" one I showed. It is never referenced as true unless it picks it up in one of the called NT indicators I use. I haven't traced into them.

            I do use references to other indicators in the strategy such as VMA.
            I use : VMA, Math, Atan, Open, Close, High, Low, CurrentBar, ClearOutputWindow, CalculateOnBarClose and that's about it.

            When running, I have a 15 minute ES chart running my custom indicator. Then I start the strategy built from the indicator from the strategy tab on the chart.

            While working from market data, the indicator shows entries by drawing entry points on the chart at precisely the points I programmed as entry. The strategy trades in the sim account one candle late.

            Interestingly, when running the indicator and strategy from historical data, the strategy is still one candle late.

            I'm still at a loss but thanks again for the help! I'll ponder the problem using recorded data this weekend.

            Comment


              #7
              If you are calling indicators from a strategy you CANNOT have any CalculateOnBarClose line at all in the indicator. Please remove these lines from the indicator code. Thank you.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                Josh,
                That was VERY clear.

                I shall post haste remove the statement.

                As Arnold says "I'll be back" later and give the result.

                I appreciate all of the help and advice!

                Comment


                  #9
                  Same problem with one bar late trading

                  Josh,
                  I'm back and have not solved the problem. I have made changes.

                  I attached a screen shot to show what is happening.

                  The strategy is now self contained and only calls NT provided indicators like VMA.

                  The flow is still:

                  publicclass ATickTrader : Strategy
                  {
                  protectedoverridevoid Initialize()
                  {
                  CalculateOnBarClose = false;}
                  protectedoverridevoid OnBarUpdate()
                  Logic
                  Logic
                  if (FirstTickOfBar)
                  {
                  enter = down_start_price
                  stop = down_start_price + initial_stop;
                  EnterShort();
                  SetStopLoss(CalculationMode.Price, stop);
                  }

                  When the "enter =" instruction is processed, my strategy considers itself to have entered a trade. The next two instructions are for NT to enter a trade from the Strategy Tab.

                  Viewing the screen shot, you see that my strategy enters on the first tick of candle #1 and exits on the first tick of candle #3. NT executes the strategy's trade on the first tick of candle #2 and exits on the first tick of candle #4.

                  This happens on a 15 minute chart so it's not like something hiccuped.

                  I'm at a loss. Thanks very much for the help!

                  Larry

                  Comment


                    #10
                    Hi, do you see this in backtest or in realtime? The screenshot did not show properly, can you please repost it? Thanks!

                    Comment


                      #11
                      Pending the screenshot, you also need to move SetStopLoss() before the EnterShort() for it to apply to the short position you just acquired.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        I apologize for the delay. I wanted to get a concrete answer on real time data this week and did not.

                        The answer to the question is that the strategy trades one candle late on both historical and recorded data regardless of the timeframe. I moved the stop to a position in front of the "Enter" instruction and it made no difference.

                        I've made another attempt to upload the pic. It looks like it worked.

                        Thanks for the help! Entering on the right candle makes a big difference as you know.

                        Larry

                        Comment


                          #13
                          Hello,

                          I am sorry, there was no screen shot attached.

                          I did not read this entire thread, but is this occuring in backtesting only? Do you have CalculateOnBarClose sent to true?
                          DenNinjaTrader Customer Service

                          Comment


                            #14
                            Thanks for the response.

                            I have had Calculate on Bar Close set to true and set to false and commented out. It makes no difference. My strategy trades on the first tick of bar one and NT trades on the first tick of the next bar.

                            The pic didn't upload apparently because it was larger than the 97KB limit of the forum. I cropped it and hopefully it is attached this time.

                            Later today I intend to have my strategy produce trades from recorded data at the end of the current bar. If NT trades at the first tick of the next bar, that will work out but will seem like a force fit. I'll get back with the result of this experiment.

                            Thanks,

                            Larry
                            Attached Files

                            Comment


                              #15
                              Hello,

                              Yes, this is expected behavior. This is the nature of backtesting using OHLCV data. The criteria is met on one bar and the exectution happens on the next available price data, which is the next bar since we are only using OHLCV data. This link may help:
                              DenNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              647 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              367 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              108 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              571 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              573 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X