Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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))[B];[/B]
      should be

      Code:
      if(CrossAbove(ema_a,ema_b,1))
      Code:
      if(diff>0)[B];[/B]
      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))[B]; [/B]// 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 Christopher_R, Today, 12:29 AM
        0 responses
        7 views
        0 likes
        Last Post Christopher_R  
        Started by sidlercom80, 10-28-2023, 08:49 AM
        166 responses
        2,235 views
        0 likes
        Last Post sidlercom80  
        Started by thread, Yesterday, 11:58 PM
        0 responses
        3 views
        0 likes
        Last Post thread
        by thread
         
        Started by jclose, Yesterday, 09:37 PM
        0 responses
        7 views
        0 likes
        Last Post jclose
        by jclose
         
        Started by WeyldFalcon, 08-07-2020, 06:13 AM
        10 responses
        1,415 views
        0 likes
        Last Post Traderontheroad  
        Working...
        X