Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adopt Account Position, SetStopLoss, SetProfitTarget

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

    Adopt Account Position, SetStopLoss, SetProfitTarget

    I am writing a strategy that needs to assume an existing account position upon startup and then place a stop loss and profit target order around the strategy position.
    The code essentials are:

    Code:
    protected override void OnStateChange()
    		{
    			if (State == State.SetDefaults)
    			{
    				IsAdoptAccountPositionAware = true;
    				StartBehavior = StartBehavior.AdoptAccountPosition;
    				
    				
    			}
    			else if (State == State.Configure)
    			{
    				SetProfitTarget(CalculationMode.Price, ProfitTargetPrice);
    				SetStopLoss(CalculationMode.Ticks, StopLossTicks);
    			}
    		}
    
    		protected override void OnBarUpdate()
    		{
                          //do something
    		}
    The problem is SetStopLoss() and SetProfitTarget() do not execute around the adopted account position upon strategy startup.

    The instructions for SetStopLoss() and SetProfitTarget() say:
    Profit target orders are submitted in real-time on incoming executions from entry orders
    Apparently AdoptAccountPosition does not simulate the necessary conditions for Set...() to work.

    Is there a way for a strategy to adopt an account position and then apply a stop loss and profit target?

    #2
    Hello Camdo,

    SetStopLoss and SetProfitTarget are going to apply to entry orders made by the strategy.

    I have built a strategy which when you apply it to a data series with an open position, will submit a stop loss and profit target using ExitShortLimit/ExitShortStopMarket for shorts and ExitLongtLimit/ExitLongStopMarket for longs. If either the stop or profit target order is filled, or if the position becomes flat through a manual trade to the account, the SL and PT orders will be canceled.

    Please let us know if you have any questions.
    Attached Files
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Alan, Thank you very much for the code sample. It is just what I needed.

      Comment


        #4
        Dear Alan,

        thanks for your work. I used parts from your code to send a stopp once the strategy is started. The only problem that I have left is the timelag that occurs as the stopp is placed from within the onbarupdate method and I have it set to calculate onbarclose the first bar needs to finish before the SL is sent. I would like to keep my settings at onbarclose though.

        Is there an easy way to have this code processed once in the beginning of the startup of a strategy?

        Comment


          #5
          sagetrade, I think the attached strategy will do what you want.
          It will assume the current position and immediately place stoploss and profit target orders.

          It was designed to carry a futures contract into to next day without triggering overnight margin requirements. After assuming the current position, it will exit the position at a chosen time (end of session), and then re-enter the position at another time (start of the next session).

          Thanks to AlanP for the code example to get me started. I hope this will do the same for you.
          AssumeExitReenter.zip

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by sidlercom80, Today, 02:40 AM
          1 response
          13 views
          0 likes
          Last Post NinjaTrader_PaulH  
          Started by b16_aln, Today, 01:52 AM
          0 responses
          14 views
          0 likes
          Last Post b16_aln
          by b16_aln
           
          Started by junkone, Yesterday, 08:55 PM
          2 responses
          12 views
          0 likes
          Last Post junkone
          by junkone
           
          Started by adaeiqendqw, 01-16-2021, 02:56 AM
          1 response
          17 views
          0 likes
          Last Post NinjaTrader_Jason  
          Started by [email protected], Yesterday, 06:10 PM
          5 responses
          17 views
          0 likes
          Last Post NinjaTrader_ChrisSch  
          Working...
          X