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

Set Stop Loss and Take Profit for current trade or trades

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

    Set Stop Loss and Take Profit for current trade or trades

    Hello,
    I have a very simple strategy, if (Close[0]>Open[1]) {EnterLong(Convert.ToInt32(DefaultQuantity), "MyEntry");}
    After this with this code, I will set SL and TP.

    Code:
    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
                            {
                                       // Handle entry orders here. The entryOrder object allows us to identify that the order that is calling the OnOrderUpdate() method is the entry order.
                                       // Assign entryOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
                                       // This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not gauranteed to be complete if it is referenced immediately after submitting
                                       if (order.Name == "MyEntry")
                                       {        
                                                   entryOrder = order;
    
                                                   // Reset the entryOrder object to null if order was cancelled without any fill
                                                   if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
                                                               entryOrder = null;
                                       }
    
                            }
    
                            protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
                            {
                                       // Print(execution.ToString());
                                       /* We advise monitoring OnExecution to trigger submission of stop/target orders instead of OnOrderUpdate() since OnExecution() is called after OnOrderUpdate()
                                       which ensures your strategy has received the execution which is used for internal signal tracking. */
                                       if (entryOrder != null && entryOrder == execution.Order)
                                       {
                                                   if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                                                   {
                                                               if (Position.MarketPosition == MarketPosition.Long || entryOrder.OrderAction == OrderAction.Buy)
                                                               {
                                                               // Stop-Loss order
                                                               stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - (Stop_Lost  )* TickSize, "MyStop", "MyEntry");
    
                                                               // Target order
                                                               targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + (Take_Profit ) * TickSize, "MyTarget", "MyEntry");
                                                               }
                                                               else if (Position.MarketPosition == MarketPosition.Short || entryOrder.OrderAction == OrderAction.Sell)
                                                               {
                                                                           stopOrder = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + (Stop_Lost )* TickSize, "MyStop", "MyEntry");
                                                                           targetOrder = ExitShortLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - (Take_Profit ) * TickSize, "MyTarget", "MyEntry");
                                                               }
    
                                                               // Resets the entryOrder object to null after the order has been filled
                                                               if (execution.Order.OrderState != OrderState.PartFilled)
                                                                           entryOrder = null;
                                                   }
                                       }
    
                                       // Reset our stop order and target orders' Order objects after our position is closed.
                                       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;
                                                   }
                                       }
    The problem appear if I have more condition for others EntryLong because in this case this logic doesn't add SL or TP.

    Someone Can help me to solve this? Thanks.
    Attached Files
    Last edited by razeus; 06-24-2019, 10:19 AM.

    #2
    Hello razeus,

    Thanks for your post.

    In your code, you are assigning entryOrder to the order with the signal name "MyEntry." In OnExecutionUpdate, you are checking if entryOrder filled to submit the Target and Stop. Since entryOrder is tied to "MyEntry," it will not be tied to other entries and your OnExecutionUpdate logic will not submit protective orders for orders that do not use this signal name.

    You could copy the code so it checks for additional entry signals so you can assign targets and stops to additional entries. I have included an example I have shared previously that demonstrates this for NinjaTrader 7. While the code cannot be copy and pasted into NinjaTrader 8, it can provide further direction on how you could design a multi entry strategy using Order objects.

    https://ninjatrader.com/support/foru...65#post1046465

    Please let me know if I can be of further assistance.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by junkone, Today, 11:37 AM
    2 responses
    12 views
    0 likes
    Last Post junkone
    by junkone
     
    Started by frankthearm, Yesterday, 09:08 AM
    12 responses
    43 views
    0 likes
    Last Post NinjaTrader_Clayton  
    Started by quantismo, 04-17-2024, 05:13 PM
    5 responses
    35 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by proptrade13, Today, 11:06 AM
    1 response
    7 views
    0 likes
    Last Post NinjaTrader_Clayton  
    Started by love2code2trade, 04-17-2024, 01:45 PM
    4 responses
    35 views
    0 likes
    Last Post love2code2trade  
    Working...
    X