extremely bugg and wrong logic can you fix # train_model.py

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

# Initialize MetaTrader5 connection
if not mt5.initialize():
    print("MetaTrader5 initialization failed")
    quit()

# Define parameters for the moving average crossover
symbol = "EURUSD"
timeframe = mt5.TIMEFRAME_M1
short_window = 10
long_window = 50

class MATradingEnv(gym.Env):
    def __init__(self):
        super(MATradingEnv, self).__init__()
        
        # Define action space for SL and TP adjustment
        self.action_space = spaces.Box(low=np.array([0.0, 0.0]), high=np.array([0.1, 0.1]), dtype=np.float32)
        
        # Observation space includes the latest close price and MAs
        self.observation_space = spaces.Box(low=-np.inf, high=np.inf, shape=(4,), dtype=np.float32)

        # Initialize variables
        self.position = 0
        self.entry_price = None
        self.stop_loss = None
        self.take_profit = None
        self.trade_history = []  # To store trade history for plotting

    def _get_observation(self):
        # Retrieve the latest data from MT5
        rates = mt5.copy_rates_from_pos(symbol, timeframe, 0, max(long_window + 1, 1000))
        df = pd.DataFrame(rates)
        
        # Calculate moving averages
        df['SMA_short'] = df['close'].rolling(window=short_window).mean()
        df['SMA_long'] = df['close'].rolling(window=long_window).mean()

        sma_short = df['SMA_short'].iloc[-1]
        sma_long = df['SMA_long'].iloc[-1]
        close_price = df['close'].iloc[-1]

        return np.array([close_price, sma_short, sma_long, self.position], dtype=np.float32), df

    def step(self, action):
        stop_loss_pct, take_profit_pct = action
        observation, df = self._get_observation()
        close_price = observation[0]
        sma_short = observation[1]
        sma_long = observation[2]

        reward = 0
        if sma_short > sma_long and self.position == 0:
            # Enter long position
            self.position = 1
            self.entry_price = close_price
            self.stop_loss = self.entry_price * (1 - stop_loss_pct)
            self.take_profit = self.entry_price * (1 + take_profit_pct)
            print(f"Entering LONG at {self.entry_price}")
            self.trade_history.append((df['time'].iloc[-1], close_price, "LONG", self.stop_loss, self.take_profit))

        elif sma_short < sma_long and self.position == 1:
            # Exit long position and reset SL/TP
            reward = close_price - self.entry_price
            self.position = 0
            print(f"Exiting LONG at {close_price}, Reward: {reward}")
            self.trade_history.append((df['time'].iloc[-1], close_price, "EXIT_LONG", self.stop_loss, self.take_profit))
            self.entry_price = None
            self.stop_loss = None
            self.take_profit = None

        elif sma_short < sma_long and self.position == 0:
            # Enter short position
            self.position = -1
            self.entry_price = close_price
            self.stop_loss = self.entry_price * (1 + stop_loss_pct)
            self.take_profit = self.entry_price * (1 - take_profit_pct)
            print(f"Entering SHORT at {self.entry_price}")
            self.trade_history.append((df['time'].iloc[-1], close_price, "SHORT", self.stop_loss, self.take_profit))

        elif sma_short > sma_long and self.position == -1:
            # Exit short position and reset SL/TP
            reward = self.entry_price - close_price
            self.position = 0
            print(f"Exiting SHORT at {close_price}, Reward: {reward}")
            self.trade_history.append((df['time'].iloc[-1], close_price, "EXIT_SHORT", self.stop_loss, self.take_profit))
            self.entry_price = None
            self.stop_loss = None
            self.take_profit = None

        # Check if stop-loss or take-profit was hit
        if self.position == 1 and close_price <= self.stop_loss:
            reward = close_price - self.entry_price
            self.position = 0
            print(f"Stop Loss hit at {close_price} (LONG), Reward: {reward}")

        elif self.position == 1 and close_price >= self.take_profit:
            reward = close_price - self.entry_price
            self.position = 0
            print(f"Take Profit hit at {close_price} (LONG), Reward: {reward}")

        elif self.position == -1 and close_price >= self.stop_loss:
            reward = self.entry_price - close_price
            self.position = 0
            print(f"Stop Loss hit at {close_price} (SHORT), Reward: {reward}")

        elif self.position == -1 and close_price <= self.take_profit:
            reward = self.entry_price - close_price
            self.position = 0
            print(f"Take Profit hit at {close_price} (SHORT), Reward: {reward}")

        done = False
        return observation, reward, done, {}

    def reset(self):
        self.position = 0
        self.entry_price = None
        self.stop_loss = None
        self.take_profit = None
        observation, _ = self._get_observation()
        return observation

    def plot_trades(self):
        # Plot closing prices with trades, SL, and TP levels
        fig, ax = plt.subplots()
        close_prices = [obs[1] for obs in self.trade_history]
        times = [obs[0] for obs in self.trade_history]

        ax.plot(times, close_prices, label="Close Price", color="blue")
        for trade in self.trade_history:
            if trade[2] == "LONG":
                ax.plot(trade[0], trade[1], 'go', label="LONG Entry" if "LONG Entry" not in ax.get_legend_handles_labels()[1] else "")
            elif trade[2] == "SHORT":
                ax.plot(trade[0], trade[1], 'ro', label="SHORT Entry" if "SHORT Entry" not in ax.get_legend_handles_labels()[1] else "")
            elif trade[2].startswith("EXIT"):
                ax.plot(trade[0], trade[1], 'bo', label="Exit" if "Exit" not in ax.get_legend_handles_labels()[1] else "")
            
            # Plot SL/TP lines
            ax.axhline(trade[3], color="red", linestyle="--", linewidth=0.5, label="Stop Loss" if "Stop Loss" not in ax.get_legend_handles_labels()[1] else "")
            ax.axhline(trade[4], color="green", linestyle="--", linewidth=0.5, label="Take Profit" if "Take Profit" not in ax.get_legend_handles_labels()[1] else "")
        
        plt.xlabel("Time")
        plt.ylabel("Price")
        plt.legend()
        plt.title("Trade Entries, Exits, SL, and TP")
        plt.show()

# Train the environment
env = MATradingEnv()
model = PPO("MlpPolicy", env, verbose=1)
model.learn(total_timesteps=500)  # Adjust based on your data and time

# Save the trained model
model.save("ppo_stop_loss_take_profit")
print("Model training completed and saved.")

# After training, plot trades
env.plot_trades()

# Shutdown MT5 connection
mt5.shutdown()