Stock Trading Program

Stock Trading Program

Python Programming Exercise

The goal of this Python programming exercise is to identify trends in stock prices. Given the following summary of stock prices (USD) of a public company each day during the last 20 business days:

Day 1: 10,
Day 2: 10.5,
Day 3: 10.6,
Day 4: 10.7,
Day 5: 10.2,
Day 6: 10.9,
Day 7: 11,
Day 8: 11.2,
Day 9: 11.8,
Day 10: 11.7,
Day 11: 11.7,
Day 12: 11.8,
Day 13: 11.5,
Day 14: 11.3,
Day 15: 11.6,
Day 16: 11.7,
Day 17: 11.6,
Day 18: 11.6,
Day 19: 11.9,
Day 20: 11.8

Write a Python script to generate Buy/Sell orders according to the following rules:

1) If the stock goes up 4 days in a row, then it is a good time to buy the stock. Add a flag the following day to buy the stock.

2) If the stock goes down 4 consecutive days, then it is a good time to sell the stock. Add a flag the following day to sell the stock.

Write a Python script that prints out the 20 days the stock price, along with a flag (buy or sell) if any of the trading rules apply on a particular day.

Expected Ouput

Day 1: Stock price 10
Day 2: Stock price 10.5
Day 3: Stock price 10.6
Day 4: Stock price 10.7
Day 5: Stock price 10.2
Day 6: Stock price 10.9
Day 7: Stock price 11
Day 8: Stock price 11.2
Day 9: Stock price 11.8 - Buy
Day 10: Stock price 11.7
Day 11: Stock price 11.7
Day 12: Stock price 11.8
Day 13: Stock price 11.5
Day 14: Stock price 11.3
Day 15: Stock price 11.6
Day 16: Stock price 11.7
Day 17: Stock price 11.6
Day 18: Stock price 11.6
Day 19: Stock price 11.9
Day 20: Stock price 11.8

Source code


def generate_buy_sell_flags(stock_data):
    days = list(stock_data.keys())
    prices = list(stock_data.values())
    buy_flags = []
    sell_flags = []

    for i in range(4, len(prices)):
        if all(prices[j] > prices[j - 1] for j in range(i, i - 4, -1)):
            buy_flags.append(i + 1)
        if all(prices[j] < prices[j - 1] for j in range(i, i - 4, -1)):
            sell_flags.append(i + 1)

    return buy_flags, sell_flags

def print_buy_sell_flags(stock_data, buy_flags, sell_flags):
    for i in range(1, len(stock_data) + 1):
        if i in buy_flags:
            print(f"Day {i}: Stock price {stock_data[str(i)]} - Buy")
        elif i in sell_flags:
            print(f"Day {i}: Stock price {stock_data[str(i)]} - Sell")
        else:
            print(f"Day {i}: Stock price {stock_data[str(i)]}")

stock_data = {'1': 10, '2': 10.5, '3': 10.6, '4': 10.7, '5': 10.2, '6': 10.9, '7': 11, '8': 11.2, '9': 11.8, '10': 11.7, '11': 11.7, '12': 11.8, '13': 11.5, '14': 11.3, '15': 11.6, '16': 11.7, '17': 11.6, '18': 11.6, '19': 11.9, '20': 11.8}

buy_flags, sell_flags = generate_buy_sell_flags(stock_data)
print_buy_sell_flags(stock_data, buy_flags, sell_flags)

Code Comments

  • The "generate_buy_sell_flags" function takes the stock data as input and applies the trading rules to determine the buy/sell flags.
  • The "print_buy_sell_flags" function prints out the 20 days, the stock price of each day, and the flag (buy or sell) if any of the trading rules apply on a particular day based on the buy/sell flags generated by the generate_buy_sell_flags function.
  • The stock prices for the 20 business days are defined as a dictionary.
  • The "generate_buy_sell_flags" function is used to calculate the buy/sell flags for the stock prices, and the results are printed using the print_buy_sell_flags function.

Here you will finf more fun Python projects to practice different programming topics.

We will be happy to hear your thoughts

Leave a reply

Python and Excel Projects for practice
Register New Account
Shopping cart