Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. Maha Almubarak
    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 2
    • Posts 2
    • Best 0
    • Groups 0

    Maha Almubarak

    @Maha Almubarak

    0
    Reputation
    1
    Profile views
    2
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    Maha Almubarak Unfollow Follow

    Latest posts made by Maha Almubarak

    • How to get the strategy return without setting cash

      Hello,
      I would like to ask you please how to get the strategy return without setting cash. I just want to know the investment return if one share was traded. Meaning I want to sum all the strategy return.

      posted in Indicators/Strategies/Analyzers
      Maha Almubarak
      Maha Almubarak
    • Buy signal is not executing at the right time

      Hello,

      I am testing candlestick strategy with a stop loss sell. I generated my strategy signal on simple python code and I wanted to run it on BT. When I run BT on my strategy the signals doesn't match my pandas data frame. I would really appreciate if someone can help me here. To get the candle stick pattern I used tlib. Tlib will give 100 if the pattern is observed. So, I had a logic to generate the buy signals as follow

      df['Buy']=np.where(df['CDLBELTHOLD']==100,1,0)
      
      

      first signal was generated at with price :
      2019-03-21 271.629418

      I created a strategy on BT as follow:

      # Create a Stratey
      class TestStrategy(bt.Strategy):
      
          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
              self.marubozu = bt.talib.CDLHARAMI(self.data.open,self.data.high,self.data.low,self.data.close) 
              self.my_atr = bt.ind.ATR(period=30)
              self.max_close = bt.ind.MaxN(self.data.close(-11))
              self.order = None
              self.buyprice = None
              self.buycomm = None
              self.sellprice = None
              self.sellcomm = None
          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:{}'.format (order.executed.price))
              elif order.issell():
                 self.log('Sell EXECUTED, Price:{}'.format (order.executed.price))
              self.order=None
              self.bar_executed=len(self)
          def next(self):
              # Simply log the closing price of the series from the reference
              self.log('Close, %.2f' % self.dataclose[0])
              if self.order:
                return
              if not self.position:
                if self.marubozu==100:
                        # BUY, BUY, BUY!!! (with all possible default parameters)
                        self.log('BUY CREATE, %.2f' % self.dataclose[0])
                        self.order=self.buy()
              else:
                if self.dataclose<(self.max_close-(5*self.my_atr)):
                  self.log('Sell Ctreated {}'.format(self.dataclose[0]))
                  self.order=self.sell()
      
      cerebro = bt.Cerebro(stdstats=True)
      cerebro.broker.setcash(1000000)
      cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name='mysharpe')
      
      # 0.1% ... divide by 100 to remove the %
      #cerebro.broker.setcommission(commission=0.005)
      symbol='SPY'
      alpaca_data = rest_api.get_bars(symbol, TimeFrame.Day, '2019-01-01', '2022-01-01', adjustment='all').df
      
      data = bt.feeds.PandasData(dataname=alpaca_data, name=symbol)
      cerebro.adddata(data)
      cerebro.addstrategy(TestStrategy)
      initial_portfolio_value = cerebro.broker.getvalue()
      print(f'Starting Portfolio Value: {initial_portfolio_value}')
      cerebro.addsizer(bt.sizers.FixedSize,stake=1000)
      results=cerebro.run()
      final_portfolio_value = cerebro.broker.getvalue()
      print(f'Final Portfolio Value: {final_portfolio_value} ---> Return: {(final_portfolio_value/initial_portfolio_value - 1)*100}%')
      strat = results[0]
      print('Sharpe Ratio:', strat.analyzers.mysharpe.get_analysis()['sharperatio'])
      

      My first signal was received at this time and this is not the case as my pandas data frame, the data is the same nothing different.

      2019-03-28, BUY CREATE, 267.94

      Thanks for your time :)

      posted in General Code/Help
      Maha Almubarak
      Maha Almubarak