File size: 15,201 Bytes
43f8a47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import gym
import numpy as np
import pandas as pd
from gym import spaces
from market_regime_detector import MarketRegimeDetector, MarketRegime

class TradingEnv(gym.Env):
    """

    Custom Gym environment for XAUUSD trading with trailing stops and dynamic exits.

    """
    def __init__(self, df, initial_balance=1000, transaction_cost=0, leverage=50, stop_loss_pct=0.02):
        super(TradingEnv, self).__init__()

        self.df = df.reset_index(drop=True)
        self.initial_balance = initial_balance
        self.transaction_cost = transaction_cost
        self.leverage = leverage
        self.stop_loss_pct = stop_loss_pct

        # Trailing stop parameters - TIGHTENED for better profit capture
        self.trailing_stop_pct = 0.025  # Reduced from 5% to 2.5% trailing stop
        self.trailing_stop_distance = 0  # Current trailing stop level
        self.highest_price_since_entry = 0  # Track highest price for trailing stops

        # Profit taking parameters - MULTIPLE SCALED TARGETS
        self.profit_targets = [0.01, 0.02, 0.05, 0.10]  # 1%, 2%, 5%, 10% profit targets
        self.take_profit_pct = 0.10  # 10% take profit target (final target)
        self.partial_take_profit_pct = 0.02  # Take partial profits at 2% (reduced from 5%)

        # Breakeven stop parameters
        self.breakeven_trigger_pct = 0.015  # Move to breakeven after 1.5% profit
        self.breakeven_activated = False

        # Dynamic exit parameters
        self.max_holding_period = 24  # Max hours to hold position
        self.entry_time = None

        # Calculate technical indicators
        self._calculate_indicators()

        # Action space: continuous action between -1 and 1
        self.action_space = spaces.Box(low=-1, high=1, shape=(1,), dtype=np.float32)

        # Observation space: price history (last 10 closes), RSI, MACD, MACD_signal, position, balance
        self.lookback = 10
        self.observation_space = spaces.Box(
            low=-np.inf, high=np.inf, shape=(self.lookback + 5,), dtype=np.float32  # 10 prices + RSI + MACD + MACD_signal + position + balance
        )

        self.trades = []  # Log all trades

        self.reset()

    def _calculate_indicators(self):
        """Calculate technical indicators for the dataset"""
        # RSI
        delta = self.df['Close'].diff()
        gain = (delta.where(delta > 0, 0)).rolling(window=14).mean()
        loss = (-delta.where(delta < 0, 0)).rolling(window=14).mean()
        rs = gain / loss
        self.df['RSI'] = 100 - (100 / (1 + rs))

        # MACD
        ema12 = self.df['Close'].ewm(span=12, adjust=False).mean()
        ema26 = self.df['Close'].ewm(span=26, adjust=False).mean()
        self.df['MACD'] = ema12 - ema26
        self.df['MACD_signal'] = self.df['MACD'].ewm(span=9, adjust=False).mean()

        self.df.fillna(0, inplace=True)  # Fill NaN with 0
        self.trailing_stop_distance = 0  # Current trailing stop level
        self.highest_price_since_entry = 0  # Track highest price for trailing stops

        # Initialize market regime detector
        self.regime_detector = MarketRegimeDetector()

        # Dynamic regime-adaptive parameters (will be updated based on current regime)
        self._update_regime_parameters()

        # Add technical indicators

        # Add technical indicators
        self._calculate_indicators()

        # Action space: continuous action between -1 and 1
        self.action_space = spaces.Box(low=-1, high=1, shape=(1,), dtype=np.float32)

        # Observation space: price history (last 10 closes), RSI, MACD, MACD_signal, position, balance
        self.lookback = 10
        self.observation_space = spaces.Box(
            low=-np.inf, high=np.inf, shape=(self.lookback + 5,), dtype=np.float32  # 10 prices + RSI + MACD + MACD_signal + position + balance
        )

        self.trades = []  # Log all trades

        self.reset()

    def reset(self):
        self.current_step = self.lookback
        self.balance = self.initial_balance
        self.position = 0  # 0: no position, positive: long amount
        self.entry_price = 0
        self.total_profit = 0
        self.done = False
        self.trades = []  # Reset trades

        # Reset trailing stop variables
        self.trailing_stop_distance = 0
        self.highest_price_since_entry = 0
        self.entry_time = None

        return self._get_observation()

    def render(self, mode='human'):
        print(f"Step: {self.current_step}, Balance: {self.balance}, Position: {self.position}, Total Profit: {self.total_profit}")

    def _update_regime_parameters(self):
        """Update trading parameters based on current market regime"""
        # Detect current regime
        current_regime, regime_params = self.regime_detector.detect_regime(self.df, self.current_step)

        # Update trading parameters based on regime
        self.profit_targets = regime_params['profit_targets']
        self.trailing_stop_pct = regime_params['trailing_stop_pct']
        self.take_profit_pct = regime_params['profit_targets'][-1]  # Last target is final take profit
        self.partial_take_profit_pct = regime_params['profit_targets'][1]  # Second target for partial profit
        self.breakeven_trigger_pct = regime_params['breakeven_trigger']
        self.max_holding_period = regime_params['max_holding_time']

        # Store current regime for logging
        self.current_regime = current_regime

    def _get_observation(self):
        prices = self.df.loc[self.current_step - self.lookback:self.current_step - 1, 'Close'].values
        rsi = self.df.loc[self.current_step - 1, 'RSI']
        macd = self.df.loc[self.current_step - 1, 'MACD']
        macd_signal = self.df.loc[self.current_step - 1, 'MACD_signal']
        return np.concatenate([prices, [rsi, macd, macd_signal, self.position, self.balance]])

    def step(self, action, confidence=1.0):
        """

        Execute action with confidence-based position sizing and dynamic exits

        confidence: float between 0 and 1, higher = more confident

        """
        # Update regime parameters dynamically
        self._update_regime_parameters()

        current_price = self.df.loc[self.current_step, 'Close']
        reward = 0

        # Apply confidence to action magnitude
        if isinstance(action, np.ndarray):
            action = action[0]

        # Scale action by confidence (minimum confidence threshold)
        min_confidence = 0.3  # Minimum confidence to trade
        if confidence < min_confidence:
            action = 0  # Force hold if not confident enough

        effective_action = action * confidence

        # Check for dynamic exits first (trailing stops, profit taking, time limits)
        exit_reason = self._check_dynamic_exits(current_price)
        if exit_reason:
            profit = (current_price - self.entry_price) * self.position
            self.balance += profit
            self.total_profit += profit

            # Reward based on exit reason
            if exit_reason == 'trailing_stop':
                reward = profit / self.initial_balance * 30  # Moderate reward for trailing stop
            elif exit_reason == 'take_profit':
                reward = profit / self.initial_balance * 100  # High reward for profit taking
            elif exit_reason == 'partial_profit':
                reward = profit / self.initial_balance * 50   # Good reward for partial profits
            elif exit_reason == 'max_time':
                reward = profit / self.initial_balance * 10   # Low reward for time-based exit
            else:
                reward = profit / self.initial_balance * 20   # Default reward

            self.trades.append({
                'step': self.current_step,
                'action': 'exit',
                'reason': exit_reason,
                'price': current_price,
                'profit': profit,
                'confidence': confidence
            })

            # Reset position
            self.position = 0
            self.entry_price = 0
            self.trailing_stop_distance = 0
            self.highest_price_since_entry = 0
            self.entry_time = None

        # Execute new trades if no position
        elif self.position == 0:
            if effective_action > 0.1:  # Buy signal
                # Position size based on action magnitude and confidence
                position_multiplier = min(effective_action, 1.0)
                confidence_multiplier = confidence ** 0.5
                self.position = self.balance * self.leverage * position_multiplier * confidence_multiplier / current_price
                self.entry_price = current_price
                self.entry_time = self.current_step
                self.highest_price_since_entry = current_price
                self.trailing_stop_distance = current_price * (1 - self.trailing_stop_pct)
                self.breakeven_activated = False  # Reset breakeven flag

                self.trades.append({
                    'step': self.current_step,
                    'action': 'buy',
                    'price': current_price,
                    'position': self.position,
                    'confidence': confidence
                })

            elif effective_action < -0.1:  # Sell signal (short)
                # Position size based on action magnitude and confidence
                position_multiplier = min(abs(effective_action), 1.0)
                confidence_multiplier = confidence ** 0.5
                self.position = -self.balance * self.leverage * position_multiplier * confidence_multiplier / current_price
                self.entry_price = current_price
                self.entry_time = self.current_step
                self.highest_price_since_entry = current_price  # For short positions, track lowest
                self.trailing_stop_distance = current_price * (1 + self.trailing_stop_pct)
                self.breakeven_activated = False  # Reset breakeven flag

                self.trades.append({
                    'step': self.current_step,
                    'action': 'sell_short',
                    'price': current_price,
                    'position': self.position,
                    'confidence': confidence
                })

        # Update trailing stops for existing positions
        else:
            self._update_trailing_stops(current_price)

        self.current_step += 1
        if self.current_step >= len(self.df) - 1:
            self.done = True

        next_obs = self._get_observation()
        return next_obs, reward, self.done, {}

    def _check_dynamic_exits(self, current_price):
        """Check for various exit conditions with improved profit-taking"""
        if self.position == 0:
            return None

        # Calculate current profit percentage
        if self.position > 0:  # Long position
            profit_pct = (current_price - self.entry_price) / self.entry_price
        else:  # Short position
            profit_pct = (self.entry_price - current_price) / self.entry_price

        # Breakeven stop activation
        if not self.breakeven_activated and profit_pct >= self.breakeven_trigger_pct:
            self.breakeven_activated = True
            # Move trailing stop to breakeven + small buffer
            buffer_pct = 0.005  # 0.5% buffer above breakeven
            if self.position > 0:
                self.trailing_stop_distance = self.entry_price * (1 + buffer_pct)
            else:
                self.trailing_stop_distance = self.entry_price * (1 - buffer_pct)

        # Scaled profit taking - take partial profits at multiple levels
        for target_pct in sorted(self.profit_targets, reverse=True):
            if profit_pct >= target_pct:
                # Calculate how much profit to take at this level
                if target_pct <= 0.02:  # Small profits (1-2%) - take 25% of position
                    profit_portion = 0.25
                    exit_reason = f'profit_{int(target_pct*100)}pct_partial'
                elif target_pct <= 0.05:  # Medium profits (5%) - take 50% of position
                    profit_portion = 0.50
                    exit_reason = f'profit_{int(target_pct*100)}pct_partial'
                else:  # Large profits (10%) - take full position
                    profit_portion = 1.0
                    exit_reason = 'take_profit'

                if profit_portion < 1.0:
                    # Partial exit
                    self.position *= (1 - profit_portion)
                    # Don't reset trailing stops for partial exits
                else:
                    # Full exit
                    self.position = 0
                    self._reset_position_state()

                return exit_reason

        # Trailing stop check (only if breakeven not activated or profit is positive)
        if self.position > 0:  # Long position
            if current_price <= self.trailing_stop_distance:
                self.position = 0
                self._reset_position_state()
                return 'trailing_stop'
        else:  # Short position
            if current_price >= self.trailing_stop_distance:
                self.position = 0
                self._reset_position_state()
                return 'trailing_stop'

        # Maximum holding time - more aggressive for losing positions
        if self.entry_time and (self.current_step - self.entry_time) >= self.max_holding_period:
            self.position = 0
            self._reset_position_state()
            return 'max_time'

        # Early exit for significant losses (stop loss)
        if profit_pct <= -0.03:  # 3% stop loss
            self.position = 0
            self._reset_position_state()
            return 'stop_loss'

        return None

    def _reset_position_state(self):
        """Reset position-related state variables"""
        self.trailing_stop_distance = 0
        self.highest_price_since_entry = 0
        self.entry_price = 0
        self.entry_time = None
        self.breakeven_activated = False

    def _update_trailing_stops(self, current_price):
        """Update trailing stop levels based on current price"""
        if self.position > 0:  # Long position
            if current_price > self.highest_price_since_entry:
                self.highest_price_since_entry = current_price
                self.trailing_stop_distance = current_price * (1 - self.trailing_stop_pct)
        else:  # Short position
            if current_price < self.highest_price_since_entry:
                self.highest_price_since_entry = current_price
                self.trailing_stop_distance = current_price * (1 + self.trailing_stop_pct)

    def render(self, mode='human'):
        print(f"Step: {self.current_step}, Balance: {self.balance}, Position: {self.position}, Total Profit: {self.total_profit}")