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/

    defining code for target price and stoploss price

    General Code/Help
    help strategy target order stoplimit
    2
    2
    319
    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.
    • Akshay Mehara
      Akshay Mehara last edited by

      hi, i am trying to get tgt and stoploss price for my startegy . i want 1% stoploss and tgt.

      """"
      import datetime
      from collections import OrderedDict
      import backtrader as bt
      import backtrader.feeds as btfeeds
      from datetime import date
      from nsepy import get_history
      d = get_history( symbol = "BPCL", start = date(2017,1,1), end = date(2020,5,1))
      d.to_csv("BPCL.csv")

      class mystrategy(bt.SignalStrategy):
      def log(self, txt, dt = None):
      dt = dt or self.datas[0].datetime.date(0)
      print("%s, %s" % (dt, txt))

      def __init__(self):
      	self.dataclose = self.datas[0].close
      	self.order = None
      	self.buyprice = None
      	self.buycomm = None
      
      	self.rsi = bt.ind.RSI_EMA(self.datas[0], period = 14)
      
      def notify_order(self, order):
      	if order.status in [order.Submitted, order.Accepted]:
      		return
      	if order.status in [order.Completed]:
      		if order.isbuy():
      			self.log("Buy executed, price %.2f, cost %.2f, comm %.2f" % (order.executed.price, order.executed.value, order.executed.comm))
      			self.buyprice = order.executed.price
      			self.buycomm = order.executed.comm
      		else:
      			self.log("Sell executed, price %.2f, cost %.2f, comm %.2f" % (order.executed.price, order.executed.value, order.executed.comm))
      		self.bar_executed = len(self)
      
      	elif order.status in [order.Canceled, order.Margin, order.Rejected]:
      		self.log("order Canceled/Margin/Rejected")
      	self.order = None
      
      def notify_trade(self, trade):
      	if not trade.isclosed:
      		return
      	self.log("Operation profit , gross %.2f, net %.2f" % (trade.pnl, trade.pnlcomm))
      	
      def next(self):
      	self.log("Close, %.2f" % self.dataclose[0])
      	if self.order:
      		return
      	if not self.position:
      		if self.rsi < 20:
      			self.log("Buy Create, %.2f" % self.dataclose[0])
      			self.order = self.buy()
      	else:
      		if self.rsi > 80:
      			self.log("Sell Create, %.2f" % self.dataclose[0])
      			self.order = self.sell()
      

      def printTradeAnalysis(analyzer):
      total_open = analyzer.total.open
      total_closed = analyzer.total.closed
      total_won = analyzer.won.total
      total_lost = analyzer.lost.total
      win_streak = analyzer.streak.won.longest
      lost_streak = analyzer.streak.lost.longest
      pnl_net = round(analyzer.pnl.net.total,2)
      strike_rate = (total_won/total_closed)*100

      h1 = ["Total Open", "Total Closed", "Total Won", "Total Lost"]
      h2 = ["Strike Rate", "Win Streak", "Losing Streak", "PnL Net"]
      r1 = [total_open, total_closed, total_won, total_lost]
      r2 = [strike_rate, win_streak, lost_streak, pnl_net]
      if len(h1) > len(h2):
      	header_length = len(h1)
      else:
      	header_length = len(h2)
      
      print_list = [h1,r1,h2,r2]
      row_format = "{:<15}" * (header_length+1)
      print("Trade Analysis Results:")
      for row in print_list:
      	print(row_format.format("", *row))
      

      def printSQN(analyzer):
      sqn = round(analyzer.sqn, 2)
      print("SQN:{}".format(sqn))

      if name == "main":
      cerebro = bt.Cerebro()
      cerebro.addstrategy(mystrategy)

      data = btfeeds.GenericCSVData(
      	dataname='BPCL.csv',
      	fromdate=datetime.datetime(2017, 1, 10),
      	todate=datetime.datetime(2020, 4, 25),
      	nullvalue=0.0,
      	dtformat=('%Y-%m-%d'),
      	datetime=0,
      	high=5,
      	low=6,
      	open=4,
      	close=8,
      	volume=10,
      	openinterest=-1,
      	)
      
      cerebro.adddata(data)
      startcash = 10000000
      cerebro.broker.setcash(startcash)
      cerebro.addsizer(bt.sizers.FixedSize, stake = 10)
      cerebro.broker.setcommission(commission = 0.01)
      
      cerebro.addanalyzer(bt.analyzers.TradeAnalyzer, _name = "ta")
      cerebro.addanalyzer(bt.analyzers.SQN, _name = "sqn")
      print("Starting Portfolio Value: %.2f" % cerebro.broker.getvalue())
      strategies = cerebro.run()
      firstStrat = strategies[0]
      
      printTradeAnalysis(firstStrat.analyzers.ta.get_analysis())
      printSQN(firstStrat.analyzers.sqn.get_analysis())
      
      print("Final Portfolio Value: %.2f" % cerebro.broker.getvalue())
      cerebro.plot(style = "candlestick")
      

      """""
      data is downloaded from nsepy for backtesting.
      plz assist me.

      1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld last edited by

        Please mind the formatting of your posts, and the usage of 'backticks' - see the top of the page. This way your code will be much more readable. Also, there is a preview pane that shows exactly how the post will be visible to others.

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