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

Enter and stop a position on the same bar

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

    Enter and stop a position on the same bar

    Hi,

    could you please have a look at the following issue. It's kind of common problem.

    Strategy based on 90-min chart:
    -------------------------------
    protected override void Initialize()
    {
    SetStopLoss(CalculationMode.Ticks, StopLoss);
    CalculateOnBarClose = true;
    }

    protected override void OnBarUpdate()
    {
    if (Bars.BarsSinceSession == TriggerBar - 1)
    {
    if (SMA(Fast)[0] > SMA(Slow)[0])
    EnterLongStop(High[0] + TickSize,"Long stop");
    }
    }


    The problem:
    ------------
    Assumption: On the next 90-min bar the EnterLongStop() is hit and that bar exceeds the stop loss limit too.
    In this case the stop loss order is executed anyway during backtest. But not necessarily in reality (1-min chart) depending on whether the stop loss trigger event occures before or after the EnterLongStop().

    What is best practice to work around this problem?

    Regards
    Ralph

    #2
    Sorry, I don't follow. What exactly is your issue?

    Comment


      #3
      Originally posted by NinjaTrader_Dierk View Post
      Sorry, I don't follow. What exactly is your issue?
      No problem, it's my fault. Here is a more general example:

      Assume there is a 1h-chart. On the last bar (trigger bar) I set a sell order at the bar's low and a stop loss order at the bar's high. The orders are valid for the next 1h-bar. This next bar finally is an outside bar, with a high above the trigger bar's high, a low below the trigger bar's low and an opening inside the trigger bar's range. Implementing this rules as a strategy in NT (1h-bars), both orders are hit during backtesting of course. At the end of the bar the system is flat.

      But that is not necessarily the case at real time (or using a 1min-chart), right? The stop loss could be hit or even not, depending on which event is triggered first: the short entry or the stop loss. This is not visible at the 1h-bar.

      However, I would like to implement the strategy based on a 1h-chart, instead of a 1min-chart. The question is how to avoid this possibility of inconsistency?

      Regards
      Ralph

      Comment


        #4
        In this case you may need to opt for using IOrder objects to manage your orders. You will want to wait till you get PartFilled or Filled on your EnterLongStop() before submitting stop orders. Please check out this reference sample: http://www.ninjatrader-support.com/v...ead.php?t=7499
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Order execution in Live-Trading should work fine with my example strategy, Josh. The problem is the backtest of the collected data, there are the discrepancies are coming from. The backtest is executed in a 1h-timeframe as described with my example.

          Regards
          Ralph

          Comment


            #6
            The best option available to you is the use of IOrder objects. It will give you the control you need. What you want is not possible with simply a 1hr bar resolution, but when coupled with multi-time framed strategy and IOrder objects you can achieve the desired behavior.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              I have attached 2 chart, depicting the problem.

              90 Min Chart:
              -------------
              This chart is the result of the backtest run. The orders executed at the left bar are those which do not reflect the reality (Sell stop 1 @ 112'165, Stop loss 1 @113'015)

              15 Min Chart:
              -------------
              The blue rectangle limits the time-borders of the concerning 90min bar and the sell stop price at the bottom & the stop loss price on top. As you can see, the sell stop order is triggered but not the stop loss order.

              I agree to have to use a multi-time frame approach. And I guess I have to leave the original 90min time frame. But, is it still possible to display the backtest results in a 90min chart, like picture #1?

              Regards
              Ralph
              Attached Files

              Comment


                #8
                The problem is one of bar resolution. If you have a 90 minute bar there is simply no way to know how it behaves within that bar. The only way you can get information on that is with higher resolution bars and this is why multi-time framed strategies is required if you wish to pursue the most realistic backtest solution possible.

                You can easily code it to take signals from the 90minute, but you will want to submit orders to the higher resolution time frames if you wish it to mimic more closely to reality. Please see this reference sample: http://www.ninjatrader-support.com/v...ead.php?t=6652
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks Josh, I missed that example, it solves the issue.
                  One question to the sample code:
                  If I use EnterLongStop() instead of EnterLong() for the secondary (1min) bar series and the life-time of EnterLongStop() should remain the life-time of the primary bar, then I have to take the control about that (liveUntilCancelled = True), right?

                  EnterLongStop(int barsInProgressIndex,...) requires to enter a quantity number. How could I apply the value defined in the strategy dialog window ?

                  Regards
                  Ralph
                  Last edited by Ralph; 07-06-2008, 10:05 AM.

                  Comment


                    #10
                    Yes, you will need to use liveUntilCancelled. If you don't want to use it just resubmit your order 90 times and it will be kept alive for 90 1min bars instead of cancelling.

                    You would need to make a user definable parameter if you want to make it available in the strategy dialog. Please see this: http://www.ninjatrader-support.com/v...ead.php?t=5782
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by Josh View Post
                      ...You would need to make a user definable parameter if you want to make it available in the strategy dialog...
                      The strategy dialog Order_Properties-section contains following entry fields already:
                      - Set_order_quantity
                      - Default_quantity

                      Is there a chance to access these fields?

                      Comment


                        #12
                        Sure. In your code just put in DefaultQuantity.
                        Josh P.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by PaulMohn, Today, 03:49 AM
                        0 responses
                        7 views
                        0 likes
                        Last Post PaulMohn  
                        Started by inanazsocial, Today, 01:15 AM
                        1 response
                        9 views
                        0 likes
                        Last Post NinjaTrader_Jason  
                        Started by rocketman7, Today, 02:12 AM
                        0 responses
                        10 views
                        0 likes
                        Last Post rocketman7  
                        Started by dustydbayer, Today, 01:59 AM
                        0 responses
                        4 views
                        0 likes
                        Last Post dustydbayer  
                        Started by trilliantrader, 04-18-2024, 08:16 AM
                        5 responses
                        23 views
                        0 likes
                        Last Post trilliantrader  
                        Working...
                        X