forked from ivanstruk/Backtesting-Pre-Market-Price-Action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests_main.py
154 lines (128 loc) · 4.68 KB
/
tests_main.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
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 19
@author: Ivan Struk
"""
from earnings_parser import get_er_dates
from EOD_parser import hunt_hist
from EOD_parser import get_hist
from EOD_parser import get_hist2
import pandas as pd
def test_sample_earnings(symbol):
stocks = symbol
stockz = []
re1 = []
re2 = []
re3 = []
for i in stocks:
try:
print("Getting historical results for {}".format(i))
dfw = pd.DataFrame(get_er_dates(i))
name = i
length_ = sum(dfw["Binary_E"])
if length_ == 0:
print("Skipping - No Data")
pass
else:
result1 = round(((sum(dfw["PreM and Intra +"])/length_)*1),4)
zero_denom_test = sum(dfw["Pre-Market D+"])
if zero_denom_test == 0:
result2 = 0
continue
else:
result2 = round(((sum(dfw["PreM and Intra +"])/sum(dfw["Pre-Market D+"]))*1),4)
result3 = round(((sum(dfw["PreM+ and Intra -"])/sum(dfw["Pre-Market D+"]))*1),4)
stockz.append(name)
re1.append(result1)
re2.append(result2)
re3.append(result3)
except KeyError:
print("{} is not registered in SEC EDGAR or does not exist".format(i))
pass
stock_parser = {
"Symbol" : []
}
results_df = pd.DataFrame(stock_parser)
results_df["Symbol"] = stockz
results_df["- increases in both premarket and intraday x% of the time."] = re1
results_df["- if increasing in premarket has a x% chance of increasing in intraday."] = re2
results_df["- if increasing in premarket has a x% change of falling in intraday."] = re3
return results_df
def test_sample_contribution(symbol):
stocks = symbol
stock_names = []
premarket_att = []
print("Crunching numbers ... (this may take a while)")
for i in stocks:
df_sub = pd.DataFrame(get_hist(i))
df = df_sub.fillna(value=0)
result = sum(df["ABS PM CHN / ABS ID + PM"])/len(df["ABS PM CHN / ABS ID + PM"])
name = i
print(name, result)
stock_names.append(name)
premarket_att.append(result)
stock_parser = {
"Symbol" : [],
"Result" : []
}
results_df = pd.DataFrame(stock_parser)
results_df["Symbol"] = stock_names
results_df["Result"] = premarket_att
average_premarket_weight = sum(results_df["Result"])/len(results_df["Result"])
print("On average premarket trading contributes to ",
(round(average_premarket_weight,4)*100),
"% of daily price action within the selected sample.")
print(" ")
return results_df
def test_sample_indication(symbol):
stock_names = []
results = []
stocks = symbol
print("This may take a while if you have defined a large number of stocks")
for i in stocks:
df = pd.DataFrame(get_hist2(i))
#df = df_sub.fillna(value=0)
result = round((sum(df["H: Both True"]))/(len(list(df["H: Both True"]))),4)
name = i
print(name, result)
stock_names.append(name)
results.append(result)
stock_parser = {
"Symbol" : [],
"Result" : []
}
results_df = pd.DataFrame(stock_parser)
results_df["Symbol"] = stock_names
results_df["Result"] = results
final_result = sum(results_df["Result"])/len(results_df["Result"])
print("A stock increasing or decreasing in value during premarket has a ",
(round(final_result,4)*100),
"% probability of maintaining price action in the same direction.")
return results_df
def test_sample_long(symbol):
stocks = symbol
stock_names = []
results = []
print("This may take a while if you have defined a large number of stocks.")
for i in stocks:
df = pd.DataFrame(hunt_hist(i))
#df = df_sub.fillna(value=0)
result = round((sum(df["Temp Calc1"]))/(sum(df["Pre-Market D+"])),4)
name = i
print(name, result)
stock_names.append(name)
results.append(result)
stock_parser = {
"Symbol" : [],
"Result" : []
}
results_df = pd.DataFrame(stock_parser)
results_df["Symbol"] = stock_names
results_df["Result"] = results
print("Result:")
final_result = sum(results_df["Result"])/len(results_df["Result"])
print("On average for the provided sample, "
"a stock increasing in value during premarket has a ",
(round(final_result,4)*100),
"% probability of further increasing in intraday.")
return results_df