Skip to content
TheChatArchiveThe Chat Archive
o1-mini-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

INT NOT PLOTITNG SAYING NO HISTRY # -- coding: utf-8 -- """ Created on Mon Nov 11…

  1. User

    INT NOT PLOTITNG SAYING NO HISTRY # -- coding: utf-8 -- """ Created on Mon Nov 11 17:03:35 2024

    @author: atom """

    import MetaTrader5 as mt5 import pandas as pd import numpy as np import gym from stable_baselines3 import PPO from gym import spaces import matplotlib.pyplot as plt import warnings import datetime

    warnings.filterwarnings("ignore")

    if not mt5.initialize(): print("MetaTrader5 initialization failed") quit() else: print("MetaTrader5 initialized successfully.")

    symbol = "EURUSD" timeframe = mt5.TIMEFRAME_M15 short_window = 10 long_window = 50 sl_percent = 0.005 # Example SL percentage (0.5%) tp_percent = 0.020 # Example TP percentage (2%)

    class MATradingEnv(gym.Env):

    metadata = {'render.modes': ['human']}
    
    def __init__(self, symbol, timeframe, short_window, long_window, data=None):
        super(MATradingEnv, self).__init__()
        
        self.symbol = symbol
        self.timeframe = timeframe
        self.short_window = short_window
        self.long_window = long_window
        
        self.action_space = spaces.Discrete(3)
        
        self.observation_space = spaces.Box(low=-np.inf, high=np.inf, shape=(3,), dtype=np.float32)
        
        if data is None:
            self.data = self._fetch_data()
        else:
            self.data = data
        
        self.data['SMA_short'] = self.data['close'].rolling(window=self.short_window).mean()
        self.data['SMA_long'] = self.data['close'].rolling(window=self.long_window).mean()
        self.data.dropna(inplace=True)  
    
        self.reset()
    
    def _fetch_data(self):
        num_bars = 100 
        start_pos = 100  # higher values fetch older data
    
        print(f"Attempting to fetch {num_bars} bars for symbol '{self.symbol}' and timeframe '{self.timeframe}' starting from position {start_pos}.")
    
        rates = mt5.copy_rates_from_pos(self.symbol, self.timeframe, start_pos, num_bars)
        
        if rates is None or len(rates) == 0:
            print("Failed to fetch data using copy_rates_from_pos. Trying alternative method using copy_rates_range.")
            print("Latest MT5 error:", mt5.last_error())
            return self._fetch_data_range()
        
        df = pd.DataFrame(rates)
        df['time'] = pd.to_datetime(df['time'], unit='s')
        print(f"Successfully fetched {len(df)} bars of data.")
        return df
    
    def _fetch_data_range(self):
        utc_from = datetime.datetime(2023, 1, 1)
        utc_to = datetime.datetime.now()
        print(f"Fetching data from {utc_from} to {utc_to} for symbol '{self.symbol}' on timeframe '{self.timeframe}'.")
        
        rates = mt5.copy_rates_range(self.symbol, self.timeframe, utc_from, utc_to)
        
        if rates is None or len(rates) == 0:
            print("Failed to fetch data using copy_rates_range.")
            print("Latest MT5 error:", mt5.last_error())
            raise ValueError("No data could be fetched from MT5 using both copy_rates_from_pos and copy_rates_range.")
        
        df = pd.DataFrame(rates)
        df['time'] = pd.to_datetime(df['time'], unit='s')
        print(f"Successfully fetched {len(df)} bars of data from {utc_from} to {utc_to}.")
        return df
    
    def reset(self):
        self.current_step = 0
        self.position = 0  
        self.entry_price = 0.0
        self.sl = 0.0
        self.tp = 0.0
        self.balance = 10000 
        self.max_steps = len(self.data) - 1
        self.trade_history = []
        self.current_trade = None 
        return self._get_observation()
    
    def _get_observation(self):
        current_data = self.data.iloc[self.current_step]
        return np.array([
            current_data['close'],
            current_data['SMA_short'],
            current_data['SMA_long']
        ], dtype=np.float32)
    
    def step(self, action):
        reward = 0
        done = False
        info = {}
        
        current_price = self.data.iloc[self.current_step]['close']
        sma_short = self.data.iloc[self.current_step]['SMA_short']
        sma_long = self.data.iloc[self.current_step]['SMA_long']
        time = self.data.iloc[self.current_step]['time']
        
        if action == 1: 
            if self.position == 0:
                self.position = 1
                self.entry_price = current_price
                self.sl = current_price * (1 - sl_percent)
                self.tp = current_price * (1 + tp_percent)
                self.current_trade = {
                    'entry_time': time,
                    'entry_price': current_price,
                    'type': 'BUY',
                    'stop_loss': self.sl,
                    'take_profit': self.tp,
                    'exit_time': None,
                    'exit_price': None,
                    'result': None
                }
                self.trade_history.append(self.current_trade)
                print(f"{time}: Entering LONG at {self.entry_price} with SL: {self.sl}, TP: {self.tp}")
        elif action == 2: 
            if self.position == 0:
                self.position = -1
                self.entry_price = current_price
                self.sl = current_price * (1 + sl_percent)
                self.tp = current_price * (1 - tp_percent)
                self.current_trade = {
                    'entry_time': time,
                    'entry_price': current_price,
                    'type': 'SELL',
                    'stop_loss': self.sl,
                    'take_profit': self.tp,
                    'exit_time': None,
                    'exit_price': None,
                    'result': None
                }
                self.trade_history.append(self.current_trade)
                print(f"{time}: Entering SHORT at {self.entry_price} with SL: {self.sl}, TP: {self.tp}")
        elif action == 0:  # Hold
            pass  
        
        trade_closed = False
        
        if self.position == 1 and sma_short < sma_long:
            reward = current_price - self.entry_price
            self.balance += reward
            self.current_trade.update({
                'exit_time': time,
                'exit_price': current_price,
                'result': 'CROSSOVER'
            })
            print(f"{time}: Exiting LONG at {current_price}, Reward: {reward:.2f} (Crossover)")
            self.position = 0
            self.entry_price = 0.0
            self.sl = 0.0
            self.tp = 0.0
            trade_closed = True
        elif self.position == -1 and sma_short > sma_long:
            reward = self.entry_price - current_price
            self.balance += reward
            self.current_trade.update({
                'exit_time': time,
                'exit_price': current_price,
                'result': 'CROSSOVER'
            })
            print(f"{time}: Exiting SHORT at {current_price}, Reward: {reward:.2f} (Crossover)")
            self.position = 0
            self.entry_price = 0.0
            self.sl = 0.0
            self.tp = 0.0
            trade_closed = True
        
        if self.position == 1:
            if current_price <= self.sl:
                reward = current_price - self.entry_price
                self.balance += reward
                self.current_trade.update({
                    'exit_time': time,
                    'exit_price': self.sl,
                    'result': 'SL'
                })
                print(f"{time}: Stop Loss hit at {current_price} (LONG), Reward: {reward:.2f}")
                self.position = 0
                self.entry_price = 0.0
                self.sl = 0.0
                self.tp = 0.0
                trade_closed = True
            elif current_price >= self.tp:
    
                reward = self.tp - self.entry_price
                self.balance += reward
                self.current_trade.update({
                    'exit_time': time,
                    'exit_price': self.tp,
                    'result': 'TP'
                })
                print(f"{time}: Take Profit hit at {current_price} (LONG), Reward: {reward:.2f}")
                self.position = 0
                self.entry_price = 0.0
                self.sl = 0.0
                self.tp = 0.0
                trade_closed = True
        elif self.position == -1:
            if current_price >= self.sl:
                reward = self.entry_price - self.sl
                self.balance += reward
                self.current_trade.update({
                    'exit_time': time,
                    'exit_price': self.sl,
                    'result': 'SL'
                })
                print(f"{time}: Stop Loss hit at {current_price} (SHORT), Reward: {reward:.2f}")
                self.position = 0
                self.entry_price = 0.0
                self.sl = 0.0
                self.tp = 0.0
                trade_closed = True
            elif current_price <= self.tp:
                reward = self.entry_price - self.tp
                self.balance += reward
                self.current_trade.update({
                    'exit_time': time,
                    'exit_price': self.tp,
                    'result': 'TP'
                })
                print(f"{time}: Take Profit hit at {current_price} (SHORT), Reward: {reward:.2f}")
                self.position = 0
                self.entry_price = 0.0
                self.sl = 0.0
                self.tp = 0.0
                trade_closed = True
        
        reward -= 0.0001
    
        self.current_step += 1
        
        if self.current_step >= self.max_steps:
            done = True
        
        return self._get_observation(), reward, done, info
    
    def render(self, mode='human'):
        profit = self.balance - 10000
        print(f'Step: {self.current_step}')
        print(f'Balance: {self.balance:.2f}')
        print(f'Profit: {profit:.2f}')
        print(f'Position: {"Long" if self.position ==1 else "Short" if self.position == -1 else "None"}')
        print(f'Entry Price: {self.entry_price}')
        print(f'Stop Loss: {self.sl}')
        print(f'Take Profit: {self.tp}')
    
    def plot_trades(self):
        
        
        trades = pd.DataFrame(self.trade_history)
        
        fig, ax = plt.subplots(figsize=(15, 7) , dpi =1000)
        ax.plot(self.data['time'], self.data['close'], label='Close Price', alpha=0.5)
        ax.plot(self.data['time'], self.data['SMA_short'], label='SMA Short', alpha=0.9, linestyle='--')
        ax.plot(self.data['time'], self.data['SMA_long'], label='SMA Long', alpha=0.9, linestyle='--')
        
        for idx, trade in trades.iterrows():
            if trade['type'] == 'BUY':
                ax.scatter(trade['entry_time'], trade['entry_price'], marker='^', color='g', label='Buy Entry')
            else:
                ax.scatter(trade['entry_time'], trade['entry_price'], marker='v', color='r', label='Sell Entry')
            
            if trade['result'] == 'CROSSOVER':
                if trade['type'] == 'BUY':
                    ax.scatter(trade['exit_time'], trade['exit_price'], marker='o', color='b', label='Buy Exit (Crossover)')
                else:
                    ax.scatter(trade['exit_time'], trade['exit_price'], marker='o', color='c', label='Sell Exit (Crossover)')
            elif trade['result'] == 'SL':
                if trade['type'] == 'BUY':
                    ax.scatter(trade['exit_time'], trade['exit_price'], marker='x', color='k', label='Buy Stop Loss')
                else:
                    ax.scatter(trade['exit_time'], trade['exit_price'], marker='x', color='y', label='Sell Stop Loss')
            elif trade['result'] == 'TP':
                if trade['type'] == 'BUY':
                    ax.scatter(trade['exit_time'], trade['exit_price'], marker='x', color='m', label='Buy Take Profit')
                else:
                    ax.scatter(trade['exit_time'], trade['exit_price'], marker='x', color='orange', label='Sell Take Profit')
            
           
            entry_time = trade['entry_time']
            exit_time = trade['exit_time']
            if pd.isnull(entry_time) or pd.isnull(exit_time):
                continue 
            
            if trade['type'] == 'BUY':
                ax.hlines(y=trade['stop_loss'], xmin=entry_time, xmax=exit_time, colors='red', linestyles='--', linewidth=0.5, label='Stop Loss (Buy)')
                ax.hlines(y=trade['take_profit'], xmin=entry_time, xmax=exit_time, colors='green', linestyles='--', linewidth=0.5, label='Take Profit (Buy)')
            else:
                ax.hlines(y=trade['stop_loss'], xmin=entry_time, xmax=exit_time, colors='green', linestyles='--', linewidth=0.5, label='Stop Loss (Sell)')
                ax.hlines(y=trade['take_profit'], xmin=entry_time, xmax=exit_time, colors='red', linestyles='--', linewidth=0.5, label='Take Profit (Sell)')
    
        handles, labels = ax.get_legend_handles_labels()
        by_label = dict(zip(labels, handles))
        ax.legend(by_label.values(), by_label.keys())
        
        ax.set_xlabel('Time')
        ax.set_ylabel('Price')
        ax.set_title('Trade History with SL and TP Levels')
        plt.show()
    

    env = MATradingEnv(symbol, timeframe, short_window, long_window) model = PPO("MlpPolicy", env, verbose=1)

    print("Starting training...") model.learn(total_timesteps=10000)
    print("Training completed.")

    model.save("ppo_mat_trading") print("Model saved as 'ppo_mat_trading.zip'.")

    obs = env.reset() for _ in range(1000): action, _states = model.predict(obs, deterministic=True) obs, rewards, done, info = env.step(action) if done: break

    env.plot_trades()

    mt5.shutdown()

  2. Assistant

    It appears that your trading script is not generating any trades, resulting in an empty trade_history and consequently no plots being created. This can be attributed to several factors ranging from data fetching issues to the reinforcement learning (RL) model not taking appropriate actions. Let's systematically debug and address the potential causes.

    1. Verify Data Fetching

    Issue: If the data isn't fetched correctly, the environment won't have the necessary information to make trading decisions.

    Solution:

    • Check Initialization Messages:

      • Ensure that MetaTrader5 initializes successfully.
      • Look for the message: "MetaTrader5 initialized successfully."
    • Confirm Data Retrieval:

      • The _fetch_data method prints messages about data fetching success or failure.
      • Ensure you see messages like:
        • "Successfully fetched X bars of data."
        • If it falls back to _fetch_data_range, verify that method as well.
    • Inspect the DataFrame:

      • After fetching, print the first few rows to ensure data integrity.

        print(env.data.head())
        
    • Sample Output Verification:

      • Check that SMA_short and SMA_long columns are correctly calculated and populated.

    2. Ensure Trade Conditions Are Met

    Issue: The RL agent might not be taking actions that trigger trade entries, or the conditions to close trades aren't being met.

    Solution:

    • Add Debugging in the Evaluation Loop:

      • Modify the evaluation loop to print actions and observe if trades are being initiated.

        obs = env.reset()
        for step in range(1000):
            action, _states = model.predict(obs, deterministic=True)
            print(f"Step {step}: Action taken: {action}")
            obs, rewards, done, info = env.step(action)
            if done:
                break
        
    • Check Action Distribution:

      • After evaluation, analyze how often each action is being taken.

        from collections import Counter
        actions = []
        obs = env.reset()
        for _ in range(1000):
            action, _states = model.predict(obs, deterministic=True)
            actions.append(action)
            obs, rewards, done, info = env.step(action)
            if done:
                break
        print("Action distribution:", Counter(actions))
        
      • Interpretation:

        • If the majority of actions are 0 (Hold), the model might not be finding profitable trading opportunities or hasn't learned to trade effectively.

    3. Increase Training Duration

    Issue: The model might not have been trained sufficiently to learn effective trading strategies.

    Solution:

    • Increase total_timesteps:

      • Training for only 10,000 timesteps might be insufficient, especially with a limited dataset.

        model.learn(total_timesteps=100000)  # Increased from 10,000 to 100,000
        
    • Monitor Training Progress:

      • Observe the training logs for improvements in the model's performance.

    4. Enhance the Reward Function

    Issue: The current reward structure might not sufficiently incentivize profitable trades.

    Solution:

    • Modify Reward Calculation:

      • Instead of only rewarding profits and slight penalties, consider incorporating more nuanced rewards.

        def step(self, action):
            ...
            # Before closing a trade
            if trade_closed:
                # Reward proportional to profit/loss
                reward += (self.balance - previous_balance) / self.balance
            else:
                # Small penalty for each step to encourage quicker decisions
                reward -= 0.001
            ...
        
    • Normalize Rewards:

      • Ensure that rewards are scaled appropriately to prevent instability during training.

    5. Review Trading Logic

    Issue: The conditions for entering and exiting trades might be too restrictive or misaligned with the data.

    Solution:

    • Simplify Trading Conditions:

      • For initial testing, simplify the conditions to ensure that trades can occur.

        # Example: Enter a trade if SMA_short crosses SMA_long
        if self.position == 0:
            if sma_short > sma_long:
                action = 1  # Buy
            elif sma_short < sma_long:
                action = 2  # Sell
        
    • Ensure Crossovers Happen:

      • Verify that your dataset contains SMA crossovers which trigger trades.

        plt.figure(figsize=(15,7))
        plt.plot(env.data['time'], env.data['close'], label='Close Price')
        plt.plot(env.data['time'], env.data['SMA_short'], label='SMA Short')
        plt.plot(env.data['time'], env.data['SMA_long'], label='SMA Long')
        plt.legend()
        plt.show()
        
      • Look for instances where SMA_short crosses SMA_long.

    6. Validate Plotting Mechanism

    Issue: Even if trades are made, plotting might fail due to issues in the plot_trades method.

    Solution:

    • Check if trade_history is Non-Empty:

      • Before plotting, ensure that trades exist.

        if not env.trade_history:
            print("No trades to plot.")
        else:
            env.plot_trades()
        
    • Adjust Plotting Labels:

      • Prevent duplicate legend entries by handling labels correctly.

        # Modify the plotting loop to avoid duplicate labels
        for idx, trade in trades.iterrows():
            ...
            label_entry = 'Buy Entry' if trade['type'] == 'BUY' else 'Sell Entry'
            label_exit = 'Buy Exit (Crossover)' if (trade['type'] == 'BUY' and trade['result'] == 'CROSSOVER') else ...
            
            if not any(label == label_entry for label in ax.get_legend().get_texts()):
                ax.scatter(..., label=label_entry)
            else:
                ax.scatter(...)
            
            # Repeat similarly for exit labels
        
    • Set Plot DPI Appropriately:

      • A DPI of 1000 is excessively high and might cause rendering issues.

        fig, ax = plt.subplots(figsize=(15, 7), dpi=100)  # Reduced DPI from 1000 to 100
        

    7. Additional Recommendations

    • Use a Larger Dataset:

      • More data provides the RL agent with varied market conditions to learn from.
    • Feature Engineering:

      • Incorporate additional technical indicators (e.g., RSI, MACD) to provide more information to the agent.
    • Model Evaluation:

      • Test the trained model on unseen data to evaluate its performance.
    • Logging:

      • Implement comprehensive logging to trace the agent's decisions and the environment's state.

    8. Sample Debugging Steps

    Here's how you can incorporate some of the debugging steps into your code:

    # After training and before saving
    print("Training completed.")
    model.save("ppo_mat_trading")
    print("Model saved as 'ppo_mat_trading.zip'.")
    
    # Evaluation with Debugging
    obs = env.reset()
    actions_taken = []
    for step in range(1000):
        action, _states = model.predict(obs, deterministic=True)
        actions_taken.append(action)
        print(f"Step {step}: Action taken: {action}")
        obs, rewards, done, info = env.step(action)
        if done:
            break
    
    print("Actions taken during evaluation:", Counter(actions_taken))
    print("Number of trades made:", len(env.trade_history))
    
    # Plot trades if any
    if env.trade_history:
        env.plot_trades()
    else:
        print("No trades to plot.")
    
    mt5.shutdown()
    

    9. Conclusion

    By following the above steps, you can identify where the issue lies—be it data fetching, trading logic, model training, or plotting. Start by ensuring that your data is correctly fetched and that the environment is set up properly to allow trades. Then, verify that your RL model is learning to take meaningful actions by increasing training duration and refining the reward structure. Lastly, ensure that the plotting function correctly visualizes the trade history.

    If after these steps you still encounter issues, consider sharing specific error messages or logs for more targeted assistance.