Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

same bar entries--Calculate on bar close=False

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

    #16
    {
    if (Historical) return;
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    //Greg> Trade Counter resets value every new bars
    if(FirstTickOfBar)
    {
    x = 0;
    }
    //Greg> Trade Counter checks for your entry condition and that the value is less than your trade count
    if (CrossAbove(SMA(Fast), SMA(Slow), 1) && x <= myTradeCounter //will count from zero to myTradeCounter, probably one more than intended?
    && Close[0] >= SessionPivots(true, PivotRange.Daily, PivotType.All, HLCCalculationMode.CalcFromIntradayData, ProfileType.TPO, 0, 0, 0).L3[0])
    {
    //Greg> Trade Counter attempts to enter long
    EnterLong(DefaultQuantity, "BuyMkt");

    //Greg> Trade Counter increments the trade count by 1
    x++;
    }

    //Greg> Trade Counter resets value every new bars
    if(FirstTickOfBar)
    {
    x = 0; //resets the counter, negating the increment if any. You probably want separate counters for short and long.
    }
    //Greg> Trade Counter checks for your entry condition and that the value is less than your trade count
    else if (CrossBelow(SMA(Fast), SMA(Slow), 1) && x <= myTradeCounter //Bearish trade conditions; replace with your own
    && Close[0] <= SessionPivots(true, PivotRange.Daily, PivotType.All, HLCCalculationMode.CalcFromIntradayData, ProfileType.TPO, 0, 0, 0).H3[0])
    {
    //Greg> Trade Counter attempts to enter long
    EnterShort(DefaultQuantity, "SellMkt");

    //Greg> Trade Counter increments the trade count by 1
    x++;
    }
    }
    Look inside the text for my comments.
    Last edited by koganam; 01-17-2013, 09:14 AM.

    Comment


      #17
      Hi Koganam:

      I appreciate your input!

      1. Hmmm...ok, what would I need to put in the Variables section and Onbarupdate code to have a tick counter for Long and Short?

      2. Does the tick counter only count initial entries only and NOT buys or sells "to cover" or will it count both entries longs/shorts and "covers" longs/shorts.

      2. If the counter, indeed, does count both initial Longs/Shorts to enter and Longs/Shorts to cover, do you believe if I simply make the tickcounter "4" instead of 1, that it will result in any one of the following (which I would find a acceptable as max attempts on one bar):

      ***1. Attempt Long; 2. Cover Stop Short; 3. Attempt Short; 4. Cover Stop Long (then, no more attempts on that bar).

      or

      ***1. Attempt Long; 2. Cover Stop Short; (and another 2nd long attempt) 3. Attempt Long; 4. Cover Stop Short; (not short attempts).

      However, thinking about it a bit more though...I have in the code, that a reversal signal can not happen on the same bar (&& BarsSinceEntry("SellMkt") >= 1). So, if I get a Long signal, then it would be impossible to go Short on the same bar because of this filter for reversals. That being said, wouldn't it be ok to simply make the Tick Counter for "2" instead of "4" if the Tick Counter does, in fact, count BOTH a entry Long signal and a Short Stop Signal?

      So, the question, I guess, is: "Does the Counter ONLY count the initial entry Long or Short?" If it only counts the initial entry Long or Short, then I guess I would be OK with making the Tick Counter (as originally thought) at "1" (if I only one to have one shot per bar give I have a reversal filter that prohibits same bar opposite entries)?

      Thanks so much!!!!



      Greg

      min code...

      not possible to short

      Comment


        #18
        Originally posted by birdog View Post
        Hi Koganam:

        I appreciate your input!

        1. Hmmm...ok, what would I need to put in the Variables section and Onbarupdate code to have a tick counter for Long and Short?
        Pretty much.
        2. Does the tick counter only count initial entries only and NOT buys or sells "to cover" or will it count both entries longs/shorts and "covers" longs/shorts.
        Code does only what you tell it to do. You have your counter gated to entry conditions, so they are the only ones that will trigger an increment. In any event, I do not see where you are covering positions in the code.
        2. If the counter, indeed, does count both initial Longs/Shorts to enter and Longs/Shorts to cover, do you believe if I simply make the tickcounter "4" instead of 1, that it will result in any one of the following (which I would find a acceptable as max attempts on one bar):

        ***1. Attempt Long; 2. Cover Stop Short; 3. Attempt Short; 4. Cover Stop Long (then, no more attempts on that bar).

        or

        ***1. Attempt Long; 2. Cover Stop Short; (and another 2nd long attempt) 3. Attempt Long; 4. Cover Stop Short; (not short attempts).

        However, thinking about it a bit more though...I have in the code, that a reversal signal can not happen on the same bar (&& BarsSinceEntry("SellMkt") >= 1). So, if I get a Long signal, then it would be impossible to go Short on the same bar because of this filter for reversals. That being said, wouldn't it be ok to simply make the Tick Counter for "2" instead of "4" if the Tick Counter does, in fact, count BOTH a entry Long signal and a Short Stop Signal?

        So, the question, I guess, is: "Does the Counter ONLY count the initial entry Long or Short?" If it only counts the initial entry Long or Short, then I guess I would be OK with making the Tick Counter (as originally thought) at "1" (if I only one to have one shot per bar give I have a reversal filter that prohibits same bar opposite entries)?

        Thanks so much!!!!



        Greg

        min code...

        not possible to short
        I do not see your reversal filter in the code either, but if it is there, then it should work pretty much as you say, unless you go flat again.

        You might want to move your FirstTickOfBar filters outside the MarketPosition block: the counters probably need to be reset regardless the market position.
        Last edited by koganam; 02-11-2013, 10:34 AM. Reason: Corrected spelling.

        Comment


          #19
          Hi Koganam & Matthew!

          I believe it is now working perfectly! I am still testing real time live and watching very closely to assure it is (but the preliminary on Friday with the forward testing/market replay & some live on a demo and live seemed to be working...

          Again...appreciate the help very much!

          Be blessed...

          Greg

          Comment


            #20
            Reducing redundant code for initial entries

            Hi Koganam/Matthew:

            On initial entries (when flat) I have this to begin before the " if then conditions":

            Code:
            protected override void OnBarUpdate()
                    {                        
                        if (Historical) return;
                        if (Position.MarketPosition == MarketPosition.Flat)
            I have 4 initial entry "if then conditions" to enter. The first begins with "if" and the three others begin with "else if". On all 4 of the initial entry conditions I have this:

            Code:
            //Greg> Trade Counter resets value every new bars
                            if(FirstTickOfBar)
                            {
                                x = 0;
                            }
                            //Greg> Trade Counter checks for your entry condition and that the value is less than your trade count
                            if (CrossAbove(SMA(Fast), SMA(Slow), 1)...etc etc etc.
            Seems to work ok, but I am wanting to reduce lines of code that may not be needed.

            Could I take this:

            Code:
            //Greg> Trade Counter resets value every new bars
                            if(FirstTickOfBar)
                            {
                                x = 0;
                            }
            away from each of the 4 "if then conditions" (because right now I have it repeat on each condition) and only have it show only one time in the beginning to but still have it apply to ALL 4 of my entry conditions like this:

            Code:
            protected override void OnBarUpdate()
                    {                        
                        if (Historical) return;
                        if (Position.MarketPosition == MarketPosition.Flat)
                         if(FirstTickOfBar)
                            {
                                x = 0;
                            }
            Then, my 4 "if then conditions" follow:

            I.E.

            if (CrossAbove(SMA(Fast), SMA(Slow), 1)...etc etc etc...
            else if "x2"...etc etc etc
            else if "x3"...etc etc etc
            else if "x4"...etc etc etc

            Would that still accomplish the same thing have it one time in the beginning?

            OR, does if have to be there for each "if then condition" in order for the FirstTickOfBar to work?
            Last edited by birdog; 01-25-2013, 10:39 AM.

            Comment


              #21
              Hello Greg,

              Correct - you do not need to reset the x value for every condition. Simply doing it at the beginning on OBU should reset x to 0 at the beginning of every new bar and will be true for all conditions below it.
              MatthewNinjaTrader Product Management

              Comment


                #22
                How would I make it only attempt 1 trade to the long side and 1 trade to the short side on one bar only...total of 2 trades but only 1 each way per bar (what would I adjust in the code to make it do that?

                Also, would I have to add anything in the #region Variables too?

                Post #16 for reference...

                Originally posted by koganam View Post
                Look inside the text for my comments.
                Last edited by birdog; 02-11-2013, 10:10 AM.

                Comment


                  #23
                  Hello,

                  Originally posted by birdog View Post
                  How would I make it only attempt 1 trade to the long side and 1 trade to the short side on one bar only...total of 2 trades but only 1 each way per bar (what would I adjust in the code to make it do that?
                  Could you clarify a little further on what you mean by one bar only?

                  You could use one a separate variable to track the long and short side trades separately and stop trading longs once 1 long trade has been made and stop trading shorts once 1 short trade has been made.

                  You would need to add this extra tracking variable to the #region Variables
                  LanceNinjaTrader Customer Service

                  Comment


                    #24
                    Originally posted by birdog View Post
                    How would I make it only attempt 1 trade to the long side and 1 trade to the short side on one bar only...total of 2 trades but only 1 each way per bar (what would I adjust in the code to make it do that?

                    Also, would I have to add anything in the #region Variables too?

                    Post #16 for reference...
                    Here is the skeleton in pseudo-code.

                    Code:
                     
                    if (FirstTickOfBar)
                    {
                    //reset counters
                    NumLongEntries = 0;
                    NumShortEntries = 0;
                    }
                    if (LongEntryConditionsMet && NumLongEntries < 1)
                    {
                    MakeLongEntry(); //EnterLong() etc
                    NumLongEntries++;
                    }
                    if (ShortEntryConditionsMet && NumShortEntries < 1)
                    {
                    MakeShortEntry(); //EnterShort() etc
                    NumShortEntries++;
                    }
                    The counters will be class variables.
                    The code assumes no protective orders: so that an entry opposite the market direction will create a reverse order. If you want to use protective orders, you will have to make arrangements to check MarketPosition and act accordingly.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    647 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    368 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    108 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    571 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    573 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X