Hello,
I'm trying to figure out how to backtest trading options near-after EPS reports.
the algo needs to get a list of symbols and dates (the day before the release of the earnings report), to buy ATM options at a specific date, hour and dollar amount and to sell them at the end of the next day (after the earnings report release).
with the help of Alexandre Catarino I got this far (code sample) but the program chooses only one symbol, buys it and then sells it at expiration date.
So how can I:
1) Trade muiltiple symbols from a list with diffrent buying and selling dates.
2) choose the dollar amount of any position.
3) choose the specific dates to sell every position.
import datetime
import matplotlib.pyplot as plt
import pandas as pd
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
class BasicTemplateOptionsAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetCash(100000)
def init_option(season_tickets, self):
for ind, row in season_tickets.iterrows():
option = self.AddOption(row[0], Resolution.Minute)
option.SetFilter(-3, 0, timedelta(7), timedelta(14))
self.SetBenchmark(row[0])
self.Schedule.On(
self.DateRules.On(int(row[6]),int(row[5]),int(row[4])),
self.TimeRules.At(10,00),
self.BuyCall)
self.option_symbol = option.Symbol
#download EPS csv file from google drive and creating dataframe
url = "https://www.dropbox.com/s/4vtx2nn6jzty7lc/CND%20EPS%20data.csv?dl=1"
file = self.Download(url).split("\n")
extract_data = [x.split(',') for x in file]
EPS_data = pd.DataFrame(extract_data)
EPS_data.columns = EPS_data.iloc[0]
EPS_data = EPS_data.drop(EPS_data.index[0])
EPS_data = EPS_data.drop(EPS_data.index[len(EPS_data)-1])
self.symbols = []
self.SetStartDate(2018, 4, 1)
self.SetEndDate(2018, 6, 30)
init_option(EPS_data, self)
def BuyCall(self):
if self.Portfolio.Invested: return
for kvp in self.CurrentSlice.OptionChains:
if kvp.Key != self.option_symbol: continue
chain = kvp.Value
# we sort the contracts to find at the money (ATM) contract with farthest expiration
contracts = sorted(sorted(sorted(chain, \
key = lambda x: abs(chain.Underlying.Price - x.Strike)), \
key = lambda x: x.Expiry, reverse=True), \
key = lambda x: x.Right)
# if found, trade it
if len(contracts) == 0: continue
symbol = contracts[0].Symbol
self.MarketOrder(symbol, 1)
#self.MarketOnCloseOrder(symbol, -1)