Navigation

    Backtrader Community

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

    Rushi Chaudhari

    @Rushi Chaudhari

    0
    Reputation
    109
    Profile views
    13
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    Rushi Chaudhari Unfollow Follow

    Latest posts made by Rushi Chaudhari

    • Still confused in Buy at Open and Sell at close

      Hi everyone,

      This post is similar to https://community.backtrader.com/topic/2648/buy-at-open-price-and-close-at-close-price-in-the-same-day, but I'm still confused. I have used cheat_on_open=True and also cerebro.broker.set_coc(True)

      class TestStrat(bt.Strategy):
          # list of parameters which are configurable for the strategy
          params = dict(
              pfast=15,  # period for the fast moving average
              pslow=30   # period for the slow moving average
          )
      
      
          def log(self, txt, dt=None):
              # return
              ''' Logging function for this strategy'''
              dt = dt or self.datas[0].datetime.datetime(0)
              print('%s, %s' % (dt, txt))
          
          def notify_order(self, order):
              if order.status in [order.Submitted, order.Accepted, order.Partial]:
                  # Buy/Sell order submitted/accepted to/by broker/partially filled - Nothing to do
                  return
      
      		# Check if an order has been completed
              if order.status in [order.Completed]:
                  if order.isbuy():
                      self.log('BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f, tradeID: %d' %(order.executed.price, order.executed.value, order.executed.comm, order.tradeid))
                  elif order.issell():
                      self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f, tradeID: %d' %(order.executed.price, order.executed.value, order.executed.comm, order.tradeid))
              elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                  self.log('Order Canceled/Margin/Rejected')
              
          def notify_trade(self, trade):
              if trade.justopened:
                  self.log('New trade just opened with id %d'%(trade.tradeid))
                  self.livetrades[trade.tradeid] = trade
              elif trade.isclosed:
                  self.log('A trade just closed with id %d'%(trade.tradeid))
                  self.livetrades.pop(trade.tradeid)
              else:
                  self.log('Trade id %d updated'%(trade.tradeid))
              
          def __init__(self):
              self.livetrades = {}
              self.counter = 0
              
              self.fastMA = bt.indicators.EMA(period=self.p.pfast)
              self.slowMA = bt.indicators.EMA(period=self.p.pslow)
              self.crossover = bt.indicators.CrossOver(self.fastMA, self.slowMA) 
              self.i=0
              self.longCrossed = False
              self.shortCrossed = False
              self.buysize=0
      
          def next(self):
              self.i += 1
              if self.position:
                  self.close(size=self.position.size)
                  print('selling' + str(self.data.close[0]))
      
          def next_open(self):
              self.i += 1
              if self.crossover > 0:  # if fast crosses slow to the upside
                  self.longCrossed = True
              if self.crossover < 0:
                  self.longCrossed = False
      
              if self.longCrossed and (self.data.open[0] > self.fastMA[-1]):
                  print('buying' + str(self.data.open[0]))
                  buysize =  int(self.broker.get_cash() / self.data.close[0])
                  self.buy(size=buysize, coc=False)
      
      
      
      
      
      cerebro = bt.Cerebro(writer=True, stdstats=True, cheat_on_open=True)
      cerebro.adddata(bt.feeds.PandasData(dataname=hourlydf))
      print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      cerebro.addstrategy(TestStrat) 
      cerebro.addwriter(bt.WriterFile, csv=True)
      cerebro.addanalyzer(BarAnalysis, _name="bar_data")
      cerebro.broker.set_coc(True)
      strat = cerebro.run()
      cerebro.plot(style='candlestick', volume=False)
      print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
      bar_data_res = strat[0].analyzers.bar_data.get_analysis()
      header = ["date", "open", "high", "low", "close", "position", "value", "cash", "slowMA", "fastMA"]
      
      df = pd.DataFrame(bar_data_res, columns= header)
      df.to_csv('strategy.csv', index=False)
      

      This is not executing the buy at open and sell at close during the same day. Can anyone help where am I going wrong?

      posted in General Code/Help
      Rushi Chaudhari
      Rushi Chaudhari
    • RE: Why the crash on windows ?

      @backtrader ?

      posted in General Code/Help
      Rushi Chaudhari
      Rushi Chaudhari
    • Why the crash on windows ?

      f34d7981-1fc7-4af2-9766-480ad399953e-image.png

      posted in General Code/Help
      Rushi Chaudhari
      Rushi Chaudhari
    • RE: How to print and plot ta-lib candlestick patterns ?

      Oh, finally got it working, can you add this in documentation, or can I ?

      posted in General Code/Help
      Rushi Chaudhari
      Rushi Chaudhari
    • RE: How to print and plot ta-lib candlestick patterns ?

      @backtrader Is it possible to check if a candlestick pattern has been observed today? How should i return yes/no value ?

      posted in General Code/Help
      Rushi Chaudhari
      Rushi Chaudhari
    • RE: Green candlesticks not appearing

      @backtrader yes, the ohlc prices seem to be read wrong, everything instead of 30-40 range is read as eg. 883637.92 can this be tweaked to work as expected in YahooFinanceCSVData()?

      posted in General Code/Help
      Rushi Chaudhari
      Rushi Chaudhari
    • RE: Green candlesticks not appearing

      @backtrader the dataset seems ok

      Date,High,Low,Open,Close,Volume,Adj Close
      2002-07-01,39.138099670410156,38.48149871826172,38.847999572753906,38.878501892089844,4031529.0,30.347339630126953
      2002-07-02,40.11539840698242,38.54249954223633,38.72579956054688,39.214500427246094,14152322.0,30.60960960388184
      2002-07-03,39.764198303222656,38.86320114135742,39.03120040893555,39.56570053100586,8456845.0,30.883745193481445
      2002-07-04,40.833099365234375,39.30609893798828,39.87110137939453,39.535099029541016,12745157.0,30.859865188598636
      2002-07-05,40.00849914550781,39.01589965820313,40.00849914550781,39.351898193359375,6445895.0,30.71685791015625
      2002-07-08,40.039100646972656,38.40510177612305,39.73360061645508,38.618900299072266,8225227.0,30.144695281982425
      2002-07-09,39.15340042114258,37.88589859008789,38.2677001953125,37.977500915527344,10865999.0,29.64403915405273
      2002-07-10,38.5884017944336,37.412498474121094,38.14550018310547,37.58050155639648,8238269.0,29.33416175842285
      2002-07-11,38.14550018310547,36.86280059814453,37.412498474121094,37.85540008544922,12711851.0,29.548738479614254
      2002-07-12,38.55780029296875,37.45840072631836,38.328800201416016,37.74850082397461,7378410.0,29.465293884277344
      2002-07-15,37.8400993347168,35.290000915527344,37.824798583984375,35.7327995300293,12436803.0,27.89190673828125
      2002-07-16,37.71789932250977,35.03030014038086,35.48849868774414,37.18349838256836,20568293.0,29.024272918701172
      2002-07-17,37.1682014465332,36.08399963378906,36.8932991027832,36.6338005065918,9150981.0,28.595195770263672
      2002-07-18,37.38199996948242,35.64120101928711,37.25979995727539,36.114601135253906,18480280.0,28.189922332763672
      2002-07-19,37.45840072631836,36.23669815063477,36.23669815063477,36.51160049438477,14291424.0,28.49980354309082
      2002-07-22,35.97710037231445,32.770301818847656,35.80910110473633,33.45750045776367,13678966.0,26.11587142944336
      2002-07-23,34.75550079345703,32.23590087890625,33.54909896850586,34.46540069580078,15143604.0,26.902606964111328
      2002-07-24,33.900299072265625,32.2510986328125,33.900299072265625,33.106300354003906,10235892.0,25.841739654541016
      2002-07-25,35.09140014648437,33.060501098632805,34.297401428222656,33.518600463867195,7693444.0,26.1635684967041
      2002-07-26,32.800899505615234,31.18219947814941,32.678699493408196,31.65559959411621,10417471.0,24.70936584472656
      2002-07-29,32.35800170898437,30.23539924621582,30.54080009460449,30.663000106811523,12310549.0,23.93457794189453
      
      posted in General Code/Help
      Rushi Chaudhari
      Rushi Chaudhari
    • Green candlesticks not appearing

      Using default matplotlib or bokeh neither of them is showing candlestick chart properly.

      delete1.png

      delete2.png

      Here is the code below, found on quickstart guide

      from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      
      import datetime  # For datetime objects
      import os.path  # To manage paths
      import sys  # To find out the script name (in argv[0])
      from backtrader_plotting import Bokeh
      from backtrader_plotting.schemes import Tradimo
      # Import the backtrader platform
      import backtrader as bt
      
      
      # Create a Stratey
      class TestStrategy(bt.Strategy):
          params = (
              ('maperiod', 15),
          )
      
          def log(self, txt, dt=None):
              ''' Logging function fot this strategy'''
              dt = dt or self.datas[0].datetime.date(0)
              print('%s, %s' % (dt.isoformat(), txt))
      
          def __init__(self):
              # Keep a reference to the "close" line in the data[0] dataseries
              self.dataclose = self.datas[0].close
      
              # To keep track of pending orders and buy price/commission
              self.order = None
              self.buyprice = None
              self.buycomm = None
      
              # Add a MovingAverageSimple indicator
              self.sma = bt.indicators.SimpleMovingAverage(
                  self.datas[0], period=self.params.maperiod)
      
              # Indicators for the plotting show
              bt.indicators.ExponentialMovingAverage(self.datas[0], period=25)
              bt.indicators.WeightedMovingAverage(self.datas[0], period=25,
                                                  subplot=True)
              bt.indicators.StochasticSlow(self.datas[0])
              bt.indicators.MACDHisto(self.datas[0])
              rsi = bt.indicators.RSI(self.datas[0])
              bt.indicators.SmoothedMovingAverage(rsi, period=10)
              bt.indicators.ATR(self.datas[0], plot=False)
      
          def notify_order(self, order):
              if order.status in [order.Submitted, order.Accepted]:
                  # Buy/Sell order submitted/accepted to/by broker - Nothing to do
                  return
      
              # Check if an order has been completed
              # Attention: broker could reject order if not enough cash
              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:  # Sell
                      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')
      
              # Write down: no pending order
              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):
              # Simply log the closing price of the series from the reference
              self.log('Close, %.2f' % self.dataclose[0])
      
              # 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:
      
                  # Not yet ... we MIGHT BUY if ...
                  if self.dataclose[0] > self.sma[0]:
      
                      # BUY, BUY, BUY!!! (with all possible default parameters)
                      self.log('BUY CREATE, %.2f' % self.dataclose[0])
      
                      # Keep track of the created order to avoid a 2nd order
                      self.order = self.buy()
      
              else:
      
                  if self.dataclose[0] < self.sma[0]:
                      # SELL, SELL, SELL!!! (with all possible default parameters)
                      self.log('SELL CREATE, %.2f' % self.dataclose[0])
      
                      # Keep track of the created order to avoid a 2nd order
                      self.order = self.sell()
      
      
      if __name__ == '__main__':
          # Create a cerebro entity
          cerebro = bt.Cerebro()
      
          # Add a strategy
          cerebro.addstrategy(TestStrategy)
      
          # Datas are in a subfolder of the samples. Need to find where the script is
          # because it could have been called from anywhere
          modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
          datapath = os.path.join(modpath, 'ZEEL.NS.csv')
      
          # Create a Data Feed
          data = bt.feeds.YahooFinanceCSVData(
              dataname=datapath,
              # Do not pass values before this date
              fromdate=datetime.datetime(2002, 1, 1),
              # Do not pass values before this date
              todate=datetime.datetime(2003, 12, 31),
              # Do not pass values after this date
              reverse=False)
      
          # Add the Data Feed to Cerebro
          cerebro.adddata(data)
      
          # Set our desired cash start
          cerebro.broker.setcash(1000.0)
      
          # Add a FixedSize sizer according to the stake
          cerebro.addsizer(bt.sizers.FixedSize, stake=10)
      
          # Set the commission
          cerebro.broker.setcommission(commission=0.0)
      
          # Print out the starting conditions
          print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
          # Run over everything
          cerebro.run()
      
          # Print out the final result
          print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
          # Plot the result
          cerebro.plot(style='candlestick')
          #b = Bokeh(filename='chart.html',style='bar', plot_mode="single", scheme=Tradimo())
          #cerebro.plot(b)
      
      
      posted in General Code/Help
      Rushi Chaudhari
      Rushi Chaudhari
    • RE: Backtesting UI

      Does this still work ?
      I am facing this https://pastebin.com/whb5g6eq

      posted in General Discussion
      Rushi Chaudhari
      Rushi Chaudhari
    • RE: How to print and plot ta-lib candlestick patterns ?

      @ab_trader Morning star returns -100 or 0 or 100
      How can I print what is being returned ?
      For eg. print(self.rsi) prints a number, but print(self.morningstar) is printing an object.

      also is it possible to circle a pattern like this ?

      7306380d-cdca-46a0-a9f0-a7ceebc8a2d4-image.png

      posted in General Code/Help
      Rushi Chaudhari
      Rushi Chaudhari