• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

freqtrade / freqtrade / 3645506801

pending completion
3645506801

push

github-actions

GitHub
Merge pull request #7818 from freqtrade/dependabot/pip/develop/torch-1.13.0

16503 of 17351 relevant lines covered (95.11%)

0.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.67
/freqtrade/freqai/RL/Base4ActionRLEnv.py
1
import logging
1✔
2
from enum import Enum
1✔
3

4
from gym import spaces
1✔
5

6
from freqtrade.freqai.RL.BaseEnvironment import BaseEnvironment, Positions
1✔
7

8

9
logger = logging.getLogger(__name__)
1✔
10

11

12
class Actions(Enum):
1✔
13
    Neutral = 0
1✔
14
    Exit = 1
1✔
15
    Long_enter = 2
1✔
16
    Short_enter = 3
1✔
17

18

19
class Base4ActionRLEnv(BaseEnvironment):
1✔
20
    """
21
    Base class for a 4 action environment
22
    """
23

24
    def set_action_space(self):
1✔
25
        self.action_space = spaces.Discrete(len(Actions))
1✔
26

27
    def step(self, action: int):
1✔
28
        """
29
        Logic for a single step (incrementing one candle in time)
30
        by the agent
31
        :param: action: int = the action type that the agent plans
32
            to take for the current step.
33
        :returns:
34
            observation = current state of environment
35
            step_reward = the reward from `calculate_reward()`
36
            _done = if the agent "died" or if the candles finished
37
            info = dict passed back to openai gym lib
38
        """
39
        self._done = False
1✔
40
        self._current_tick += 1
1✔
41

42
        if self._current_tick == self._end_tick:
1✔
43
            self._done = True
1✔
44

45
        self._update_unrealized_total_profit()
1✔
46

47
        step_reward = self.calculate_reward(action)
1✔
48
        self.total_reward += step_reward
1✔
49

50
        trade_type = None
1✔
51
        if self.is_tradesignal(action):
1✔
52
            """
53
            Action: Neutral, position: Long ->  Close Long
54
            Action: Neutral, position: Short -> Close Short
55

56
            Action: Long, position: Neutral -> Open Long
57
            Action: Long, position: Short -> Close Short and Open Long
58

59
            Action: Short, position: Neutral -> Open Short
60
            Action: Short, position: Long -> Close Long and Open Short
61
            """
62

63
            if action == Actions.Neutral.value:
1✔
64
                self._position = Positions.Neutral
×
65
                trade_type = "neutral"
×
66
                self._last_trade_tick = None
×
67
            elif action == Actions.Long_enter.value:
1✔
68
                self._position = Positions.Long
1✔
69
                trade_type = "long"
1✔
70
                self._last_trade_tick = self._current_tick
1✔
71
            elif action == Actions.Short_enter.value:
1✔
72
                self._position = Positions.Short
1✔
73
                trade_type = "short"
1✔
74
                self._last_trade_tick = self._current_tick
1✔
75
            elif action == Actions.Exit.value:
1✔
76
                self._update_total_profit()
1✔
77
                self._position = Positions.Neutral
1✔
78
                trade_type = "neutral"
1✔
79
                self._last_trade_tick = None
1✔
80
            else:
81
                print("case not defined")
×
82

83
            if trade_type is not None:
1✔
84
                self.trade_history.append(
1✔
85
                    {'price': self.current_price(), 'index': self._current_tick,
86
                     'type': trade_type})
87

88
        if self._total_profit < 1 - self.rl_config.get('max_training_drawdown_pct', 0.8):
1✔
89
            self._done = True
×
90

91
        self._position_history.append(self._position)
1✔
92

93
        info = dict(
1✔
94
            tick=self._current_tick,
95
            total_reward=self.total_reward,
96
            total_profit=self._total_profit,
97
            position=self._position.value
98
        )
99

100
        observation = self._get_observation()
1✔
101

102
        self._update_history(info)
1✔
103

104
        return observation, step_reward, self._done, info
1✔
105

106
    def is_tradesignal(self, action: int) -> bool:
1✔
107
        """
108
        Determine if the signal is a trade signal
109
        e.g.: agent wants a Actions.Long_exit while it is in a Positions.short
110
        """
111
        return not ((action == Actions.Neutral.value and self._position == Positions.Neutral) or
1✔
112
                    (action == Actions.Neutral.value and self._position == Positions.Short) or
113
                    (action == Actions.Neutral.value and self._position == Positions.Long) or
114
                    (action == Actions.Short_enter.value and self._position == Positions.Short) or
115
                    (action == Actions.Short_enter.value and self._position == Positions.Long) or
116
                    (action == Actions.Exit.value and self._position == Positions.Neutral) or
117
                    (action == Actions.Long_enter.value and self._position == Positions.Long) or
118
                    (action == Actions.Long_enter.value and self._position == Positions.Short))
119

120
    def _is_valid(self, action: int) -> bool:
1✔
121
        """
122
        Determine if the signal is valid.
123
        e.g.: agent wants a Actions.Long_exit while it is in a Positions.short
124
        """
125
        # Agent should only try to exit if it is in position
126
        if action == Actions.Exit.value:
1✔
127
            if self._position not in (Positions.Short, Positions.Long):
1✔
128
                return False
1✔
129

130
        # Agent should only try to enter if it is not in position
131
        if action in (Actions.Short_enter.value, Actions.Long_enter.value):
1✔
132
            if self._position != Positions.Neutral:
1✔
133
                return False
1✔
134

135
        return True
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2024 Coveralls, Inc