Hi everyone.
About the problem with the position calculation in my simple example. I think it is a problem with bt-ccxt-store. I create a workarround, ignoring the position value and store this value manually inside the Strategy Class. Other customization is the buy()
and sell()
functions, the sizer does not work very well with binance. When the fee is charged in BNB, it does not include the wallet value. I'm using this class to create custom buy and sell operations. For now, I'm using backtrader to get the historical information and indictators. All other operations I'm doing manually.
Custom code: https://gist.github.com/rodrigo-brito/8c82020f04e946e3f0c39c7243cfe1ee

Posts made by Rodrigo Brito
-
RE: Anyone use backtrader to do live trading on Bitcoin exchange?
-
Money Flow Index (MFI)
I implemented the MFI for Backtrader, but I think this can be improved:
#!/usr/bin/env python3 import math import backtrader as bt class MFI(bt.Indicator): lines = ('mfi', 'money_flow_raw', 'typical', 'money_flow_pos', 'money_flow_neg') plotlines = dict( money_flow_raw=dict(_plotskip=True), money_flow_pos=dict(_plotskip=True), money_flow_neg=dict(_plotskip=True), typical=dict(_plotskip=True), ) params = ( ('period', 14), ) def next(self): typical_price = (self.data.close[0] + self.data.low[0] + self.data.high[0]) / 3 money_flow_raw = typical_price * self.data.volume[0] self.lines.typical[0] = typical_price self.lines.money_flow_raw[0] = money_flow_raw self.lines.money_flow_pos[0] = money_flow_raw if self.lines.typical[0] >= self.lines.typical[-1] else 0 self.lines.money_flow_neg[0] = money_flow_raw if self.lines.typical[0] <= self.lines.typical[-1] else 0 pos_period = math.fsum(self.lines.money_flow_pos.get(size=self.p.period)) neg_period = math.fsum(self.lines.money_flow_neg.get(size=self.p.period)) if neg_period == 0: self.lines.mfi[0] = 100 return self.lines.mfi[0] = 100 - 100 / (1 + pos_period / neg_period)
I used two auxiliary lines to sum negative/positive values when typical_price change. There are some alternative to reduce it and compare two different lines given a period?
-
Standard MACD vs Talib MACD - Different Values
Talib MACD returns a different value than std MACD with re-sample data.
A report of @mr-m0nst3rUsing this example: https://gist.github.com/rodrigo-brito/4f779001647ce1ce7235a7d254b7e75b
With this dataset: https://github.com/rodrigo-brito/backtrader-binance-bot/blob/master/dataset/binance_nov_18_mar_19_btc.csvWithout re-sample, it is ok:
EMA12 = 6960.21 EMA26 = 6949.12 DIFF = 11.09 STD MACD = 11.09 TALIB MACD = 11.09 -------------- EMA12 = 6961.31 EMA26 = 6950.47 DIFF = 10.84 STD MACD = 10.84 TALIB MACD = 10.84 -------------- EMA12 = 6961.38 EMA26 = 6951.31 DIFF = 10.08 STD MACD = 10.08 TALIB MACD = 10.08 --------------
But, with a re-sample of 30 minutes, it returns a different value:
cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=30)
-------------- EMA12 = 7031.31 EMA26 = 7057.54 DIFF = -26.23 STD MACD = -26.23 TALIB MACD = -83.05 -------------- EMA12 = 7012.22 EMA26 = 7046.41 DIFF = -34.19 STD MACD = -34.19 TALIB MACD = -90.54 -------------- EMA12 = 7004.46 EMA26 = 7040.14 DIFF = -35.68 STD MACD = -35.68 TALIB MACD = -88.64 --------------
The graphic result is also different:
-
RE: Example of usage with Bitcoin in Binance
Hi @diamonddogs95, I used https://github.com/xFFFFF/Gekko-Datasets to generate a database in SQLite. Then I open it and run this query https://github.com/rodrigo-brito/backtrader-binance-bot/blob/master/dataset/queries.sql
-
RE: Anyone use backtrader to do live trading on Bitcoin exchange?
Hi, @pfederra
- I have the same issue in
notify_order()
, it works only in simulations to me. - I think the fork has a lot of issues, but work very well for the main features, like order and feed.
- The get positions does not work for me very well, it has a precision issue. I'm using a function to get my wallet ballance and settting a custom sizer. I'm also defining a global variable with the last operation (BUY/SELL) to control the next operation. It is a working arrond, but working in live.
- I have the same issue in
-
RE: Anyone use backtrader to do live trading on Bitcoin exchange?
@coolernax I don't persist the feed or strategies positions. The feed loads a delayed history when started. About the trade position, I'm start always with no funds in crypto, the first operation should be a buy. Maybe it can be improved with a simple flag in the script.
-
RE: How to draw point on the chart without using indicator?
It is an interesting feature. Sometimes I want to visualize critical points such as "stop loss".
-
RE: Anyone use backtrader to do live trading on Bitcoin exchange?
I published a simple example with Binance integration: https://github.com/rodrigo-brito/backtrader-binance-bot
-
Example of usage with Bitcoin in Binance
Hi everyone,
I published a little contribution in my Github. A example of usage of Backtrader with Binance integration. Using the CCXT lib for broker and feed.
The base strategy uses a simple RSI + SMA filter, with stop loss of 3%.
Feel free to contribute and improve the example: https://github.com/rodrigo-brito/backtrader-binance-bot
Best regards,
Rodrigo Brito -
RE: Example code for live trading using binance
Hi @pfederra. I'm receiving zero values too. I think it is a issue related to bt-ccxt-store.
-
RE: Example code for live trading using binance
Hi @Noah-Bastola, to install, you should the pip:
pip install git+git://github.com/Dave-Vallance/bt-ccxt-store@master#bt-ccxt-store
Maybe, are you using an old version. Make shure that parameter
fromdate
are defined with a enough value for you indicators. -
RE: Example code for live trading using binance
Hi @Noah-Bastola,
I'm using backtrader for bitcoin/usdt trades in production. The easy way is integrate with binance API through CCXT Store lib. A simple example is available below:
https://gist.github.com/rodrigo-brito/3b0fca2487c92ad97869247edd5fd852
-
Coins balance in backtesting
Hi guys,
I'm using the backtrader with Binance, through CCXT Store lib. The CCXT broker give me my coin balance with
self.broker.get_wallet_balance("BTC")
. But, in simulations only the cash values is available withself.broker.get_cash()
. How I can access the coin amount in backtests?The second question is: I'm using the follow code to use all my funds to buy with 100% of the value. There is a different way to define an automatic size to use all my funds do buy coins?
P.S: 0.99 is used to avoid decimal precision issues.price = self.data0.close[0] amount = (self.broker.get_cash() / price) * 0.99 self.buy(size=amount)
-
RE: Binance API or updating backtrader every 5 min
I am also trying to use the CCXT lib from https://github.com/Dave-Vallance/bt-ccxt-store. The feed works very fine with 1 minute time frame. But with other compressions, does not work.
I also tried to add to cerebro with:
cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=5)
But does not work too.
@guwop do you have some progress with it?