Last active
May 9, 2024 03:06
-
-
Save backtrader/456cf0891a7a62ceb2bbb5e4234c01e7 to your computer and use it in GitHub Desktop.
Interactive Brokers in Python with backtrader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8; py-indent-offset:4 -*- | |
############################################################################### | |
# | |
# Copyright (C) 2018 Daniel Rodriguez | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
# | |
############################################################################### | |
from __future__ import (absolute_import, division, print_function, | |
unicode_literals) | |
import backtrader as bt | |
class St(bt.Strategy): | |
def logdata(self): | |
txt = [] | |
txt.append('{}'.format(len(self))) | |
txt.append('{}'.format(self.data.datetime.datetime(0).isoformat())) | |
txt.append('{:.2f}'.format(self.data.open[0])) | |
txt.append('{:.2f}'.format(self.data.high[0])) | |
txt.append('{:.2f}'.format(self.data.low[0])) | |
txt.append('{:.2f}'.format(self.data.close[0])) | |
txt.append('{:.2f}'.format(self.data.volume[0])) | |
print(','.join(txt)) | |
data_live = False | |
def notify_data(self, data, status, *args, **kwargs): | |
print('*' * 5, 'DATA NOTIF:', data._getstatusname(status), *args) | |
if status == data.LIVE: | |
self.data_live = True | |
def notify_order(self, order): | |
if order.status == order.Completed: | |
buysell = 'BUY ' if order.isbuy() else 'SELL' | |
txt = '{} {}@{}'.format(buysell, order.executed.size, | |
order.executed.price) | |
print(txt) | |
bought = 0 | |
sold = 0 | |
def next(self): | |
self.logdata() | |
if not self.data_live: | |
return | |
if not self.bought: | |
self.bought = len(self) # keep entry bar | |
self.buy() | |
elif not self.sold: | |
if len(self) == (self.bought + 3): | |
self.sell() | |
def run(args=None): | |
cerebro = bt.Cerebro(stdstats=False) | |
store = bt.stores.IBStore(port=7497) | |
data = store.getdata(dataname='TWTR', timeframe=bt.TimeFrame.Ticks) | |
cerebro.resampledata(data, timeframe=bt.TimeFrame.Seconds, compression=10) | |
cerebro.broker = store.getbroker() | |
cerebro.addstrategy(St) | |
cerebro.run() | |
if __name__ == '__main__': | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Jamesfryer1998
Does it work now? I wonder how to fix it in my Windows PC. Thanks a lot:)