Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

CrossAbove/Below

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

    CrossAbove/Below

    Dear ladies and gentlemen,

    i try to understand the methode crossabove and Below, therefore I createt a simple strategy, and yes it trades a lot, but where is the signal coming from to trade?

    public class Test2 : Strategy
    {
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "Test2";
    Calculate = Calculate.OnPriceChange;
    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 = true;
    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;
    }
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute, 1);

    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 10)
    return;

    // Set 1
    if (CrossAbove(GetCurrentAsk(0), High, 1))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), "Enter Long");
    SetStopLoss("Enter Long", CalculationMode.Price, Low[0], false);
    SetProfitTarget("Enter Long", CalculationMode.Ticks, 10);
    }

    }
    }

    is it possible to see the code for this methode, to understand how it works. The ninjatrader Help guide is to less for me.

    thanks for your help!

    lg

    #2
    Hello sane1111,

    Thanks for your post.

    The CrossAbove condition will check if one value crosses above another value over the specified bar look-back period.

    For example, the code you shared would check if the current ask price crosses above the current High price on the previous bar (look-back period of 1) and would call your order entry method and Set methods. However, it seems there might be an issue with your syntax. Does the code you shared compile without errors?

    The condition should look something like this.

    if (CrossAbove(High, GetCurrentAsk(0), 1))
    {
    }


    Note that Set methods should be called before the entry order method. Such as:

    Code:
    if (condition 1)
    {
        SetStopLoss("Enter Long", CalculationMode.Price, Low[0], false);
        SetProfitTarget("Enter Long", CalculationMode.Ticks, 10);
        EnterLong(Convert.ToInt32(DefaultQuantity), "Enter Long");
    }
    The SampleMACrossOver strategy that comes default with NinjaTrader is a great example of using crossabove/crossbelow conditions in a strategy. To view the script, open a New > NinjaScript Editor window, open the Strategies folder, and double-click the SampleMACrossOver file to view the code.

    See the help guide documentation below for more information and sample code.
    CrossAbove: https://ninjatrader.com/support/help...crossabove.htm
    CrossBelow: https://ninjatrader.com/support/help...crossbelow.htm

    Let me know if I may assist further.
    Last edited by NinjaTrader_BrandonH; 07-18-2022, 09:46 AM.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Originally posted by sane1111 View Post
      if (CrossAbove(GetCurrentAsk(0), High, 1))

      is it possible to see the code for this methode, to understand how it works. The ninjatrader Help guide is to less for me.
      Sure, I can help.

      But first, does that code even compile?
      It should not -- because it appears to be wrong.

      From the documentation point of view, anyways, your arguments
      are in the wrong order. It should be this,

      CrossAbove(High, GetCurrentAsk(0), 1)

      -=o=-

      CrossAbove/Below is trying to determine the moment when a series
      becomes greater/lesser than another series or another value -- that's
      why there are two signatures in the documentation.

      How is a 'cross' event determined?

      It's pretty simple -- I'll discuss the cross 'above' event, the 'below'
      event is just a matter of the comparison operator employed.

      The algorithm is similar to this,

      Code:
      private bool MyCrossAbove(Series<double> ds, double Threshold, int Lookback)
      {
          for (int indx = 0; indx < Lookback; ++indx)
              if (ds[indx] > Threshold && ds[indx+1] < Threshold)
                 return true;
      
          return false;
      }
      Let's look carefully at the first iteration of the loop.

      The idea is the current value at ds[0] must be greater than Threshold,
      while at the same time the previous series value ds[1] must still be less
      than Threshold. If that's true, you've found a 'cross above' event.

      Careful ...
      Just because ds[0] is higher than Threshold is not enough, the 'cross'
      happened only when the previous value on the previous bar ds[1] is also
      lower than the Threshold -- that is, the 'cross above' event occurs when
      the current ds value is higher but previous ds value is still lower.

      Last point ...
      The code is using '>' because '>=' means they could be equal, which is
      arguably wrong, because when they're equal that means ds[indx]
      isn't actually 'above' Threshold yet.

      Make sense?



      PS: You could also use this instead,

      if (ds[indx] > Threshold && ds[indx+1] <= Threshold)

      rather than,

      if (ds[indx] > Threshold && ds[indx+1] < Threshold)

      That is, using <= is a little more lenient, and arguably still correct, since
      here it's ok to be equal -- why? because 'equal' is not 'above'.
      Last edited by bltdavid; 07-18-2022, 10:00 AM.

      Comment


        #4
        thanks BrandonH and bltDavid for your reply,

        yes it is possible to combile the code. In Visual Stuido there is the following defintion.



        public bool CrossAbove(ISeries<double> series1, ISeries<double> series2, int lookBackPeriod);

        public bool CrossAbove(ISeries<double> series1, double value, int lookBackPeriod);

        public bool CrossAbove(double value, ISeries<double> series2, int lookBackPeriod);


        bltDavid thanks for untieding the knot




        Last edited by sane1111; 07-19-2022, 05:19 AM.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        65 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        139 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        75 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        45 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        50 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X