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

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 algospoke, Today, 06:40 PM
          0 responses
          2 views
          0 likes
          Last Post algospoke  
          Started by maybeimnotrader, Today, 05:46 PM
          0 responses
          6 views
          0 likes
          Last Post maybeimnotrader  
          Started by quantismo, Today, 05:13 PM
          0 responses
          6 views
          0 likes
          Last Post quantismo  
          Started by AttiM, 02-14-2024, 05:20 PM
          8 responses
          167 views
          0 likes
          Last Post jeronymite  
          Started by cre8able, Today, 04:22 PM
          0 responses
          8 views
          0 likes
          Last Post cre8able  
          Working...
          X