For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
2019-10-02: The community is currently in read-only mode

Force exit or exit open position according to risk management



  • Hi All,

    I'm working on exit conditions for my strategy and just wanted to backtest it with this great framework. Below is the brief about what i'm trying to achieve : -

    • Exit the the open position (or Force Exit ) when current value of portfolio drop by rules defined by RMS.

    • If Possible, perform this operation during intraday price move too. (Note: I've tried to achieve this through Cheat_on_open option. But, that's crazy configuration and it has changed performance of my strategy drastically.)

    Below is the code snipet, which i'm trying to execute for above logic. However, respective code block for exit position is not triggering.

    def next(self):
           # Check if an order is pending ... if yes, we cannot send a 2nd one
           if self.order:
    	   return
    	
          # Check if we are in the market
          if not self.position:
    		
    		#BUY
    		if (self.breakout[0]>1):
    			self.log('BUY CREATE, %.2f' % self.dataclose[0])
    			# Keep track of the created order to avoid a 2nd order
    			self.order = self.buy()
    		#SELL	
    		elif (self.breakout[0]<0):
    			self.log('SELL CREATE, %.2f' % self.dataclose[0])
    			# Keep track of the created order to avoid a 2nd order
    			self.order = self.sell()
    		else : 
    			#self.log('No Action, %.2f' % self.dataclose[0])
    			return
    			
    	else:        
    		# Already in the market, Verfiy Position
    		
    		#Hard Exit if Account value drop by according to Risk management
    		currAccVal = round(self.broker.getvalue(),2)
    		riskVal = round((currAccVal*0.03),2)
    		thresholdVal = round((currAccVal - riskVal),2)
    		
    		self.log("Current value : %.2f Threshold value : %.2f "%(currAccVal,thresholdVal))
    		
    		if(currAccVal < thresholdVal):
    			self.log("Force Exit : RMS Active !!")
    			self.order = self.close()
    

    I'm able to get get current account value and able to place condition as given in below code for force exit. But, this code block is not working at all.

    #Hard Exit if Account value drop by according to Risk management
    currAccVal = round(self.broker.getvalue(),2)
    riskVal = round((currAccVal*0.03),2)
    thresholdVal = round((currAccVal - riskVal),2)
    		
    self.log("Current value : %.2f Threshold value : %.2f "%(currAccVal,thresholdVal))
    		
    if(currAccVal < thresholdVal):
        self.log("Force Exit : RMS Active !!")
        self.order = self.close()
    

    Would be great, if anyone can suggest something here.

    Thanks in advance !!



  • Also, I'm working with csv feed for daily data.



  • @backtrader Any help would be appreciated ?

    Thanks in advance !!



  • In your calculation you do the following:

    • get (current value of the account)
    • get threshold as (current value of the account - risk)

    In this case (current value of the account) will be always higher than (current value of the account - risk) by math. So you will never sell anything. :)

    You may want to revise the rules and measure your account drop from yesterday value or from max value during certain period.

    If Possible, perform this operation during intraday price move too. (Note: I've tried to achieve this through Cheat_on_open option. But, that's crazy configuration and it has changed performance of my strategy drastically.)

    To achieve this you may want to (a) use intraday prices or (b) use daily updated stop order with the price defined by your risk value.


  • administrators

    @codesutras said in Force exit or exit open position according to risk management:

    Exit the the open position (or Force Exit ) when current value of portfolio drop by rules defined by RMS.

    Do as indicated by @ab_trader. Get value and exit when it's below your threshold.

    @codesutras said in Force exit or exit open position according to risk management:

    If Possible, perform this operation during intraday price move too. (Note: I've tried to achieve this through Cheat_on_open option. But, that's crazy configuration and it has changed performance of my strategy drastically.)

    @codesutras said in Force exit or exit open position according to risk management:

    Also, I'm working with csv feed for daily data.

    You want to exit intraday but you have daily data. There is physical impossibility here. Cheat on open is meant to let you calculate an entry against the opening price (which you may approximately experience during the pre-opening auction). It has nothing to do with intraday


Log in to reply
 

});