-
Notifications
You must be signed in to change notification settings - Fork 24
/
sell_signal_agent.py
56 lines (43 loc) · 2.08 KB
/
sell_signal_agent.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
from agent import Agent
from model import SellSignalModel
class SellSignalAgent(Agent):
state = None # save the state to be trained
action = None # save the action needed to pass to fit method
def __init__(self, environment):
super().__init__(environment)
# high turning point 5*8, low turning point 5*8, technical indicator 4*8, profit 8
self.model = SellSignalModel(2, [120], 50, str(self.__class__.__name__))
def process_action(self, sell_action, last_state_date):
market_data = self.environment.get_market_data_by_date(last_state_date)
# get next day for training
next_day = self.environment.get_next_day(last_state_date)
if (market_data is None) or (next_day is None):
# terminated
return False
# for training
next_state = self.environment.get_sell_signal_states_by_date(self.environment.get_buy_price(), next_day)
close = market_data['Close']
roc = market_data['rate_of_close']
if close is None:
# terminate
return False
profit = (self.environment.get_buy_price() - close) / close
if sell_action or (profit > 0.3) or (profit < -0.2):
# force sell signal agent to sell if profit is in certain condition, or sell action
self.environment.invoke_sell_order_agent()
else:
reward = 10 * roc
if not self.environment.get_evaluation_mode():
self.model.fit(self.state.value, reward, sell_action, next_state.value)
return True
def process_next_state(self, date):
# print("Sell signal - processing date: " + str(date))
self.state, sell_action = self.produce_state_and_get_action(date)
if self.state is None or sell_action is None:
# stop training
self.environment.process_epoch_end(None, True)
else:
this_state_date = self.state.date
result = self.process_action(sell_action, this_state_date)
if not result:
self.environment.process_epoch_end(None, True)