• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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 ntbone, 07-13-2019, 11:37 PM
    9 responses
    27 views
    0 likes
    Last Post Spaggetti Ninjja  
    Started by nicknamed, Today, 05:10 PM
    1 response
    15 views
    0 likes
    Last Post NinjaTrader_EricB  
    Started by pstrusi, Today, 04:57 PM
    0 responses
    6 views
    0 likes
    Last Post pstrusi
    by pstrusi
     
    Started by williamzz, Today, 04:18 PM
    0 responses
    6 views
    0 likes
    Last Post williamzz  
    Started by ct, Today, 01:57 PM
    2 responses
    14 views
    0 likes
    Last Post ct
    by ct
     
    Working...
    X