Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

StopLoss is being cancelled not filled, crashes strategy

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

    StopLoss is being cancelled not filled, crashes strategy

    I am developing a trend-following strategy using Renko, SMA, and OBV. I use breakeven to move my initial stop when a certain profit target is hit, then update it to follow the SMA until it gets stopped out by a trend reversal. However, the reversal of the trend seems to be killing the strategy, as the stop gets cancelled instead of filled, and the rest of the strategy (in this case, the short side) does not play out. I have included images of the sequence below. The code I have so far is copied below as well. If anyone could tell me why the order is crashing instead of getting filled, I would appreciate it. I am not a coder by any means.

    region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class RenkoOBVSMA : Strategy
    {
    private double StopPriceLong;
    private double StopPriceShort;
    private double BETarget;
    private bool BEBool;

    private SMA SMA1;
    private OBV OBV1;
    private Momentum Momentum1;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "RenkoOBVSMA";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 3;
    EntryHandling = EntryHandling.UniqueEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    Breakeven = 4;
    BreakOffset = 0;
    StopPriceLong = 1;
    StopPriceShort = 1;
    BETarget = 1;
    BEBool = false;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    SMA1 = SMA(Close, 10);
    OBV1 = OBV(Close);
    Momentum1 = Momentum(OBV1, 10);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;

    // Set 1
    if ((Position.MarketPosition == MarketPosition.Flat)
    && (Close[0] > SMA1[0])
    && (Momentum1[0] > 0))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), @"Long");
    StopPriceLong = SMA1[1];
    BEBool = false;
    }

    // Set 2
    if ((Position.MarketPosition == MarketPosition.Long)
    && (BEBool == false))
    {
    ExitLongStopMarket(Convert.ToInt32(DefaultQuantity ), StopPriceLong, @"ExitLong", @"Long");
    BETarget = (Position.AveragePrice + (Breakeven * TickSize)) ;
    }

    // Set 3
    if ((Position.MarketPosition == MarketPosition.Long)
    && (Close[0] >= BETarget))
    {
    BEBool = true;
    }

    // Set 4
    if ((Position.MarketPosition == MarketPosition.Long)
    && (BEBool == true))
    {
    ExitLongStopMarket(Convert.ToInt32(DefaultQuantity ), (Position.AveragePrice + (BreakOffset * TickSize)) , @"ExitLong", @"Long");
    }

    // Set 5
    if (Position.MarketPosition == MarketPosition.Long)
    {
    StopPriceLong = SMA1[1];
    }

    // Set 6
    if ((Position.MarketPosition == MarketPosition.Long)
    && (BEBool == true)
    && (StopPriceLong > (Position.AveragePrice + (BreakOffset * TickSize)) ))
    {
    ExitLongStopMarket(Convert.ToInt32(DefaultQuantity ), StopPriceLong, @"ExitLong", @"Long");
    }

    // Set 7
    if ((Position.MarketPosition == MarketPosition.Flat)
    && (Close[0] < SMA1[0])
    && (Momentum1[0] < 0))
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), @"Short");
    StopPriceShort = SMA1[1];
    }

    // Set 8
    if (Position.MarketPosition == MarketPosition.Short)
    {
    ExitShortStopMarket(Convert.ToInt32(DefaultQuantit y), StopPriceShort, @"ExitShort", @"Short");
    }

    // Set 9
    if (Position.MarketPosition == MarketPosition.Short)
    {
    StopPriceShort = SMA1[1];
    }

    }

    region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="Breakeven", Order=1, GroupName="Parameters")]
    public int Breakeven
    { get; set; }

    [NinjaScriptProperty]
    [Range(0, int.MaxValue)]
    [Display(Name="BreakOffset", Order=2, GroupName="Parameters")]
    public int BreakOffset
    { get; set; }
    #endregion

    }
    }

    #2
    Hello DZ469,

    Thank you for your post.

    Are there errors appearing on the Log tab of the Control Center?

    To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints (outside of any conditions) that print the date time of the bar and all values compared in every condition that places an order.

    The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

    The debugging print output should clearly show what the condition is, what time the conditions are being compared, all values being compared, and how they are being compared.

    Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

    Further, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true. TraceOrders will output to the NinjaScript Output window a message when orders are being submitted, ignored, cancelled, or rejected.

    After enabling TraceOrders remove the instance of the strategy from the Configured list in the Strategies window and add a new instance of the strategy from the Available list.

    Run or backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.

    Below is a link to a support article that demonstrates using informative prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.



    I am happy to assist you with analyzing the output from the output window.​

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    116 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    61 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    40 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    44 views
    0 likes
    Last Post TheRealMorford  
    Started by Mindset, 02-28-2026, 06:16 AM
    0 responses
    82 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Working...
    X