• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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 futurenick, Today, 06:26 AM
                      1 response
                      9 views
                      0 likes
                      Last Post nkhoi
                      by nkhoi
                       
                      Started by NinjaCustomer, Today, 12:29 PM
                      1 response
                      6 views
                      0 likes
                      Last Post NinjaTrader_ShawnB  
                      Started by Steve L, Today, 11:32 AM
                      0 responses
                      5 views
                      0 likes
                      Last Post Steve L
                      by Steve L
                       
                      Started by prabbala, Today, 10:41 AM
                      0 responses
                      3 views
                      0 likes
                      Last Post prabbala  
                      Started by bebbus, Today, 10:36 AM
                      0 responses
                      4 views
                      0 likes
                      Last Post bebbus
                      by bebbus
                       
                      Working...
                      X