text_plain_output_1.png
from datetime import datetime

import datetime

import numpy as np

import numpy as np

import numpy as np # linear algebra

import yfinance as yf

import yfinance as yf

ticker = 'SPY'
start_time = datetime(2020, 5, 1)
end_time = datetime(2023, 5, 1)
spy = yf.download(ticker, start=start_time, end=end_time)

spy = yf.download(ticker, start=start_time, end=end_time)
start_time = datetime(2020, 5, 1)
end_time = datetime(2023, 5, 1)
spy['5_day_MA'] = spy['Close'].rolling(window=5).mean()
spy['30_day_MA'] = spy['Close'].rolling(window=30).mean()
initial_capital = 100000
capital = initial_capital
position = 0
stop_loss = None
for i in range(30, len(spy) - 1):
    pct_change = spy['Close'][i] / spy['Close'][i - 1] - 1
    if spy['5_day_MA'][i] > spy['30_day_MA'][i] and position <= 0:
        position = capital * 0.3
        capital -= position
        stop_loss = spy['Close'][i] * 0.9
    elif spy['5_day_MA'][i] < spy['30_day_MA'][i] and position >= 0:
        position = -capital * 0.2
        capital -= position
        stop_loss = spy['Close'][i] * 1.1
    if position > 0:
        if pct_change >= 0.1:
            additional_position = capital * 0.3
            position += additional_position
            capital -= additional_position
            stop_loss = spy['Close'][i] * 0.9
        elif pct_change >= 0.2:
            additional_position = capital * 0.4
            position += additional_position
            capital -= additional_position
            stop_loss = spy['Close'][i] * 0.9
        elif pct_change <= -0.1:
            capital += position
            position = 0
            stop_loss = None
    elif position < 0:
        if pct_change <= -0.1:
            additional_position = capital * 0.3
            position -= additional_position
            capital += additional_position
            stop_loss = spy['Close'][i] * 1.1
        elif pct_change <= -0.2:
            additional_position = capital * 0.4
            position -= additional_position
            capital += additional_position
            stop_loss = spy['Close'][i] * 1.1
        elif pct_change >= 0.1:
            capital -= position
            position = 0
            stop_loss = None
portfolio_value = capital + position

import numpy as np
import yfinance as yf
import pandas as pd
def strategy(data, short_window, long_window, long_allocation, short_allocation):
    capital = 100000
    position = 0
    stop_loss = None
    for i in range(long_window, len(data) - 1):
        pct_change = data['Close'][i] / data['Close'][i - 1] - 1
        if data[f'{short_window}_day_MA'][i] > data[f'{long_window}_day_MA'][i] and position <= 0:
            position = capital * long_allocation
            capital -= position
            stop_loss = data['Close'][i] * 0.9
        elif data[f'{short_window}_day_MA'][i] < data[f'{long_window}_day_MA'][i] and position >= 0:
            position = -capital * short_allocation
            capital -= position
            stop_loss = data['Close'][i] * 1.1
    return capital + position
symbol = 'SPY'
start_date = '2020-05-01'
end_date = '2023-05-01'
data = yf.download(symbol, start=start_date, end=end_date)
best_result = 0
best_params = None
short_windows = np.arange(3, 21, 2)
long_windows = np.arange(20, 61, 5)
long_allocations = np.arange(0.1, 0.51, 0.1)
short_allocations = np.arange(0.1, 0.51, 0.1)
for short_window in short_windows:
    for long_window in long_windows:
        if short_window >= long_window:
            continue
        data[f'{short_window}_day_MA'] = data['Close'].rolling(window=short_window).mean()
        data[f'{long_window}_day_MA'] = data['Close'].rolling(window=long_window).mean()
        for long_allocation in long_allocations:
            for short_allocation in short_allocations:
                result = strategy(data, short_window, long_window, long_allocation, short_allocation)
                if result > best_result:
                    best_result = result
                    best_params = (short_window, long_window, long_allocation, short_allocation)
print(f'Best result: {best_result}')
print(f'Best parameters: {best_params}')