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

Short Position StopLoss and Profit Targets (Managed)

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

    Short Position StopLoss and Profit Targets (Managed)

    Hi there,

    I think this could be a really simple question im just missing something here.
    Im trying to set the Profit Targets and Stop Loss for Long or Short position within OnExecutionUpdate. However the below code works only for Long position and does not activate the stoploss or profit target for the short positions in the backtest they are are not recognised at all unlike the long position stops.

    I have followed the various samples from the help guide and sample strategy codes from "sampleOnOrderUpdate" but i noticed that all the samples and examples only cover long positions and i cant find any examples where it combines both long and short anywhere. So i have given it a go below: What am i doing wrong?

    Code:
    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
      {
       if (entryOrder != null && entryOrder == execution.Order && entryOrder.IsLong)
       {
        if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
        {
         if (EnableProfitTarget)
         {
          targetOrder = ExitLongLimit(1, true, execution.Order.Filled, execution.Order.AverageFillPrice + (InitialProfitTarget * TickSize), "Initial TP", "Long Entry");
         }
         if (EnableStopLoss)
         {
          stopOrder = ExitLongStopMarket(1, true, execution.Order.Filled, execution.Order.AverageFillPrice - (InitialStopLoss * TickSize), "Initial SL", "Long Entry");
         }
        }
        if (execution.Order.OrderState != OrderState.PartFilled)
          entryOrder = null;
       }
       if (entryOrder != null && entryOrder == execution.Order && entryOrder.IsShort)
       {
        if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
        {
         if (EnableProfitTarget)
         {
          targetOrder = ExitShortLimit(1, true, execution.Order.Filled, execution.Order.AverageFillPrice - (InitialProfitTarget * TickSize), "Initial TP", "[COLOR=#FF0000][B]Long Entry[/B][/COLOR]");
         }
         if (EnableStopLoss)
         {
          stopOrder = ExitShortStopMarket(1, true, execution.Order.Filled, execution.Order.AverageFillPrice + (InitialStopLoss * TickSize), "Initial SL", "[COLOR=#FF0000][B]Long Entry[/B][/COLOR]");
         }
        }
        if (execution.Order.OrderState != OrderState.PartFilled)
          entryOrder = null;
       }
       if ((stopOrder != null && stopOrder == execution.Order) || (targetOrder != null && targetOrder == execution.Order))
       {
        if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
        {
         stopOrder = null;
         targetOrder = null;
        }
       }
      }
    Any help on this would be greatly appreciated!
    Thankyou
    Last edited by mr_trade; 06-01-2019, 08:32 AM. Reason: Oops! Spot the deliberate mistake! Problem solved it now works!

    #2
    oopsie! I see it now. Spot the deliberate mistake.
    Code:
    [LEFT][COLOR=#252C2F][FONT=Helvetica][SIZE=13px]      targetOrder = ExitShortLimit(1, true, execution.Order.Filled, execution.Order.AverageFillPrice - (InitialProfitTarget * TickSize), "Initial TP", "[/SIZE][/FONT][/COLOR][/LEFT][LEFT][COLOR=#FF0000][FONT=Helvetica][SIZE=13px][B]Long Entry[/B][/SIZE][/FONT][/COLOR][/LEFT][LEFT][COLOR=#252C2F][FONT=Helvetica][SIZE=13px]");[/SIZE][/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#252C2F][FONT=Helvetica][SIZE=13px]     }[/SIZE][/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#252C2F][FONT=Helvetica][SIZE=13px]     if (EnableStopLoss)[/SIZE][/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#252C2F][FONT=Helvetica][SIZE=13px]     {[/SIZE][/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#252C2F][FONT=Helvetica][SIZE=13px]      stopOrder = ExitShortStopMarket(1, true, execution.Order.Filled, execution.Order.AverageFillPrice + (InitialStopLoss * TickSize), "Initial SL", "[/SIZE][/FONT][/COLOR][/LEFT][LEFT][COLOR=#FF0000][FONT=Helvetica][SIZE=13px][B]Long Entry[/B][/SIZE][/FONT][/COLOR][/LEFT][LEFT][COLOR=#252C2F][FONT=Helvetica][SIZE=13px]");[/SIZE][/FONT][/COLOR][/LEFT]
    Well at least it works now!

    Comment


      #3
      Hello mr_trade,

      I'm glad you were able to find the error.

      As a heads up, the Order variables should be assigned from order object from OnOrderUpdate.

      From the help guide:
      "OnOrderUpdate() will run inside of order methods such as EnterLong() or SubmitOrderUnmanaged(), therefore attempting to assign an order object outside of OnOrderUpdate() may not return as soon as expected. If your strategy is dependent on tracking the order object from the very first update, you should try to match your order objects by the order.Name (signal name) from during the OnOrderUpdate() as the order is first updated."

      Below is a link to the help guide.
      https://ninjatrader.com/support/help...rderupdate.htm

      And a link to an example ProfitChaseStopTrailExitOrdersExample that does this.
      https://ninjatrader.com/support/foru...269#post802269

      Also, the code you have provided does not show the entry orders being placed, so we wouldn't be able to see what signal names are used for the entry to ensure these match the exit. Just something to note should you have a similar inquiry.
      Chelsea B.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Spiderbird, 10-03-2022, 10:15 PM
      6 responses
      82 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by Marble, 09-02-2022, 12:24 PM
      29 responses
      159 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by music_p13, Today, 08:45 AM
      0 responses
      4 views
      0 likes
      Last Post music_p13  
      Started by b16_aln, Today, 06:33 AM
      3 responses
      34 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by shorty, 10-05-2022, 06:20 PM
      7 responses
      65 views
      0 likes
      Last Post NinjaTrader_Emily  
      Working...
      X