Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SetStopLoss is not working in Strategy

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

    SetStopLoss is not working in Strategy

    Hi

    It seems like SetStopLoss is not working in my strategy.


    1)When using EnterLong() -by default it used the close of the Candle -- Is it possible to use Open of the Candle ?


    Code:
        if (Basp.ChannelingUP[0])
                {
                    double entryPrice = Bars.GetOpen(CurrentBars[0]); // Get the open price of the current candle
                    double stopLossPrice = entryPrice + 8; // Calculate the stop loss price (adjust as needed)
                    string signalName = "ENTERLONG";
    
                    Print("Entering long at open price: " + entryPrice); // Print the entry price
                    EnterLongLimit(entryPrice, signalName);
    
                    Print("Adding stop loss at price: " + stopLossPrice); // Print the stop loss price
                    SetStopLoss("StopLos****OnLP", CalculationMode.Price, stopLossPrice, false);
                }
                else if (Basp.ChannelingDOWN[0])
                {
                    double stopLossPrice = Close[0] + 8; // Calculate the stop loss price (adjust as needed)
                    string signalName = "ENTERSHORT";
                    double entryPrice = Bars.GetOpen(CurrentBars[0]); // Get the open price of the current candle
    
                    Print("Entering short at open price: " + entryPrice); // Print the entry price
                    EnterShortLimit(entryPrice, signalName);
    
                    Print("Adding stop loss at price: " + stopLossPrice); // Print the stop loss price
                    SetStopLoss("StopLos****OnSP", CalculationMode.Price, stopLossPrice, false);
                }​
    In Attachment Trades report you can see --Even I've set stoploss to 8 Point in my Code --it's never hitting the stoploss.
    Trading NQ23 future -1 Quantity.


    Someoutput from the code

    Code:
    Time: 12/1/2023 11:54:00 AM | ChannelingDOWN[0]: True
    Entering short at open price: 16017.25
    Adding stop loss at price: 16021.75
    Time: 12/1/2023 11:55:00 AM | ChannelingUP[0]: True
    Entering long at open price: 16013.5
    Adding stop loss at price: 16021.5
    Time: 12/1/2023 12:07:00 PM | ChannelingDOWN[0]: True
    Entering short at open price: 16026
    Adding stop loss at price: 16020.5
    Time: 12/1/2023 12:12:00 PM | ChannelingUP[0]: True
    Entering long at open price: 16022.25
    Adding stop loss at price: 16030.25
    Time: 12/1/2023 12:16:00 PM | ChannelingDOWN[0]: True
    Entering short at open price: 16019.25
    Adding stop loss at price: 16026.5
    Time: 12/1/2023 12:27:00 PM | ChannelingUP[0]: True
    Entering long at open price: 16020.5
    Adding stop loss at price: 16028.5
    Time: 12/1/2023 12:39:00 PM | ChannelingDOWN[0]: True
    Entering short at open price: 16024.25
    Adding stop loss at price: 16026.25
    Time: 12/1/2023 12:56:00 PM | ChannelingUP[0]: True
    Entering long at open price: 16006
    Adding stop loss at price: 16014
    Time: 12/1/2023 1:02:00 PM | ChannelingDOWN[0]: True
    Entering short at open price: 16019.25
    Adding stop loss at price: 16020
    Time: 12/1/2023 1:05:00 PM | ChannelingUP[0]: True
    Entering long at open price: 16025
    Adding stop loss at price: 16033
    Time: 12/1/2023 1:49:00 PM | ChannelingDOWN[0]: True
    Entering short at open price: 16033
    Adding stop loss at price: 16040.75​
    Attached Files

    #2
    Hello futtrader,

    Thanks for your post.

    You mentioned you are calling EnterLong() but I do not see that method in the code you shared. Did you mean that you are calling EnterLongLimit()?

    I see you are using Bars.GetOpen() in your script. Note that Open[0] should be used in a strategy script to get the open price of the current bar.

    Where exactly are you calling this code in your script?

    Is it being called in OnStateChange() or in OnBarUpdate()?

    Please share a reduced test script that demonstrates the behavior you are reporting so we may look into this further.

    Note that a reduced copy refers to a copy of the script that contains the minimum amount of code needed to reproduce the issue. All other code is commented out or removed.

    To create a copy of your script to modify, open a New > NinjaScript Editor, select your script, right-click in the Editor, select 'Save as', name the script, and click OK.​

    I look forward to assisting further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Actually I found the issue - Signal name was not consistent cross the order
      thanks

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Today, 05:17 AM
      0 responses
      38 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      124 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      64 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      41 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      46 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X