-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathbackTester.py
142 lines (126 loc) · 7.27 KB
/
backTester.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import csv
import datetime
import decimal
import logging
import queue
from dataHandler import csvData
from events import event as event_class
from riskManager import putVerticalRiskManagement
from strategyManager import putVerticalStrat
from portfolioManager import portfolio
from collections import defaultdict
"""
This file runs the end-to-end backtesting session.
"""
class BackTestSession(object):
"""Class for holding all parameters of backtesting session."""
def __init__(self):
# Create queue to hold events (ticks, signals, etc.).
self.eventQueue = queue.Queue()
# Create CsvData class object.
dataProviderPath = './dataHandler/dataProviders.json'
dataProvider = 'iVolatility'
filename ='./sampleData/spx_sample_ivolatility.csv'
self.dataHandler = csvData.CsvData(csvPath=filename, dataProviderPath=dataProviderPath,
dataProvider=dataProvider, eventQueue=self.eventQueue)
# Parameters for strategy.
startDateTime = '01/01/1990'
startDateTimeFormatted = datetime.datetime.strptime(startDateTime, '%m/%d/%Y')
# Save maxCapitalToUse in the session since the run function requires it.
self.maxCapitalToUse = decimal.Decimal(0.75) # Up to 75% of net liq can be used in trades.
maxCapitalToUsePerTrade = decimal.Decimal(0.40) # 40% max capital to use per trade / strategy.
startingCapital = 1000000
strategyName = 'PUT_VERTICAL_STRAT'
riskManagement = 'HOLD_TO_EXPIRATION'
closeDuration = 0 # Number of days from expiration to close the trade.
optPutToBuyDelta = -0.01
maxPutToBuyDelta = -0.1
minPutToBuyDelta = -0.005
optPutToSellDelta = -0.25
maxPutToSellDelta = -0.30
minPutToSellDelta = -0.11
underlyingTicker = 'SPX'
orderQuantity = 1
contractMultiplier = 100
optimalDTE = 25
minimumDTE = 20
maximumDTE = 55
maxBidAsk = decimal.Decimal(15) # Set to a large value to effectively disable.
minCreditDebit = decimal.Decimal(1.00)
# Set up portfolio and position monitoring.
self.positionMonitoring = defaultdict(list)
pricingSource = 'tastyworks'
pricingSourceConfigFile = './dataHandler/pricingConfig.json'
self.portfolioManager = portfolio.Portfolio(decimal.Decimal(startingCapital), self.maxCapitalToUse,
maxCapitalToUsePerTrade, positionMonitoring=self.positionMonitoring)
if strategyName != 'PUT_VERTICAL_STRAT':
raise ValueError('Strategy not supported.')
else:
# TODO(msantoro): If statements below should use polymorphism where the riskManagement.py has all of the
# base risk management types.
if riskManagement == 'HOLD_TO_EXPIRATION':
riskManagement = putVerticalRiskManagement.PutVerticalManagementStrategyTypes.HOLD_TO_EXPIRATION
elif riskManagement == 'CLOSE_AT_50_PERCENT':
riskManagement = putVerticalRiskManagement.PutVerticalManagementStrategyTypes.CLOSE_AT_50_PERCENT
elif riskManagement == 'CLOSE_AT_50_PERCENT_OR_21_DAYS':
riskManagement = putVerticalRiskManagement.PutVerticalManagementStrategyTypes.CLOSE_AT_50_PERCENT_OR_21_DAYS
elif riskManagement == 'CLOSE_AT_50_PERCENT_OR_21_DAYS_OR_HALFLOSS':
riskManagement = putVerticalRiskManagement.PutVerticalManagementStrategyTypes.CLOSE_AT_50_PERCENT_OR_21_DAYS_OR_HALFLOSS
elif riskManagement == 'CLOSE_AT_21_DAYS':
riskManagement = putVerticalRiskManagement.PutVerticalManagementStrategyTypes.CLOSE_AT_21_DAYS
else:
raise ValueError('Risk management type not supported.')
if closeDuration <= 0:
closeDuration = None
riskManagementStrategy = putVerticalRiskManagement.PutVerticalRiskManagement(riskManagement, closeDuration)
self.strategyManager = putVerticalStrat.PutVerticalStrat(
self.eventQueue, optPutToBuyDelta, maxPutToBuyDelta, minPutToBuyDelta, optPutToSellDelta,
maxPutToSellDelta, minPutToSellDelta, underlyingTicker, orderQuantity, contractMultiplier,
riskManagementStrategy, pricingSource, pricingSourceConfigFile, startDateTimeFormatted, optimalDTE,
minimumDTE, maximumDTE, maxBidAsk=maxBidAsk, maxCapitalToUsePerTrade=maxCapitalToUsePerTrade,
minCreditDebit=minCreditDebit)
# Write params to log file to be able to track experiments.
# Set up logging for the session.
logging.basicConfig(filename='log.log', level=logging.DEBUG)
logging.info(
'optPutToSellDelta: {} maxPutToSellDelta: {} minPutToSellDelta: {} optPutToBuyDelta: {}'
' maxPutToBuyDelta: {} minPutToBuyDelta: {} underlyingTicker: {} orderQuantity: {}'
' optimalDTE: {} minimumDTE: {} maximumDTE: {} maxBidAsk: {} minCredit: {} riskManagement: {}'
' startingCapital: {} self.maxCapitalToUse: {} maxCapitalToUsePerTrade: {}'
' pricingSource: {}'.format(
optPutToSellDelta, maxPutToSellDelta, minPutToSellDelta, optPutToBuyDelta, maxPutToBuyDelta,
minPutToBuyDelta, underlyingTicker, orderQuantity, optimalDTE, minimumDTE, maximumDTE, maxBidAsk,
minCreditDebit, riskManagementStrategy.getRiskManagementType(), startingCapital,
self.maxCapitalToUse, maxCapitalToUsePerTrade, pricingSource))
def run(currentSession):
while 1: # Infinite loop to keep processing items in queue.
try:
event = currentSession.eventQueue.get(False)
except queue.Empty:
# Get data for tick event.
if not currentSession.dataHandler.getNextTick():
# Get out of infinite while loop; no more data available.
break
else:
if event is not None:
if event.type == event_class.EventTypes.TICK:
currentSession.portfolioManager.updatePortfolio(event)
# We pass the net liquidity and available buying power to the strategy.
availableBuyingPower = decimal.Decimal(currentSession.maxCapitalToUse) * (
currentSession.portfolioManager.netLiquidity) - currentSession.portfolioManager.totalBuyingPower
currentSession.strategyManager.checkForSignal(event, currentSession.portfolioManager.netLiquidity,
availableBuyingPower)
elif event.type == event_class.EventTypes.SIGNAL:
currentSession.portfolioManager.onSignal(event)
else:
raise NotImplemented("Unsupported event.type '%s'." % event.type)
if __name__ == "__main__":
# Create a session and configure the session.
session = BackTestSession()
# Run the session.
run(session)
# Write position monitoring to CSV file.
with open('monitoring.csv', 'w') as outfile:
writer = csv.writer(outfile)
writer.writerow(session.positionMonitoring.keys())
writer.writerows(zip(*session.positionMonitoring.values()))