-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy.py
78 lines (58 loc) · 1.83 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
from config import *
import ccxt
import pandas as pd
import talib
import numpy as np
#Data
def strategy(crypto, type, security):
global key, secret
#reseting buy trigger and strategytype
security = 0
type = 0
exchange = ccxt.binance({
'apiKey': key,
'secret': secret,
})
bars = exchange.fetch_ohlcv(crypto, limit=100)
df = pd.DataFrame(bars[:-1], columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
close = np.array(df['close'])
#Indicators
#Bollingerbands
upperband, middleband, lowerband = talib.BBANDS(close, timeperiod=5, nbdevup=2, matype=0)
if lowerband[-1] > close[-1]:
security += 1
type = 'Bollinger Bands'
elif upperband[-1] < close[-1]:
security -= 1
else:
security *= 1
#Rsi-Index
rsi_value = talib.RSI(close, timeperiod=14)
if rsi_value[-1] < 30:
security += 1
type = 'RSI Index'
elif rsi_value[-1] >= 70:
security -= 1
else:
security *= 1
#MACD
macd, macdsignal, macdhist = talib.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9)
if macd[-1] >= macdsignal[-1] and macdhist[-1] >= 0 and macdhist[-1] < 0.03:
security += 1
type = 'MACD'
elif macd[-1] <= macdsignal[-1] and macdhist[-1] <= 0 and macdhist[-1] < 0.03:
security -= 1
else:
security *= 1
#Average True Range
# atr_value = talib.ATR(np.array(df['high']), np.array(df['low']), np.array(close), timeperiod=14)
# print(atr_value)
#Moving Average (long buy)
ma_value = talib.MA(close, timeperiod=30, matype=0)
difference = close[-1] * 0.95
if ma_value[-1] <= close[-1] and ma_value[-1] >= difference:
security += 1
type = 'MA'
else:
security *= 1
return security