Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Force exit or exit open position according to risk management

    Indicators/Strategies/Analyzers
    3
    5
    798
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      codesutras last edited by

      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 !!

      B 1 Reply Last reply Reply Quote 0
      • C
        codesutras last edited by

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

        1 Reply Last reply Reply Quote 0
        • C
          codesutras last edited by

          @backtrader Any help would be appreciated ?

          Thanks in advance !!

          1 Reply Last reply Reply Quote 0
          • A
            ab_trader last edited by ab_trader

            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.

            • If my answer helped, hit reputation up arrow at lower right corner of the post.
            • Python Debugging With Pdb
            • New to python and bt - check this out
            1 Reply Last reply Reply Quote 2
            • B
              backtrader administrators @codesutras last edited by

              @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

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors