Created
June 22, 2024 15:55
-
-
Save ChadThackray/b2ffc800c72267ac39b4fd325f0bf9bd to your computer and use it in GitHub Desktop.
Backtesting Candlestick Patterns in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Licensed under the MIT License. See comment below for full licence information. | |
import vectorbt as vbt | |
import yfinance as yf | |
import pandas as pd | |
from datetime import datetime, timedelta | |
import talib | |
data = pd.read_csv("Bitstamp_BTCUSD_1h.csv") | |
data["unix"] = pd.to_datetime(data["unix"], unit= "s") | |
data.set_index("unix", inplace = True) | |
data = data.iloc[::-1] | |
data = data[["open","high","low","close"]] | |
data.columns = ["Open", "High","Low","Close"] | |
print(data) | |
window_length =240 | |
strategy_returns = [] | |
basic_returns = [] | |
for x in range(0, len(data) - window_length, 24): | |
window_data = data.iloc[x:x+window_length] | |
hammer = talib.CDLHAMMER(window_data.Open, window_data.High, | |
window_data.Low, window_data.Close) | |
hanging_man = talib.CDLHANGINGMAN(window_data.Open, window_data.High, | |
window_data.Low, window_data.Close) | |
buys = (hammer==100) | |
sells = (hanging_man==-100) | |
pf = vbt.Portfolio.from_signals(window_data.Close, buys, sells, fees = 0.005) | |
strategy_returns.append(pf.total_return()) | |
basic_returns.append((window_data.Close.iloc[-1]/window_data.Close.iloc[0])-1) | |
print("Average return (strategy):",sum(strategy_returns)/len(strategy_returns)) | |
print("Average return (benchmark):",sum(basic_returns)/len(strategy_returns)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Data can be found here to download:
https://www.cryptodatadownload.com/data/bitstamp/