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

Backtesting Buy on Close

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

    Backtesting Buy on Close

    Hi,

    I'm new to NT and have an idea for a swingtrading strategy I would like to backtest in NT.

    The basic idea of the strategy is quite simple; to go long at todays close when FastStochastics(1,2) is less than 10, and exit when FastStocastics(1,2) is more than 50.

    Now to the problem: I would like to backtest this strategy based on DAILY data (OHLC), since I do not have access to more granular data. I know this will give a hypothetical result since you cannot actually buy at the close price. However, I would like the backtesting to be done with daily OHLC data, and if the right conditions are met, I would like NT to buy at the theoretical Close price, and sell at the theoretical close price when my conditions are met. Is this possible in NT? How?

    My current code is like this:
    protected override void Initialize()
    {

    CalculateOnBarClose = true;
    Add(StochasticsFast(1, FastK));
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (StochasticsFast(1, FastK).D[0] < FastKBuy)
    {
    EnterLong(DefaultQuantity, "Buy");
    }

    // Condition set 2
    if (StochasticsFast(1, FastK).D[0] > FastKSell)
    {
    ExitLong("Exit", "Sell");
    }
    }

    With the current code, the order is placed the next day, which is not what I want.

    Regards
    / Johan

    #2
    Hello,

    In this case you may want to use Limit or Stop orders along with the proper Fill type setting below :

    Default
    An algorithm that takes a conservative and more realistic approach to filling limit and stop limit orders.
    • Limit orders only fill if the limit price was penetrated
    • Limit orders are always filled at the limit price specified never better (for example, if a limit order is submitted on bar n, NinjaTrader will check if the order is filled on bar n+1, if this bar gaps down and the limit order was a buy, the order would be filled at the limit price and NOT the high of bar n+1)

    Liberal
    An algorithm that takes a liberal approach to filling limit and stop limit orders.
    • Limit orders fill if the limit price was touched
    • On gap down bars, buy limit orders will fill at the high of the gap down bar
    • On gap up bars, sell limit orders will fill at the low of the gap up bar

    Here is some more information on backtesting below :

    Backtesting : http://www.ninjatrader.com/support/h...a_strategy.htm

    How bars are built : http://www.ninjatrader.com/support/h..._are_built.htm

    Discrepancies in backtesting : http://www.ninjatrader.com/support/h...ime_vs_bac.htm

    Note that with your lack of intrabar granularity you would typically get these fills listed as on the next bar. You may consider getting a supplementary data provider such as Kinetick, or a demo account from one of our supported providers to get the intrabar data for more accurate backtesting results.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      I encounter similiar problem in the past and wrote my own FilllOnClose.
      The code is attached.
      Just place the file in the Type folder and compile Ninja.

      Good luck!
      Attached Files

      Comment


        #4
        Thanks Freewind,

        I now get the price from the Close, but the actual trades are logged the next day, so all graphs and tradelogs are wrong (+1 day).

        If I just wanted to test one stock I could live with that error. However, I plan to take this a little bit further and rotate between different stocks where I get the Buy signal, and it will become messy if the orders are logged the wrong day.

        Is there a way to solve that too?

        Comment


          #5
          To make sure I'm understanding, you mean that you would like to execute an order at the Close price of a Tuesday bar, and see these results for Tuesday? But currently it's showing the graphs/results for Wednesday?
          MatthewNinjaTrader Product Management

          Comment


            #6
            Originally posted by NinjaTrader_Matthew View Post
            To make sure I'm understanding, you mean that you would like to execute an order at the Close price of a Tuesday bar, and see these results for Tuesday? But currently it's showing the graphs/results for Wednesday?
            Yes, that is exacly what I want.

            Comment


              #7
              The Close price is not determined until the end of the session. This means that the order would not be executed until the next session, which would be reported as the next day.
              MatthewNinjaTrader Product Management

              Comment


                #8
                Originally posted by NinjaTrader_Matthew View Post
                The Close price is not determined until the end of the session. This means that the order would not be executed until the next session, which would be reported as the next day.
                So, are you saying that there is no way to trigger the order the same day then? (even if it means changing the ordermechanisms).

                As I see it, most (easily) available data is daily OHLC data. This should be enough to develop/evaluate swingtrading strategies. I really hope there is a way to do this in Ninjatrader, otherwise I'll have to go somewhere else.

                Usually there is some sort of workaround. I would be surprised if I am the only one struggling with this.

                Comment


                  #9
                  If you would like to execute on the same day, you would need intraday data to execute the order before the close of that day.
                  MatthewNinjaTrader Product Management

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by algospoke, Today, 06:40 PM
                  0 responses
                  10 views
                  0 likes
                  Last Post algospoke  
                  Started by maybeimnotrader, Today, 05:46 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post maybeimnotrader  
                  Started by quantismo, Today, 05:13 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post quantismo  
                  Started by AttiM, 02-14-2024, 05:20 PM
                  8 responses
                  168 views
                  0 likes
                  Last Post jeronymite  
                  Started by cre8able, Today, 04:22 PM
                  0 responses
                  10 views
                  0 likes
                  Last Post cre8able  
                  Working...
                  X