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

How to program two orders with different Stop loss and target ?

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

    How to program two orders with different Stop loss and target ?

    Hi everybody,

    I create a new topic to ask if someone have an exemple of code to have two orders running at the same time with different tp/sl. With the unmanaged approach, everything's great when running order one by one, but two at the same time isn't possible. I'm working since two weeks on this issue and it drives me crazy, and I don't understand what's wrong so if someone have already program this little piece of code...

    Thanks

    #2
    Hello After,

    Thank you for your post.

    I am not aware of such an example. I will put together and example and attach it to this post.

    I look forward to assisting you further.

    Comment


      #3
      Thanks Pat ! An exemple of this is all what I need

      Comment


        #4
        Hello After,

        Thank you for your patience.

        This example uses IOrder objects, but keep in mind I am not resetting the objects to null on fill and the OCO id is static.
        Code:
                #region Variables
        		private IOrder enter1;
        		private IOrder enter2;
        		private IOrder pt1;
        		private IOrder sl1;
        		private IOrder pt2;
        		private IOrder sl2;
                #endregion
        		
                protected override void Initialize()
                {
        			Unmanaged = true;
                }
        		
                protected override void OnBarUpdate()
                {	
        			if(Close[0] > Open[0]
        				&& Position.MarketPosition == MarketPosition.Flat)
        			{
        				// Two market orders
        				enter1 = SubmitOrder(0, OrderAction.Buy, OrderType.Market, 1, 0, 0, "", "enter1");
        				enter2 = SubmitOrder(0, OrderAction.Buy, OrderType.Market, 1, 0, 0, "", "enter2");
        			}
        			
        			// When we are long 2 suggesting the orders filled, place our protective orders.
        			if(Position.MarketPosition == MarketPosition.Long
        				&& Position.Quantity == 2)
        			{
        				// Profit Target and Stop Loss set 1
        				pt1 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, enter1.AvgFillPrice+(5*TickSize), 0, "brckt1", "pt1");
        				sl1 = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, enter1.AvgFillPrice-(5*TickSize), 0 , "brckt1", "sl1");
        				
        				// Profit Target and Stop Loss set 2
        				pt2 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, enter2.AvgFillPrice+(10*TickSize), 0, "brckt2", "pt2");
        				sl2 = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, enter2.AvgFillPrice-(10*TickSize), 0 , "brckt2", "sl2");
        			}
                }

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by 1001111, Today, 01:35 AM
        0 responses
        1 view
        0 likes
        Last Post 1001111
        by 1001111
         
        Started by ETFVoyageur, Yesterday, 07:05 PM
        1 response
        15 views
        0 likes
        Last Post ETFVoyageur  
        Started by MarianApalaghiei, Today, 12:35 AM
        1 response
        7 views
        0 likes
        Last Post MarianApalaghiei  
        Started by Rogers101, 05-05-2024, 11:30 AM
        17 responses
        56 views
        0 likes
        Last Post Rogers101  
        Started by haas88, 03-21-2024, 02:22 AM
        13 responses
        153 views
        0 likes
        Last Post haas88
        by haas88
         
        Working...
        X