• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

CrossAbove(), CrossBelow() precision

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

    CrossAbove(), CrossBelow() precision

    Could you please enlighten me on the precision that CrossAbove(), CrossBelow() uses?

    In testing I am comparing the values of two EMAs and I see that they are triggering both a CrossAbove and CrossBelow. The difference between the two ema is approximately "0.00002465761999". I know there are some issues comparing doubles/floating numbers because of rounding. That is why I am asking.

    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:00:00 AM Current Bar #: 6177 EMA_A: 1.13646432653814
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:00:00 AM Current Bar #: 6177 EMA_B: 1.13650664140247
    Crossed Above
    Crossed Below
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:01:00 AM Current Bar #: 6178 EMA_A: 1.13643699773493
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:01:00 AM Current Bar #: 6178 EMA_B: 1.13647664982387
    Crossed Above
    Crossed Below
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:02:00 AM Current Bar #: 6179 EMA_A: 1.13653677702276
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:02:00 AM Current Bar #: 6179 EMA_B: 1.13652887766036
    Crossed Above
    Crossed Below
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:03:00 AM Current Bar #: 6180 EMA_A: 1.13660114789647
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:03:00 AM Current Bar #: 6180 EMA_B: 1.13656480732913
    Crossed Above
    Crossed Below
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:04:00 AM Current Bar #: 6181 EMA_A: 1.13659371596549
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:04:00 AM Current Bar #: 6181 EMA_B: 1.13656987155275
    Crossed Above
    Crossed Below
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:05:00 AM Current Bar #: 6182 EMA_A: 1.13660901643294
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:05:00 AM Current Bar #: 6182 EMA_B: 1.13658435881295
    Crossed Above
    Crossed Below
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:06:00 AM Current Bar #: 6183 EMA_A: 1.13664078325543
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:06:00 AM Current Bar #: 6183 EMA_B: 1.13661068365307
    Crossed Above
    Crossed Below
    PHP Code:

                        string t 
    string.Format("{0:T}"Time[0]);
                        Print(
    "From Strategy - Calculate." Calculate.ToString() + " " Bars.BarsPeriod " " " Current Bar #: " CurrentBar "  EMA_A: " ema_a[0]);
                        Print(
    "From Strategy - Calculate." Calculate.ToString() + " "  Bars.BarsPeriod " " " Current Bar #: " CurrentBar "  EMA_B: " ema_b[0]);


                    if(
    CrossAbove(ema_a,ema_b,1));
                    {
                            Print(
    "Crossed Above");
                            
    EnterLong(0,DefaultQuantity,"TestLong");
                    }

                    if(
    CrossBelow(ema_a,ema_b,1));
                    {
                        Print(
    "Crossed Below");
                        
    EnterShort(0,DefaultQuantity,"TestShort");

                    } 

    #2
    Ok So I found something really weird, or maybe its just me going crazy. I thought I could just multiply the decimal by a large number to get away from round but that didn't work. I cannot explain this but I took the difference between the two ema's and multiplied it by a large number
    PHP Code:
    diff = (ema_a[0]-ema_b[0])*100000000
    and I am checking to see if the difference is negative or positive for the criteria of a cross over.

    But the comparison still passes for both above zero and below zero. That should not be the case. It should either be above, or below, impossible for it to be both. Look at my prints below. I have attached a test strategy to duplicate.

    Diff Before Comparison :-7610.283771875
    Diff > 0 :-7610.283771875
    Crossed Above
    Diff < 0 :-7610.283771875
    Crossed Below
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:51:00 AM Current Bar #: 4787 EMA_A: 1.13652736148614
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:51:00 AM Current Bar #: 4787 EMA_B: 1.13660346432386
    Diff Before Comparison :-7610.283771875
    Diff > 0 :-7610.283771875
    Crossed Above
    Diff < 0 :-7610.283771875
    Crossed Below
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:51:00 AM Current Bar #: 4787 EMA_A: 1.1365340281528
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:51:00 AM Current Bar #: 4787 EMA_B: 1.13660679765719
    Diff Before Comparison :-7276.9504385839
    Diff > 0 :-7276.9504385839
    Crossed Above
    Diff < 0 :-7276.9504385839
    Crossed Below
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:52:00 AM Current Bar #: 4788 EMA_A: 1.13652689827316
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:52:00 AM Current Bar #: 4788 EMA_B: 1.13658008732702
    Diff Before Comparison :-5318.90538584268
    Diff > 0 :-5318.90538584268
    Crossed Above
    Diff < 0 :-5318.90538584268
    Crossed Below
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:52:00 AM Current Bar #: 4788 EMA_A: 1.13652689827316
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 12:52:00 AM Current Bar #: 4788 EMA_B: 1.13658008732702
    Diff Before Comparison :-5318.90538584268
    Diff > 0 :-5318.90538584268
    Crossed Above
    Diff < 0 :-5318.90538584268
    Crossed Below
    Disabling NinjaScript strategy 'Test/152893863'
    Attached Files

    Comment


      #3
      Hello cutzpr,

      I looked at the tests but the conditions you have made are not valid.

      in C# you cannot use a semi colon with an if statement, you have effectively just ignored the if statements:

      Code:
      if(CrossAbove(ema_a,ema_b,1));
      should be

      Code:
      if(CrossAbove(ema_a,ema_b,1))
      Code:
      if(diff>0);
      should be:
      Code:
      if(diff>0)
      Placing braces around code just scopes that code so it will still be run even if the if statement is being omitted:

      Code:
      if(CrossAbove(ema_a,ema_b,1)); // this is ignored
      
      { // will always run this code, does not apply to the above condition
      
      }
      I look forward to being of further assistance.
      JesseNinjaTrader Customer Service

      Comment


        #4
        Jesse,

        That makes absolute sense. I cant believe I missed that. Thank you so much for clarifying.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by bouncetherubble, Yesterday, 08:42 PM
        1 response
        11 views
        0 likes
        Last Post NinjaTrader_ChrisL  
        Started by bortz, Yesterday, 08:18 AM
        5 responses
        35 views
        0 likes
        Last Post bortz
        by bortz
         
        Started by GARZONJ, Yesterday, 09:02 PM
        1 response
        15 views
        0 likes
        Last Post NinjaTrader_ChrisL  
        Started by siroya, Today, 06:32 AM
        4 responses
        19 views
        0 likes
        Last Post NinjaTrader_ChrisL  
        Started by cstern71, 09-02-2019, 10:30 AM
        2 responses
        40 views
        0 likes
        Last Post tgn55
        by tgn55
         
        Working...
        X