mail@pastecode.io avatar
2 months ago
1.6 kB
Sure! Here is an example of a simple Forex Trading system in Python:

class ForexTradingSystem:
    def __init__(self, starting_balance):
        self.balance = starting_balance

    def buy_currency(self, currency, amount, exchange_rate):
        cost = amount * exchange_rate
        if cost > self.balance:
            print("Insufficient balance to buy {} amount of {}".format(amount, currency))
            self.balance -= cost
            print("Successfully bought {} {} at exchange rate {} for a total cost of {}".format(amount, currency, exchange_rate, cost))

    def sell_currency(self, currency, amount, exchange_rate):
        earnings = amount * exchange_rate
        self.balance += earnings
        print("Successfully sold {} {} at exchange rate {} for total earnings of {}".format(amount, currency, exchange_rate, earnings))

# Initialize the Forex Trading System with a starting balance of $1000
forex_system = ForexTradingSystem(1000)

# Buy 10 units of currency USD at an exchange rate of 1.5
forex_system.buy_currency('USD', 10, 1.5)

# Sell 5 units of currency EUR at an exchange rate of 1.2
forex_system.sell_currency('EUR', 5, 1.2)

# Check the remaining balance
print("Remaining balance: $", forex_system.balance)

This code block defines a simple ForexTradingSystem class with methods to buy and sell currency based on exchange rates. You can create an instance of the class with an initial starting balance, perform buy and sell transactions, and check the remaining balance after each transaction.
Leave a Comment