Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

why position always return flat?

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

    why position always return flat?

    Hi,

    I am doing backtest with CL. I am using 5 min bar as primary bar and I enable intrabar trade feature by adding a smaller granularity time frames, such as 1 min bar. something like this:

    Code:
    protected override void Initialize()
     {
          BarsRequired = 20;
          Add(PeriodType.Minute, 1); 
          SetStopLoss(CalculationMode.Price, 500);
          SetProfitTarget(CalculationMode.Ticks, 1000);
          CalculateOnBarClose = false;
    }
    and I try to enter trade with condition judgement at 5min bars and send trade cmd to secondary bars, like this:

    Code:
    PrintWithTimeStamp ("BarNo: " + CurrentBar + " at: " + BarsInProgress + " " + Positions[0].MarketPosition + " " + Positions[1].MarketPosition);
                if (BarsInProgress == 0) {
                       bool Condition = Close[1] < ParabolicSAR(BarsArray[0], 0.02, 0.2, 0.02)[0];
                       if (Condition) {
                         if (Position.MarketPosition == MarketPosition.Flat) {
                               EnterShort(1, DefaultQuantity, "sell");
                          } 
                    } else {
                          if (Position.MarketPosition == MarketPosition.Flat) {
                            EnterLong (1, DefaultQuantity, "buy");
                          }
                    } 
                } else if (BarsInProgress == 1) {
                      if (Position.MarketPosition != MarketPosition.Flat) {
                          if (FirstTickOfBar) {
                             SetStopLoss (CalculationMode.Price, ParabolicSAR(BarsArray[0], 0.02, 0.2, 0.02)[0]);
                           }
                       }
                }

    but 1). the print statement I added will print "xx:xx:xx at 1 flat flat" even if the time scope has trades and is not flat.
    2) there are a lots of strange trades which only last 1 5min bar and triggered by preset stop-loss order, but I am not sure why the stop-loss price is not same with my print. and the strange thing is the stop-loss price is same with entry price in such weird trades, please see my attached pic for sample.
    Attached Files
    Last edited by snowbig; 08-25-2012, 09:45 AM.

    #2
    Hello snowbig,
    Your strategy is sending orders at every bar. Thus it is returning the position as flat. Please modify your coding logic so that it does not send orders on every bar.

    Also you have appended the stop as
    Code:
    SetStopLoss(CalculationMode.Price, 500);
    Here the stop order price is $500, which may be unrealistic depending on what instrument you are testing on.

    Please refer to this sample code which demonstrates a intrabar backtesting
    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
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Joydeep View Post
      Your strategy is sending orders at every bar. Thus it is returning the position as flat. Please modify your coding logic so that it does not send orders on every bar.
      what did you mean? changing CalculateOnBarClose from false to true? I have done it, no improve here. could you introduce more details why I have to this? whether it means i have to allow sending orde on every bar at live trading and stop it at backtest?
      I also shared my code, could you have a try?

      Originally posted by NinjaTrader_Joydeep View Post
      Here the stop order price is $500, which may be unrealistic depending on what instrument you are testing on.
      where are the limitation from? I didn' find such statements from help guide.

      Originally posted by NinjaTrader_Joydeep View Post
      Please refer to this sample code which demonstrates a intrabar backtesting
      http://ninjatrader.com/support/forum...ead.php?t=6652
      [/QUOTE]

      my code was written just by refering this thread, but no help at all.

      Comment


        #4
        Hello snowbig,
        I did backtested your code. You need to make sure the stop/target price are valid price.

        Please use the below code in the Initialize section of the strategy to find out the exact reasons for the order being such.
        Code:
        TraceOrders = true;


        For more debugging tips please refer to this post.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Joydeep View Post
          Hello snowbig,
          I did backtested your code. You need to make sure the stop/target price are valid price.

          Please use the below code in the Initialize section of the strategy to find out the exact reasons for the order being such.
          Code:
          TraceOrders = true;
          http://www.ninjatrader.com/support/h...raceorders.htm

          For more debugging tips please refer to this post.
          http://www.ninjatrader.com/support/f...ead.php?t=3418
          Hi, I added TraceOrders like what you said, but it has no help at all.

          I still couldn't understand why "SetProfitTarget(CalculationMode.Price, 1000);" brings such ridiculous result.

          Setting the profit to 1000 is a invalid price? could you explain why it is? is it the bug of backtest?

          Comment


            #6
            Hello
            Calculation mode price will set an limit order with a price of $1000. You are backtesting the code on an instrument with a price of 1.2XXX

            If you replace the code as below then can you backtest it properly.

            Code:
            SetProfitTarget(CalculationMode.Ticks, 10);   //10 tick target
            Same logic for the stop order too.

            I look forward to assisting you further.
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Joydeep View Post
              Hello
              Calculation mode price will set an limit order with a price of $1000. You are backtesting the code on an instrument with a price of 1.2XXX

              If you replace the code as below then can you backtest it properly.

              Code:
              SetProfitTarget(CalculationMode.Ticks, 10);   //10 tick target
              Same logic for the stop order too.

              I look forward to assisting you further.
              thank your clarification very much.

              The explanation in user manual really misled me (I am not a native speaker) to treat the meaning of 2nd parameter as the profit target amount in currency when the calucation mode is chosen as Price.
              It spent me two days to root cause this issue...

              anyway, I would suggest there is a warning or alert to tell user how to locate such issue. when I set it to 1000, there is no any alert except strange order execution and backtest result.

              thanks for your help again.

              Comment


                #8
                sorry, I have another question about SetStopLoss() & SetProfitTarget().

                is there a way to make them to be filled at intrabar at backtest?

                from the my trading log, the enter order can be filled at intrbar after using a smaller interval time data series. but it seems all exit orders (I did not explicitly call ExitXXX() instruction to exit, I am using SetProfitTarget() and SetStopLoss() to exit) are executed at the edge of my primary bar.

                Comment


                  #9
                  Hello snowbig,
                  In backtest the calculations are always done at the end of the bar (Calculation on bar close = true). However you can use an multi series strategy to submit the orders on a higher granularity bar series which will replicate what you are trying to achieve.

                  This sample code essentially demonstrates the process
                  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
                  JoydeepNinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_Joydeep View Post
                    Hello snowbig,
                    In backtest the calculations are always done at the end of the bar (Calculation on bar close = true). However you can use an multi series strategy to submit the orders on a higher granularity bar series which will replicate what you are trying to achieve.

                    This sample code essentially demonstrates the process
                    http://ninjatrader.com/support/forum...ead.php?t=6652
                    Hi, Joydeep

                    Please be aware that I have followed this and the discussion above are all based on this.

                    now I am asking why SetProfitTarget and SetStopLoss are all executed at the end of primary bar but not at the intrabar?

                    Comment


                      #11
                      Hello snowbig,
                      Thanks for the clarification.

                      Yes, Set() methods will be for the primary series only.

                      Please use the Exit() methods with to exit your positions. Please make sure you are submitting the orders to the higher granularity bar by using the specific overload.

                      ExitLongStop(int barsInProgressIndex, bool liveUntilCancelled, int quantity, double stopPrice, string signalName, string fromEntrySignal)

                      ExitLong(int barsInProgressIndex, int quantity, string signalName, string fromEntrySignal)
                      JoydeepNinjaTrader Customer Service

                      Comment


                        #12
                        ok, got you.

                        I still have problem on backtest. I run a strategy which is very similar with the above one and get a lots of weird trade log in which some order entry's time is behind order exit.

                        did you know why it happens?
                        Attached Files

                        Comment


                          #13
                          Hello snowbig,
                          To assist you further can you please send a toy NinjaScript code* replicating the behavior to support[AT]ninjatrader[DOT]com

                          Please append Attn:Joydeep in the subject line of the email and give a reference of this thread in the body of the email.

                          I look forward to assisting you further.

                          *The "toy" just means something that is a stripped down version that isn't necessarily the whole logic. It makes things easier to rout out.
                          JoydeepNinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by algospoke, Yesterday, 06:40 PM
                          2 responses
                          24 views
                          0 likes
                          Last Post algospoke  
                          Started by ghoul, Today, 06:02 PM
                          3 responses
                          15 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by jeronymite, 04-12-2024, 04:26 PM
                          3 responses
                          46 views
                          0 likes
                          Last Post jeronymite  
                          Started by Barry Milan, Yesterday, 10:35 PM
                          7 responses
                          23 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by AttiM, 02-14-2024, 05:20 PM
                          10 responses
                          181 views
                          0 likes
                          Last Post jeronymite  
                          Working...
                          X