<jupyter_start><jupyter_text>Moving averagesHere, I will be demonstrating a moving average crossover strategy. We will use two moving averages, one we consider *fast*, and the other *slow*. The strategy is:Trade the asset when the fast moving average crosses over the slow moving average.Exit the trade when the fast moving average crosses over the slow moving average again.The fast version is a 20-day moving average and the slow one a 50-day moving average.<jupyter_code>for fast, slow in [[1, 50], [2, 5], [5, 10], [10, 20], [15, 30], [20, 50], [50, 100], [100, 200]]:
    # Correction to days
    fast *= 12 * 24
    slow *= 12 * 24
    ma = MovingAverage()
    ma = ma.fit(ts.BTC_POT_CLOSE, window_fast=fast, window_slow=slow)
    plt.plot(ma.signals)
    plt.show()
    plt.close()
    results = ma.predict()
    print(moving_average_score(ts.BTC_POT_CLOSE, results))<jupyter_output><empty_output><jupyter_text>Graphics<jupyter_code>signals = ma.signals
regimes = ma.regimes
chart = ma.y
plot = chart.to_frame()
plot['SIGNALS'] = signals
plot['REGIMES'] = regimes
plot.SIGNALS.plot()
fig, ax = plt.subplots(2, 1)
fig.suptitle('Regimes and Signals')

ax1 = plt.subplot(2, 1, 1)
plt.plot(plot.SIGNALS)
ax1.set_title('Signals')

ax2 = plt.subplot(2, 1, 2)
plt.plot(plot.REGIMES)
ax2.set_title('Regimes')
plt.save
plt.plot(chart)
plt.plot(regimes)
plt.plot()
trades = plot.BTC_POT_CLOSE[(plot.SIGNALS == -1) | (plot.SIGNALS == 1)].reset_index()
trades.rename(columns={'date': 'FROM', 'BTC_POT_CLOSE': 'BTC_POT_FROM'}, inplace=True)
trades['RETURNS'] = (trades.BTC_POT_FROM - trades.BTC_POT_FROM.shift(1)) / trades.BTC_POT_FROM
trades['BTC_POT_TO'] = trades.BTC_POT_FROM.shift(1)
trades['TO'] = trades.FROM.shift(1)
trades = trades[trades.index % 2 != 0]
trades
from collections import OrderedDict
plt.plot(plot.BTC_POT_CLOSE)
for row in trades.iterrows():
    color = 'green' if row[1]['RETURNS'] > 0 else 'red'
    label = 'gain' if row[1]['RETURNS'] > 0 else 'loss'
    plt.plot([row[1]['FROM'], row[1]['TO']], [row[1]['BTC_POT_FROM'], row[1]['BTC_POT_TO']], 'k-', lw=2, color=color, label=label)
    handles, labels = plt.gca().get_legend_handles_labels()
    by_label = OrderedDict(zip(labels, handles))
    plt.legend(by_label.values(), by_label.keys())<jupyter_output><empty_output><jupyter_text>Test MovingAverage<jupyter_code>from src.prediction_models.moving_average import MovingAverage
ma = MovingAverage()
ma = ma.fit(np.arange(1000))
print(ma)
check_estimator(MovingAverage)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1523809523809524, 'fraction_numerical': 0.02976190476190476, 'mean_word_length': 5.51421188630491, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '507165', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1039, 'n_tokens_neox': 951, 'n_words': 218, 'path': 'src/exploration/prediction_moving_average.ipynb', 'repo_name': 'tobiasraabe/crypto', 'size': '2520'}