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 futtrader, 04-21-2024, 01:50 AM
    4 responses
    41 views
    0 likes
    Last Post futtrader  
    Started by Option Whisperer, Today, 09:55 AM
    1 response
    11 views
    0 likes
    Last Post bltdavid  
    Started by port119, Today, 02:43 PM
    0 responses
    3 views
    0 likes
    Last Post port119
    by port119
     
    Started by Philippe56140, Today, 02:35 PM
    0 responses
    4 views
    0 likes
    Last Post Philippe56140  
    Started by 00nevest, Today, 02:27 PM
    0 responses
    2 views
    0 likes
    Last Post 00nevest  
    Working...
    X