Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. codesutras
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    C
    • Profile
    • Following 0
    • Followers 0
    • Topics 1
    • Posts 3
    • Best 0
    • Controversial 0
    • Groups 0

    codesutras

    @codesutras

    0
    Reputation
    57
    Profile views
    3
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    codesutras Unfollow Follow

    Latest posts made by codesutras

    • RE: Force exit or exit open position according to risk management

      @backtrader Any help would be appreciated ?

      Thanks in advance !!

      posted in Indicators/Strategies/Analyzers
      C
      codesutras
    • RE: Force exit or exit open position according to risk management

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

      posted in Indicators/Strategies/Analyzers
      C
      codesutras
    • 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 !!

      posted in Indicators/Strategies/Analyzers
      C
      codesutras