Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SMA crossover and Calculate on bar close FALSE

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

    SMA crossover and Calculate on bar close FALSE

    Hi there:

    I am working with a strategy that has a SMA crossover on 2 averages. I have it set to Calculate on bar close FALSE.

    Right now, upon enabling, it pretty much enters a trade long or short if I choose FALSE depending on if the crossover is bullish or bearish immediately.

    I have Wait until flat before executing live set in the Options by the way too...

    Also, I have if (Historical) return; included too...

    When I have it set to TRUE this is not a issue...only when it is set to FALSE is this a issue.

    Here is what I would like to prevent it from doing...upon enabling the strategy when it is set to FALSE, I only want it to take trades once the crossovers actually touch or cross.

    Here is the basic set up as an example for a long:

    Code:
    protected override void OnBarUpdate()
            {
                if (Historical) return; 
                if (Position.MarketPosition == MarketPosition.Flat)                                                        
                    if(FirstTickOfBar)
                    {
                        x = 0;
                    }
                    if (BarsInProgress != 0) 
                    return;
                    
                    if (CrossAbove(SMA(Fast), SMA(Slow), 1)
                        && x < myTradeCounter
                        && (EMA(BarsArray[1], 1)[0] > EMA(BarsArray[1], 3)[0] 
                        && EMA(BarsArray[2], 1)[0] > EMA(BarsArray[2], 3)[0]))
                    {
                        EnterLong(DefaultQuantity, "BuyMkt"); 
                        x++;
                    }
    So, I just want to be able to enable the Strategy and not have it go long or short immediately and to make sure it WAITS until the crossover of the primary signal above (the SMA...i.e. if (CrossAbove(SMA(Fast), SMA(Slow), 1)...) actually touches/crosses either direction when I have it set to Calculate on bar close FALSE....like I said it is not a issue when I have it set to TRUE...but, I would like to run it on FALSE.

    No matter how far away the crossover has happened long or short, upon enabling, it would just immediately go long or short when I have it set to FALSE. However, once enabled and the first signal is out of the way, then it runs perfectly. My issue is when it initially enables for the first time.

    Please let me know what I need to include to ensure signal executions only fire upon the crossovers actually touching/crossing when using Calculate on bar close FALSE...?

    Thanks folks



    Greg
    Last edited by birdog; 01-31-2013, 06:16 AM.

    #2
    Hello,

    I'm not seeing anything apparently wrong with the code posted here but there are some variables I'm unable to verify on my end.

    Could you please post the full source file so that I may test it on my end?

    Additionally you may find this to be a better way for checking trade count: http://www.ninjatrader.com/support/f...ead.php?t=4084

    I look forward to assisting you further.
    LanceNinjaTrader Customer Service

    Comment


      #3
      Lance...thanks for the trade count info...I will look into it...

      Could you, maybe, just post several code snippets of ideas to filter out that 1st signal upon enabling? After the 1st signal everything works great...



      Greg

      Comment


        #4
        Hello,

        From the looks of it you're dealing with at least three different bar series and it's hard for me to determine what these values are to see why the logic is being set to true right away.

        Also, your EnterLong() doesn't specify a BIP. Consider using the 5th overload here to ensure it's being called to the correct bars series: http://www.ninjatrader.com/support/h...?enterlong.htm

        I would strongly suggest using Print statements to make sure the values are coming through when you expect them to and the historical bars are being ignored.
        Code:
        Print(Time[0] + "EMA 1: " + EMA(BarsArray[1], 1)[0]);
        Print(Time[0] + "SMA Fast: " + SMA(Fast));
        //etc
        I'm guessing you might see that the SMA value changes with each call to OnBarUpdate.

        Also consider adding this to prevent out of bounds errors:
        Code:
        // Checks to ensure all Bars objects contain enough bars before beginning      if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired || CurrentBars[2] <= BarsRequired)
                  return;
        http://www.ninjatrader.com/support/h...urrentbars.htm

        Let me know if I can be of further help.
        LanceNinjaTrader Customer Service

        Comment


          #5
          Sorry...I will start over and explain it another way...

          Lance,

          Thanks for the valuable input...I will look into all you have suggested...but...

          As far as this issue, can I try explaining a different way? You can disregard the code I posted in this thread earlier (as it really does not have a bearing on what I am wanting to do...I probably confused you more than anything...so, let me try again here below...)...

          Let me explain it another way:

          I use calculate on bar close (false). I have a SMA crossover 18 and 40 (as an example). When the 18 crosses above the 40 it goes long. When the 18 crosses below the 40 it goes short. Keep in mind it is calculate on bar close (false) so the SMA moves in real time with price as the bar is forming. What I want it to do is that once the 18 crosses either way, then the signal fires for a execution. It runs perfectly as expected now EXCEPT when initially enabling the Strategy (because of the calculate on bar close being false it will immediately enter long or short depending on if the 18 is above or below no matter how far away the 18 is from the 40). The 18 could be far beyond were the actual crossover actually happened because I have it set for calculate on bar close false. After the 1st trade it works fine (it is just when it is first enabled).

          So, I just want add a simple and clean piece of code (as I am a minimalist...hehehe) that will, upon enabling the Strategy initially, take only the first signal/execution for a trade at the actual point of the SMA crossover...just a filter or qualifier of sort. This way, if the 18 is far beyond and above the 40 it will not fire and if the 18 is far beyond and below it will not fire. The first trade, then, would wait until the actual point of crossover only of the 18 and 40 before the first trade would fire.

          What code can I use in the OnBarUpdate section to allow for that to take place (just another reminder...the Strategy is set to Calculate on bar close false...so, the SMA lines move in real-time as price moves)?

          I think that should clear up what I am looking to do...smile...

          Greg
          Last edited by birdog; 01-31-2013, 09:12 AM.

          Comment


            #6
            Hello,

            I've attached a simple cross strategy that enters long with CrossAbove and exits with a CrossBelow

            This will not enter at the start even when COBC = false

            There is likely something else in your code that isn't triggering as expected.

            I know you said you have it set to Wait Until Flat set check marked but I wanted to provide the following for reference as well: http://www.ninjatrader.com/support/h..._positions.htm
            Attached Files
            LanceNinjaTrader Customer Service

            Comment


              #7
              Originally posted by birdog View Post
              Lance,

              Thanks for the valuable input...I will look into all you have suggested...but...

              As far as this issue, can I try explaining a different way? You can disregard the code I posted in this thread earlier (as it really does not have a bearing on what I am wanting to do...I probably confused you more than anything...so, let me try again here below...)...

              Let me explain it another way:

              I use calculate on bar close (false). I have a SMA crossover 18 and 40 (as an example). When the 18 crosses above the 40 it goes long. When the 18 crosses below the 40 it goes short. Keep in mind it is calculate on bar close (false) so the SMA moves in real time with price as the bar is forming. What I want it to do is that once the 18 crosses either way, then the signal fires for a execution. It runs perfectly as expected now EXCEPT when initially enabling the Strategy (because of the calculate on bar close being false it will immediately enter long or short depending on if the 18 is above or below no matter how far away the 18 is from the 40). The 18 could be far beyond were the actual crossover actually happened because I have it set for calculate on bar close false. After the 1st trade it works fine (it is just when it is first enabled).

              So, I just want add a simple and clean piece of code (as I am a minimalist...hehehe) that will, upon enabling the Strategy initially, take only the first signal/execution for a trade at the actual point of the SMA crossover...just a filter or qualifier of sort. This way, if the 18 is far beyond and above the 40 it will not fire and if the 18 is far beyond and below it will not fire. The first trade, then, would wait until the actual point of crossover only of the 18 and 40 before the first trade would fire.

              What code can I use in the OnBarUpdate section to allow for that to take place (just another reminder...the Strategy is set to Calculate on bar close false...so, the SMA lines move in real-time as price moves)?

              I think that should clear up what I am looking to do...smile...

              Greg
              It sounds as if your code is checking for relative position of the moving averages rather than an explicit crossover condition.

              Comment


                #8
                Koganam:

                Yes, you nailed it!

                This what I have for the primary signal (the other EMAs do not matter as they can be, and actually must be, relative). But, this one needs to be explicit:

                For longs
                Code:
                if (CrossAbove(SMA(Fast), SMA(Slow), 1)
                and vice versa for shorts

                What code or adjustment would I need to make to qualify this signal in particular as explicit?

                As you said it...absolutely...I just need to know how to make it explicit and that should do it I think...smile?

                Comment


                  #9
                  Koganam and Lance...please look at my last post when you are able, but also...

                  Koganam and Lance...please look at my last post when you are able, but also I am testing this out to see if it works to solve the issue. I wanted to get your thoughts though on a solution for the explicit vs relative signals and how to differentiate and put into code (that would be huge help on multiple fronts)...That is #1...

                  #2, below I add code and remove the other to test to see if it resolve the initial trade issue I was having with enabling Strategies using COBC. I just enabled it a several instruments and it "seems" to be working but really I have to monitor...but at least when I enable them all they all do not immediately enter positions any longer...a few did but I think there was a crossover event on them...have to check that...here is what i did:

                  I removed which was by itself:

                  Code:
                  if (BarsInProgress != 0)
                  return;
                  I replaced with this:

                  Code:
                  if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired || CurrentBars[2] <= BarsRequired)return; // Checks to ensure all Bars objects contain enough bars before beginning 
                  if (BarsInProgress != 0)
                  Do you think that would change the behavior to cause the change?

                  Also, what is the difference between if (BarsInProgress != 0) and this if (BarsInProgress == 0)

                  I am currently using if (BarsInProgress != 0). My main signal is the SMA on the primary chart (index=0 on the BarArray which I am looking for that explicit signal Koganam pointed out) and the 2 other EMAs in the BarArray index=1 and index=2 are simply just 2 other higher time frames that I have which I only want it to check them for relative position in the direction of my trade). Am I ok with if (BarsInProgress != 0) or should I consider if (BarsInProgress == 0)

                  Anyway...I am testing it now to see the behavior...love to get both of your inputs etc when you have a chance sometime...



                  Greg

                  Comment


                    #10
                    Originally posted by birdog View Post
                    Koganam and Lance...please look at my last post when you are able, but also I am testing this out to see if it works to solve the issue. I wanted to get your thoughts though on a solution for the explicit vs relative signals and how to differentiate and put into code (that would be huge help on multiple fronts)...That is #1...

                    #2, below I add code and remove the other to test to see if it resolve the initial trade issue I was having with enabling Strategies using COBC. I just enabled it a several instruments and it "seems" to be working but really I have to monitor...but at least when I enable them all they all do not immediately enter positions any longer...a few did but I think there was a crossover event on them...have to check that...here is what i did:

                    I removed which was by itself:

                    Code:
                    if (BarsInProgress != 0)
                    return;
                    I replaced with this:

                    Code:
                    if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired || CurrentBars[2] <= BarsRequired)return; // Checks to ensure all Bars objects contain enough bars before beginning 
                    if (BarsInProgress != 0)
                    Do you think that would change the behavior to cause the change?

                    Also, what is the difference between if (BarsInProgress != 0) and this if (BarsInProgress == 0)

                    I am currently using if (BarsInProgress != 0). My main signal is the SMA on the primary chart (index=0 on the BarArray which I am looking for that explicit signal Koganam pointed out) and the 2 other EMAs in the BarArray index=1 and index=2 are simply just 2 other higher time frames that I have which I only want it to check them for relative position in the direction of my trade). Am I ok with if (BarsInProgress != 0) or should I consider if (BarsInProgress == 0)

                    Anyway...I am testing it now to see the behavior...love to get both of your inputs etc when you have a chance sometime...



                    Greg
                    Hard to say without seeing all your code, but from what you describe, you seem to have it the wrong way around. Not having seen your code, I can only respond with pseudocode.

                    Code:
                     
                    protected override void OnBarUpdate()
                    {
                    //CurrentBars check goes here. (Just as you already have it)
                    if (BarsInProgress == 0) //we want to check for crossover on the primary BarsArray
                    {
                    //Check for crossover condition on primary timeframe, and do what you need to do.
                    //Entries can only be made here.
                    }
                    if (BarsInProgress != 0) //on other BarsArrays, we just want to check relative position
                    {
                    //Check relative position of MA's for confirmation or reverse signal or whatever, and act on that. Do NOT make entries here. I suppose you can make exits.
                    }
                    }

                    Comment


                      #11
                      Koganam:

                      So, when using if (BarsInProgress == 0) that would be considered using the bar objects of the primary bar & ALSO the function of it being a EXPLICIT crossover as opposed to a relative position of the crossover?

                      and

                      when using if (BarsInProgress != 0) that would be considered using the bar objects of the primary bar & ALSO the function of it being a RELATIVE crossover as opposed to a explicit crossover (at the actual time of crossover)?

                      Then, I could change the number to 1, 2, 3 etc for different BarArray indexes?

                      Just want to be sure I go it...

                      Comment


                        #12
                        Originally posted by birdog View Post
                        Koganam:

                        So, when using if (BarsInProgress == 0) that would be considered using the bar objects of the primary bar & ALSO the function of it being a EXPLICIT crossover as opposed to a relative position of the crossover?

                        and

                        when using if (BarsInProgress != 0) that would be considered using the bar objects of the primary bar & ALSO the function of it being a RELATIVE crossover as opposed to a explicit crossover (at the actual time of crossover)?

                        Then, I could change the number to 1, 2, 3 etc for different BarArray indexes?

                        Just want to be sure I go it...

                        Not quite.

                        (BarsInProgress == 0) means that you are dealing and referencing the Primary BarsArray, only, irrespective of what is happening on any other bar. As you are looking for the crossover on the primary bar series, this is where you check if a crossover occurred. Make sure that you are checking for a crossover, not just if one MA is above another, which is just checking relative positioning.

                        (BarsInProgress != 0) means that you are looking at all Bar series except the primary, and what happens on the primary is not part of the decision tree involved in whatever you are doing here.

                        IOW, BarsInProgress allows you to isolate your actions relative to the Bars Series of interest, and without interference from the other, excluded, bar series.

                        Comment


                          #13
                          Hey Koganam:

                          Ok, I am seeing it clearer now I believe.

                          If you had this, how would you command the logic to check for a explicit crossover condition on the primary bars (the SMA) and then check for the 2 secondary EMA bars for relative position in the direction of the crossover of the primary to confirm the trade direction using the BarsInProgress...I am testing combination but just want to be sure I am on the right track. Here is a basic example to work with (how would I position the BarsInProgress to do that...this is what I have now)?:

                          ***What and where do I need to adjust the BarsInProgress to make the primary look for a explicit crossover on the primary bars but at the same time look for a relative crossover on the two EMAs?

                          ***Do I need to have BarsInProgress in more than just one place in the begining etc?

                          Code:
                          if (Historical) return; 
                          if (Position.MarketPosition == MarketPosition.Flat) 
                          if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired || CurrentBars[2] <= BarsRequired)return; 
                          if (BarsInProgress == 0) 
                          {            
                          if(FirstTickOfBar)
                          {
                               x = 0;
                          }
                                                      
                                 if (CrossAbove(SMA(Fast), SMA(Slow), 1) //Explicit Crossover Bullish Conditions
                                              // Checks  if the 18 period EMA is above the 40 period EMA on both the                       Daily and Weekly time frames
                                              && x < myTradeCounter
                                              // Relative Position of the 18 being above the 40 EMA Bullish Conditions                       && (EMA(BarsArray[1], 1)[0] > EMA(BarsArray[1], 3)[0] 
                                              && EMA(BarsArray[2], 1)[0] > EMA(BarsArray[2], 3)[0]))
                                 {
                                              //Greg> Trade Counter attempts to enter long
                                              EnterLong(DefaultQuantity, "BuyMkt"); 
                                              //Greg> Trade Counter increments the trade count by 1
                                              x++;
                                  }
                          Last edited by birdog; 02-02-2013, 01:51 PM.

                          Comment


                            #14
                            Hello birdog,
                            Yes, now it seem to be right. With this you wont get the entries @ the start of the strategy.

                            Thanks koganam for your valuable inputs.
                            JoydeepNinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by birdog View Post
                              Hey Koganam:

                              Ok, I am seeing it clearer now I believe.

                              If you had this, how would you command the logic to check for a explicit crossover condition on the primary bars (the SMA) and then check for the 2 secondary EMA bars for relative position in the direction of the crossover of the primary to confirm the trade direction using the BarsInProgress...I am testing combination but just want to be sure I am on the right track. Here is a basic example to work with (how would I position the BarsInProgress to do that...this is what I have now)?:

                              ***What and where do I need to adjust the BarsInProgress to make the primary look for a explicit crossover on the primary bars but at the same time look for a relative crossover on the two EMAs?

                              ***Do I need to have BarsInProgress in more than just one place in the begining etc?

                              Code:
                              if (Historical) return; 
                              if (Position.MarketPosition == MarketPosition.Flat) 
                              if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired || CurrentBars[2] <= BarsRequired)return; 
                              if (BarsInProgress == 0) 
                              {            
                              if(FirstTickOfBar)
                              {
                                   x = 0;
                              }
                               
                                     if (CrossAbove(SMA(Fast), SMA(Slow), 1) //Explicit Crossover Bullish Conditions
                                                  // Checks  if the 18 period EMA is above the 40 period EMA on both the                       Daily and Weekly time frames
                                                  && x < myTradeCounter
                                                  // Relative Position of the 18 being above the 40 EMA Bullish Conditions                       && (EMA(BarsArray[1], 1)[0] > EMA(BarsArray[1], 3)[0] 
                                                  && EMA(BarsArray[2], 1)[0] > EMA(BarsArray[2], 3)[0]))
                                     {
                                                  //Greg> Trade Counter attempts to enter long
                                                  EnterLong(DefaultQuantity, "BuyMkt"); 
                                                  //Greg> Trade Counter increments the trade count by 1
                                                  x++;
                                      }
                              Your Crossover check looks correct: your EMA checks look funny.

                              If you are checking for the 18MA over the 40MA, then you should be using those values for your periods.

                              Code:
                               
                              EMA(BarsArray[1], 18)[0] > EMA(BarsArray[1], 40)[0]  et.c.,
                              As you have chosen to use the BarsArray and are doing all operations from BarsInProgress0, you should not need any other BarsInProgress filters. They are alternatives; you use the BIP filter or you access the BarsArray by their position.

                              Comment

                              Latest Posts

                              Collapse

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