Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

BackTesting Stop Loss problem

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

    BackTesting Stop Loss problem

    Hi,
    I created a simple strategy that work fine on real time but when i am trying to run it in back-testing there is a problem :
    i have a entry point at the high of the last bar and then set the Setstoploss to the low of the last bar .
    the problem is if the bar i am going in was first below the low of the last bar - i will get a entry point - but immideatly the back-testing will show me that it is trigger a stop loss - even that the share was at that price before it hit my entry point trigger .

    as similar problem : i am moving the stop loss to entery once i reach the target and then sell 50% of my order size but then when the price hit the profit in the same bar that the entry was the system will trigger the stop loss even that the price was in the stop loss before it h the profit .

    i though on using a tick chart as my primary data set and a 5minute bar as a secondary -> but the setstoploss and the setprofittarget are working on the primer data so i wont be able to read the chart correctly :-(.
    that could have work only if i can show on the chart in the back testing the second data series ( 5 minu) and hide the tick chart . but i dont think that is an option

    As an example i added a snapshot of one case
    ; entry was on the blue arrow then it hit a profit on the top pink arrow, but then as i moved stop to the entry it immideatly took the stop - but actually it was only on the way up , and the stop was visited before it hit the profit .....


    i hope there is someone that can help me .
    Attached Files

    #2
    mandid,

    What is your calculate on bar close setting?

    Some of what you are describing could be due to the lack of intrabar granularity here.

    Here is a reference sample on backtesting with it : http://www.ninjatrader.com/support/f...ead.php?t=6652

    And here is a way to enter on one time frame and exit on another.

    Adam P.NinjaTrader Customer Service

    Comment


      #3
      problem is with the exit

      Hi Adam ,
      thx for the response - but proebly i didnt explain myself enough.

      the entry is no problem . the problem is that when i use the Setstoploss function that run on the primary data set - if i would use the primary as the tick - then i wont be able to understand why it went in or out from a trade ( as the entry is based on a 5 minute ,))
      and i cant show both graph - 5minute and the tick chart .

      the two sample code that you refered to explain only about entry to a trade .

      as for your question about calculation on bar close - i just want to go in when the candle goes above the last high .snd exit if it go below the last low . very simple example .
      Hope that explain more .
      thx
      ido.

      Comment


        #4
        mandid,

        The stop loss wouldn't be hit based on tick-by-tick operation here. Essentially the strategy analyzer backtests using the close of the bar, and then simulates the fills based on open/high/low for both entrance and exit orders.

        I would suggest trying the reference sample on "backtesting with intrabar granularity".

        You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by


        "Calculate on bar close" is the setting that tells your strategy to either operate on the closing of the bar only, or tick-by-tick in real time operation. When "CalculateOnBarClose" = true, this means that the strategy will only call OnBarUpdate() once every time a bar closes. When "CalculateOnBarClose" = false, the strategy will call OnBarUpdate() each incoming tick. When backtesting, its always the same as if "CalculateOnBarClose" = true unless you use that reference sample I provided.

        I believe what is happening is that you are expecting your stops to operate tick-by-tick, which without that reference sample would actually be simulated by the low and direction of the bar, etc. only when a bar closes in backtesting. Since the strategy isn't operating tick-by-tick it may open and close an order in the same bar if the conditions for entrance, and hitting a stop loss occurred, and since the bar lacks granularity there is no way to know how the bar was built from the incoming ticks.

        Please correct me if I am wrong. I would be happy to clarify, explain, etc. further. If you post your code it would be helpful as well.
        Last edited by NinjaTrader_AdamP; 05-23-2012, 07:11 AM.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          here is the code hope that might help

          Hi Adam,
          sorry i cant understand how the sample strategy that u refereed me can help me on the stop as they just showing how to go in the trade and my issue is the exit .
          i am totally understand that in the back-test the calculating is in the end of the bar but
          as long as i not able to the back testing stop loss run on shorter time frame then the primary data i can find a solution .

          i hope the next snap from the backs testing and a sample code with help me to explain
          first the basic code :

          in that case all i want is - when i have a red candle to go in long once we are above the high of the last bar and when i have green bar to go short below the low of the last bar .

          when i have a trigger i want to go long/short in tree part each with different target but all with the same stop

          and when i reached on one to the profit- i will move the stop loss of the other two to entry point .


          BUT as can be seen from the pic - when i the profit trigger it acctualy triger the stop loss on the first big green bar - and that not correct as after we reached the profit we continue up and not down .

          in this case i am working on a 1 minute bar .

          protected override void Initialize()
          {

          CalculateOnBarClose = true;
          }

          /// <summary>
          /// Called on each bar update event (incoming tick)
          /// </summary>
          protected override void OnBarUpdate()
          {
          double quant = risk/(High[0]-Low[0]+0.01);
          int quantP1 = Convert.ToInt32( quant/2 );
          int quantP23 = Convert.ToInt32( quant/4 );

          if ((ToTime(Time[0]) < ToTime(16, 30, 0)) ||(ToTime(Time[0]) > ToTime(19, 30, 0)))
          return;

          // Condition set 1 Long
          if (Open[0] > Close[0] && High[0]-Low[0] >0.03 )
          {
          SetProfitTarget("DRevUpP1", CalculationMode.Ticks, 100*(High[0]-Low[0]-0.01));
          SetProfitTarget("DRevUpP2", CalculationMode.Ticks, 100*2*(High[0]-Low[0]));
          SetProfitTarget("DRevUpP3", CalculationMode.Ticks, 100*4*(High[0]-Low[0]));
          SetStopLoss("DRevUpP1", CalculationMode.Ticks,100*(High[0]-Low[0]+0.02), false);
          SetStopLoss("DRevUpP2", CalculationMode.Ticks, 100*(High[0]-Low[0]+0.02), false);
          SetStopLoss("DRevUpP3", CalculationMode.Ticks, 100*(High[0]-Low[0]+0.02), false);

          entryOrderL1=EnterLongStopLimit(quantP1, High[0] + LmtSizeLong * TickSize, High[0] + StopSizeLong * TickSize, "DRevUpP1");
          EnterLongStopLimit(quantP23, High[0] + LmtSizeLong * TickSize, High[0] + StopSizeLong * TickSize, "DRevUpP2");
          EnterLongStopLimit(quantP23, High[0] + LmtSizeLong * TickSize, High[0] + StopSizeLong * TickSize, "DRevUpP3");

          }
          // Condition set 2 Short
          if ( Open[0] < Close[0] && High[0]-Low[0] >0.03 )
          {
          SetProfitTarget("DRevDownP1", CalculationMode.Ticks, 100*(High[0]-Low[0]-0.01));
          SetProfitTarget("DRevDownP2", CalculationMode.Ticks, 100*2*(High[0]-Low[0]));
          SetProfitTarget("DRevDownP3", CalculationMode.Ticks, 100*4*(High[0]-Low[0]));
          SetStopLoss("DRevDownP1", CalculationMode.Ticks,100*(High[0]-Low[0]+0.02), false);
          SetStopLoss("DRevDownP2", CalculationMode.Ticks, 100*(High[0]-Low[0]+0.02), false);
          SetStopLoss("DRevDownP3", CalculationMode.Ticks, 100*(High[0]-Low[0]+0.02), false);

          EnterShortStopLimit(quantP1, Low[0] - LmtSizeLong * TickSize, Low[0] - StopSizeLong * TickSize, "DRevDownP1");
          EnterShortStopLimit(quantP23, Low[0] - LmtSizeLong * TickSize, Low[0] - StopSizeLong * TickSize, "DRevDownP2");
          EnterShortStopLimit(quantP23, Low[0] - LmtSizeLong * TickSize, Low[0] - StopSizeLong * TickSize, "DRevDownP3");

          }
          }

          protected override void OnExecution(IExecution execution)
          {
          if (execution.Order != null && execution.Order.Name == "Profit target")
          {
          SetStopLoss("DRevUpP2", CalculationMode.Ticks, 2, false);
          SetStopLoss("DRevUpP3", CalculationMode.Ticks, 2, false);
          SetStopLoss("DRevDownP2", CalculationMode.Ticks, 2, false);
          SetStopLoss("DRevDownP3", CalculationMode.Ticks, 2, false);
          }

          }




          hope this will help and if not can i contact you by phone ? :-)

          thx
          Ido.
          Attached Files

          Comment


            #6
            Hello Ido,

            This code should help - however you can clarify what you have "risk" set to, as well as the LmtSizeLong, StopSizeLong?

            If you can include your region variables/properties section, this should help us test your code
            MatthewNinjaTrader Product Management

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by FishTrade, Today, 03:42 PM
            0 responses
            1 view
            0 likes
            Last Post FishTrade  
            Started by Richozzy38, Today, 01:06 PM
            3 responses
            14 views
            0 likes
            Last Post Richozzy38  
            Started by ttrader23, Today, 09:04 AM
            2 responses
            12 views
            0 likes
            Last Post ttrader23  
            Started by geotrades1, Today, 07:54 AM
            10 responses
            26 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by ninza33, Yesterday, 12:31 PM
            3 responses
            16 views
            0 likes
            Last Post NinjaTrader_LuisH  
            Working...
            X