Mabye trading robot 1

 avatar
user_0645964
plain_text
14 days ago
1.8 kB
2
Indexable
Never
import random

class TradingBot:
    def __init__(self, initial_balance=10000):
        self.balance = initial_balance
        self.stock = 0
        self.rsi_period = 14  # RSI calculation period
        self.buy_threshold = 30
        self.sell_threshold = 70
        self.prices = []  # Historical prices (mock data for demonstration)

    def calculate_rsi(self):
        # Replace with actual RSI calculation logic
        # This is just a mock function
        if len(self.prices) < self.rsi_period:
            return random.randint(1, 100)
        else:
            return random.randint(1, 100)

    def buy_stock(self):
        if self.calculate_rsi() <= self.buy_threshold:
            # Buy stocks using available balance
            # Replace with actual buy logic (e.g., API call)
            self.stock += 1
            self.balance -= self.prices[-1]  # Assuming last price for simplicity
            print(f"Bought 1 stock at {self.prices[-1]}")

    def sell_stock(self):
        if self.calculate_rsi() >= self.sell_threshold and self.stock > 0:
            # Sell all stocks
            # Replace with actual sell logic (e.g., API call)
            self.balance += self.stock * self.prices[-1]  # Assuming last price for simplicity
            print(f"Sold {self.stock} stocks at {self.prices[-1]}")
            self.stock = 0

    def run_strategy(self):
        # Replace with actual price fetching and streaming logic
        # For simplicity, we generate random prices here
        for _ in range(100):
            self.prices.append(random.randint(50, 150))
            self.buy_stock()
            self.sell_stock()
            print(f"Current Balance: {self.balance}, Stocks Owned: {self.stock}")

# Example usage
if __name__ == "__main__":
    bot = TradingBot()
    bot.run_strategy()