-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStrategy.py
80 lines (62 loc) · 2.28 KB
/
Strategy.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
import numpy as np
import Indicators
import yfinance as yf
def data(symbol, timeframe, sma_len):
# get our data
# valid downloadable periods: 1d,5d,1mo,3mo,6mo,1y,2y,5y,10y,ytd,max
df = yf.download(symbol, period='1y', interval=timeframe)
df['SMA'] = Indicators.SMA(df['Close'], int(sma_len))
# should you wish to use another strategy
# add your indicators here
buy = []
sell = []
tpbuy = []
tpsell = []
for i in range(0, len(df['Close'])):
buy.append(np.nan)
tpbuy.append(np.nan)
sell.append(np.nan)
tpsell.append(np.nan)
df['Long'] = buy
df['Short'] = sell
df['TP Long'] = tpbuy
df['TP Short'] = tpsell
# strategy in backtest
long = []
short = []
tpLong = []
tpShort = []
isLong = False
isShort = False
isTPLong = False
isTPShort = False
'''hereunder an example of strategy using simple moving average in the last X periods (SMA)
you may change with your own strategy
buy orders when current SMA is above last SMA
sell orders when current SMA is below last SMA
creation of a list for backtesting and chart purpose for every buy, sell, take profit buy, take profit sell orders
including time and price value at candle close'''
for i in range(1, len(df['Close'])):
sma = df['SMA'][i]
sma1 = df['SMA'][i-1]
if not isLong and sma > sma1:
long.append([df.index[i], df['Close'][i]])
df['Long'][i] = df['Close'][i]
isLong = True
isTPLong = False
elif isLong and not isTPLong and sma < sma1:
tpLong.append([df.index[i], df['Close'][i]])
df['TP Long'][i] = df['Close'][i]
isLong = False
isTPLong = True
elif not isShort and sma < sma1:
short.append([df.index[i], df['Close'][i]])
df['Short'][i] = df['Close'][i]
isShort = True
isTPShort = False
elif isShort and not isTPShort and sma > sma1:
tpShort.append([df.index[i], df['Close'][i]])
df['TP Short'][i] = df['Close'][i]
isShort = False
isTPShort = True
return df, long, short, tpLong, tpShort