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 and sell at limit on daily bar chart

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

    backtesting Buy and sell at limit on daily bar chart

    Hello,

    I would like to back test a strategy which does the following:

    EnterShort at open[0] + 10 ticks
    EnterLong at open[0] - 10 ticks
    Exit All Positions On Close

    if the high > Open + 10 ticks, I should be short
    if the Low is < Open + 10 ticks I should be long

    whatever happens next, i should be squaring positions on the close.

    Could anyone tell me how to create this simple strategy ?

    My issue so far is that orders happen on next daily bar only once (meaning i am short or long whichever the system has decided should be first) and the open position is closed only when the system reverse.

    I have set the number of positions more than 1
    I name all entries with a different name
    I apply the backtest on daily bars

    Should i be using the unmanaged methodology ?

    Thanks for your help

    Guilhem

    #2
    Hello guilhem,

    Thank you for your post.

    The following example demonstrates this:

    Code:
     protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0) 
                    return;
    
                if (CurrentBars[0] < 1)
                return;
    
                //if the high > Open + 10 ticks, I should be short
                //if the Low is < Open + 10 ticks I should be long
                if(Position.MarketPosition == MarketPosition.Long)
                {
                    ExitLong();
                }
    
                if(Position.MarketPosition == MarketPosition.Short)
                {
                    ExitShort();
                }
    
                if(High[0] > Open[0] + TickSize*10)
                {
                    EnterShort();
                }
    
                if(Low[0] < Open[0] + TickSize*10)
                {
                    EnterLong();
                }
            }
    When you run this logic on daily bars, it will first check if you are already in a position, then it will evaluate your logic after the exit checks.

    Please let me know if I can assist further.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      My problems are the following:
      a - execution happens on next bar
      b - i can only enter one of those trades although it is perfectly ok to do both trades in reality
      c - i would like to sellbuy at my price with a limit order

      should i be using the unmanaged approach therefore ?

      Rgds

      Comment


        #4
        Hello guilhem,

        Thank you for your reply.

        This should not require the use of the unmanaged approach. The execution is happening on the next bar because the strategy is programmed to exit on the close of the bar. The logic will take place at the first tick of the next bar.

        Could you please clarify point 'b'. My test strategy was able to make long and short trades.

        To use a limit order instead of a market order, you may use either EnterShortLimit() or EnterLongLimit() These take a limit price as a parameter and will allow you to target a specific price level.

        If possible, could you please export your script and post it here so I can review?

        I look forward to assisting further.
        Last edited by NinjaTrader_ChrisL; 03-18-2019, 02:22 PM.
        Chris L.NinjaTrader Customer Service

        Comment


          #5

          protected override void OnBarUpdate() { if (BarsInProgress != 0) return; if (CurrentBars[0] < 1) return; EnterShortLimit(Open[0] + 0.10); EnterLongLimit(Open[0] - 0.10); } on the strategy analyser, i set the positions to be closed at eod. I was expecting to get filled on both order if High and Low exceeded the limit orders for example. Never more than one trade takes place even if both high and low are further to the open than 10 ticks Also, which is annoying, positions are open on next day rather than current day. And finally, when positions are closed, they are on next bar rather than current bar. I was hoping to get done on the current bar if my buying order was likely to be filled ie Low < Open of current bar - 10 tick i was hoping to get done on the current bar if my selling order was likely to be filled ie High > Open of current bar + 10 tick and I was expecting then all positions to be closed if any order got filled...

          Comment


            #6
            your export link does not work :-(

            Comment


              #7
              Hello guilhem,

              Thanks for your patience.

              I fixed the link for exporting in my last post. I should have an example for you tomorrow morning.

              I look forward to assisting further.
              Chris L.NinjaTrader Customer Service

              Comment


                #8
                Let me start over.
                Here is what I would like to do.
                I want to backtest on daily data the following:


                if (high of current bar > Open of current bar + 10 ticks) then, I want to be short 1 lot at Open of current bar + 10 ticks in the current bar.
                if (low of current bar < Open of current bar - 10 ticks) then, i want to be long 1 lot at Open of current bar - 10 ticks in the current bar
                Whatever happened, I want to close all positions during the current bar at close of market.

                I am unable to do this on daily bars.

                I suppose the best way to do this therefore has to be using intraday bars ?

                But considering that there is not forward looking in my algo, I would have thought there should be a way to get this done on daily bars ?

                Best

                Guilhem

                Comment


                  #9
                  Here is the secret weapon ;-)

                  Attached Files

                  Comment


                    #10
                    my issue really is why can t i get an order filled on a daily bar chart on current bar if it is not forward looking ? which order type should i use to do so ?

                    thanks for your replies ;-)

                    Best

                    Guilhem

                    Comment


                      #11
                      Hello guilhem,

                      Thank you for your patience.

                      If you need intrabar resolution, I would recommend running the logic on a 1-minute series so you can check the time of day down to the minute. I have attached a simple example where the strategy goes long at the beginning of the session and exits at the end of the session (CME US Index Futures RTH). This idea can be expanded by setting up two limit orders at the beginning of the session, one to go long and the other to go short. If one of those orders are filled, cancel the other order with CancelOrder. For an example of how to use CancelOrder properly, please see this reference sample on CancelOrder(). At the end of the session, if you are long, exit the long position. If the position is short, close the short position. You may place this file within Documents\NinjaTrader 8\bin\Custom\Strategies to compile and run it.

                      Please let me know if I can assist further.
                      Attached Files
                      Chris L.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Christopher_R, Today, 12:29 AM
                      0 responses
                      8 views
                      0 likes
                      Last Post Christopher_R  
                      Started by sidlercom80, 10-28-2023, 08:49 AM
                      166 responses
                      2,235 views
                      0 likes
                      Last Post sidlercom80  
                      Started by thread, Yesterday, 11:58 PM
                      0 responses
                      3 views
                      0 likes
                      Last Post thread
                      by thread
                       
                      Started by jclose, Yesterday, 09:37 PM
                      0 responses
                      7 views
                      0 likes
                      Last Post jclose
                      by jclose
                       
                      Started by WeyldFalcon, 08-07-2020, 06:13 AM
                      10 responses
                      1,415 views
                      0 likes
                      Last Post Traderontheroad  
                      Working...
                      X