Quantitative Finance Asked by dordal on January 20, 2021
I’ve developed some software which generates sets of trades, and I’d like to backtest those trades. My software currently outputs a CSV file with details of each trade:
2011-03-31,MSFT,Buy,100
2011-04-02,AAPL,Buy,50
2011-05-10,MSFT,Sell,100
Is there any backtesting software out there that lets you bring in a set of your own trades, and see how it would have done? All the software I’ve found so far requires to you write your algos directly in the package, and doesn’t simply let you say ‘Buy X, Sell Y’.
Edit based on comments:
You're not really asking how to backtest a strategy. You already have run a backtest to generate simulated trades. What you're asking for is a way to assess the performance of those simulated trades.
You can do this with the R package blotter. You'll need to setup your account and portfolio, then loop over each row in your CSV and call addTxn
. For example:
trades <- read.csv("trades.csv")
symbols <- unique(trades$symbol)
# Set up a portfolio object and an account object in blotter
initPortf(name='default', symbols=symbols, initDate=initDate)
initAcct(name='default', portfolios='default', initDate=initDate, initEq=initEq)
verbose = TRUE
for(i in 1:NROW(trades)) {
addTxn('default', Symbol=trades$symbol[i], TxnDate=trades$date[i],
TxnPrice={"object-with-price"}, TxnQty=trades$quantity[i], TxnFees=0, verbose=verbose)
}
# Calculate P&L and resulting equity with blotter
updatePortf(Portfolio='default', Dates=CurrentDate)
updateAcct(name='default', Dates=CurrentDate)
updateEndEq(Account='default', Dates=CurrentDate)
# Look at performance
chart.Posn(Portfolio='defaut',Symbol='MSFT')
chart.Posn(Portfolio='defaut',Symbol='AAPL')
Correct answer by Joshua Ulrich on January 20, 2021
TuringTrader (https://www.turingtrader.org/) might be worth a look. Or Zorro (https://www.zorro-trader.com/).
Answered by user42108 on January 20, 2021
With Backtrader in Python should be easy.
For example:
from datetime import datetime
import backtrader as bt
class SmaCross(bt.SignalStrategy):
def __init__(self):
sma1, sma2 = bt.ind.SMA(period=10), bt.ind.SMA(period=30)
crossover = bt.ind.CrossOver(sma1, sma2)
self.signal_add(bt.SIGNAL_LONG, crossover)
cerebro = bt.Cerebro()
cerebro.addstrategy(SmaCross)
data0 = bt.feeds.YahooFinanceData(dataname='MSFT', fromdate=datetime(2011, 1, 1),
todate=datetime(2012, 12, 31))
cerebro.adddata(data0)
cerebro.run()
cerebro.plot()
To use your own CSV data, you can define your own custom feed class, e.g.:
import itertools
...
import backtrader as bt
class MyCSVData(bt.CSVDataBase):
def start(self):
# Nothing to do for this data feed type
pass
def stop(self):
# Nothing to do for this data feed type
pass
def _loadline(self, linetokens):
i = itertools.count(0)
dttxt = linetokens[next(i)]
# Format is YYYY-MM-DD
y = int(dttxt[0:4])
m = int(dttxt[5:7])
d = int(dttxt[8:10])
dt = datetime.datetime(y, m, d)
dtnum = date2num(dt)
self.lines.datetime[0] = dtnum
self.lines.open[0] = float(linetokens[next(i)])
self.lines.high[0] = float(linetokens[next(i)])
self.lines.low[0] = float(linetokens[next(i)])
self.lines.close[0] = float(linetokens[next(i)])
self.lines.volume[0] = float(linetokens[next(i)])
self.lines.openinterest[0] = float(linetokens[next(i)])
return True
Then load your data and add your strategy:
cerebro = bt.Cerebro()
data = MyCSVData(dataname='file.csv', fromdate=datetime(2019, 1, 1), todate=datetime(2019, 2, 28))
cerebro.adddata(data)
cerebro.addstrategy(MyStrategy)
Answered by kenorb on January 20, 2021
With a backtesting library such as Backtesting.py and some Python, you could do something like:
import pandas as pd
trades = pd.read_csv('my_trades.csv',
index_col=0,
parse_dates=True,
infer_datetime_format=True)
buys = trades[trades.iloc[:,1] == 'Buy']
sells = trades[trades.iloc[:,1] == 'Sell']
from backtesting import Strategy
class MyTrades(Strategy):
def next(self):
if self.data.index in buys:
self.buy()
if self.data.index in sells:
self.sell()
Answered by K3---rnc on January 20, 2021
Answering my own question... based on the above comments and a lot of research, it looks like there aren't any packages out there that do this 'out of the box'. So coding your own is the best way to go.
Answered by dordal on January 20, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP