I'm trying to code something to test with OANDA. I want to ensure that any existing orders are closed before I begin.
I'm working with btoandav20. I can't seem to find an example of canceling an order and my guesses aren't getting me anywhere.
Thanks. Code follows.
import backtrader as bt
import btoandav20 as bto
import json
import datetime
import math
''' Order info '''
class TestStrategy(bt.Strategy):
params = (
("period", 20),
("devfactor", 2.5),
("atr_length", 50),
#("size", 9000),
("debug", False),
("verbose", False),
('atr_stop', 1),
('atr_profit', 2)
)
def __init__(self):
self.orders = None
self.prev_size = 0
#Provides any notificaitons about the data.
def notify_data(self, data, status, *args, **kwargs):
#print('*' * 5, 'DATA NOTIF:', data._getstatusname(status), *args)
pass
def notify_store(self, msg, *args, **kwargs):
#print('*' * 5, 'STORE NOTIF:', msg)
pass
def notify_order(self, order):
if order.status in [order.Completed, order.Cancelled, order.Rejected]:
self.order = None
print('-' * 50, 'ORDER BEGIN', datetime.datetime.now())
print(order)
print('-' * 50, 'ORDER END')
def notify_trade(self, trade):
print('-' * 50, 'TRADE BEGIN', datetime.datetime.now())
print(trade)
print('-' * 50, 'TRADE END')
def next(self):
print('trying to cancel orders')
#orders = self.orders()
#print(orders)
for o in self.broker.orders:
print("order info: {} " . format(o))
self.broker.cancel(o)
bt.Order.cancel(o)
#for o in bt.broker.orders:
# print('order info: {} ' .format(o))
txt = list()
#txt.append('Data0')
#txt.append('%04d' % len(self.data0))
dtfmt = '%Y-%m-%dT%H:%M:%S.%f'
#txt.append('{:f}'.format(self.data.datetime[0]))
txt.append('%s' % self.data.datetime.datetime(0).strftime(dtfmt))
txt.append('{:f}'.format(self.data.open[0]))
txt.append('{:f}'.format(self.data.high[0]))
txt.append('{:f}'.format(self.data.low[0]))
txt.append('{:f}'.format(self.data.close[0]))
txt.append('{:6d}'.format(int(self.data.volume[0])))
txt.append('{:d}'.format(int(self.data.openinterest[0])))
#txt.append('{:f}'.format(self.atr[0]))
print(', '.join(txt))
if len(self.datas) > 1 and len(self.data1):
txt = list()
txt.append('Data1')
txt.append('%04d' % len(self.data1))
dtfmt = '%Y-%m-%dT%H:%M:%S.%f'
txt.append('{}'.format(self.data1.datetime[0]))
txt.append('%s' % self.data1.datetime.datetime(0).strftime(dtfmt))
txt.append('{}'.format(self.data1.open[0]))
txt.append('{}'.format(self.data1.high[0]))
txt.append('{}'.format(self.data1.low[0]))
txt.append('{}'.format(self.data1.close[0]))
txt.append('{}'.format(self.data1.volume[0]))
txt.append('{}'.format(self.data1.openinterest[0]))
txt.append('{}'.format(float('NaN')))
print(', '.join(txt))
def start(self):
#if self.order:
# self.order.cancel()
if self.data0.contractdetails is not None:
print('-- Contract Details:')
print(self.data0.contractdetails)
print('Started')
acc_cash = cerebro.broker.getcash()
acc_val = cerebro.broker.getvalue()
print('Account Cash = {}'.format(acc_cash))
print('Account Value = {}'.format(acc_val))
#orders = self.broker.get_orders_open()
#if:
# self.cancel(self.order)
#if orders:
# for order in orders:
# self.broker.cancel(order)
with open("config.json", "r") as file:
config = json.load(file)
storekwargs = dict(
token=config["oanda"]["token"],
account=config["oanda"]["account"],
practice=config["oanda"]["practice"],
#use_positions = True,
#notif_transactions = True,
stream_timeout=2,
account_poll_freq = 1
)
store = bto.stores.OandaV20Store(**storekwargs)
datakwargs = dict(
timeframe=bt.TimeFrame.Minutes,
compression=30,
#tz='Europe/Berlin',
tz = 'UTC',
bidask = False,
backfill=False,
backfill_start=False,
)
data = store.getdata(dataname="EUR_USD", **datakwargs)
#data.resample(
# timeframe=bt.TimeFrame.Minutes,
# compression=30) # rightedge=True, boundoff=1), compression is the minutes or seconds
cerebro = bt.Cerebro()
cerebro.adddata(data)
#broker = bto.brokers.OandaV20Broker()
cerebro.setbroker(store.getbroker())
cerebro.addstrategy(TestStrategy)
cerebro.run()