Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help building strategy

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

    Help building strategy

    Hi,
    I am looking for some advise on how to fully automate a strategy I use in a discretionary way. The rules are simple. I enter short each time the open is above the previous close and the market does a new high, so basically the condition to go short are:
    Code:
    if( Open[0] < Close[1] 
      && Low[0] < Low1])
    Now come the trick part. In the next period I would only enter short on the break of Low[0]-TickSize, with a SetStopLoss at High[0]+TickSize.

    Can anybody advise on how to code this? I've been trying but with limited success

    Thanks,

    #2
    Hello sburtt,

    For something like that you would want to use the EnterShortLimit() and the SetStopLoss().

    For Example:
    Code:
    if( *entry condition* )
    {
       SetStopLoss(High[0]+TickSize);
       EnterShortLimit(Low[0]-TickSize);
    }
    JCNinjaTrader Customer Service

    Comment


      #3
      This is exactly the code i've been using, however it's not working properly in backtesting as i get stopped on each trade on the same exact candle i enter long. This could be due to the fact that i am using 8 hour candles, and NT is unable to see how the price develops during each 8 hour period. Hence, if the stop level has been traded during the period, aslo before i enter long, NT registers the trade as a loss. Could this be what is happening?

      Comment


        #4
        Originally posted by sburtt View Post
        .. Hence, if the stop level has been traded during the period, aslo before i enter long, NT registers the trade as a loss. Could this be what is happening?
        Most probably, and that would be the better behavior for Backtest, as it is assuming the worst possible case. You always want to see your worst possible case, because if the worst you can do is make money, then you are in mighty fine shape.

        Comment


          #5
          I see NinjaScript supports multi-time frame and instruments in a single script. Hence, given that I am using 8h candles for this set-up, I tried coding the strategy as below, and backtesting it on 1min candles, but I get the same result. Am I doing something wrong? Is there a different way to backtest MTF strategies?

          Code:
          protected override void Initialize() 
          {
              Add(PeriodType.Minute, 480);
          }
          Code:
          protected override void OnBarUpdate()
          
          if(BarsInProgress == 1) 
          {
          if( *entry condition* )
          {
             SetStopLoss(High[0]+TickSize);
             EnterShortLimit(Low[0]-TickSize);
          }
          }

          Comment


            #6
            Originally posted by sburtt View Post
            I see NinjaScript supports multi-time frame and instruments in a single script. Hence, given that I am using 8h candles for this set-up, I tried coding the strategy as below, and backtesting it on 1min candles, but I get the same result. Am I doing something wrong? Is there a different way to backtest MTF strategies?

            Code:
            protected override void Initialize() 
            {
                Add(PeriodType.Minute, 480);
            }
            Code:
            protected override void OnBarUpdate()
             
            if(BarsInProgress == 1) 
            {
            if( *entry condition* )
            {
               SetStopLoss(High[0]+TickSize);
               EnterShortLimit(Low[0]-TickSize);
            }
            }
            8 hours is 8 hours, no matter how you slice it, so you are still entering on 8 hour candles the way that you have written it.

            The problem is with your logic. You are entering with a Short Limit order. That means that it will be filled at your price or better. In this case, it would seem that you will almost always be stopped out, not only in Backtest but also in live trading, as essentially you are entering at best at a price that would be one tick from the Stop Loss. (Your entry price, at which you are selling, is waaaaay better than the Low[0] - TickSize that you have specified).

            It seems to me that you want to enter short with a Stop or StopLimit order.

            Comment


              #7
              It looks like I am not properly expressing myself on want I want NT to do. Basically when the entry condition occurs, only for the next 8h candle I want to enter short if we trade 1tick below the low of the previous candle, placing a stoploss 1tick above the high of the previous candle. So basically if the previous candle (t-1) high was 1.25 and low was 1.24 and the entry condition occured. In period (t+0) I would enter short at 1.2399 (+slippage) placing a stoploss at 1.2501. Isn't this was I've coded in my previous post? thanks,

              Comment


                #8
                Originally posted by sburtt View Post
                It looks like I am not properly expressing myself on want I want NT to do. Basically when the entry condition occurs, only for the next 8h candle I want to enter short if we trade 1tick below the low of the previous candle, placing a stoploss 1tick above the high of the previous candle. So basically if the previous candle (t-1) high was 1.25 and low was 1.24 and the entry condition occured. In period (t+0) I would enter short at 1.2399 (+slippage) placing a stoploss at 1.2501. Isn't this was I've coded in my previous post? thanks,
                That is what you have coded, in absolute terms. What will happen depends on what your entry condition is. IOW, what will happen depends on what the price is at the time of entry. This statement of yours: "I enter short each time the open is above the previous close and the market does a new high" kind of gives me the impression that the market is at the high when you place your Short order. Look in the OutputWindow to see what your actual entry price is.
                Last edited by koganam; 02-25-2013, 10:01 AM.

                Comment


                  #9
                  I enter short each time the open is above the previous close and the market does a new high"
                  this is the condition, coded like this:

                  Code:
                  protected override void OnBarUpdate()   { if( Open[0] < Close[1]    && Low[0] < Low1])  {    SetStopLoss(High[0]+TickSize);    EnterShortLimit(Low[0]-TickSize); } }

                  Comment


                    #10
                    I'm unable to access the Output window currently, but I can tell you that entry price is correct. I think the issue is the following: what if during the 8h period price trades above the stop level and only on the close of session it trades below the low, hence triggering my entershort order? NT will not be able to view in detail how price has moves within each 8h candle, unless we use MTF, or am I saying something stupid?

                    Comment


                      #11
                      Originally posted by sburtt View Post
                      this is the condition, coded like this:

                      Code:
                      protected override void OnBarUpdate()   { if( Open[0] < Close[1]    && Low[0] < Low1])  {    SetStopLoss(High[0]+TickSize);    EnterShortLimit(Low[0]-TickSize); } }
                      So then, the picture is an example of what you coded. The middle ochre arrows are your first condition; the lower ochre arrows are your second condition.

                      Your Short order will be filled at the red arrow (as the price is much better than your stated Limit Price at the bottom of the bar). You will be stopped out on the next bar, pretty much as expected with a 1-tick Stop Loss.
                      Attached Files
                      Last edited by koganam; 02-26-2013, 12:08 AM. Reason: Corrected capitalization.

                      Comment


                        #12
                        you must excuse me, but I've reported a mistaken code, please see below the code I am using, and what I want it to do in the picture:
                        Code:
                         protected override void OnBarUpdate()
                                {
                                     if (Position.MarketPosition == MarketPosition.Flat)
                                            {
                                                   if (Open[0] > Close[1] && High[0] > High[1])
                                                        {
                                                        SetStopLoss(High[0]+TickSize);// this just doesn't happen            
                                                        EnterShortStop(Low[0]-TickSize);
                                                        }
                                            }
                        Attached Files

                        Comment


                          #13
                          Hello sburtt,

                          Are you setting a Stop Loss somewhere else inside of your code?

                          You may want to use "TraceOrders = true;" inside of your Initialize() method to see what your Stop Loss is being set to or why it is being canceled inside of the Output window (Control Center -> Tools -> Output window).
                          JCNinjaTrader Customer Service

                          Comment


                            #14
                            there are no other stoporders, i will try what you say and revert. thx

                            Comment


                              #15
                              Originally posted by sburtt View Post
                              you must excuse me, but I've reported a mistaken code, please see below the code I am using, and what I want it to do in the picture:
                              Code:
                               protected override void OnBarUpdate()
                                      {
                                           if (Position.MarketPosition == MarketPosition.Flat)
                                                  {
                                                         if (Open[0] > Close[1] && High[0] > High[1])
                                                              {
                                                              SetStopLoss(High[0]+TickSize);// this just doesn't happen            
                                                              EnterShortStop(Low[0]-TickSize);
                                                              }
                                                  }
                              What is the error in your log?

                              I expect it to say: "You are accessing an index with a value that is invalid since its out of range ...", in which case you need to escape your first bar.

                              In which case, I would suggest that you look at my response in this tread.

                              ref: http://www.ninjatrader.com/support/f...ape+currentbar

                              Comment

                              Latest Posts

                              Collapse

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