Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Set stop at latest swing low. Gives not correct results.

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

    Set stop at latest swing low. Gives not correct results.

    I am working on a strategy where I place a Stoploss at the last swing low. The entry condition right now is a simple close above the EMA. Just for testing purposes.

    This is the code I have setup:

    Code:
    protected override void OnBarUpdate()
    {
         if (BarsInProgress != 0 || CurrentBars[0] < BarsRequiredToTrade)
         {
            return; // Wait until there are enough bars
         }
        
        if (Position.MarketPosition == MarketPosition.Flat && Close[0] > EMA1[0])
        {
            EnterLong(Convert.ToInt32(DefaultQuantity), "LongEntry");
            SetStopLoss(CalculationMode.Price, Swing(5).SwingLow[0]);  
         }
    }​
    If I test it in Strategy Analyser I get this (see attachments).

    I have plotted a Swing indicator on my chart with the strength of 5

    Code:
    private EMA EMA1;
            private Swing swingLowIndicator;
            private Swing swingHighIndicator;​
    Code:
    else if (State == State.DataLoaded)
                {                
                    EMA1= EMA(Close, 21);
                    EMA1.Plots[0].Brush = Brushes.Goldenrod;
                    AddChartIndicator(EMA1);
                    
                    swingLowIndicator = Swing(5);  // Swing indicator with a strength of 5
                    swingHighIndicator = Swing(5);  // Swing indicator with a strength of 5
                    AddChartIndicator(swingLowIndicator);
                    AddChartIndicator(swingHighIndicator);
                    
                    
                    SetProfitTarget("", CalculationMode.Ticks, 90);    
                }​
    As you can see on the images is that the Last Swing Low is not being used for the Stop Loss placement.
    Can anyone help me out with this setup?





    ​​
    Attached Files

    #2
    Hello wuannetraam,

    The Swing does change its values historically. Where a bar may not have had a swing value as that bar was processed, later this bars value may be changed and may show a value.

    When the entry is made, the swing high or low might not have a value.

    The help guide provides sample code on printing the high of the bar with the most recent swing high.

    From the help guide:
    Print("The high of the swing bar is " + High[Math.Max(0, Swing(5).SwingHighBar(0, 1, 10))]);


    I would also recommend printing the time of the high swing bar (and the time of the current bar) to show which bar the value is pulled from.

    Print(string.Format( "CurrentBar Time: {0}, SwingHighBar: {1}, SwingHighBar Time: {2}, High[{1}]: {3}", Time[0], Swing(5).SwingHighBar(0, 1, 10), Time[Math.Max(0, Swing(5).SwingHighBar(0, 1, 10))], High[Math.Max(0, Swing(5).SwingHighBar(0, 1, 10))] ));

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello wuannetraam,

      The Swing does change its values historically. Where a bar may not have had a swing value as that bar was processed, later this bars value may be changed and may show a value.

      When the entry is made, the swing high or low might not have a value.

      The help guide provides sample code on printing the high of the bar with the most recent swing high.

      From the help guide:
      Print("The high of the swing bar is " + High[Math.Max(0, Swing(5).SwingHighBar(0, 1, 10))]);


      I would also recommend printing the time of the high swing bar (and the time of the current bar) to show which bar the value is pulled from.

      Print(string.Format( "CurrentBar Time: {0}, SwingHighBar: {1}, SwingHighBar Time: {2}, High[{1}]: {3}", Time[0], Swing(5).SwingHighBar(0, 1, 10), Time[Math.Max(0, Swing(5).SwingHighBar(0, 1, 10))], High[Math.Max(0, Swing(5).SwingHighBar(0, 1, 10))] ));
      thank you. That makes sense. Is there a way to update the stop loss to a newer swing low? with the condition that the newer swing may not be after the entry time. So only the new lows before entry time.

      Comment


        #4
        Hello wuannetraam,

        You can call SetStopLoss() again when the SwingHighBar() updates to a new value. Each time you call SetStopLoss() it will update to the new price or distance.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        58 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        133 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        73 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