Example code for live trading using binance
-
Hi all,
I was wondering if there was any kind of reference that I could look at to get started with live trading. I use the platform Binance, and I want to feed in downloaded csv data into the backtesting strategy I have made.
In the code below, I download the most recent 5 minute bar data and feed it into the backtrader. My plan was to re-run the backtrader every 5 minutes with the next set of downloaded data and send orders requests using the Binance API.
# Add a strategy cerebro.addstrategy(TestStrategy) df = pd.DataFrame(get_klines('DLTBTC',5)) df[0]=pd.to_datetime(df[0],unit='ms') df.to_csv('data.csv',index=False) datapath = os.path.join(os.getcwd() + '/data.csv')#modpath, '../../datas/orcl-1995-2014.txt') # datapath = os.path.join(os.getcwd() + '/'+path)#'/historicalDLTBTC.csv'#modpath, '../../datas/orcl-1995-2014.txt') # Create a Data Feed data = bt.feeds.GenericCSVData( dataname=datapath, # Do not pass values before this date dtformat = ('%Y-%m-%d %H:%M:%S'), datetime = 0, high = 2, low = 3, open=1, close=4, volume=5, openinterest=-1, timeframe = bt.TimeFrame.Minutes, compression=5, )
If this is the best way to go about live trading using binance, how can I retrieve variables from the init portion of the TestStrategy class so that I can keep track of the trades and position?
# To keep track of pending orders and buy price/commission self.order = None self.buyprice = None self.buycomm = None self.loopIteration = 0 self.buyType = None self.inTrade = False
-
The instance of the executed strategies are returned by
cerebro.run
-
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
-
@rodrigo-brito How do I import ccxtbt, as shown in your imports?
-
@rodrigo-brito Actually I was able to load the library, but could you run me through how the code works? I ran the code as given, but it doesn't seem to fetch any data. This is the output I get:
2019-03-18 17:28:40.131205 - fetch_ohlcv - Attempt 0
Fetching: BTC/USDT, TF: 5m, Since: 1552944300000, Limit: 99999 -
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. -
Hi @Noah-Bastola, I did follow the below and managed to setup the environment with the correct versions:
conda create --name DVX python=3.7
source activate DVX
conda install -c anaconda git
pip install ccxt
pip install git+https://github.com/Dave-Vallance/bt-ccxt-store.gitGive it a try!
-
Hi @Rodrigo Brito,
As you mentioned, you are using Binance for prod trading. I have a question in regards to the order status. I do place order correctly on Binance since I can see the order in the dashboard. However, in the notify_order() when it comes to printing this line:
self.log (order.executed.price,order.executed.value,order.executed.comm)It always prints 0 for all 3 values, I am not sure why I cant see the order execution details. Does it work for you?
Thanks
-
Hi @pfederra. I'm receiving zero values too. I think it is a issue related to bt-ccxt-store.
-
Thanks for the confirmation. Do you know if there is any active development happening on the CCXT integration to Back-trader? Is there anywhere that we can report the bugs?
-
Hi @Rodrigo Brito, On another note, have you faced any other major/minor issues/bugs while running the Backtrader+CCXT live so we need to be aware of before go live? Any special error handling or Binance broker specific issues we might face? Thanks