Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Managing a stoploss in NinjaScript

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

    Managing a stoploss in NinjaScript

    Hey all, I was trying to come up with a way for my strategies to have the ability to manually move the stoploss once the strategy is in a position. It took a bit of thinking but I came up with the following:

    private double currentStop = 0;
    private double firstStop = 0;​

    ~~~~~~
    firstStop = Close[0] - 80;

    if (Position.MarketPosition == MarketPosition.Flat)

    {
    currentStop = 0;
    }​
    ~~~~~~
    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {
    if (execution.Order.OrderState == OrderState.Filled)
    {
    if (currentStop > 0)
    {
    ExitLongStopMarket(0, true, Position.Quantity, currentStop, "", "");
    }
    if (currentStop == 0)
    {
    ExitLongStopMarket(0, true, Position.Quantity, firstStop, "", "");
    }
    }
    }
    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
    {
    if (order.OrderType == OrderType.StopMarket && order.OrderState == OrderState.Accepted)
    currentStop = order.StopPrice;
    }​

    The ~~~~ lines are just to show that these things are in different places in the code. Theory being: When flat, current stop is set to 0. firstStop is updated on every bar to the be the correct value for an initial stop position. Once a position is entered, it enters ExitStopMarket orders and checks to see if currentStop is anything but 0, otherwise uses the value of FirstStop. When I manually move the stop using ChartTrader, once the stop is accepted it sends the order accepted signal from the exchange and currentStop is updated to this value (the place I just moved it to). Thus, the value is greater than 0 so when it does the check on the next bar, the stop is set again to same value I set it at. Anyways, it seems to be working perfectly on some, but on some others today I noticed it was still resetting the stop to the initial position. Would it be better if I changed the order of things in the OnExecutionUpdate or used ElseIf instead if? I am just trying to figure out why it is working well sometimes and others not.

    Thanks

    #2
    Hello RaygunWizzle,

    Thanks for your post.

    If the strategy is working as expected sometimes but not other times, debugging prints will need to be added to the strategy to understand exactly how each of your values in your logic is evaluating when the strategy orders work as expected versus when they do not work as expected.

    You could consider trying either changing the order of things in OnExecutionUpdate or using ElseIf statements instead of If statements to see which option suits your overall goals the best.

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121
    <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

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    53 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