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

MultiLeg Strategy with different stop types

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

    MultiLeg Strategy with different stop types

    I'm intending to write a strategy that enters with 3 unique order names. Each has its own unique profit target, which is working properly. Stops for each order type use the following rules:

    Upon entry:
    - All orders share the same fixed stop loss

    Upon hitting profit target1
    - Set trailing stops to 20 pips for the 2 remaining legs

    Upon hitting profit taregt 2
    - Set the trailing stop to 50 pips for the 1 remaining leg

    I'm trying to do that via the following code. Please note that I set EntryHandling = EntryHandling.UniqueEntries.

    This is a snippet from OnBarUpdate()

    Code:
    				if( bullish && Position.MarketPosition != MarketPosition.Long )
    				{
    					EnterLong(Units1, TARGET1);
    					EnterLong(Units2, TARGET2);
    					EnterLong(Units3, TARGET3);
    					
    					SetStopLoss(TARGET1, CalculationMode.Ticks, InitialStop, false);
    					SetTrailStop(TARGET2,CalculationMode.Ticks, InitialStop, false);
    					SetTrailStop(TARGET3,CalculationMode.Ticks, InitialStop, false);					
    				}
    Then, I'm doing the following in OnOrderUpdate to try to reset everything:
    Code:
    		protected override void OnOrderUpdate(IOrder order)
    		{
    			if( order.OrderAction == OrderAction.BuyToCover || order.OrderAction == OrderAction.Sell )
    			{						
    				switch(order.Name)
    				{
    					case TARGET1:
    						SetTrailStop(TARGET2,CalculationMode.Ticks, Trailing, false);
    						SetTrailStop(TARGET3,CalculationMode.Ticks, Trailing, false);
    						break;
    					case TARGET2:
    						SetTrailStop(TARGET3,CalculationMode.Ticks, TrailingFinal, false);
    						break;				
    					default:					
    						break;
    				}
    			}
    		}
    The attached screenshot illustrates the fact that I can't make my trailing stop exit.
    Attached Files

    #2
    Hello texasnomad,

    Thank you for your note.

    When I include test prints to check the result of order.Name, I’m seeing Stop Loss/Profit Target/Trail Stop printed, but never an output which would make your switch statements true. Thus its unlikely case Target1 or case Target2 are ever becoming true, thus no TSL is being submitted.

    I set up a strategy which does what you’d like, submits 3 long orders, sets an initial stop of 10 ticks for LongA and mimics a SL of 10 ticks for LongB and LongC (See line 57). You would not be able to use SetStopLoss in conjunction with SetTrailStop for the same entry name, thus line 57. When a profit target of 10 ticks is hit, Long A is closed and a trailing stop loss for LongB and LongC is set to 20 ticks. When profit target of 15 ticks is hit, LongB is closed and the trailing stop for LongC is moved to 50 ticks.

    Please let us know if you need further assistance.
    Attached Files
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Thank you for this and for the code. +1 for thoroughness!

      I used the incorrect property to track my orders. I used order.Name, whereas I need to be using order.FromEntrySignal to correctly implement my intentions. As soon as I made that change, it all worked brilliantly.

      Thank you!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by frslvr, 04-11-2024, 07:26 AM
      8 responses
      111 views
      1 like
      Last Post NinjaTrader_BrandonH  
      Started by stafe, 04-15-2024, 08:34 PM
      10 responses
      44 views
      0 likes
      Last Post stafe
      by stafe
       
      Started by rocketman7, Today, 09:41 AM
      3 responses
      8 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by traderqz, Today, 09:44 AM
      2 responses
      5 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by rocketman7, Today, 02:12 AM
      7 responses
      31 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Working...
      X