Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to get entrie, Stoploss and TakeProfit for a breakout-strategie?

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

    How to get entrie, Stoploss and TakeProfit for a breakout-strategie?

    Hello,
    I started programming strategies just some days ago and now I have a big problem. So, would you please help me? Excuse me for my poor English, please.
    I want to build a strategie where a special candle (1 hour chart) is taken.
    If the price goes 1 Pip above the candle, I want to go Long (stoploss 1 Pip below the candle, TakeProfit = candlehigh + the high of the candle).
    If the price goes 1 Pip below the candle I want to go short (stoploss 1 Pip above the candle and TakeProfit = candlelow – the high of the candle.
    I use NinjaTrader 6.5 (V 7 beta crashes all few minutes) and I learned already that with this version it is not possible to set a LongStopBuy and a ShortStopSell at the same time. So I decided to make two strategies – one for long scenario and one for short scenario.

    My problem is to get exactly entry 1 Pip above the candle and to get an exactly stoploss and TakeProfit.

    I tried EnterLong() with ExitLong(), but the price is always set at the next candle . It is ot exactly 1 Pip above the candle.
    I made it with “if(GetCurrentAsk() > price) EnterLong().

    I tried SetStopLoss() and SetProfitTarget , but to get an entry I have to take take EnterLong and it is not an exact entry anymore like above.
    And I tried entryOrder = EnterLongStop(…) with stopOrder = ExitLongStop (…) and targetOrder = ExitLongLimit(…). But when the setup is broken because of special circumstances, I want to cancel and start new some candles later. But when I delete entryOrder with CancelOrder(), all entries are deleted, not only this one. There are no entries anymore in backtesting.

    I don’t know now what to do. Can you tell me please what is the right way to a solution for my breakout-strategie?

    Thanks for your help!

    Rainer

    #2
    Hi Boju_B,

    You may be getting the entry one bar later than expected because...
    1. You are backtesting, in which case that is expected.
    2. You have CalculateOnBarClose = true
    More on each of these at - http://www.ninjatrader-support.com/H...ateOnBarClose1

    As for the cancelled orders, how are you using CancelOrder()? can you proivde a code snippet?
    TimNinjaTrader Customer Service

    Comment


      #3
      Hello,
      Thank you for your answer.

      >You may be getting the entry one bar later than expected because...
      >1. You are backtesting, in which case that is expected.

      Yes, I am backtesting and would like to have exact prices. If I understand you right, then with EnterLong() it is not possible what I want and I sould use 'entryOrder=...'

      >2. You have CalculateOnBarClose = true

      I tried in backtesting both, true and false, but there was no difference. I assume this is only relevant for live-trading.

      >As for the cancelled orders, how are you using CancelOrder()? can you proivde a code snippet?

      Yes, thank you for having a look on it. For setting Stoploss and Takeprofit to my entryOrder I used the file ‘SampleCancelOrder’ which I found in this Forum.

      This is the section OnBarUpdate:
      /// ################################################## ################################################## ###################
      /// The Strategy takes the 1 h Candel from 7:00 to 8:00 of a day.
      /// If the price from one of the next candels (until market is closed) ist 1 PiP higher, it goes long with a stoploss and a limit.
      ///
      /// In case the price goes during the next candles 1 pip or more below the candle the setup is destroyed.
      /// Orders must be cancelled and no new order is made anymore until the next 7:00 - 8:00 candle comes
      /// ################################################## ################################################## ###################


      protectedoverridevoid OnBarUpdate()
      {
      // Find the second 7:00 - 8:00 Candel of every day
      string str = ToTime(Time[0]).ToString();
      // there should be no position open, but if so:

      // close it one candle before the 8:00 candle
      if( str == "70000") ExitLong();
      if( str == "80000") // Stundenkerze 7:00 bis 8:00
      {
      // next lines concern only this 1 h candle.
      kerzenhoch = High[0]; // kerzenhoch = candles High
      kerzentief = Low[0]; // kerzentief = candles Low
      kerzenhoehe = Math.Max(kerzenhoch - kerzentief, kerzentief - kerzenhoch);//High of candle in Pips
      setupok = true;

      // If the price comes 1 Pip above the Candle: go long
      entryOrder = EnterLongStop(0, true, 1, kerzenhoch + TickSize, signalnamelong);
      setupok = true;
      }
      // next lines concern the candles after the 1h candle from 7:00 - 8:00
      // if the price has been at leat 1 time 1 PIP or more below candle:
      // setup is destroyed. If entryOrder was not executed, delete entryOrder, TakeProfit and Stoploss

      if(GetCurrentAsk() < kerzentief)
      {
      // ############## The next Line deletes All orders, also those made all days before in backtesting.
      // ############## If I comment it with // orders are made!
      CancelOrder(entryOrder); // This line deletes all orders!!
      setupok = false;
      }
      }

      Comment


        #4
        Hi Boju_B,

        1. When backtesting, the entry will always be one bar "late" since the conditions aren't satisfied until a bar is completed.

        If you want to add granularity to your backtesting please see this sample - http://www.ninjatrader.com/support/f...ead.php?t=6652

        2. Correct, the COBC setting only applies to live.

        3. The only entry I see is "entryOrder" which you explicitly cancel, can you please clarify which orders you expect to be cancelled, and which you do not.
        TimNinjaTrader Customer Service

        Comment


          #5
          Hi Tim,
          >3. The only entry I see is "entryOrder" which you explicitly cancel, can you please clarify
          >which orders you expect to be cancelled, and which you do not.


          There is only one EntryOrder every day. Every day when the 8:00 1hour candle is finished, the entryOrder should set a StopBuy Limit 1 Pip above the candle. If this price comes, the entryOrder is executed. The Stoploss and TakeProfit orders are given in the ‘OnExecution()’ section which I took from this forum (file SampleCancelOrder). Both work fine if entryOrder is executed.

          The problem is: If the price goes not up, but down, the entryOrder is not executed and still there living. But my setup is destroyed and that’s why I want to delete this Order. Otherwise, the price could go down a long time and maybe many hours later it comes up again and this entryOrder would be executed.

          That means it must be deleted at once if the setup is destroyed.

          At the next 8:00 candle next day, there is a new setup and, of course, I need a new entryOrder again and it must be set again concerning to this new candle.

          When I use CancelOrder(entryOrder) in backtesting, this one order it concerns to is cancelled like I want, but also ALL other entryOrders from the days before are deleted too. There is NOTHING left there. All orders disappeared, also those where the setup was ok.

          I don’t understand why all my entryOrders disappear although I cancel only the current one.

          I took the script as a text file to
          http://www.schmitznet.de/NinjaTrader/VDForum.txt

          Comment


            #6
            Hi Boju_B,

            You may have some exit orders still "hanging around", try using TraceOrders to track your orders and see if any are being cancelled due to handling rules.

            More info on TraceOrders at - http://www.ninjatrader.com/support/f...ead.php?t=3627

            Handling rules at bottom of page at - http://www.ninjatrader-support.com/H...tml?Overview36
            TimNinjaTrader Customer Service

            Comment


              #7
              Hi Tim, thanks for your answer.
              >You may have some exit orders still "hanging around", try using TraceOrders to track your orders >and see if any are being cancelled due to handling rules.
              There is only one ExitLong() line and one CancelOrder() in my script. The 'ExitLong()' was only for security. I took it off but the problem still exists.

              TraceOrders give me the following information. It seems, there is only one order made and this is canceled at oce. Then there is no order anymore. The script should make a new order every day ( at every 8:00 candel). But it does not. Today I im looking again for a solution since many hour, but without success.


              05.01.2010 08:00:00 Entered internal PlaceOrder() method at 05.01.2010 08:00:00: Action=Buy OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1,6138 SignalName='entrysignal' FromEntrySignal=''

              Cancelled custom managed order at 05.01.2010 09:00:00: Order='NT-00000/Back101' Name='entrysignal' State=Working Instrument='6B 06-10' Action=Buy Limit price=0 Stop price=1,6138 Quantity=1 Strategy='VDforUserForum' Type=Stop Tif=Gtc Oco='' Filled=0 Fill price=0 Token='95053f9ff539438e8565bed71014b7d5' Gtd='01.12.2099 00:00:00'

              Comment


                #8
                Hi Boju_B,

                You appear to be using IOrder's....
                Code:
                entryOrder = EnterLongStop(0, true, 1, kerzenhoch + TickSize, signalnamelong);
                However, I do not see anywhere where you are setting the correct setup for IOrders
                More info at - http://www.ninjatrader-support.com/H...de.html?IOrder

                You may want to consider using simple Enter() orders, then letting them expire once the conditions are no longer valid. Continue to simply your code, get the entries working, perhaps let them ExitOnClose, then work with your exits.
                TimNinjaTrader Customer Service

                Comment


                  #9
                  Hi Tim,

                  >You may want to consider using simple Enter() orders, then letting them expire once the
                  >conditions are no longer valid. Continue to simply your code, get the entries working, perhaps let
                  >them ExitOnClose, then work with your exits.

                  This is not possible because with Enter() I would not get exact prices in backtesting.

                  >You appear to be using IOrder's....
                  Yes, because as I assume it is the only way to get exact prices in backtesting. I used the file SampleCancelOrde which I found in this forum. There I changed only orderprice and signalName and at the end I changed these two lines. Thats all.

                  //stopOrder = ExitLongStop(0, true, 1, execution.Price - 15 * TickSize, "stop", "market order");// I changed to the next line
                  stopOrder = null; // Ich changed this line to 'null' because the Order must be deleted, not changed
                  //targetOrder = ExitLongLimit(0, true, 1, execution.Price + 30 * TickSize, "target", "market order");// I changed to the next line
                  targetOrder = null; // Ich changed this line to 'null' because the Order must be deleted, not changed

                  I took the whole script as a text file to
                  http://www.schmitznet.de/NinjaTrader/VDForum.txt

                  Comment


                    #10
                    Hi Boju_B,

                    You do not need IOrders to obtain limit prices.

                    You can use EnterLongStop() for example, without setting it to a variable, as in IOrders.

                    Please review this page - http://www.ninjatrader-support.com/H...tml?Overview36

                    I don't recommend trying to simply copy another script in it's entirety unless you fully understand what happening in the code, instead it's best to start with your own objective and what is steps you are taking, as you make them.
                    TimNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    582 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    338 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    103 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    554 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    552 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X