-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathGeneticStrategy.py
80 lines (62 loc) · 2.54 KB
/
GeneticStrategy.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
from StrategyHandler import AbstractStrategyHandler
import logging
from deap import gp
from GeneticProgram import GeneticProgram
logger = logging.getLogger('TradingStrategy123')
logger.setLevel(logging.INFO)
class GeneticStrategy(AbstractStrategyHandler, GeneticProgram):
""""
A lambda doge baby.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
GeneticProgram.__init__(self)
self.individual = kwargs['individual']
def make_signal(self) -> int:
from StrategyHandler import BUY, SELL, IGNORE
doge = gp.compile(self.individual, self.pset)
outcome = doge([])
if outcome == self.buy:
self.signal = BUY
elif outcome == self.sell:
self.signal = SELL
elif outcome == self.ignore:
self.signal = IGNORE
else:
raise Exception("Unknown outcome produced by doge baby... singularity?!?")
return self.signal
@property
def used_indicators(self):
return ["RSI", "SMA"] # TODO: add logic to auto-populate from individual
def rsi(self, input):
return self.get_indicator("RSI")
def sma50(self, input):
return self.get_indicator("SMA")
def price(self, input):
return self.get_indicator("price")
# TODO: implement the methods below once the API supports them
def ema50(self, input):
pass
def sma200(self, input):
pass
def ema200(self, input):
pass
def check_strategy_gp(event, context):
logger.info("\n-------------------\n" +
"Trading Strategy GP" +
"\n-------------------")
logger.info('Event: {e}\nContext: {c}'.format(e=event, c=context))
try:
logger.info("initiating objects...............")
mock_individual = 'if_then_else(gt(price(ARG0), sma50(ARG0)), if_then_else(gt(17.64793885409774, 84.48578956778925), ' \
'if_then_else(True, ignore, ignore), sell), if_then_else(and_(True, False), ' \
'if_then_else(True, sell, ignore), if_then_else(True, buy, ignore)))'
mock_individual = 'if_then_else(gt(rsi(ARG0), sma50(ARG0)), buy, sell)'
this_trading_strategy = GeneticStrategy(sns_event=event, individual=mock_individual)
logger.info("running..........................")
this_trading_strategy.run()
logger.info("saving...........................")
this_trading_strategy.save()
except Exception as e:
logger.warning("Exception: {}".format(e))
return