# Simulate and Compare TQQQ and QQQ

This tutorial utilizes Python to simulate and compare the performance of TQQQ and QQQ. The fundamental observation is that TQQQ tends to outperform QQQ when the number of up days exceeds the number of down days. Conversely, if the market experiences an equal number of up days and down days, TQQQ is expected to underperform compared to QQQ.

## Situation 1: 40 days up and 40 days down

We can simulate a situation where 40 days down and then 40 days up. We set QQQ to be 1%, and then TQQQ will be 3% using the following Python code.

``````import numpy as np
import matplotlib.pyplot as plt

# Set the initial parameters
initial_price = 150.0  # Initial stock price
num_days = 40  # Number of trading days to simulate

# Set up arrays to store the simulated prices for TQQQ and QQQ
tqqq_price_series = [initial_price]
qqq_price_series = [initial_price]

# Simulate the TQQQ stock price going down by 3% for the first 20 days
for i in range(1, num_days + 1):
tqqq_price_series.append(tqqq_price_series[i - 1] * 0.97)

# Simulate the TQQQ stock price going up by 3% for the next 20 days
for i in range(num_days + 1, num_days * 2 + 1):
tqqq_price_series.append(tqqq_price_series[i - 1] * 1.03)

# Simulate the QQQ stock price going down by 1% for the first 20 days
for i in range(1, num_days + 1):
qqq_price_series.append(qqq_price_series[i - 1] * 0.99)

# Simulate the QQQ stock price going up by 1% for the next 20 days
for i in range(num_days + 1, num_days * 2 + 1):
qqq_price_series.append(qqq_price_series[i - 1] * 1.01)

# Plot the simulated stock prices
plt.plot(tqqq_price_series, label='TQQQ')
plt.plot(qqq_price_series, label='QQQ')
plt.ylabel("Stock Price")
plt.title("Simulation of TQQQ and QQQ Stock Prices")
plt.legend()
plt.grid(True)
plt.show()``````

The following is the output figure, and we can see that in this situation TQQQ is slightly worse then QQQ.

## Situation 2: Up 1 day and down 1 day

We can simulate a situation where 1 day down and then 1 day up for 80 days. We set QQQ to be 1%, and then TQQQ will be 3% using the following Python code.

``````import matplotlib.pyplot as plt

# Set the initial parameters
initial_price = 150.0  # Initial stock price
num_days = 80  # Number of trading days to simulate

# Set up arrays to store the simulated prices for TQQQ and QQQ
tqqq_price_series = [initial_price]
qqq_price_series = [initial_price]

# Simulate the TQQQ stock price going up and down by 3% every other day
for i in range(1, num_days + 1):
if i % 2 == 0:
tqqq_price_series.append(tqqq_price_series[i - 1] * 0.97)
else:
tqqq_price_series.append(tqqq_price_series[i - 1] * 1.03)

# Simulate the QQQ stock price going up and down by 1% every other day
for i in range(1, num_days + 1):
if i % 2 == 0:
qqq_price_series.append(qqq_price_series[i - 1] * 0.99)
else:
qqq_price_series.append(qqq_price_series[i - 1] * 1.01)

# Plot the simulated stock prices
plt.plot(tqqq_price_series, label='TQQQ')
plt.plot(qqq_price_series, label='QQQ')
plt.ylabel("Stock Price")
plt.title("Simulation of TQQQ and QQQ Stock Prices")
plt.legend()
plt.grid(True)
plt.show()``````

## Situation 3: Up 2 days and down 1 day

We can simulate a situation where 1 day down and then 2 days up for 80 days.

``````import matplotlib.pyplot as plt

# Set the initial parameters
initial_price = 150.0  # Initial stock price
num_days = 40  # Number of trading days to simulate

# Set up arrays to store the simulated prices for TQQQ and QQQ
tqqq_price_series = [initial_price]
qqq_price_series = [initial_price]

# Simulate the TQQQ stock price with one down day followed by two up days in sequence
for i in range(1, num_days + 1):
if (i - 1) % 3 == 0:
tqqq_price_series.append(tqqq_price_series[i - 1] * 0.97)
else:
tqqq_price_series.append(tqqq_price_series[i - 1] * 1.03)

# Simulate the QQQ stock price with one down day followed by two up days in sequence
for i in range(1, num_days + 1):
if (i - 1) % 3 == 0:
qqq_price_series.append(qqq_price_series[i - 1] * 0.99)
else:
qqq_price_series.append(qqq_price_series[i - 1] * 1.01)

# Plot the simulated stock prices
plt.plot(tqqq_price_series, label='TQQQ')
plt.plot(qqq_price_series, label='QQQ')
plt.ylabel("Stock Price")
plt.title("Simulation of TQQQ and QQQ Stock Prices")
plt.legend()
plt.grid(True)
plt.show()``````

## Situation 4: 1 down 2 up for 40 days and then reverse

``````import matplotlib.pyplot as plt

# Set the initial parameters
initial_price = 150.0  # Initial stock price
num_days = 80  # Number of trading days to simulate
num_periods = 40  # Number of periods for each pattern

# Set up arrays to store the simulated prices for TQQQ and QQQ
tqqq_price_series = [initial_price]
qqq_price_series = [initial_price]

# Simulate the TQQQ stock price with one down day followed by two up days for the first 40 days,
# and two down days followed by one up day for the next 40 days
for i in range(1, num_days + 1):
if i <= num_periods:
if (i - 1) % 3 == 0:
tqqq_price_series.append(tqqq_price_series[i - 1] * 0.97)
else:
tqqq_price_series.append(tqqq_price_series[i - 1] * 1.03)
else:
if (i - num_periods - 1) % 3 != 0:
tqqq_price_series.append(tqqq_price_series[i - 1] * 0.98)
else:
tqqq_price_series.append(tqqq_price_series[i - 1] * 1.02)

# Simulate the QQQ stock price with one down day followed by two up days for the first 40 days,
# and two down days followed by one up day for the next 40 days
for i in range(1, num_days + 1):
if i <= num_periods:
if (i - 1) % 3 == 0:
qqq_price_series.append(qqq_price_series[i - 1] * 0.99)
else:
qqq_price_series.append(qqq_price_series[i - 1] * 1.01)
else:
if (i - num_periods - 1) % 3 != 0:
qqq_price_series.append(qqq_price_series[i - 1] * 0.98)
else:
qqq_price_series.append(qqq_price_series[i - 1] * 1.02)

# Plot the simulated stock prices
plt.plot(tqqq_price_series, label='TQQQ')
plt.plot(qqq_price_series, label='QQQ')