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 lightsun47, Today, 03:51 PM
      0 responses
      0 views
      0 likes
      Last Post lightsun47  
      Started by 00nevest, Today, 02:27 PM
      1 response
      8 views
      0 likes
      Last Post 00nevest  
      Started by futtrader, 04-21-2024, 01:50 AM
      4 responses
      41 views
      0 likes
      Last Post futtrader  
      Started by Option Whisperer, Today, 09:55 AM
      1 response
      12 views
      0 likes
      Last Post bltdavid  
      Started by port119, Today, 02:43 PM
      0 responses
      8 views
      0 likes
      Last Post port119
      by port119
       
      Working...
      X