Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

EMA cross Question

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

    EMA cross Question

    I am trying to code a basic entry of when the 9 EMA crosses the 34 EMA to enter into a position. With no look back period. Therefore I changed in the code

    if (CrossAbove(EMA(9), EMA(34), 1))
    to
    if (CrossAbove(EMA(9), EMA(34), 0))

    and included the conditions ToTime coded as such:

    if (ToTime(Time[0]) >= 61500 && ToTime(Time[0]) <= 111500)
    if (ToTime(Time[0]) >= 131500 && ToTime(Time[0]) <= 161500)
    if (ToTime(Time[0]) >= 191500 && ToTime(Time[0]) <= 231500)

    Now the the trading strategy is always exiting the trade once entered, paying the spread.

    I am trying to eneter into One position on the 9 Crossing the 34 EMA with no look back period and only trading from 6:15 AM to 11:15AM and 13:15PM to 16:15PM and from 19:15PM to 23:15PM

    I've posted my coding below if anyone is so kind enough to please point me out my mistakes.


    ///<summary>
    /// Called on each bar update event (incoming tick)
    ///</summary>
    protectedoverridevoid OnBarUpdate()
    {
    // Condition set 1
    if (CrossAbove(EMA(9), EMA(34), 0))
    {
    }
    // Condition set 2
    if (ToTime(Time[0]) >= 61500 && ToTime(Time[0]) <= 111500)
    if (ToTime(Time[0]) >= 131500 && ToTime(Time[0]) <= 161500)
    if (ToTime(Time[0]) >= 191500 && ToTime(Time[0]) <= 231500)
    {
    }
    EnterLong(DefaultQuantity, "");
    // Condition set 4
    if (CrossBelow(EMA(9), EMA(34), 0))
    {
    }
    // Condition set 5
    if (ToTime(Time[0]) >= 61500 && ToTime(Time[0]) <= 111500)
    if (ToTime(Time[0]) >= 131500 && ToTime(Time[0]) <= 161500)
    if (ToTime(Time[0]) >= 191500 && ToTime(Time[0]) <= 231500)
    {
    }
    EnterShort(DefaultQuantity, "");
    }
    Last edited by Ninja Learner; 12-03-2007, 06:29 PM.

    #2
    Unfortunately we can not review complete trading strategies for bandwidth reasons. I suggest starting simple as possible, adding logic line by line and using debugging techniques as per here: http://www.ninjatrader-support.com/v...ead.php?t=3418

    Comment


      #3
      To lead you in the right direction, you will need watch where your brackets are. You create a condition if( blah blah blah) then you have brackets { }. The if condition only applies to things inside the {}.

      So for instance you have
      Code:
      [FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] (CrossAbove(EMA([/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]9[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]), EMA([/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]34[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]), [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]))[/SIZE][/FONT]
      [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
      [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]

      This means you've created a cross above condition that needs to be evaluated true, BUT you are not applying this "filter" to anything.
      Code:
      [FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] (CrossAbove(EMA([/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]9[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]), EMA([/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]34[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]), [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]))[/SIZE][/FONT]
       [SIZE=2][FONT=Courier New]{
           // Do something when this is true
      [/FONT][/SIZE]  [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
      Josh P.NinjaTrader Customer Service

      Comment


        #4
        CrossAbove/Below Look Back Period

        Hey Josh,

        I have

        If (CrossAbove(SMA(Fast), SMA(Slow), 0)
        {
        EnterLong()
        }

        My question is, by setting my look back period to 0, does that mean the second my last bar closes and hence a cross over occurs, that it will execute the code inside my bracket?

        For some reason, (and this is all real time trading), I can't seem to fire the trade on immediate after the bar closes, hence putting me in the trade based on the crossabove/below occurance. It seems to wait a whole other bar (when it was set to 1, it worked, but a whole bar went by, that's a lot of lag on say a 10/15m bar, no?)


        Thanks,

        Comment


          #5
          r2kTrader,

          You should use a lookback period of at least 1. To determine a cross, NinjaTrader requires two data points. One to identify that the SMA(Fast) was below the SMA(Slow). Another to identify that the SMA(Fast) is above the SMA(Slow). When you set a lookback period to 0 you are looking at the same data point and no crosses can occur when comparing on one data point.

          If you want to trade immediately intrabar use a lookback period of 1 and CalculateOnBarClose = false. The moment it crosses, you will trade. Remember crosses can flip flop many times throughout the building of a bar.
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            Bug?

            Josh,

            As per my last post, it seems that if I use 0 for the look back period, that the strategy does not work. No error, it just doesn't fire any trades.

            Can the look back period be set to zero in real time trading? How can I get rid of that one bar lag.


            thanks.

            Originally posted by r2kTrader View Post
            Hey Josh,

            I have

            If (CrossAbove(SMA(Fast), SMA(Slow), 0)
            {
            EnterLong()
            }

            My question is, by setting my look back period to 0, does that mean the second my last bar closes and hence a cross over occurs, that it will execute the code inside my bracket?

            For some reason, (and this is all real time trading), I can't seem to fire the trade on immediate after the bar closes, hence putting me in the trade based on the crossabove/below occurance. It seems to wait a whole other bar (when it was set to 1, it worked, but a whole bar went by, that's a lot of lag on say a 10/15m bar, no?)


            Thanks,

            Comment


              #7
              As stated in my prior post. You need to use a lookback period >= 1 with CalculateOnBarClose = false. There is no lag already.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                CalculateOnBarClose = True in my code.

                Are you saying I have to have it set to false?

                If I am understanding you correctly, all I need to do is change look back to 1 and calc to False?

                Isn't that more processor intensive though? Is that the only solution?



                Originally posted by NinjaTrader_Josh View Post
                As stated in my prior post. You need to use a lookback period >= 1 with CalculateOnBarClose = false. There is no lag already.

                Comment


                  #9
                  Yes. If you want to do anything intrabar you have to use CalculateOnBarClose = false. This will only make a difference in real-time strategies too.

                  It is the tradeoff you have to decide on. You either calculate intrabar and have it be more computationally intensive or you calculate at end of the bar and have "delays".
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Josh,

                    There is one thing I don't quite understand. Even if I set CalculateOnBarClose to true, why doesn't it fire signal on the next bar? This doesn't seem logic.

                    The bar closed, it crossed above/below, next bar should fire.

                    I did test what you said and it did fire correctly, but I don't get the logic.


                    THanks,

                    Comment


                      #11
                      r2kTrader, the bar is 'closed' when you receive the open tick of the next bar, so essentially the order is send / placed then.

                      Comment


                        #12
                        Bertrand,

                        Thank you for the reply. Can we walk through this like a 5 year old

                        Let's say the system is set to run at 9:30 am on 5m bars.

                        we look for crossover above/below an SMA

                        15m has rolled by with the last 5m bar closing 1 tick ago. I can see the crossover drawn on the screen, but the trade won't go off until the next bar.

                        Can you explain why this is happening?

                        These are the only additions I have added to the sample SMA example. I don't want historical orders to submit, so I put in the If Historical test.

                        CalculateOnBarClose = true
                        If(Historical)
                        {return};

                        Comment


                          #13
                          r2kTrader,

                          If you want to trade intrabar you cannot use CalculateOnBarClose = true. You need to set it to false.
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #14
                            Originally posted by NinjaTrader_Josh View Post
                            r2kTrader,

                            If you want to trade intrabar you cannot use CalculateOnBarClose = true. You need to set it to false.
                            Josh,

                            I understand that you cannot trade IntraBar with CalcOnBarClose set to true. I really do. My question (see above posts), is regarding why this is considered Intrabar? I am waiting for the bar to close.

                            Thanks,

                            Comment


                              #15
                              r2kTrader,

                              It is absolutely intrabar. You have a signal bar. You see the cross on the signal bar. The signal bar evaluates your condition to true. Next tradeable location is the open of the next bar because you are using CalculateOnBarClose = true and as such the order goes in on the next bar.
                              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
                              648 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              369 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
                              572 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              574 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X