Announcement

Collapse
No announcement yet.

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.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Mindset, 04-21-2026, 06:46 AM
    0 responses
    87 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    132 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    68 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    118 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    67 views
    0 likes
    Last Post PaulMohn  
    Working...
    X