Run projections and backtests in parallel.

This commit is contained in:
sam
2024-11-15 10:22:12 -08:00
parent 16ce8823f3
commit 5b80eac207
2 changed files with 63 additions and 50 deletions
+62 -49
View File
@@ -5,6 +5,7 @@ import matplotlib.pyplot as plt
import seaborn as sns import seaborn as sns
from scipy.stats import norm from scipy.stats import norm
from scipy.signal import savgol_filter from scipy.signal import savgol_filter
from multiprocessing import Process
# Utility functions # Utility functions
@@ -948,68 +949,80 @@ def create_backtest_plot(
return historical_projections return historical_projections
if __name__ == "__main__": def run_projection(df):
analysis, df = analyze_bitcoin_prices("prices.csv") projections = create_plots(df, start="2016-07-09", project_days=365 * 4)
# create_plots(df) # Full history
# create_plots(df, start='2022-01-01') # From 2022 onwards
# create_plots(df, start='2023-01-01', end='2023-12-31') # Just 2023
# Create plots with different time ranges and projections
projections = create_plots(df, start="2011-01-01", project_days=365 * 4)
print("\nProjected Prices at Key Points:") print("\nProjected Prices at Key Points:")
print(projections.iloc[[29, 89, 179, 364]].round(2)) # 30, 90, 180, 365 days print(projections.iloc[[29, 89, 179, 364]].round(2)) # 30, 90, 180, 365 days
def run_backtest(params, df):
print(
f"\nRunning backtest from {params['start_date']} to {params['backtest_date']}"
)
backtest_projections = create_backtest_plot(df, **params)
# Print some key projection points vs actual prices
print("\nBacktest Results - Projected vs Actual Prices:")
validation_df = df[df["Date"] > params["backtest_date"]]
actual_prices = validation_df.set_index("Date")["Close"]
for days in [30, 90, 180, 365]:
target_date = pd.to_datetime(params["backtest_date"]) + pd.Timedelta(days=days)
if (
target_date in actual_prices.index
and target_date in backtest_projections.index
):
projected = backtest_projections.loc[target_date]
actual = actual_prices.loc[target_date]
print(f"\n{days} days out ({target_date.strftime('%Y-%m-%d')}):")
print(f"Actual Price: ${actual:,.2f}")
print(f"Projected (Expected): ${projected['Expected_Trend']:,.2f}")
print(
f"Projected Range: ${projected['Lower_95']:,.2f} - ${projected['Upper_95']:,.2f}"
)
if __name__ == "__main__":
analysis, df = analyze_bitcoin_prices("prices.csv")
procs = []
# Create main projection
proc = Process(target=run_projection, args=(df,))
proc.start()
procs.append(proc)
# Create multiple backtests for different periods # Create multiple backtests for different periods
backtests = [ backtests = [
# First to second halving # First until fourth halving
{ {
"start_date": "2012-11-28", # First halving "start_date": "2012-11-28",
"backtest_date": "2016-07-09", # Second halving "backtest_date": "2024-04-19",
"project_days": 1460, # 4 years "project_days": 1460,
}, },
# Second to third halving # First until third halving
{ {
"start_date": "2016-07-09", # Second halving "start_date": "2012-11-28",
"backtest_date": "2020-05-11", # Third halving "backtest_date": "2020-05-11",
"project_days": 1460, # 4 years "project_days": 1460,
}, },
# Third halving onwards # Second until fourth halving
{ {
"start_date": "2020-05-11", # Third halving "start_date": "2016-07-09",
"backtest_date": "2024-04-19", # Fourth halving (projected) "backtest_date": "2024-04-19",
"project_days": 1460, # 4 years "project_days": 1460,
},
{
"start_date": "2016-07-09", # Second halving
"backtest_date": "2024-04-19", # Fourth halving (projected)
"project_days": 1460, # 4 years
}, },
] ]
# Run all backtests # Run all backtests
for params in backtests: for params in backtests:
print( proc = Process(
f"\nRunning backtest from {params['start_date']} to {params['backtest_date']}" target=run_backtest,
args=(
params,
df,
),
) )
backtest_projections = create_backtest_plot(df, **params) procs.append(proc)
proc.start()
# Print some key projection points vs actual prices for proc in procs:
print("\nBacktest Results - Projected vs Actual Prices:") proc.join()
validation_df = df[df["Date"] > params["backtest_date"]]
actual_prices = validation_df.set_index("Date")["Close"]
for days in [30, 90, 180, 365]:
target_date = pd.to_datetime(params["backtest_date"]) + pd.Timedelta(
days=days
)
if (
target_date in actual_prices.index
and target_date in backtest_projections.index
):
projected = backtest_projections.loc[target_date]
actual = actual_prices.loc[target_date]
print(f"\n{days} days out ({target_date.strftime('%Y-%m-%d')}):")
print(f"Actual Price: ${actual:,.2f}")
print(f"Projected (Expected): ${projected['Expected_Trend']:,.2f}")
print(
f"Projected Range: ${projected['Lower_95']:,.2f} - ${projected['Upper_95']:,.2f}"
)
+1 -1
View File
@@ -1,5 +1,5 @@
"Date","Price","Open","High","Low","Vol.","Change %" "Date","Price","Open","High","Low","Vol.","Change %"
"11/15/2024","88,096.2","87,297.2","88,348.6","87,276.2","109.10K","0.92%" "11/15/2024","89,481.8","87,297.2","90,670.6","87,118.8","159.36K","2.51%"
"11/14/2024","87,294.0","90,424.8","91,726.7","86,740.2","153.34K","-3.46%" "11/14/2024","87,294.0","90,424.8","91,726.7","86,740.2","153.34K","-3.46%"
"11/13/2024","90,422.2","87,971.2","93,226.6","86,168.4","257.57K","2.82%" "11/13/2024","90,422.2","87,971.2","93,226.6","86,168.4","257.57K","2.82%"
"11/12/2024","87,941.3","88,665.0","89,929.6","85,122.2","288.68K","-0.82%" "11/12/2024","87,941.3","88,665.0","89,929.6","85,122.2","288.68K","-0.82%"
1 Date Price Open High Low Vol. Change %
2 11/15/2024 88,096.2 89,481.8 87,297.2 88,348.6 90,670.6 87,276.2 87,118.8 109.10K 159.36K 0.92% 2.51%
3 11/14/2024 87,294.0 90,424.8 91,726.7 86,740.2 153.34K -3.46%
4 11/13/2024 90,422.2 87,971.2 93,226.6 86,168.4 257.57K 2.82%
5 11/12/2024 87,941.3 88,665.0 89,929.6 85,122.2 288.68K -0.82%