Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. the world
    3. Posts
    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 9
    • Posts 19
    • Best 2
    • Groups 0

    Posts made by the world

    • RE: Can't get indicator trigger date to show up in logs

      @davidavr said in Can't get indicator trigger date to show up in logs:

      community are really attrac

      lean uses C# for its core, shouldn't it be faster?

      posted in General Code/Help
      the world
      the world
    • Ways to accelerate the interactive plot of your backtest result.

      1.downsample
      2.gpu accelerate (bokeh and plotly have the gpu api)
      The two points mentioned above are just my thoughts and have not taken action. When I realize it, I will update it in this post. Before, I drew a K-line with a one minute interval of three months data. When dragging and zooming, the speed was very slow, so I came up with the idea of improving the speed

      posted in General Code/Help
      the world
      the world
    • RE: Is there any high performance plot python packages?

      @leggomaeggo I have tried this, still this problem, when there is a large dataset

      posted in General Code/Help
      the world
      the world
    • Is there any high performance plot python packages?

      When my data has 10000 points, It will be very slow when I want to view the details of the plot (zoom and drag), but it is easy to zoom and drag the plot drawn by MATLAB. Does Python have a high-performance drawing tool like matlab's plot

      posted in General Code/Help
      the world
      the world
    • RE: data feeds with different start date

      @dasch said in data feeds with different start date:

      you could provide some code to see what issue you have, its better than just guesswork what your issues may be.

      The trade just happened, but no plot.
      I think it is too little data in C98, if I change to another data not so little (with BTC data 90% of the length), then the graph will show normal. There should be some problem with the logic of plot.
      You can try to reproduce this whit data feeds of different start time. But need to override the prenext method to make trade happen when there is only one data feeds have valid data

      posted in General Code/Help
      the world
      the world
    • RE: data feeds with different start date

      @run-out There is no buy-sell plot in the second symbol(C98USDT), I don't know why.

      posted in General Code/Help
      the world
      the world
    • RE: Can't get indicator trigger date to show up in logs

      @davidavr how about the backtest speed of LEAN

      posted in General Code/Help
      the world
      the world
    • How to optimize the strategy selection

      https://www.backtrader.com/blog/posts/2016-10-29-strategy-selection/strategy-selection/
      Two strategies have different parameters, How to pass the parameter to the strategy selection

      posted in Indicators/Strategies/Analyzers
      the world
      the world
    • RE: optimization on Strategy Selection example

      @backtrader said in optimization on Strategy Selection example:

      You then need to have 5 optimizations (the sample shows 2) and the StFetcher instantiating the appropriate strategy with the appropriate parameters. In the example idx is used as the key to decide which strategy to return. The same idx can be used to decide which strategy to instantiate and with which parameters.

      Can you give me an example code? thank you

      posted in General Code/Help
      the world
      the world
    • data feeds with different start date

      There is no buy-sell triangles in symbol C98's figure. But the cash grows which means there are trades in C98
      329a29b4-50ee-4af0-8e40-df30ccf7f5a4-image.png

      posted in General Code/Help
      the world
      the world
    • RE: How to speed up almost 100 times when add data and preload data?

      @dehati_paul said in How to speed up almost 100 times when add data and preload data?:

      es only when my data classes are derived from bt.feeds.PandasData, but when the classes are derived from bt.feeds.PandasDirectData (to speed up first time loading) I get the following error. Any insights?
      _pickle.PicklingError: Can't pickle <class 'pandas.core.frame.Pandas'>: attribute lookup Pa

      I got the same error. Have you fixed it?

      posted in General Discussion
      the world
      the world
    • If I add two strategies, how to manage the positions of each strategy

      two strategies have the same next() method

          def next(self):
              for i,d in enumerate(self.datas):
                  pos = self.getposition(d)
                  if pos.size:
                      if self.inds[d]['signal'] < 0:
                          self.close(data=d)
                          print(self.p.name)
      
                  elif self.inds[d]['signal'] > 0:
                      self.buy(data=d)
              pass
      

      when I use these codes, the strategy will do short selling making the cash higher than the total account value. But I don't want to short, so how to make each strategy have their own position

      posted in General Discussion
      the world
      the world
    • RE: How to speed up almost 100 times when add data and preload data?

      @the-world I solved this error by modifying the code using the pickle in the cerebro.py

                  if self.p.optdatas and self._dopreload and self._dorunonce:
                      if self.p.load_my_data:
                          begin_time = time.time()
                          self.datas = self.load_my_data_from_pickle()
                          end_time = time.time()
                          print("every time pre_load from pkl consume time :{}".format(end_time - begin_time))
                      else:
                          begin_time = time.time()
                          for data in self.datas:
                              data.reset()
                              if self._exactbars < 1:  # datas can be full length
                                  data.extend(size=self.params.lookahead)
                              data._start()
                              if self._dopreload:
                                  data.preload()
                          end_time = time.time()
                          print("every time pre_load from raw consume time :{}".format(end_time-begin_time))
      
      
      posted in General Discussion
      the world
      the world
    • RE: How to speed up almost 100 times when add data and preload data?

      When I using optstrategy, It turns out errors.

      posted in General Discussion
      the world
      the world
    • How to get the TimeReturn of one data, When there are multiple data feeds,

      How to get the TimeReturn of one data, When there are multiple data feeds,

      posted in General Discussion
      the world
      the world
    • What will happen when i access the drawdown[0], when the observers are calculated after the "next" method

      In the document
      526406e4-31ad-418f-8c97-26bbcb40e16e-image.png

      posted in General Discussion
      the world
      the world
    • RE: How to improve the performance when backtest on multi-data

      @run-out Thanks for your suggestion. I need the coins in the same portfolio, but I will tune the params separately using the optimize.

      posted in General Code/Help
      the world
      the world
    • How to plot the profit of each asset?

      when there are multiple assets, how to plot the profit of each asset rather than the total value of the broker.
      5cc6abec-4ef1-4224-a307-1bc2f9af7c82-image.png
      And how to make the trades results of different data have different legends.
      a3d6311c-cb5a-47e0-82a8-a1767cca4231-image.png

      posted in General Code/Help
      the world
      the world
    • How to improve the performance when backtest on multi-data

      When I backtest on one coin data(such as BTC), it costs 7s. When I add the other data(such as ETH), it cost 14s. How to improve it? There are hundreds of symbols.

      import time
      from datetime import datetime,timedelta
      from zipfile import ZipFile
      from backtrader_plotting import Bokeh
      from backtrader_plotting.schemes import Tradimo
      import numpy as np
      import os
      import pandas as pd
      import backtrader as bt
      def data_kline_load(symbol,interval,start,end):
          dirpath = 'D:\\Seafile\\trading\\binance-public-data-master\\python\\data\\spot\\daily\\klines\\{}\\{}\\'.format(symbol,interval)
          time = start
          data=pd.DataFrame(columns=['Open','High','Low','Close','Volume','uVolume'])
          while(time!=end+timedelta(days=1)):
              filename = symbol+'-{}-'.format(interval)+time.strftime('%Y-%m-%d')
              filepath = dirpath+filename+'.zip'
              if(os.path.exists(filepath)==False):
                  time = time + timedelta(days=1)
                  continue
              #print(filepath)
              z = ZipFile(filepath)
              f = z.open(filename+'.csv')
              df = pd.read_csv(f,header=None)
              df.index = pd.to_datetime(df[0],unit='ms')
              z.close()
              f.close()
              df = df[[1,2,3,4,5]]
              df.columns = ['Open','High','Low','Close','Volume']
              data = pd.concat([data,df],axis=0)
              time = time + timedelta(days=1)
          print(symbol)
          print(data.shape[0])
          return data
      
      class BinancePdData(bt.feeds.PandasData):
          params = (
              ('datetime', None),
              ('dtformat',('%Y-%m-%d %H:%M:%S')),
              ('open', -1),
              ('high', -1),
              ('low', -1),
              ('close', -1),
              ('volume', -1),
              ('uvolume',-1),
              ('openinterest', -1),
          )
      
      # Create a Stratey
      class TestStrategy(bt.Strategy):
          params = (
              ('pfast', 7),
              ('pslow', 25),
              ('printlog', False),
          )
      
          def log(self, txt, dt=None, doprint=False):
              ''' Logging function fot this strategy'''
              if self.params.printlog or doprint:
                  dt = dt or self.datas[0].datetime.datetime(0)
                  print('%s, %s' % (dt.isoformat(sep=' '), txt),file=self.log_file)
      
          def __init__(self):
              # Keep a reference to the "close" line in the data[0] dataseries
              self.log_file = open('position_log.txt','w')
              self.inds = dict()
      
              for i,d in enumerate(self.datas):
                  self.inds[d] = dict()
                  self.inds[d]['sma1'] = bt.ind.SMA(d.close,period=self.p.pfast)
                  self.inds[d]['sma2'] = bt.ind.SMA(d.close,period=self.p.pslow)
                  self.inds[d]['cross'] = bt.ind.CrossOver(self.inds[d]['sma1'],self.inds[d]['sma2'],plot = False)
      
      
      
              # To keep track of pending orders and buy price/commission
              self.order = None
              self.buyprice = None
              self.buycomm = None
      
      
      
          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,Symbol: %s, Price: %.5f, Cost: %.5f, Comm %.5f' %
                          (order.data._name,
                           order.executed.price,
                           order.executed.value,
                           order.executed.comm))
      
      
                  else:  # Sell
                      self.log('SELL EXECUTED,Symbol: %s Price: %.5f, Cost: %.5f, Comm %.5f' %
                               (order.data._name,
                                order.executed.price,
                                order.executed.value,
                                order.executed.comm))
      
      
              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 %.5f, NET %.5f' %
                       (trade.pnl, trade.pnlcomm))
      
          def next(self):
              for i,d in enumerate(self.datas):
                  dt,dn = d.datetime.datetime(0),d._name
                  #持仓
                  pos = self.getposition(d)
      
                  if not pos.size:#没有持仓
                      # if d.close[0]>d.close[-1]*1.01:
                      if self.inds[d]['cross'] > 0:
                          self.order = self.buy(data=d)
                  else:
                      if self.inds[d]['cross']<0:
                          self.order = self.close(data=d)
                          pass
      
      
          def stop(self):
              self.log_file.close()
      
      
      if __name__ == '__main__':
          cerebro = bt.Cerebro()
          cerebro.addstrategy(TestStrategy,pfast=7,pslow=25)
          # cerebro.optstrategy(
          #     TestStrategy,
          #     maperiod1=range(2, 10),maperiod2=range(15,35,2))
          # cerebro.addanalyzer(bt.analyzers.SharpeRatio)
      
          #symbols = ['BTCUSDT','ADAUSDT','QTUMUSDT','GTOUSDT','DENTUSDT','MTLUSDT','XVSUSDT','XRPUSDT','TCTUSDT','OMGUSDT','ANKRUSDT','ZILUSDT','DOGEUSDT','ETHUSDT','CFXUSDT','BCHUSDT','DOTUSDT','WINUSDT','FILUSDT','UNIUSDT','REEFUSDT','WRXUSDT','LUNAUSDT','DREPUSDT','TRXUSDT','SHIBUSDT','CAKEUSDT','ENJUSDT','BTTUSDT','TFUELUSDT','BNBUSDT','BTSUSDT','NKNUSDT','CHRUSDT','CHZUSDT','WAVESUSDT','RVNUSDT']
          symbols = ['FILUSDT','CHRUSDT']
          interval = '1m'
          start = datetime(2021,7,1)
          end = datetime(2021,7,31)
          for symbol in symbols:
              df = data_kline_load(symbol=symbol,interval=interval,start=start,end=end)
              data = BinancePdData(dataname=df)
              cerebro.adddata(data,name=symbol)
          time1=time.perf_counter()
          cerebro.broker.setcash(10000.0)
          cerebro.addsizer(bt.sizers.PercentSizer,percents=25)
          #cerebro.broker.setcommission(commission=0.001)
      
          print('Starting Portfolio Value: %.5f' % cerebro.broker.getvalue())
          cerebro.run(maxcpus=16)
          print('Final Portfolio Value: %.5f' % cerebro.broker.getvalue())
          print(time.perf_counter()-time1)
          b = Bokeh(style='bar', scheme=Tradimo())
          cerebro.plot(b)
      
      posted in General Code/Help
      the world
      the world
    • 1 / 1