Thank you it worked!
For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
Best posts made by Brian Zheng
Latest posts made by Brian Zheng
-
RE: Live Trading Example
I tried the first link, however I am getting the error below
Server Version: 76 TWS Time at connection:20200505 12:36:00 PST -------------------------------------------------- Strategy Created -------------------------------------------------- Timezone from ContractDetails: EST (Eastern Standard Time) Datetime, Open, High, Low, Close, Volume, OpenInterest, SMA ***** STORE NOTIF: <error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:usfarm.nj> ***** STORE NOTIF: <error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:cashfarm> ***** STORE NOTIF: <error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:usfarm> ***** STORE NOTIF: <error id=-1, errorCode=2106, errorMsg=HMDS data farm connection is OK:ushmds> ***** STORE NOTIF: <error id=-1, errorCode=2158, errorMsg=Sec-def data farm connection is OK:secdefil> ***** DATA NOTIF: DELAYED ***** DATA NOTIF: NOTSUBSCRIBED ***** STORE NOTIF: <error id=16777217, errorCode=354, errorMsg=Requested market data is not subscribed.Delayed market data is available.Error&BEST/STK/Top&BEST/STK/Top>
and here is my interactive brokers api configuration page
can you help? Thank you -
Live Trading Example
Hi, I am just wondering if anyone will be kind enough to provide an example on how to access live data on backtrader. Thank you.
-
ValueError: could not convert string to float: '10:00:00' when trying to backtest on intraday data
As the title suggests, I am having problems loading intraday data into my code. It is giving ValueError, but I don't know why.
Here is my code:from __future__ import (absolute_import, division, print_function, unicode_literals) import datetime import os.path import sys import backtrader as bt class TestStrategy(bt.Strategy): params = ( ('exitbars', 5), ) 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): self.dataclose = self.datas[0].close self.order = None self.buyprice = None self.buycomm = 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: %.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') 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): self.log('Close, %.2f' % self.dataclose[0]) if self.order: return if not self.position: if self.dataclose[0] < self.dataclose[-1]: if self.dataclose[-1] < self.dataclose[-2]: self.log('BUY CREATE, %.2f' % self.dataclose[0]) self.order = self.buy() else: if len(self) >= (self.bar_executed + self.params.exitbars): # SELL, SELL, SELL!!! (with all possible default parameters) self.log('SELL CREATE, %.2f' % self.dataclose[0]) self.order = self.sell() if __name__ == '__main__': cerebro = bt.Cerebro() cerebro.addstrategy(TestStrategy) data = bt.feeds.BacktraderCSVData(dataname="dat.csv") cerebro.adddata(data) cerebro.broker.setcash(100000.0) cerebro.addsizer(bt.sizers.FixedSize, stake=25) print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) cerebro.run() cerebro.plot() print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
Here is the error it is giving me:
Starting Portfolio Value: 100000.00 Traceback (most recent call last): File "code.py", line 79, in <module> cerebro.run() File "C:\Users\Brian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\backtrader\cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "C:\Users\Brian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\backtrader\cerebro.py", line 1212, in runstrategies data.preload() File "C:\Users\Brian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\backtrader\feed.py", line 688, in preload while self.load(): File "C:\Users\Brian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\backtrader\feed.py", line 479, in load _loadret = self._load() File "C:\Users\Brian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\backtrader\feed.py", line 710, in _load return self._loadline(linetokens) File "C:\Users\Brian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\backtrader\feeds\btcsv.py", line 52, in _loadline self.lines.open[0] = float(next(itoken)) ValueError: could not convert string to float: '10:00:00'
and here is a peek inside my data, which is a csv file.
Date,Time,Open,High,Low,Close,Volume 2020-04-28,10:00:00,1.16,1.16,1.15,1.16,2660 2020-04-28,10:01:00,1.16,1.16,1.155,1.1565,11098 2020-04-28,10:02:00,1.16,1.16,1.15,1.155,4958 2020-04-28,10:03:00,1.16,1.16,1.15,1.1501,45815 2020-04-28,10:04:00,1.1439,1.1499,1.14,1.1422,8953 2020-04-28,10:05:00,1.14,1.1485,1.14,1.1439,4114 2020-04-28,10:06:00,1.15,1.15,1.15,1.15,425 2020-04-28,10:07:00,1.15,1.15,1.14,1.15,8364 2020-04-28,10:08:00,1.15,1.16,1.1496,1.16,42291 2020-04-28,10:09:00,1.1501,1.16,1.15,1.16,4751 2020-04-28,10:10:00,1.15,1.155,1.15,1.155,10705 2020-04-28,10:11:00,1.1501,1.16,1.1501,1.155,2971 2020-04-28,10:12:00,1.16,1.16,1.155,1.155,1776 2020-04-28,10:13:00,1.16,1.16,1.1515,1.16,3115 2020-04-28,10:14:00,1.16,1.16,1.155,1.16,2321 2020-04-28,10:15:00,1.16,1.16,1.155,1.16,875
Any help is appreciated. Thank you!