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 NullPointStrategies, Today, 05:17 AM
    0 responses
    52 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    130 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    70 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    44 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    49 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X