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() seemingly not working right in some instance

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

    CrossAbove() seemingly not working right in some instance

    Hi,

    We're using CrossAbove() with stochastics in our strategy in backtesting and observed that sometimes it doesn't return true when it should have (based on visuals) and in one instance it seems to have gotten triggered twice.

    Here is the settings:

    Click image for larger version

Name:	TestCrossAboveSettings.JPG
Views:	234
Size:	49.0 KB
ID:	1158061


    These two I thought it should've returned true. I use the values in Crossed Above 1.

    Click image for larger version

Name:	TestCrossAbove_ReturnedFalse.JPG
Views:	191
Size:	253.6 KB
ID:	1158062


    Click image for larger version

Name:	TestCrossAbove_ReturnedFalse2.JPG
Views:	188
Size:	274.6 KB
ID:	1158063

    --------------------------------------------------------------
    --------------------------------------------------------------


    In this one Crossed Above 1 appears to return true twice in succession but visually it shouldn't have.


    Click image for larger version

Name:	TestCrossAbove_CrossedAboveTwiceSomehow.JPG
Views:	180
Size:	291.9 KB
ID:	1158064

    ------------------------------------------------------------------
    ------------------------------------------------------------------


    Here is the test strategy that prints out log of CrossAbove() and CrossBelow() results along with stochastics D and K values for bars ago 0, 1, and 2.

    PHP Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class 
    TestCrossAbove Strategy
    {
    private 
    Stochastics stochastics;

    protected 
    override void OnStateChange()
    {
    if (
    State == State.SetDefaults)
    {
    Description = @"TestCrossAbove";
    Name "TestCrossAbove";
    Calculate Calculate.OnEachTick;
    EntriesPerDirection 1;
    EntryHandling EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy true;
    ExitOnSessionCloseSeconds 30;
    IsFillLimitOnTouch false;
    MaximumBarsLookBack MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution OrderFillResolution.Standard;
    Slippage 0;
    StartBehavior StartBehavior.WaitUntilFlat;
    TimeInForce TimeInForce.Gtc;
    TraceOrders false;
    RealtimeErrorHandling RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration true;
    IsUnmanaged true;
    }
    else if (
    State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute1);
    AddDataSeries(Data.BarsPeriodType.Tick1);
    }
    else if (
    State == State.DataLoaded)
    {
    stochastics Stochastics(BarsArray[0], 10106);
    stochastics.Lines[0].Value 30;
    stochastics.Lines[1].Value 70;
    stochastics.Plots[0].Brush Brushes.DodgerBlue;
    stochastics.Plots[1].Brush Brushes.Goldenrod;
    AddChartIndicator(stochastics);
    }
    }

    protected 
    override void OnBarUpdate()
    {
    if (
    BarsInProgress == && CurrentBar 10) return;

    if (
    BarsInProgress == && IsFirstTickOfBar)
    {
    bool crossedAbove0 CrossAbove(stochastics.Kstochastics.D0);
    bool crossedAbove1 CrossAbove(stochastics.Kstochastics.D1);
    bool crossedBelow0 CrossBelow(stochastics.Kstochastics.D0);
    bool crossedBelow1 CrossBelow(stochastics.Kstochastics.D1);
    Print(
    String.Format("{0:dd/MM/yyyy HH:mm:ss.ffffff}"Bars.GetTime(CurrentBar)) + " >>> " +
    ", Crossed Above 0: " crossedAbove0 +
    ", Crossed Above 1: " crossedAbove1 +
    ", Crossed Below 0: " crossedBelow0 +
    ", Crossed Below 1: " crossedBelow1 +
    ", Stochs.D[0]=" stochastics.D[0] + ", Stochs.D[1]=" stochastics.D[1] + ", Stochs.D[2]=" stochastics.D[2] +
    ", Stochs.K[0]=" stochastics.K[0] + ", Stochs.K[1]=" stochastics.K[1] + ", Stochs.K[2]=" stochastics.K[2]);
    }
    }
    }


    #2
    Hello cmarkb,

    Below is a link to a forum post that demonstrates how to print the previous bar values for two series and the current bars values for two series to see if these have crossed.


    To save the output from the NinjaScript Output window so that you can share this, right-click the output window -> select Save As -> give the file a name -> click Save.

    To export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
    1. Click Tools -> Export -> NinjaScript...
    2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
    3. Click the 'Export' button
    4. Enter a unique name for the file in the value for 'File name:'
    5. Choose a save location -> click Save
    6. Click OK to clear the export location message
    By default your exported file will be in the following location:
    • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
    Below is a link to the help guide on Exporting NinjaScripts.
    http://ninjatrader.com/support/helpG...-us/export.htm


    Are you certain this is the code you are running?

    Are you using 8.0.24.2?

    If you print just the Time, BarsInProgress, and IsFirstTickOfBar without anything else, are you seeing IsFirstTickOfBar is true twice for the same BarsInProgress on the same bar?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi ChelseaB,

      > Are you certain this is the code you are running?
      Yes. I reran it today and checked the areas I had in screenshot in my last post and the issue is still there.

      > Are you using 8.0.24.2?
      Currently using 8.0.23.2. Will try updating and retesting after this submitting this reply.

      > If you print just the Time, BarsInProgress, and IsFirstTickOfBar without anything else, are you seeing IsFirstTickOfBar is true twice for the same BarsInProgress on the same bar?
      Are you referring to my 3rd screenshot, the on on 2021/03/19 22:05 and 22:10? Not on the same bar. Just on two bars right next to each other.
      I've edited the script to just print Time, IsFirstTickOfBar and BarsInProgress and both 22:05 and 22:10 only had IsFirstTickOfBar=Yes once at the very first one.
      But in the chart though K% only crossed once so shouldn't in only be true for only one of them? Or am I wrong about this?

      I've attached the script and both logs -- one with the CrossAbove,CrossBelow, stochastic values and the one with just Time,IsFirstTickOfBar, and BarsInProgress.

      Thanks.
      Attached Files

      Comment


        #4
        I've updated to NT 8.0.24.2 and reran the script. The result is the same.

        Comment


          #5
          Hello cmarkb,

          Your code has bool crossedBelow1 = CrossBelow(stochastics.K, stochastics.D, 1);.

          Do note that you are looking at the first tick of a new bar. This means the plot on that bar is going to have a different value once that bar closes.

          To confirm you inquiry is with the specific output at 22:

          19/03/2021 22:05:00.000000 >>> , Crossed Above 0: False, Crossed Above 1: True, Crossed Below 0: False, Crossed Below 1: False, Stochs.D[0]=15.2438931391159, Stochs.D[1]=19.4992271681041, Stochs.D[2]=25.5415744126917, Stochs.K[0]=16.0744857371373, Stochs.K[1]=11.5305030064304, Stochs.K[2]=7.38843823634629
          19/03/2021 22:10:00.000000 >>> , Crossed Above 0: False, Crossed Above 1: True, Crossed Below 0: False, Crossed Below 1: False, Stochs.D[0]=11.914744385461, Stochs.D[1]=14.9686637813177, Stochs.D[2]=19.4992271681041, Stochs.K[0]=15.1997431795639, Stochs.K[1]=13.3221921591557, Stochs.K[2]=11.5305030064304

          22:05:00 - Stochs.D[0]: 15.2438931391159 < Stochs.K[0]: 16.0744857371373 && Stochs.D: 19.4992271681041 > Stochs.K[1]: 11.5305030064304

          15.24 is less than 16.07 and 19.49 is greater than 11.53

          22:10:00 - Stochs.D[0]: 11.914744385461 < Stochs.K[0]: 15.1997431795639 && Stochs.D: 14.9686637813177 > Stochs.K[1]: 13.3221921591557

          11.91 is less than 15.19 and 14.96 is greater than 13.32.

          As far as I can tell, on the very first tick of this bar, there was a cross on both bars right when the bar was opening. Later while that bar was open it may have reversed and longer been a cross.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Hi ChelseaB,

            Thanks for the reply.

            > Do note that you are looking at the first tick of a new bar. This means the plot on that bar is going to have a different value once that bar closes.
            > As far as I can tell, on the very first tick of this bar, there was a cross on both bars right when the bar was opening. Later while that bar was open it may have reversed and longer been a cross.

            I see now that the current forming bar [0] shouldn't be include in the CrossAbove() call. So my solution is to add two Series<double> for the K% and D% and do something like:

            PHP Code:
            stochsK[0] = stochastics.K[1];
            stochsD[0] = stochastics.D[1];
            bool crossedAbove CrossAbove(stochsKstochsD1); 
            This seems to solve my issues. The parts that weren't triggered were now triggering and that one instance with two successive bar triggers now only does it once.

            Thanks again.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Haiasi, Today, 06:53 PM
            1 response
            3 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by ScottWalsh, Today, 06:52 PM
            1 response
            6 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by ScottW, Today, 06:09 PM
            1 response
            4 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by ftsc2022, 10-25-2022, 12:03 PM
            5 responses
            256 views
            0 likes
            Last Post KeyonMatthews  
            Started by Board game geek, 10-29-2023, 12:00 PM
            14 responses
            244 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Working...
            X