Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trade one time only after the indicator is true in strategy

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

    Trade one time only after the indicator is true in strategy

    Hello all, please help me.

    i make a indicator like this

    protected override void Initialize()
    {
    CalculateOnBarClose = true;
    Overlay = false;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (CurrentBar<1)
    return;
    bool insideBar = (High[0] < High [1] && Low[0] > Low[1]);
    if (insideBar)
    {
    BarColor = Color.Yellow;
    }

    }

    The "insideBar" is showing correctly in chart.
    I want to
    1. trade on next bar
    2. get the High/Low value of the "insidebar" that i can compare with current price.
    3. just trade once untill next indicator appear.

    Please guiding me instead of give me the code only if possible, thx a lot.

    Sam
    Rgds

    #2
    Hello Auntthree30,

    Thank you for your note,

    You would want to initialize variables iHigh and iLow under region variables which will allow you to reference their values later.

    #region Variables

    private double iHigh = 0;

    private double iLow =0;

    #endregion

    You’ll want to add under protected override void Initialize(),

    {

    CalculateOnBarClose = true;

    EntriesPerDirection = 1;

    }

    Under OnBarUpdate;

    if (CurrentBar<1) return;

    bool insideBar = (High[0] < High [1] && Low[0] > Low[1]);

    if (insideBar)

    {

    BarColor = Color.Yellow;

    iHigh = High[0]; //Declares the value of iHigh as the high of the inside bar

    iLow = Low[0]; //Declares the value of iLow as the high of the inside bar

    EnterLong(1,"");

    Print (iLow +" "+ iHigh +" " +Time[0].ToString()); //Example of referencing iLow and iHigh.

    }

    Calculate on bar close should be set to true and this strategy will execute on the next bar.

    Please let us know if you need further assistance.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Thank you so much, AlanP, it's help!

      Comment


        #4
        How to EnterLong when the current price is over 1 tick of iHigh?

        Thx

        Comment


          #5
          Hello Auntthree30,

          To do this you would want to create another variable called iHighPlus1 which will add 1 to iHigh, and your entry order method will change from EnterLong to EnterLongStop which will be placed at a price equal to the high of the inside bar plus 1 tick, and triggered if price takes out that level.

          To do this, you would want to amend the #region Variables to,

          #region Variables

          private double iHigh = 0;

          private double iLow =0;

          private double iHighPlus1= 0;
          #endregion


          Amend OnBarUpdate() to,

          {

          BarColor = Color.Yellow;

          iHigh = High[0];

          iLow = Low[0];

          iHighPlus1 =iHigh + 1*TickSize;

          EnterLongStop(1, iHighPlus1, "Buy");
          }

          Please let us know if you need further assistance.
          Alan P.NinjaTrader Customer Service

          Comment


            #6
            And i also need to Short if the current price is below 1 tick of insiderbar,
            exit the order if the price is equal to the value of close-open 1 bar b4 the insiderbar

            Thank you

            Comment


              #7
              it seem done, pls gaive me some advice, thx

              private double iHigh = 0;
              private double iLow =0;
              private double iHighPlus1= 0;
              private double iLowMinus1= 0;
              private double iExit = 0;


              // User defined variables (add any user defined variables below)
              #endregion

              /// <summary>
              /// This method is used to configure the strategy and is called once before any strategy method is called.
              /// </summary>
              protected override void Initialize()
              {
              SetStopLoss(CalculationMode.Ticks, iExit);
              SetProfitTarget(CalculationMode.Ticks, iExit);
              CalculateOnBarClose = true;
              EntriesPerDirection = 1;

              }

              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
              if (CurrentBar < 1) return;
              bool insideBar = (High[0] < High [1] && Low[0] > Low[1]);
              if (insideBar)

              {
              BarColor = Color.Yellow;

              iHigh = High[0]; //Declares the value of iHigh as the high of the inside bar
              iLow = Low[0]; //Declares the value of iLow as the high of the inside bar
              iHighPlus1 = iHigh + 1*TickSize;
              iLowMinus1 = iLow - 1*TickSize;
              iExit = iHigh - iLow;
              bool iBuy = (Close[0] == High[0]);
              bool iSell = (Close[0] == Low[0]);

              if (iBuy)
              EnterLongStop (1, iHighPlus1, "Buy");
              else if (iSell)
              EnterShortStop (1, iLowMinus1, "Sell");

              Comment


                #8
                Hello,

                To add the short logic you'd follow the steps I did in the last message but use a different variable name, say iLowMinus1. Entry price would be iLow - 1*TickSize, and you'd enter with EnterShortStop rather than EnterLongStop.

                Under region Varilables add,
                private double iLowMinus1= 0;
                Under OnBarUpdate add,
                iLowMinus1= =iLow - 1*TickSize;
                EnterShortStop(1, iLowMinus1, "Sell");

                The value of your exit logic "close-open" 1 bar before the inside bar would calculate to a tiny value relative to entry price, for example in the ES 09-16 the Daily close yesterday was 2173.50, Open =2175.00, thus your exit price would be equal to 1.5. So I may further assist you please provide more details about your exit logic.

                I've provided a link to the Managed Approach of Strategies in our Helpguide so you can better understand how code is put together.


                I look forward to your reply.
                Alan P.NinjaTrader Customer Service

                Comment


                  #9
                  Hello Auntthree30,

                  I’ve tested this and this appears to work as expected. Here is a link to a video test and I’ve attached script I used for testing.

                  Video: http://screencast.com/t/kWSs10bwlnh

                  Please let us know if you need further assistance.
                  Attached Files
                  Alan P.NinjaTrader Customer Service

                  Comment


                    #10
                    yes, thanks, but, is it only stop loss but didn't take profit?
                    for the exit logic, i want to know is it possible get the value that Calculate from any bar.
                    Last edited by Auntthree30; 08-29-2016, 10:49 PM.

                    Comment


                      #11
                      Hello,

                      Your StopLoss and Profit Target are set to a value of iExit, which you set to a value of iHigh-iLow. This value is tiny and should be applied to something like a high or low to come up with a realistic value. Or you could replace the iExit in SetStopLoss and SetProfitTargets with values such as, 20 and 50, which will enter your Stop 20 ticks away from entry and your target 50 ticks from entry. For example,

                      protected override void Initialize()
                      {
                      CalculateOnBarClose = true;
                      SetStopLoss(CalculationMode.Ticks, 10);
                      SetProfitTarget(CalculationMode.Ticks, 25);
                      CalculateOnBarClose = true;
                      EntriesPerDirection = 1;
                      }

                      So I may better assist, could you please clarify by what you mean in regards to “is it possible get the value that calculate from any bar”?

                      See SetProfitTarget section of our Helpguide:


                      SetStopLoss Section of our Helpguide:


                      I look forward to your reply.
                      Alan P.NinjaTrader Customer Service

                      Comment


                        #12
                        if insidebar was triggered in 30 mins chart, in 1 min chart, Enter Long if the current price is above the insiderbar 1*TickSize,

                        Comment


                          #13
                          Hello Auntthree30,

                          To do this you would need to add multiple data series under Initialize(). For example,

                          Add(PeriodType.Minute, 1);
                          Add(PeriodType.Minute, 30);

                          Then you would need to add Current Bar checks for the two new data series, using CurrentBars rather than CurrentBar. For example,

                          if(CurrentBars[0] < 1 || CurrentBars[1] < 1 || CurrentBars[2] < 1)
                          return;

                          You would want to ensure the 30 minute data was the series checked for an inside bar, and assign your entry variable iHighPlus1 and iLowMinus1 using 30 minute values. You could do this by putting the following line under your current bar checks,

                          if (BarsInProgress !=2) return;

                          Finally, amending the EnterLongStop to the method variation which includes barsInProgressIndex, so you can reference the 1-minute data series for your entry.

                          EnterLongStop (1, iHighPlus1, 'Buy');

                          Becomes,

                          EnterLongStop(1, true, 1, iHighPlus1, "Buy");// where the 1 before “true” references the 1 minute data series.

                          See EnterLongStop section of our helpguide covering barsInProgressIndex and LiveUntilCancelled information in our Helpguide:


                          You would do the same change for EnterLongStop as you would your EnterShortStop.

                          Please let us know if you need further assistance.
                          Alan P.NinjaTrader Customer Service

                          Comment


                            #14
                            InsideBarHigh.Set(High[0]);
                            InsideBarLow.Set(Low[0]);

                            Is it correct to store the value of insideBar?

                            Can i using the value in future not just the next bar and use the value once only?

                            Comment


                              #15
                              Hello Auntthree30,

                              The code you posted would not be the correct way of storing the values for the inside high/low. In an earlier reply to this thread I posted the code to store the value of the Inside Bars high and low.

                              iHigh = High[0]; //Declares the value of iHigh as the high of the inside bar
                              iLow = Low[0]; //Declares the value of iLow as the high of the inside bar

                              Can you please clarify what you mean by using the value in the future and use the value once only?

                              I look forward to your reply.
                              Alan P.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              580 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              335 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              102 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