79686240

Date: 2025-07-01 15:02:33
Score: 1
Natty:
Report link

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

# Simulated price data (or replace with real futures data)

np.random.seed(42)

dates = pd.date_range(start='2010-01-01', periods=3000)

price = 100 + np.cumsum(np.random.normal(0, 1, size=len(dates)))

df = pd.DataFrame(data={'Close': price}, index=dates)

# Pure technical indicators: moving averages

df['SMA_50'] = df['Close'].rolling(window=50).mean()

df['SMA_200'] = df['Close'].rolling(window=200).mean()

# Strategy: Go long when 50 SMA crosses above 200 SMA (golden cross), exit when below

df['Position'] = 0

df['Position'][df['SMA_50'] > df['SMA_200']] = 1

df['Position'] = df['Position'].shift(1) # Avoid lookahead bias

# Calculate returns

df['Return'] = df['Close'].pct_change()

df['Strategy_Return'] = df['Return'] * df['Position']

# Performance

cumulative_strategy = (1 + df['Strategy_Return']).cumprod()

cumulative_buy_hold = (1 + df['Return']).cumprod()

# Plotting

plt.figure(figsize=(12, 6))

plt.plot(cumulative_strategy, label='Strategy (Technical Only)')

plt.plot(cumulative_buy_hold, label='Buy & Hold', linestyle='--')

plt.title('Technical Strategy vs Buy & Hold')

plt.legend()

plt.grid(True)

plt.tight_layout()

plt.show()

Reasons:
  • Long answer (-1):
  • No code block (0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: denise chloe