-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathref.py
87 lines (68 loc) · 2.88 KB
/
ref.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
#!/usr/bin/env python3
import math
import random
import yfinance as yf
import pandas as pd
from datetime import date, timedelta
from pandas_datareader import data as pdr
# override yfinance with pandas – seems to be a common step
yf.pdr_override()
# Get stock data from Yahoo Finance – here, asking for about 3 years
today = date.today()
decadeAgo = today - timedelta(days=1095)
# Get stock data from Yahoo Finance – here, Gamestop which had an interesting
#time in 2021: https://en.wikipedia.org/wiki/GameStop_short_squeeze
data = pdr.get_data_yahoo('BP.L', start=decadeAgo, end=today)
print(data.shape)
print(data.Close[200])
# Other symbols: TSLA – Tesla, AMZN – Amazon, ZM – Zoom, ETH-USD – Ethereum-Dollar etc.
# Add two columns to this to allow for Buy and Sell signals
# fill with zero
data['Buy']=0
data['Sell']=0
# Find the signals – uncomment print statements if you want to
# look at the data these pick out in some another way
# e.g. check that the date given is the end of the pattern claimed
for i in range(2, len(data)):
body = 0.01
# Three Soldiers
if (data.Close[i] - data.Open[i]) >= body \
and data.Close[i] > data.Close[i-1] \
and (data.Close[i-1] - data.Open[i-1]) >= body \
and data.Close[i-1] > data.Close[i-2] \
and (data.Close[i-2] - data.Open[i-2]) >= body:
data.at[data.index[i], 'Buy'] = 1
# print("Buy at ", data.index[i])
# Three Crows
if (data.Open[i] - data.Close[i]) >= body \
and data.Close[i] < data.Close[i-1] \
and (data.Open[i-1] - data.Close[i-1]) >= body \
and data.Close[i-1] < data.Close[i-2] \
and (data.Open[i-2] - data.Close[i-2]) >= body:
data.at[data.index[i], 'Sell'] = 1
# print("Sell at ", data.index[i])
# convert data into dictionary of lists
# Store the above in a S3 bucket
'''
return redirect(/'results')
@app.route('/results')
def RandomHandler
return doRender('auditpage.html, risk_values)
'''
# Data now contains signals, so we can pick signals with a minimum amount
# of historic data, and use shots for the amount of simulated values
# to be generated based on the mean and standard deviation of the recent history
# Lambda
minhistory = 3
shots = 10000
for i in range(minhistory, len(data)):
if data.Buy[i]==1: # if we’re interested in Buy signals
mean=data.Close[i-minhistory:i].pct_change(1).mean()
std=data.Close[i-minhistory:i].pct_change(1).std()
# generate much larger random number series with same broad characteristics
simulated = [random.gauss(mean,std) for x in range(shots)]
# sort and pick 95% and 99% - not distinguishing long/short risks here
simulated.sort(reverse=True)
var95 = simulated[int(len(simulated)*0.95)]
var99 = simulated[int(len(simulated)*0.99)]
print(var95, var99) # so you can see what is being produced