-
Notifications
You must be signed in to change notification settings - Fork 3
/
printcondordata_retire20150910.py
255 lines (232 loc) · 12.9 KB
/
printcondordata_retire20150910.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# -*- coding: utf-8 -*-
"""
Created on Sat May 9 16:43:23 2015
@author: jmalinchak
"""
class perform:
def __init__(self,
symbol = 'SPY'
, numberofweeksahead = 1
, expirationday = 'friday' #'friday' #'wednesday for index'
):
self.find(symbol
, numberofweeksahead
, expirationday
)
def dayofweek_int(self,dayofweek_word):
rv = int(-1)
if dayofweek_word.lower() == 'friday':
rv = int(4)
if dayofweek_word.lower() == 'saturday':
rv = int(5)
if dayofweek_word.lower() == 'sunday':
rv = int(6)
if dayofweek_word.lower() == 'monday':
rv = int(0)
if dayofweek_word.lower() == 'tuesday':
rv = int(1)
if dayofweek_word.lower() == 'wednesday':
rv = int(2)
if dayofweek_word.lower() == 'thursday':
rv = int(3)
return rv
def find(self
, symbol = 'SPY'
, numberofweeksahead = 1
, expirationday = 'friday' #'friday' #'wednesday for index'
):
import config
import pandas as pd
# ##########
# Parameters
# symbol = 'SPY'
# mycomparesym = '^VIX'
# numberofweeksahead = 8
# expirationday = 'friday' #'friday' #'wednesday for index'
# daysbackmid = 0
# myspreadindollars = 1
# mycumprobthreshold = 80 #Percent in whole number 80 = 80%
# mycumprob_to_sell_price_lowrange = 0
# mycumprob_to_sell_price_highrange = 100 # was 95
# numberofweekstolookback = 150
# RollingNumberOfPeriods = 120
# showresults = 0
myoutputfolder = config.myoutputfolder #'C:\\Batches\\rts\\output\\condor\\candidates'
#mycandidatesfolder = config.mycandidatesfolder #'C:\\Batches\\rts\\output\\condor\\candidates'
#mysourcedatafolder = config.mysourcedatafolder #'C:\\Batches\\rts\\output\\condor\\candidates'
#import csv
import os
#candidatesfolderwithsymbol = os.path.join(mycandidatesfolder,symbol)
#sourcedatafolderwithsymbol = os.path.join(mysourcedatafolder,symbol)
import mytools
#mygeneral = mytools.general()
#mygeneral.make_sure_path_exists(candidatesfolderwithsymbol) #candidatesfolderwithsymbol
#mygeneral.make_sure_path_exists(sourcedatafolderwithsymbol) #sourcedatafolderwithsymbol
## ##########
## Date setup
#import datetime
#
#today_datetime = datetime.datetime.today()
#today_date = datetime.date.today()
# ##########
# Date setup
import datetime
today_datetime = datetime.datetime.today()
today_date = datetime.date.today()
today_datestring = today_date.strftime('%Y-%m-%d')
iter_date = today_date
for expirationcounter in range(numberofweeksahead):
if expirationcounter + 1 == numberofweeksahead:
print('expirationcounter',expirationcounter)
print(' range(numberofweeksahead)', range(numberofweeksahead))
while iter_date.weekday() != self.dayofweek_int(expirationday):
iter_date += datetime.timedelta(1)
expirationdate_string = str(iter_date)
expirationdatetime_string = str(iter_date)+' 16:00'
#print('expirationdatetime_string',expirationdatetime_string)
iter_date += datetime.timedelta(1)
print('Doing...',expirationcounter,expirationdate_string)
while True :
# today_date = today_date
expire_date = datetime.datetime.strptime(expirationdate_string,'%Y-%m-%d').date()
expire_datetime = datetime.datetime.strptime(expirationdatetime_string,'%Y-%m-%d %H:%M')
#print('expire_datetime',expire_datetime)
if today_date != expire_date:
break
today_date = today_date - datetime.timedelta(hours=24)
delta_precise = expire_datetime - today_datetime
delta_precise_in_seconds = delta_precise.total_seconds()
delta_precise_in_minutes = delta_precise_in_seconds/60
delta_precise_in_hours = delta_precise_in_seconds/60.0/60.0
#print('delta_precise',delta_precise)
print('delta_precise_in_seconds',delta_precise_in_seconds)
delta = expire_date - today_date
# ####################################################
# Get Option Prices
pricingsymbol = symbol
if pricingsymbol in ['VIX','RUT']:
pricingsymbol = '^'+symbol
import pullprices as pp
df_optionpricescurrent = pp.options_to_dataframe(pricingsymbol,expirationdate_string,0)
#df_stockprice_realtime_to_dataframe = pp.stockprice_realtime_to_dataframe(pricingsymbol)
#print('$$$$$$$$$$$$$ cvcvcvcvc $$$$$$$$$$$$$$$$$')
#print(df_optionpricescurrent)
if len(df_optionpricescurrent) == 0:
print('-:-:-:-:-:--:-:-:-:-:--:-:-:-:-:- no option prices found for',pricingsymbol,expirationdate_string)
else:
# ########
# Initialize notes
print('Initialized:','calculatecumulativeprobabilityofpricechangebasedonexpiration.py')
print('-----------')
print('Symbol:',symbol)
print(' Now:',today_datetime)
today_datetimestring = today_datetime.strftime('%Y-%m-%d %H:%M:%S')
print(' Now string',today_datetimestring)
print(' Expire Date:',expire_date )
print(' Number of Days to Expiration:',delta.days)
print(' Number of Hours to Expiration:',delta_precise_in_hours)
print(' Number of minutes to expiration:',delta_precise_in_minutes)
#lastprice = df_stockprice_realtime_to_dataframe.iloc[0]['last']
#time = df_stockprice_realtime_to_dataframe.iloc[0]['time']
# print(df_optionpricescurrent)
# print(df_optionpricescurrent.iloc[0])
# print(type(df_optionpricescurrent.iloc[0]))
stockprice = df_optionpricescurrent.iloc[0]['stockprice']
time = df_optionpricescurrent.iloc[0]['time']
print(' Last stock trade(price+time):',stockprice,time)
#Evaluate spreads
#print(df_optionpricescurrent)
maxstrikecall = 0
maxstrikeput = 10000
df_optionpricesindexed = df_optionpricescurrent.set_index(['optionsymbol'])
for index, row in df_optionpricesindexed.iterrows():
seriesshort = row
#print(row)
optiontype = mytools.get_from_optionsymbol.optiontype(index)
optionsymbolshort = ''
if optiontype == 'C':
iter_offset = 1
if float(row.strike) > float(stockprice):
optionsymbolshort = index
if optiontype == 'P':
iter_offset = -1
if float(row.strike) < float(stockprice):
optionsymbolshort = index
if len(optionsymbolshort) > 0:
optionsymbollong = mytools.get_from_optionsymbol.offsetstrikeoptionsymbol(index,iter_offset)
if optionsymbollong in df_optionpricesindexed.index:
serieslong = df_optionpricesindexed.loc[optionsymbollong]
shortmid = (float(seriesshort.bid) + float(seriesshort.ask))/2
longmid = (float(serieslong.bid) + float(serieslong.ask))/2
midspread = round(shortmid - longmid,2)
deltapct = round((float(seriesshort.strike) - float(stockprice)) / float(stockprice),3)
if midspread > 0.20:
if optiontype == 'C':
if float(seriesshort.strike) > maxstrikecall:
maxstrikecall = float(seriesshort.strike)
ls_bestcall = [optionsymbolshort,seriesshort.strike, serieslong.strike,midspread,deltapct]
if optiontype == 'P':
if float(seriesshort.strike) < maxstrikeput:
maxstrikeput = float(seriesshort.strike)
ls_bestput = [optionsymbolshort,seriesshort.strike, serieslong.strike,midspread,deltapct]
print(ls_bestcall)
print(ls_bestput)
# for optionsymbol in df_optionpricescurrent['optionsymbol']:
# #df.append(Function(x))
# iter_strike = mytools.get_from_optionsymbol.strike(optionsymbol)
# iter_optiontype = mytools.get_from_optionsymbol.optiontype(optionsymbol)
# if iter_optiontype == 'C':
# if iter_strike > stockprice:
# print(iter_optiontype,iter_strike,optionsymbol)
# print(df_optionpricesindexed.loc[optionsymbol])
'''@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
make dataframe
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'''
dataframerows = []
dataframeheader = [
'datestamp',
'symbol','stockprice','time'
, 'callsymbol','putsymbol'
, 'capturecall','captureput'
, 'deltapctcall','deltapctput'
, 'mintoexp'
]
dataframerows.append(dataframeheader)
dataframerows.append([
today_datetimestring,symbol,stockprice,time
, ls_bestcall[0],ls_bestput[0]
, ls_bestcall[3],ls_bestput[3]
, ls_bestcall[4],ls_bestput[4]
, delta_precise_in_minutes
])
headers = dataframerows.pop(0)
df = pd.DataFrame(dataframerows,columns=headers)
df_2 = df.set_index('datestamp')
print(df_2)
'''@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
output to CSV
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'''
filelocation_string = myoutputfolder + "\\intraday " + today_datestring + ' ' + symbol +'.csv'
if os.path.exists(filelocation_string) != True:
df_2.to_csv(filelocation_string,columns=(
# 'datestamp',
'symbol','stockprice','time'
, 'callsymbol','putsymbol'
, 'capturecall','captureput'
, 'deltapctcall','deltapctput'
, 'mintoexp'
))
else:
with open(filelocation_string, 'a') as f:
df_2.to_csv(f, header=False)
#print(today_datetime.weekday())
#myoutputfolder
if __name__=='__main__':
#o = perform(symbol='NFLX',numberofweeksahead=1)
import sys
print(len(sys.argv))
if len(sys.argv) > 1:
o = perform(symbol=sys.argv[1],numberofweeksahead=1)
else:
o = perform(symbol='nflx',numberofweeksahead=1)
#main(sys.argv[1:])