Synced repo using 'sync_with_huggingface' Github Action
Browse files- app.py +0 -1
- strategies.py +1 -0
- utils.py +92 -1
app.py
CHANGED
|
@@ -23,7 +23,6 @@ def run(stock, interval, period, strategy, swing_hl, ema1=9, ema2=21, cross_clos
|
|
| 23 |
|
| 24 |
backtest_plot = gr.Plot()
|
| 25 |
|
| 26 |
-
|
| 27 |
# Plotting backtest plot based on strategy.
|
| 28 |
if strategy == "Order Block":
|
| 29 |
backtest_plot = smc_plot_backtest(data, 'test.html', swing_hl)
|
|
|
|
| 23 |
|
| 24 |
backtest_plot = gr.Plot()
|
| 25 |
|
|
|
|
| 26 |
# Plotting backtest plot based on strategy.
|
| 27 |
if strategy == "Order Block":
|
| 28 |
backtest_plot = smc_plot_backtest(data, 'test.html', swing_hl)
|
strategies.py
CHANGED
|
@@ -150,6 +150,7 @@ class SMCStructure(TrailingStrategy):
|
|
| 150 |
swings = swings[~np.isnan(swings['Level'])]
|
| 151 |
return swings['Level'].iloc[-2]
|
| 152 |
|
|
|
|
| 153 |
|
| 154 |
if __name__ == "__main__":
|
| 155 |
from utils import fetch
|
|
|
|
| 150 |
swings = swings[~np.isnan(swings['Level'])]
|
| 151 |
return swings['Level'].iloc[-2]
|
| 152 |
|
| 153 |
+
strategies = {'Order Block': SMC_test, 'Order Block with EMA': SMC_ema , 'Structure trading': SMCStructure}
|
| 154 |
|
| 155 |
if __name__ == "__main__":
|
| 156 |
from utils import fetch
|
utils.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import yfinance as yf
|
| 2 |
from backtesting import Backtest
|
|
|
|
|
|
|
| 3 |
|
| 4 |
from strategies import SMC_test, SMC_ema, SMCStructure
|
| 5 |
|
|
@@ -32,6 +34,95 @@ def smc_ema_backtest(data, ema1, ema2, closecross, **kwargs):
|
|
| 32 |
def smc_structure_backtest(data, swing_hl, **kwargs):
|
| 33 |
return Backtest(data, SMCStructure, **kwargs).run(swing_hl=swing_hl)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
if __name__ == "__main__":
|
|
|
|
| 36 |
# data = fetch('RELIANCE.NS', period='1y', interval='15m')
|
| 37 |
-
df = yf.download('RELIANCE.NS', period='1yr', interval='15m')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import yfinance as yf
|
| 2 |
from backtesting import Backtest
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import random
|
| 5 |
|
| 6 |
from strategies import SMC_test, SMC_ema, SMCStructure
|
| 7 |
|
|
|
|
| 34 |
def smc_structure_backtest(data, swing_hl, **kwargs):
|
| 35 |
return Backtest(data, SMCStructure, **kwargs).run(swing_hl=swing_hl)
|
| 36 |
|
| 37 |
+
def random_test(strategy: str, period: str, interval: str, no_of_stocks: int = 5, **kwargs):
|
| 38 |
+
nifty50 = pd.read_csv("data/ind_nifty50list.csv")
|
| 39 |
+
ticker_list = pd.read_csv("data/Ticker_List_NSE_India.csv")
|
| 40 |
+
|
| 41 |
+
# Merging nifty50 and ticker_list dataframes to get 'YahooEquiv' column.
|
| 42 |
+
nifty50 = nifty50.merge(ticker_list, "inner", left_on=['Symbol'], right_on=['SYMBOL'])
|
| 43 |
+
|
| 44 |
+
# Generating random indices between 0 and len(nifty50).
|
| 45 |
+
random_indices = random.sample(range(0, len(nifty50)), no_of_stocks)
|
| 46 |
+
|
| 47 |
+
df = pd.DataFrame()
|
| 48 |
+
|
| 49 |
+
for i in random_indices:
|
| 50 |
+
# Fetching ohlc of random ticker_symbol.
|
| 51 |
+
ticker_symbol = nifty50['YahooEquiv'].values[i]
|
| 52 |
+
data = fetch(ticker_symbol, period, interval)
|
| 53 |
+
|
| 54 |
+
if strategy == "Order Block":
|
| 55 |
+
backtest_results = smc_backtest(data, kwargs['swing_hl'])
|
| 56 |
+
elif strategy == "Order Block with EMA":
|
| 57 |
+
backtest_results = smc_ema_backtest(data, kwargs['ema1'], kwargs['ema2'], kwargs['cross_close'])
|
| 58 |
+
elif strategy == "Structure trading":
|
| 59 |
+
backtest_results = smc_structure_backtest(data, kwargs['swing_hl'])
|
| 60 |
+
else:
|
| 61 |
+
raise Exception('Strategy not found')
|
| 62 |
+
|
| 63 |
+
# Converting pd.Series to pd.Dataframe
|
| 64 |
+
backtest_results = backtest_results.to_frame().transpose()
|
| 65 |
+
|
| 66 |
+
backtest_results['stock'] = ticker_symbol
|
| 67 |
+
|
| 68 |
+
# Reordering columns.
|
| 69 |
+
# cols = df.columns.tolist()
|
| 70 |
+
# cols = cols[-1:] + cols[:-1]
|
| 71 |
+
cols = ['stock', 'Start', 'End', 'Return [%]', 'Equity Final [$]', 'Buy & Hold Return [%]', '# Trades', 'Win Rate [%]', 'Best Trade [%]', 'Worst Trade [%]', 'Avg. Trade [%]']
|
| 72 |
+
backtest_results = backtest_results[cols]
|
| 73 |
+
|
| 74 |
+
df = pd.concat([df, backtest_results])
|
| 75 |
+
|
| 76 |
+
df = df.sort_values(by=['Return [%]'], ascending=False)
|
| 77 |
+
|
| 78 |
+
return df
|
| 79 |
+
|
| 80 |
+
def complete_test(strategy: str, period: str, interval: str, **kwargs):
|
| 81 |
+
nifty50 = pd.read_csv("data/ind_nifty50list.csv")
|
| 82 |
+
ticker_list = pd.read_csv("data/Ticker_List_NSE_India.csv")
|
| 83 |
+
|
| 84 |
+
# Merging nifty50 and ticker_list dataframes to get 'YahooEquiv' column.
|
| 85 |
+
nifty50 = nifty50.merge(ticker_list, "inner", left_on=['Symbol'], right_on=['SYMBOL'])
|
| 86 |
+
|
| 87 |
+
df = pd.DataFrame()
|
| 88 |
+
|
| 89 |
+
for i in range(len(nifty50)):
|
| 90 |
+
# Fetching ohlc of random ticker_symbol.
|
| 91 |
+
ticker_symbol = nifty50['YahooEquiv'].values[i]
|
| 92 |
+
data = fetch(ticker_symbol, period, interval)
|
| 93 |
+
|
| 94 |
+
if strategy == "Order Block":
|
| 95 |
+
backtest_results = smc_backtest(data, kwargs['swing_hl'])
|
| 96 |
+
elif strategy == "Order Block with EMA":
|
| 97 |
+
backtest_results = smc_ema_backtest(data, kwargs['ema1'], kwargs['ema2'], kwargs['cross_close'])
|
| 98 |
+
elif strategy == "Structure trading":
|
| 99 |
+
backtest_results = smc_structure_backtest(data, kwargs['swing_hl'])
|
| 100 |
+
else:
|
| 101 |
+
raise Exception('Strategy not found')
|
| 102 |
+
|
| 103 |
+
# Converting pd.Series to pd.Dataframe
|
| 104 |
+
backtest_results = backtest_results.to_frame().transpose()
|
| 105 |
+
|
| 106 |
+
backtest_results['stock'] = ticker_symbol
|
| 107 |
+
|
| 108 |
+
# Reordering columns.
|
| 109 |
+
# cols = df.columns.tolist()
|
| 110 |
+
# cols = cols[-1:] + cols[:-1]
|
| 111 |
+
cols = ['stock', 'Start', 'End', 'Return [%]', 'Equity Final [$]', 'Buy & Hold Return [%]', '# Trades', 'Win Rate [%]', 'Best Trade [%]', 'Worst Trade [%]', 'Avg. Trade [%]']
|
| 112 |
+
backtest_results = backtest_results[cols]
|
| 113 |
+
|
| 114 |
+
df = pd.concat([df, backtest_results])
|
| 115 |
+
|
| 116 |
+
df = df.sort_values(by=['Return [%]'], ascending=False)
|
| 117 |
+
|
| 118 |
+
return df
|
| 119 |
+
|
| 120 |
+
|
| 121 |
if __name__ == "__main__":
|
| 122 |
+
# random_testing("")
|
| 123 |
# data = fetch('RELIANCE.NS', period='1y', interval='15m')
|
| 124 |
+
# df = yf.download('RELIANCE.NS', period='1yr', interval='15m')
|
| 125 |
+
|
| 126 |
+
rt = all_testing("Order Block", '1mo', '15m', swing_hl=20)
|
| 127 |
+
rt.to_excel('test/all_testing_1.xlsx', index=False)
|
| 128 |
+
print(rt)
|