Problem Formalization:
I am examining a problem where a stock price $X_t$ follows a symmetric random walk starting at 10, and increments or decrements by 1 unit at each step with equal likelihood. The process stops when the price hits 0 or 20. Initially, there are 10 coins, each purchased at a price of 10.
Trading rules:
- If $X_t$ increases, I sell 1 coin.
- If $X_t$ decreases, I buy 1 coin.
The aim is to analyze the expected profit at the end of the process. I have run simulations to observe the behavior and calculate approximate expected values, but I am seeking a more rigorous mathematical analysis of the expected outcomes and any stochastic modeling that can explain the observed results.
Simulation Code and Visualization:
import random
import matplotlib.pyplot as plt
def simulate_stock_trading_profit():
start = 10
end_low = 0
end_high = 20
x = start
stock_count = 10
cost = start * (-stock_count)
history_x = [x]
history_stocks = [stock_count]
history_profit = [0] # Initialize profit/loss to 0
buy_points = []
sell_points = []
while x > end_low and x < end_high:
prev_x = x
x += random.choice([-1, 1]) # Move randomly left or right
history_x.append(x)
if x > prev_x: # Moving up, sell stock
stock_count -= 1
cost += x
sell_points.append((len(history_x)-1, x)) # Record selling point
elif x < prev_x: # Moving down, buy stock
stock_count += 1
cost -= x
buy_points.append((len(history_x)-1, x)) # Record buying point
# Update profit/loss record
current_profit = cost + (stock_count * x)
history_stocks.append(stock_count)
history_profit.append(current_profit)
return buy_points, sell_points, history_x, history_stocks, history_profit
def visualize_stock_trading_profit(buy_points, sell_points, history_x, history_stocks, history_profit):
plt.figure(figsize=(12, 6))
plt.plot(history_x, label='Position (x)', color='blue', marker='', linestyle='-')
plt.plot(history_stocks, label='Stock Count', color='green', marker='', linestyle='--')
plt.plot(history_profit, label='Profit/Loss', color='red', marker='', linestyle='-.')
plt.axhline(y=10, color='gray', linestyle='--', linewidth=0.75, label='Baseline (y=10)')
# Mark buy and sell points
if buy_points:
buy_x, buy_y = zip(*buy_points)
plt.scatter(buy_x, [history_x[i] for i in buy_x], color='lightgreen', label='Buy Points', marker='o', s=50)
if sell_points:
sell_x, sell_y = zip(*sell_points)
plt.scatter(sell_x, [history_x[i] for i in sell_x], color='salmon', label='Sell Points', marker='x', s=50)
plt.title('Position, Stock Count, and Profit/Loss Over Time with Trade Points')
plt.xlabel('Step')
plt.ylabel('Value')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
# Call the function to perform a simulation
visualize_stock_trading_profit(*simulate_stock_trading_profit())
# Simulate expected profit
trading_profit = []
cnt = int(1e8)
for i in range(cnt):
profit = simulate_stock_trading_profit()[-1][-1]
trading_profit.append(profit)
# Positive profit cases
positive_profits = [p for p in trading_profit if p > 0]
positive_count = len(positive_profits)
average_positive_profit = sum(positive_profits) / positive_count if positive_count > 0 else 0
# Negative profit cases
negative_profits = [p for p in trading_profit if p < 0]
negative_count = len(negative_profits)
average_negative_profit = sum(negative_profits) / negative_count if negative_count > 0 else 0
print(
"Total simulations run: {}".format(cnt),
"Maximum profit: {}".format(max(trading_profit)),
"Minimum profit: {}".format(min(trading_profit)),
"Average profit: {:.2f}".format(sum(trading_profit) / len(trading_profit)),
"Count of positive profits: {}".format(positive_count),
"Average positive profit: {:.2f}".format(average_positive_profit),
"Count of negative profits: {}".format(negative_count),
"Average negative profit: {:.2f}".format(average_negative_profit)
)
Attached Image:
Simulation Results:
- Total simulations run: 100,000,000
- Maximum profit: 773
- Minimum profit: -145
- Average profit: -0.000117
- Count of positive profits: 51,535,851
- Average positive profit: 98.239414
- Count of negative profits: 48,426,011
- Average negative profit: -104.548431
Discussion: In the simulation results, the expected profit approximates to zero. However, each time the price returns to the starting point, a profit is realized, leading to confusion about the zero result. I am seeking a rigorous mathematical explanation for these observations, focusing on the stochastic processes involved.
I would appreciate any insights or references to mathematical theories that could explain the dynamics observed in this simulation.