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

Break even, Close half of the position and Trailing Stop Loss

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

    Break even, Close half of the position and Trailing Stop Loss

    I wish to achieve the following for profit taking:

    A. When condition A satisfies, move the stop loss to break even
    B. When condition B satisfies, take half of the profit
    C. For the remaining half positions, uses a 6 pips trailing stop loss.

    How to achieve A, B and C in Strategy Builder of Ninja Trader 8?

    #2
    Hello jack1234,

    Thanks for opening the thread.

    The Strategy Builder was designed to make it easier to create simple strategies, Some of the behaviors you are looking for can only be achieved by unlocking the code and programming it yourself.

    A. When condition A satisfies, move the stop loss to break even
    The Strategy Builder does not allow you to place dynamic Profit Targets or Stop Losses. As such, you will not be able to create an Auto-Breakeven strategy within the confines of the Strategy Builder.

    You do have some options though.

    1. You can create conditions that will exit your position after so many ticks of loss instead of using the static Stop Loss created in the Strategy Builder.
    2. You can unlock the code and add a call to SetStopLoss() to update your Stop Loss within OnBarUpdate() dynamically
    3. You can also hire a NinjaScript Consultant to write the strategy for you.

    I have created a video that demonstrates the first two methods for creating an Auto Breakeven - https://www.screencast.com/t/dB1LBNMmFS

    Keep in mind that strategies are iterated within OnBarUpdate() and this gets iterated on the formation of each bar of data series that you attach the strategy to. This would mean that you may see more ticks of loss before the position gets exited from a new iteration of OnBarUpdate(). You can use Calculate.OnEach tick for finer granularity. Or you may use an additional data series for the granularity.

    B. When condition B satisfies, take half of the profit
    As the Strategy Builder is limited in the amount of math that can be done, you will have to unlock the code to add inline mathematical operations to exit methods.

    For example, you may create an action to ExitLong(DefaultQuantity, "","") When the code is unlocked you will see:
    Code:
    ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
    You may modify it as such for half of order quantity. (Take half profit)
    Code:
    ExitLong(Convert.ToInt32(DefaultQuantity / 2), "", "");
    C. For the remaining half positions, uses a 6 pips trailing stop loss.
    You can set a trailing stop within the Stops and Targets section of the Strategy Builder. SetTrailStop() cannot be used with SetStopLoss() with the same entry signal. You can use separate signals to use both a SetStopLoss() and SetTrailStop() but this would not model the goal to Take Half Profit.

    You would have to manually add logic to call SetStopLoss() so your Stop Loss behaves like an Auto Trail for the remaining contracts. This requires unlocking the code and programming the logic on your own.

    If you would like to hire a NinjaScript Consultant to design the code for you, please write in to platformsupport[at]ninjatrader[dot]com with the thread URL and the text ATTN Jim. I will then have a member of our Business Development team pass over a list of NinjaScript Consultants who would be happy to create the code for you.

    If you wish to do the programming on your own, please reference the relevant articles of the help guide for complete understanding of how these methods work internally.

    SetStopLoss() — https://ninjatrader.com/support/help...etstoploss.htm
    SetTrailStop() - https://ninjatrader.com/support/help...ttrailstop.htm
    Calculate — https://ninjatrader.com/support/help.../calculate.htm

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

    Comment


      #3
      Jim,
      I followed the video to duplicate your breakeven strategy. But when I run it thru strategy analyzer the position just stays open until session close?



      Any Suggestions on what is happening?

      Comment


        #4
        Hello woodyfox,

        It appears that you are using an exit condition called PriceEntered that never returns true. This is because it is not being declared as a public variable and it is always set to 1.
        Do you intend to set the PriceEntered variable to the entry price of your order? If so, I am including a link to our help guide documentation on accessing information about your orders.

        Help Guide- Orders
        https://ninjatrader.com/support/help...-us/?order.htm

        Please let me know if you have any further questions.
        Josh G.NinjaTrader Customer Service

        Comment


          #5
          Hi Josh,
          To clarify, I'm using (NinjaTrader_Jim's) auto breakeven example that he go's over in this video https://www.screencast.com/t/dB1LBNMmFS
          Once price closes 7 ticks above his Long order, the stop loss is moved to breakeven.
          He specifically uses PriceEntered in his example.
          I copied the example to a T. At least I think. Do you think maybe his example is wrong?

          Comment


            #6
            Hello,
            Thanks for your note.

            I see where the disconnect happened. At the beginning of the video Jim mentions the limitations of creating an auto breakeven strategy in the Strategy Builder. He mentions this because his PriceEntered variable would need to be dynamic to accomplish such a strategy and simply forgot to update that value once his code was unlocked.

            What you are going to want to do here is set the value for PriceEntered inside OnOrderUpdate() so that the value is updated whenever your entry order is filled. You will also want to make sure that the PriceEntered variable is being set as public somewhere inside the class. Something similar to the snippet below should suffice for this.

            Please let me know if you have any further questions.

            Code:
            		public double PriceEntered;
            		protected override void OnBarUpdate()
            		{
            			
            			if (BarsInProgress != 0 || CurrentBars[0] < BarsRequiredToTrade)
            				return;
            			
            			if ((Position.MarketPosition == MarketPosition.Flat)
            				&& (Close[0] > Open[0]))
            			{
            				EnterLong(Convert.ToInt32(DefaultQuantity), "myEntryOrder");
            			}
            			
            			if (Close[0] <= PriceEntered 
            				&& (PriceEntered != 0))
            			{
            				ExitLong(Convert.ToInt32(DefaultQuantity), "myExitOrder", "myEntryOrder");
            			}
            			
            			if ((Close[0] >= PriceEntered + (7 * TickSize)) 
            				&& (PriceEntered != 0))
            			{
            				SetStopLoss( "myEntryOrder", CalculationMode.Price, PriceEntered, false);
            			}
            			
            		}
            		
            		protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
            		{
            
            		  if (order.Name == "myEntryOrder")
            		      PriceEntered = order.AverageFillPrice;
            		}
            Last edited by NinjaTrader_JoshG; 01-12-2018, 08:34 AM.
            Josh G.NinjaTrader Customer Service

            Comment


              #7
              Thanks Josh, Exactly what I was looking to do. Thanks for helping me.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by jclose, Today, 09:37 PM
              0 responses
              5 views
              0 likes
              Last Post jclose
              by jclose
               
              Started by WeyldFalcon, 08-07-2020, 06:13 AM
              10 responses
              1,413 views
              0 likes
              Last Post Traderontheroad  
              Started by firefoxforum12, Today, 08:53 PM
              0 responses
              11 views
              0 likes
              Last Post firefoxforum12  
              Started by stafe, Today, 08:34 PM
              0 responses
              11 views
              0 likes
              Last Post stafe
              by stafe
               
              Started by sastrades, 01-31-2024, 10:19 PM
              11 responses
              169 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Working...
              X