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

Using Entry Signals and Scaling Out by % of Position

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

    Using Entry Signals and Scaling Out by % of Position

    I'm using entry signals for managed orders in my strategy and I can't quite figure out how to apply this...

    If Position Quantity is a user-defined parameter ContractNumber and I always want to close 2/3 of position at Target1 and remaining 1/3 of position at Target2, what is the best way accomplish this while keeping signal name "intact", for lack of better word, no matter if ContractNumber is 2, 3, 4 or 9? Best to use percentages as opposed to trying to convert fractions, correct?

    #2
    Hello swjake,

    Thanks for your post.

    To accomplish this you would need to come up with your own custom logic to calculate a percentage of the current position and use that calculated value for the quantity being passed into your stop/target orders in the script.

    Position.Quantity could be used to get the strategy's current position size.

    Position.Quantity: https://ninjatrader.com/support/help...n_quantity.htm

    So if you were 9 long and you wanted to scale out, you can place 2 Exit orders for your stop/targets. One with a quantity of 6 and one with a quantity of 3. Again, you would need to implement your own custom logic to calculate a percentage of the strategy's current position size to get those quantities to pass into the stops/targets.

    Scaling into a position is controlled with the signal names of your strategy, and properties EntryHandling and EntriesPerDirection. This reference sample can help with scaling in and out of a strategy.



    This sample can assist further with managing multiple exit/entry signals:


    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thanks NinjaTrader_BrandonH. Thought I had it, but still a little fuzzy. If I have 2 targets (t1 & t2), those signal names need to be referenced on entrance (EnterLong(xx, @"t1") & EnterLong(xx, @"t2")), is that correct? So wouldn't I need to calculate position size before order entry? In my thinking, Position.Quantity wouldn't populate until after order fill.

      I tried this to get position size before entry but can't quite get it working...

      in OnBarUpdate before entry conditions:


      Code:
      private double t1, t2;
      private int contractNumber;​
      ​
      t1 = Math.Floor(contractNumber * .667);
      t2 = Math.Floor(contractNumber * .334);​
      then entry conditions met

      Code:
                                      SetStopLoss("t1", CalculationMode.Price, (thisLevel - 2 * TickSize), false);
                                      SetStopLoss("t2", CalculationMode.Price, (thisLevel - 2 * TickSize), false);
                                      EnterLong(Convert.ToInt32(t1)), @"t1");
                                      EnterLong(Convert.ToInt32(t2)), @"t2");
                                      SetProfitTarget(@"t1", CalculationMode.Price, (thatLevel - 2 * TickSize), false);
                                      SetProfitTarget(@"t2", CalculationMode.Price, (thatotherLevel - 2 * TickSize), false);​
      Am I close? In your answer above it sounds like Position.Quantity * .667 would do it, but I don't know where to reference Position.Quantity...OnOrderUpdate?

      Comment


        #4
        Hello swjake,

        Thanks for your notes.

        If you are passing in a Signal Name of "t1" for an Entry() method and you want to place a stop/target for that specific entry, you should passing in the Signal Name of the Entry() method into the FromEntrySignal argument of the Set methods. So the FromEntrySignal for the Set method in this case would be "t1"

        That said, I suggest using a unique Signal Name/FromEntrySignal for the Entry() methods and Set methods. Since you have a double variable name "t1 and "t2", you could change the SignalName/FromEntrySignal to something like "EnterLong1" and "EnterLong2" to prevent possible confusion between the double variable and Signal Name/FromEntrySignal.

        All Set methods should be called before the Entry() methods since Set methods prep NinjaTrader to submit protective orders when an entry order is filled.

        From the SetStopLoss()/SetProfitTarget() help guide documentation:

        "Since they are submitted upon receiving an execution, the Set method should be called prior to submitting the associated entry order to ensure an initial level is set."

        This means the SetProfitTarget() methods should be called before the Entry() order methods like the SetStopLoss() methods are.

        SetStopLoss(): https://ninjatrader.com/support/help...etstoploss.htm
        SetProfitTarget(): https://ninjatrader.com/support/help...ofittarget.htm

        In OnOrderUpdate(). order.Filled would represent the filled amount of an order and order.Quantity would represent the quantity of an order.

        OnOrderUpdate(): https://ninjatrader.com/support/help...rderupdate.htm
        Order: https://ninjatrader.com/support/help.../nt8/order.htm

        Further, I recommend adding debugging prints to the script to understand exactly how your logic is behaving when running the script.

        Below is a link to a forum post that demonstrates how to use prints to understand behavior.


        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Thank you. I made the changes to the Set Methods you recommended. I do use prints throughout the script to track behavior, but what I'm struggling with is this from you in post #2:
          To accomplish this you would need to come up with your own custom logic to calculate a percentage of the current position and use that calculated value for the quantity being passed into your stop/target orders in the script.
          Can the calculation be done before the order is submitted? If contractNumber is quantity and I want that split into two orders, Long1 (2/3rds) & Long2(1/3rd), where do I do the calculation below?

          Code:
          Math.Floor(contractNumber * .667);
          Math.Floor(contractNumber * .334);​

          Comment


            #6
            NinjaTrader_BrandonH I got it. I tend to overcomplicate things, then remember to keep it simple. Thanks for the direction and input.

            Comment


              #7
              Hello swjake,

              Thanks for your notes.

              To clarify, contractNumber is a private variable in the script and the default value of that variable is being defined before placing the order. Is that correct?

              Are you adding prints to the script that print out the t1 and t2 variables one line before calling your order methods to see how those values are being calculated in the logic?

              If not, one line above where you are calling your order methods add prints to the script that print out the t1 and t2 variables as well as prints out Convert.ToInt32(t1) and Convert.ToInt32(t2) to see exactly how those values are evaluating in the script.

              Note that you would need to calculate the percentage before calling the order methods so you could pass those calculated values into the order methods.

              You can also print out the order object in OnOrderUpdate() to see how the orders are behaving when they are Submitted, Accepted, Working, Filled, and the quantity of the orders.
              Brandon H.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by AaronKoRn, Today, 09:49 PM
              0 responses
              10 views
              0 likes
              Last Post AaronKoRn  
              Started by carnitron, Today, 08:42 PM
              0 responses
              9 views
              0 likes
              Last Post carnitron  
              Started by strategist007, Today, 07:51 PM
              0 responses
              10 views
              0 likes
              Last Post strategist007  
              Started by StockTrader88, 03-06-2021, 08:58 AM
              44 responses
              3,980 views
              3 likes
              Last Post jhudas88  
              Started by rbeckmann05, Today, 06:48 PM
              0 responses
              9 views
              0 likes
              Last Post rbeckmann05  
              Working...
              X