-
Notifications
You must be signed in to change notification settings - Fork 10
/
signals.py
executable file
·88 lines (71 loc) · 4.67 KB
/
signals.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
from config import config
from math import isnan
# Signals are defined in alphabetical order
class signals:
def buy_sma_crossover_rsi( self, ticker, data ):
# Moving Average Crossover with RSI Filter
# Credits: https://trader.autochartist.com/moving-average-crossover-with-rsi-filter/
# Buy when Fast-SMA crosses Slow-SMA from below, and stays above for 3 consecutive readings, and RSI > buy threshold (50 suggested)
return(
# Make sure the data is valid
not isnan( data.iloc[ -1 ][ ticker + '_SMA_F' ] ) and
not isnan( data.iloc[ -2 ][ ticker + '_SMA_F' ] ) and
not isnan( data.iloc[ -3 ][ ticker + '_SMA_F' ] ) and
not isnan( data.iloc[ -4 ][ ticker + '_SMA_F' ] ) and
not isnan( data.iloc[ -1 ][ ticker + '_SMA_S' ] ) and
not isnan( data.iloc[ -2 ][ ticker + '_SMA_S' ] ) and
not isnan( data.iloc[ -3 ][ ticker + '_SMA_S' ] ) and
not isnan( data.iloc[ -4 ][ ticker + '_SMA_S' ] ) and
not isnan( data.iloc[ -1 ][ ticker + '_RSI' ] ) and
# Fast-SMA crossed Slow-SMA and stays above
data.iloc[ -1 ][ ticker + '_SMA_F' ] >= data.iloc[ -1 ][ ticker + '_SMA_S' ] and
data.iloc[ -2 ][ ticker + '_SMA_F' ] >= data.iloc[ -2 ][ ticker + '_SMA_S' ] and
data.iloc[ -3 ][ ticker + '_SMA_F' ] >= data.iloc[ -3 ][ ticker + '_SMA_S' ] and
data.iloc[ -4 ][ ticker + '_SMA_F' ] < data.iloc[ -4 ][ ticker + '_SMA_S' ] and
# ... and they diverge
data.iloc[ -1 ][ ticker + '_SMA_F' ] - data.iloc[ -1 ][ ticker + '_SMA_S' ] >= data.iloc[ -2 ][ ticker + '_SMA_F' ] - data.iloc[ -2 ][ ticker + '_SMA_S' ] and
# RSI above threshold
data.iloc[ -1 ][ ticker + '_RSI' ] > config[ 'rsi_threshold' ][ 'buy' ]
)
def buy_sma_rsi_threshold( self, ticker, data ):
# Simple Fast-SMA and RSI
# Buy when price is below Fast-SMA and RSI is below threshold
return (
not isnan( data.iloc[ -1 ][ ticker + '_SMA_F' ] ) and
not isnan( data.iloc[ -1 ][ ticker + '_RSI' ] ) and
# Is the current price below the Fast-SMA by the percentage defined in the config file?
data.iloc[ -1 ][ ticker ] <= data.iloc[ -1 ][ ticker + '_SMA_F' ] - ( data.iloc[ -1 ][ ticker + '_SMA_F' ] * config[ 'buy_below_moving_average' ] ) and
# RSI below the threshold
data.iloc[ -1 ][ ticker + '_RSI' ] <= config[ 'rsi_threshold' ][ 'buy' ]
)
def sell_above_buy( self, asset, data ):
# Simple percentage
return (
data.iloc[ -1 ][ asset.ticker ] > asset.price + ( asset.price * config[ 'profit_percentage' ] )
)
def sell_sma_crossover_rsi( self, asset, data ):
# Moving Average Crossover with RSI Filter
# Credits: https://trader.autochartist.com/moving-average-crossover-with-rsi-filter/
return(
# Make sure the data is valid
not isnan( data.iloc[ -1 ][ asset.ticker + '_SMA_F' ] ) and
not isnan( data.iloc[ -2 ][ asset.ticker + '_SMA_F' ] ) and
not isnan( data.iloc[ -3 ][ asset.ticker + '_SMA_F' ] ) and
not isnan( data.iloc[ -4 ][ asset.ticker + '_SMA_F' ] ) and
not isnan( data.iloc[ -1 ][ asset.ticker + '_SMA_S' ] ) and
not isnan( data.iloc[ -2 ][ asset.ticker + '_SMA_S' ] ) and
not isnan( data.iloc[ -3 ][ asset.ticker + '_SMA_S' ] ) and
not isnan( data.iloc[ -4 ][ asset.ticker + '_SMA_S' ] ) and
not isnan( data.iloc[ -1 ][ asset.ticker + '_RSI' ] ) and
# Fast-SMA crossed Slow-SMA and stays above
data.iloc[ -1 ][ asset.ticker + '_SMA_F' ] <= data.iloc[ -1 ][ asset.ticker + '_SMA_S' ] and
data.iloc[ -2 ][ asset.ticker + '_SMA_F' ] <= data.iloc[ -2 ][ asset.ticker + '_SMA_S' ] and
data.iloc[ -3 ][ asset.ticker + '_SMA_F' ] <= data.iloc[ -3 ][ asset.ticker + '_SMA_S' ] and
data.iloc[ -4 ][ asset.ticker + '_SMA_F' ] > data.iloc[ -4 ][ asset.ticker + '_SMA_S' ] and
# ... and they diverge
data.iloc[ -1 ][ ticker + '_SMA_S' ] - data.iloc[ -1 ][ ticker + '_SMA_F' ] >= data.iloc[ -2 ][ ticker + '_SMA_S' ] - data.iloc[ -2 ][ ticker + '_SMA_F' ] and
# RSI below threshold
data.iloc[ -1 ][ ticker + '_RSI' ] <= config[ 'rsi_threshold' ][ 'sell' ] and
# Price is greater than purchase price by at least profit percentage
data.iloc[ -1 ][ asset.ticker ] >= asset.price + ( asset.price * config[ 'profit_percentage' ] )
)