Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with Range Based Strategy

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

    #31
    And the rest of the code . . .

    ///<summary>
    /// Called on each bar update event (incoming tick)
    ///</summary>
    protectedoverridevoid OnBarUpdate()
    {

    {
    // recording the first highest high
    if (ToTime(Time[0]) >= 93000 && ToTime(Time[0]) <= 94500)
    {
    if (High[0] > highestHigh)
    highestHigh = High[
    0];
    }


    }

    // Entry Condition: Submit a buy stop order two ticks above the highest high after 9:45am
    if (ToTime(Time[0]) > 94500 && ToTime(Time[0]) <= 110000)
    {
    // Only allow entries if we have no current positions open
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    /* Enters long position.
    */
    EnterLongStop(highestHigh + TickSize * 2);

    Comment


      #32
      EntriesPerDirection only controls the number of simultaneous trades. It does not control how many trades per day.

      Instead you need to use some flag variable.

      Untested code. Something along these lines.
      Code:
      [FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]if (Bars.FirstBarOfSession && FirstTickOfBar)
           tradeFlag = true;[/SIZE][/FONT][/SIZE][/FONT]
      
      [FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] (ToTime(Time[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]]) > [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]94500[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] && ToTime(Time[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]]) <= [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]110000[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]
      [COLOR=Black]{[/COLOR]
           if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] (Position.MarketPosition == MarketPosition.Flat && tradeFlag == true)[/SIZE][/FONT]
           [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
      [/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]          EnterLongStop(highestHigh + TickSize * [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]2[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]);
           }
      
           else if (Position.MarketPosition == MarketPosition.Long && tradeFlag == true)
                tradeFlag = false;[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]
      }[/SIZE][/FONT][/SIZE][/FONT]
      Josh P.NinjaTrader Customer Service

      Comment


        #33
        I see. But wouldn't that code just prevent position taking while in a long? What happens when you you close that position, wouldn't the flag become "true" again, hence allowing another long?

        If the logic works, do I have to define the "tradeFlag" anywhere else?

        Is there a way to count the number of longs taken between the trading timeframe specified?

        Comment


          #34
          What does FirstBarofSession define? . . How is "session" defined?

          if I set my time session times to 12:00AM to 12:00AM, then with this strategy, is the first bar the bar at 12:00AM or at 9:30AM when my highestHigh recording period starts?

          Comment


            #35
            You need to define tradeFlag in the Variables section.
            private bool tradeFlag = false;

            This limits you to one trade per session. FirstBarOfSession is defined as you have it on the chart properties. Whatever chart you run your strategy on will have session begin/end definitions. That is what is referenced here.

            Once you have made a trade your tradeFlag will turn false. Once it is false it will stay false until the beginning of the next session.
            Josh P.NinjaTrader Customer Service

            Comment


              #36
              Ok, it works . . . now when I see the results of the strategy, I notice there are some places where there is no entry and entry is late . . . same question as before . . .

              not sure why that would be . . .

              Comment


                #37
                Insearch,

                If you feel the code is not doing what you want need to debug it. Please see these tips: http://www.ninjatrader-support2.com/...ead.php?t=3418
                Josh P.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                631 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                364 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                105 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                566 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                568 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X