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

One SetStopLoss to adjust all named EnterLong positions?

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

    One SetStopLoss to adjust all named EnterLong positions?

    Let's say I have multiple long entry signals: Condition1, Condition2, Condition3, and that I call EnterLong with a signalName, i.e., EnterLong("Condition1"), EnterLong("Condition2"), EnterLong("Condition3").

    If the strategy is limited to only one entry at a time, is it possible to adjust the stoploss for a long position, regardless of which condition triggered entry, with only one SetStopLoss?

    Or is it only possible to adjust the stoploss for a given long position by calling SetStopLoss with the fromEntrySignal parameter? Meaning that to adjust stoplosses, I'd have to have at least one SetStopLoss for every signalName.

    #2
    Hello,

    Thank you for the question.

    When looking at the SetStopLoss method in NinjaScript there are multiple ways to use this method. In three cases you can use this as a general catch-all stop loss that would apply to any entry which would be these :

    Code:
    SetStopLoss(double currency)
    SetStopLoss(double currency, bool simulated)
    SetStopLoss(CalculationMode mode, double value)
    Alternately you can use a specific entry for the stoploss by using the final set of overloads here:

    Code:
    SetStopLoss(string fromEntrySignal, CalculationMode mode, double value, bool simulated)
    When you see fromEntrySignal this method can only be called if the entry signal is seen, once it is it would happen.

    If no entry signal is specified NinjaTrader assumes you mean apply to any entry.

    For your questions it depends how you want this to work.

    If you want a general amount for all entries you can use a static stoploss value in your Initialize and not using a signal name. This stoploss would apply across the board and it wouldn't matter which order happened, it would apply. This is only static though it could not be adjusted when using it in Initialize.

    Instead if you use this in OnBarUpdate without a signal name it would apply to any order but also have the ability to be updated.

    Finally you can also use a signal name in OnBarUpdate but if you had 3 separate entries you would need to make a call for each signal name for the update.

    Please let me know if I may be of additional assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Instead if you use this in OnBarUpdate without a signal name it would apply to any order but also have the ability to be updated.
      I need to update the stoploss, so it will have to be called in OnBarUpdate.

      If I call SetStopLoss without a signal name, it only applies to orders that do NOT have a signal name, correct?

      Originally posted by NinjaTrader_Jesse View Post
      Finally you can also use a signal name in OnBarUpdate but if you had 3 separate entries you would need to make a call for each signal name for the update.
      Right, as I understand it, once you give a trade a signal name, you can only adjust the stoploss for that trade by calling SetStopLoss with that signal name.

      Ideally I'd like to give all my entries signal names, but have the ability to adjust stop losses without specifying a signal name - i.e., setstoploss adjustment applies to long position, regardless of which entry signal triggered the trade entry.

      Comment


        #4
        Hello,

        Thank you for the questions.

        If I call SetStopLoss without a signal name, it only applies to orders that do NOT have a signal name, correct?
        While this is correct it depends on which overload you are talking about.

        If you are using an overload that requires a signal name and you use a empty string or "" yes you are correct this would not apply.

        If you are using a overload that does not ask for a signal name such as the example below, it is a catch-all and will apply to orders with or without a signal name

        Here is a simple way to test what you are asking :

        Code:
        protected override void Initialize()
        {
             CalculateOnBarClose = false;
             SetStopLoss(CalculationMode.Ticks, 10);
        }
        		
        protected override void OnBarUpdate()
        {
        	if(!Historical)
        		if(Position.MarketPosition == MarketPosition.Flat)
        			EnterLong("test");
        }
        This is also the answer to your second question, If you use a catch-all or overload that does not require a signal name, it can be used across all orders, if you use an overload that includes a signal name it needs to be associated with the order with the same name.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Jesse View Post
          If you are using a overload that does not ask for a signal name such as the example below, it is a catch-all and will apply to orders with or without a signal name

          Here is a simple way to test what you are asking :

          Code:
          protected override void Initialize()
          {
               CalculateOnBarClose = false;
               SetStopLoss(CalculationMode.Ticks, 10);
          }
          		
          protected override void OnBarUpdate()
          {
          	if(!Historical)
          		if(Position.MarketPosition == MarketPosition.Flat)
          			EnterLong("test");
          }
          This is also the answer to your second question, If you use a catch-all or overload that does not require a signal name, it can be used across all orders, if you use an overload that includes a signal name it needs to be associated with the order with the same name.
          I am calling SetStopLoss as follows: SetStopLoss(CalculationMode.Price, [some calculated price])

          But I am calling this from OnBarUpdate as I wish to make adjustments to the stoploss. Calling SetStopLoss this way, as opposed to your call from Initialize, does NOT seem to change the stoploss for a position entered using a signal name. Is this the anticipated behavior when using SetStopLoss without a fromEntrySignal parameter (and no "") from OnBarUpdate?

          Or should a call to SetStopLoss(CalculationMode.Price, [some calculated price]) behave the same whether called from Initialize or OnBarUpdate?

          Comment


            #6
            Hello,

            Thank you for the question.

            This is because the StopLoss is referencing the Average price of the order that was placed which would not change.

            If you have a price value that you wish to update every bar you would need to use a price value instead.

            This is the CalculationMode.Price opposed to CalculationMode.Ticks

            Here is another example of how to update the price during the OnBarUpdate.

            This is simply to demonstrate the movement, this would otherwise not make sense as the price will never touch the stoploss as the stoploss is always being set to the CurrentClose - 10 ticks. You would need to set a price that would be hit in your actual code.

            Code:
            protected override void OnBarUpdate()
            {
            	if(Historical) return;
            			
            	if(Position.MarketPosition == MarketPosition.Flat)
            		EnterLong("test");
            	if(Position.MarketPosition == MarketPosition.Long)
            		SetStopLoss(CalculationMode.Price, Close[0] - 10 * TickSize);
            }
            SetStopLoss(CalculationMode.Ticks, 10); would set a value of 10 ticks from the entry price, this would be static, you need to change the value from 10 ticks to another amount for the stop to actually change positions and be updated.

            The Price mode would be a better example as this allows you to use a Price and subtract ticks from that if needed or just use the price exactly.

            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              duplicate post
              Last edited by dolomite; 01-17-2015, 04:31 PM.

              Comment


                #8
                Originally posted by NinjaTrader_Jesse View Post
                Hello,

                Thank you for the question.

                This is because the StopLoss is referencing the Average price of the order that was placed which would not change.

                I don't understand why SetStopLoss(CalculationMode.Price, [some calculated price]) is refering the average order price. [some calculated price] is, for example, the low of the previous bar.

                Originally posted by NinjaTrader_Jesse View Post
                If you have a price value that you wish to update every bar you would need to use a price value instead.

                This is the CalculationMode.Price opposed to CalculationMode.Ticks
                I am using CalculationMode.Price, as per the following:

                Originally posted by dolomite View Post
                I am calling SetStopLoss as follows: SetStopLoss(CalculationMode.Price, [some calculated price])
                Originally posted by NinjaTrader_Jesse View Post
                Code:
                protected override void OnBarUpdate()
                {
                	if(Historical) return;
                			
                	if(Position.MarketPosition == MarketPosition.Flat)
                		EnterLong("test");
                	if(Position.MarketPosition == MarketPosition.Long)
                		SetStopLoss(CalculationMode.Price, Close[0] - 10 * TickSize);
                }
                Based on this example, I assume the answer to my question below is in the affirmative, i.e., SetStopLoss(CalculationMode.Price, [some calculated price]) should behave the same whether called from Initialize or OnBarUpdate.

                Originally posted by dolomite View Post
                But I am calling this from OnBarUpdate as I wish to make adjustments to the stoploss. Calling SetStopLoss this way, as opposed to your call from Initialize, does NOT seem to change the stoploss for a position entered using a signal name. Is this the anticipated behavior when using SetStopLoss without a fromEntrySignal parameter (and no "") from OnBarUpdate?

                Or should a call to SetStopLoss(CalculationMode.Price, [some calculated price]) behave the same whether called from Initialize or OnBarUpdate?
                In that case, perhaps I need to dig deeper as to why changing from:
                Code:
                SetStopLoss(fromEntrySignal, CalculationMode.Price, [some calculated price], false)
                to

                Code:
                SetStopLoss(CalculationMode.Price, [some calculated price])
                gives me a different result on a simple script using only one instance of enterLong (for which I assign a signal name). In the latter version (SetStopLoss without fromEntrySignal), the stoploss is not being adjusted.

                Comment


                  #9
                  Hello,

                  don't understand why SetStopLoss(CalculationMode.Price, [some calculated price]) is refering the average order price. [some calculated price] is, for example, the low of the previous bar.
                  The SetStopLoss(CalculationMode.Price, [some calculated price]) would not be referring to the average order price at all, I was referencing the SetStopLoss(CalculationMode.Ticks, 10) from the earlier example and explaining the difference.

                  SetStopLoss(CalculationMode.Price, [some calculated price]) should behave the same whether called from Initialize or OnBarUpdate.
                  Basically yes except the one in Initialize would only be called once and then could not be updated later. It would be placed firm at the price you set. If this is instead used in OnbarUpdate it would be using the price you set and updating each OnBarUpdate so if the price it is using were to change, the stop loss would also change.

                  There are two ways to accomplish what you are trying to do.

                  The first is to use SetStopLoss(CalculationMode.Price, [some calculated price])
                  This would apply to all orders, you can place this in Initialize if you do not need to change the price later, otherwise this would need to go in OnBarUpdate to change the prices at a later time.

                  The second would be to specify signal names for all the entries and add
                  SetStopLoss(fromEntrySignal, CalculationMode.Price, [some calculated price], false)
                  for every signal name.

                  Both will perform virtually the same although the first is open to any entry order where as the second option is more specific.

                  I look forward to being of further assistance.
                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    OK, I figured out the problem.

                    The gist of what I'm trying to do is have a strategy with multiple entry conditions. For each entry condition, I'd like to set an initial stop loss that is tied to that entry. I also want to adjust stop losses, regardless of entry condition.

                    So I was calling EnterLong with a signal name, e.g., Condition1. Then I called SetStopLoss with fromEntrySignal, in this case, Condition1 (let's call this SetStopLoss Condition1).

                    I also call SetStopLoss without fromEntrySignal (let's call this SetStopLoss Universal). SetStopLoss Universal is NOT being applied to the Condition1 entries.

                    However, if I change SetStopLoss Condition1 to remove fromEntrySignal, then SetStopLoss Universal does get applied to Condition1 entries.

                    So it seems SetStopLoss(CalculationMode.Price, [some calculated price]) applies to all entries ONLY if such entry has never been linked with another SetStopLoss that uses fromEntrySignal.

                    To illustrate using your example, this works:
                    Code:
                    protected override void OnBarUpdate()
                    {
                    	if(Historical) return;
                    			
                    	if(Position.MarketPosition == MarketPosition.Flat)
                    		EnterLong("test");
                    	if(Position.MarketPosition == MarketPosition.Long)
                    		SetStopLoss(CalculationMode.Price, [some calculated price]);
                    }
                    While this does not work (i.e., stoploss does not get adjusted).
                    Code:
                    protected override void OnBarUpdate()
                    {
                    	if(Historical) return;
                    			
                    	if(Position.MarketPosition == MarketPosition.Flat)
                    		EnterLong("test");
                    		SetStopLoss("test", CalculationMode.Price, [some calculated price], false);
                    	if([some condition])
                                    SetStopLoss(CalculationMode.Price, [some other calculated price]);
                    }

                    So to accomplish what I'm trying to do, it seems there are limited options.
                    1. I could eliminate all uses of fromEntrySignal in SetStopLoss calls (because using fromEntrySignal prevents associated entries from being modified by subsequent universal SetStopLoss calls).

                    OR

                    2. Every time I call SetStopLoss to adjust stop loss (my universal stop loss adjustments), I have to make multiple calls, one for each entry condition.

                    Any other suggestions?
                    Last edited by dolomite; 01-17-2015, 05:53 PM.

                    Comment


                      #11
                      Hello dolomite,

                      2. Every time I call SetStopLoss to adjust stop loss (my universal stop loss adjustments), I have to make multiple calls, one for each entry condition.
                      That would be correct.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by algospoke, Yesterday, 06:40 PM
                      2 responses
                      24 views
                      0 likes
                      Last Post algospoke  
                      Started by ghoul, Today, 06:02 PM
                      3 responses
                      15 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by jeronymite, 04-12-2024, 04:26 PM
                      3 responses
                      46 views
                      0 likes
                      Last Post jeronymite  
                      Started by Barry Milan, Yesterday, 10:35 PM
                      7 responses
                      23 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by AttiM, 02-14-2024, 05:20 PM
                      10 responses
                      181 views
                      0 likes
                      Last Post jeronymite  
                      Working...
                      X