File size: 4,675 Bytes
aab0a9b c3fc9c2 aab0a9b c3fc9c2 aab0a9b c3fc9c2 aab0a9b c3fc9c2 aab0a9b c3fc9c2 aab0a9b c3fc9c2 aab0a9b c3fc9c2 aab0a9b c3fc9c2 aab0a9b c3fc9c2 349a356 922de0b ddc92c4 922de0b 349a356 c3fc9c2 aab0a9b c3fc9c2 aab0a9b c3fc9c2 aab0a9b c3fc9c2 aab0a9b c3fc9c2 aab0a9b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
import yfinance as yf
from backtesting import Backtest
import pandas as pd
import random
import os
from multiprocessing import Pool
from itertools import repeat
import time
from strategies import SMC_test, SMC_ema, SMCStructure
def fetch(symbol, period, interval):
df = yf.download(symbol, period=period, interval=interval)
df.columns =df.columns.get_level_values(0)
return df
def smc_plot_backtest(data, filename, swing_hl, **kwargs):
bt = Backtest(data, SMC_test, **kwargs)
bt.run(swing_window=swing_hl)
return bt.plot(filename=filename, open_browser=False)
def smc_ema_plot_backtest(data, filename, ema1, ema2, closecross, **kwargs):
bt = Backtest(data, SMC_ema, **kwargs)
bt.run(ema1=ema1, ema2=ema2, close_on_crossover=closecross)
return bt.plot(filename=filename, open_browser=False)
def smc_structure_plot_backtest(data, filename, swing_hl, **kwargs):
bt = Backtest(data, SMCStructure, **kwargs)
bt.run(swing_window=swing_hl)
return bt.plot(filename=filename, open_browser=False)
def smc_backtest(data, filename, swing_hl, **kwargs):
bt = Backtest(data, SMC_test, **kwargs)
results = bt.run(swing_window=swing_hl)
bt.plot(filename=filename, open_browser=False)
return results
def smc_ema_backtest(data, filename, ema1, ema2, closecross, **kwargs):
bt = Backtest(data, SMC_ema, **kwargs)
results = bt.run(ema1=ema1, ema2=ema2, close_on_crossover=closecross)
bt.plot(filename=filename, open_browser=False)
return results
def smc_structure_backtest(data, filename, swing_hl, **kwargs):
bt = Backtest(data, SMCStructure, **kwargs)
results = bt.run(swing_window=swing_hl)
bt.plot(filename=filename, open_browser=False)
return results
def run_strategy(ticker_symbol, strategy, period, interval, kwargs):
# Fetching ohlc of random ticker_symbol.
retries = 3
for i in range(retries):
try:
data = fetch(ticker_symbol, period, interval)
except:
raise Exception(f"{ticker_symbol} data fetch failed")
if len(data) == 0:
if i < retries - 1:
print(f"Attempt{i + 1}: {ticker_symbol} ohlc is empty")
else:
raise Exception(f"{ticker_symbol} ohlc is empty")
else:
break
filename = f'{ticker_symbol}.html'
if strategy == "Order Block":
backtest_results = smc_backtest(data, filename, kwargs['swing_hl'])
elif strategy == "Order Block with EMA":
backtest_results = smc_ema_backtest(data, filename, kwargs['ema1'], kwargs['ema2'], kwargs['cross_close'])
elif strategy == "Structure trading":
backtest_results = smc_structure_backtest(data, filename, kwargs['swing_hl'])
else:
raise Exception('Strategy not found')
with open(filename, 'r', encoding='utf-8') as f:
plot = f.read()
os.remove(filename)
# Converting pd.Series to pd.Dataframe
backtest_results = backtest_results.to_frame().transpose()
backtest_results['stock'] = ticker_symbol
backtest_results['plot'] = plot
# Reordering columns.
# cols = df.columns.tolist()
# cols = cols[-1:] + cols[:-1]
cols = ['stock', 'Start', 'End', 'Return [%]', 'Equity Final [$]', 'Buy & Hold Return [%]', '# Trades',
'Win Rate [%]', 'Best Trade [%]', 'Worst Trade [%]', 'Avg. Trade [%]', 'plot']
backtest_results = backtest_results[cols]
return backtest_results
def complete_test(strategy: str, period: str, interval: str, multiprocess=True, **kwargs):
nifty50 = pd.read_csv("data/ind_nifty50list.csv")
ticker_list = pd.read_csv("data/Ticker_List_NSE_India.csv")
# Merging nifty50 and ticker_list dataframes to get 'YahooEquiv' column.
nifty50 = nifty50.merge(ticker_list, "inner", left_on=['Symbol'], right_on=['SYMBOL'])
if multiprocess:
with Pool() as p:
result = p.starmap(run_strategy, zip(nifty50['YahooEquiv'].values, repeat(strategy), repeat(period), repeat(interval), repeat(kwargs)))
else:
result = [run_strategy(nifty50['YahooEquiv'].values[i], strategy, period, interval, kwargs) for i in range(len(nifty50))]
df = pd.concat(result)
df['plot'] = df['plot'].astype(str)
df = df.sort_values(by=['Return [%]'], ascending=False)
return df.reset_index().drop(columns=['index'])
if __name__ == "__main__":
# random_testing("")
# data = fetch('RELIANCE.NS', period='1y', interval='15m')
# df = yf.download('RELIANCE.NS', period='1yr', interval='15m')
rt = complete_test("Order Block", '1mo', '15m', swing_hl=20)
rt.to_excel('test/all_testing_2.xlsx', index=False)
print(rt) |