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

Stop condition set to max or min of a group of indicators

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

    Stop condition set to max or min of a group of indicators

    Hello,

    I am trying to create a custom stop variable that considers the max of a group of initialized indicators such as SMA, Swing, PSAR for example on a long:

    else if (State == State.DataLoaded)
    { //Other strategy variables defined here...​
    LongStopLoss1 = Math.Max(Swing1.SwingLow,PSAR1,SMA1);
    ShortStopLoss1 = Math.Min(Swing1.SwingHigh,PSAR1,SMA1);
    }
    //....define the long and short entry conditions
    protected override void OnBarUpdate()
    {​//Define a long entry and a short entry
    //Set3
    if (Position.MarketPosition==MarketPosition.Long)
    {if (CrossBelow(Close,LongStopLoss1))
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
    }​


    And do the reverse for defining a ShortStopLoss1 condition and test if Close crosses above it. Currently I couldn't get the Math.Max to work so I am getting around creating a Stop variable by testing if each indicator is crossed like this:

    // Set 3
    if (Position.MarketPosition==MarketPosition.Long)
    {​
    if (CrossBelow(Close, PSAR1,1)
    || CrossBelow
    ​​(Close, Swing1.SwingLow,1)
    || CrossBelow
    ​​(Close, SMA1,1))​
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
    }​


    But I get inconsistent results which trigger a close of the long in what seem like a random selection of one of the three programmed conditions and sometimes not even one of those. The Close crosses below SMA1, but is not triggered until later when Close is below PSAR1 which is not the max for example.

    Any guidance on how to create a single stop variable that will be evaluated against Close and also if a symbol can be added to the chart is appreciated. Thanks!

    Michael

    #2
    Hello Binarytrader,

    Thank you for your post.

    Math.Max takes two arguments. You could nest them together to get the max of multiple values. For example,

    Math.Max(Swing1.SwingLow​, Math.Max(PSAR1,SMA1​));



    You can use a drawing object to draw a symbol on the chart. Below is a Help Guide page which lists all of the available drawing objects:



    Please let me know if I can assist further.
    Last edited by NinjaTrader_Gaby; 03-19-2024, 06:20 AM.
    Gaby V.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Jonafare, 12-06-2012, 03:48 PM
    5 responses
    3,984 views
    0 likes
    Last Post rene69851  
    Started by Fitspressorest, Today, 01:38 PM
    0 responses
    2 views
    0 likes
    Last Post Fitspressorest  
    Started by Jonker, Today, 01:19 PM
    0 responses
    2 views
    0 likes
    Last Post Jonker
    by Jonker
     
    Started by futtrader, Today, 01:16 PM
    0 responses
    6 views
    0 likes
    Last Post futtrader  
    Started by Segwin, 05-07-2018, 02:15 PM
    14 responses
    1,791 views
    0 likes
    Last Post aligator  
    Working...
    X