-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
51 lines (42 loc) · 2.21 KB
/
main.py
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
import pandas as pd
from settings import PATH_TO_TMP
from tabulate import tabulate
import logging
from backtest import backtester
import stock_selector.funcs
from stock_selector.funcs import finviz_get_tickers_as_list, yfinance_get_historic_data
from tabulate import tabulate
from technical_analysis import ta_funcs
from trade_builder.sl_rules import tbs_first_candle_close_hard
from trade_builder.tp_rules import tbs_multiple_of_dcc
# filters = ["idx_sp500"]
# sp_500 = finviz_get_tickers_as_list(filters=filters, save_to_tmp=True, order=[])
# ticker_price_data = yfinance_get_historic_data(ticker_list=sp_500,
# interval="1d",
# period="5y", save_to_tmp=True)
# Set logging level:
logging.basicConfig(level=logging.INFO)
price_rawdf = pd.read_csv(f"{PATH_TO_TMP}/yahoo_ticklist_price_data_2024-02-11.csv", header=[0, 1], index_col=0)
ticker_list = list(price_rawdf.columns.get_level_values(1).unique())
# Stores the dataframes that should be passed to the backtester for simulating execution of trades.
ticker_data_w_trades = {}
for ticker in ticker_list:
logging.info(f"Analysing {ticker}...")
ticker_data = price_rawdf.xs(ticker, level=1, axis=1).dropna().reset_index()
results = ta_funcs.iterate_three_bar_spring(price_df=ticker_data, drop_percentage=-1)
# If dataframe contains any valid trades:
if results["Three_bar_spring"].any():
logging.info(f"{ticker} has >= 1 valid trade.")
# Add Stop loss and take profit values for each trade.
results = tbs_first_candle_close_hard(price_df=results)
results = tbs_multiple_of_dcc(price_df=results, profit_factor=3)
# Set SL and TP value.- Can add more into here for dynamic SL etc.
# Adds a key:value pair to a dict containing the ticker name and the price_df with associated valid trades.
ticker_data_w_trades[ticker] = results
# Pass to backtester.
backtester = backtester.Backtester(price_df_dict=ticker_data_w_trades,
max_sl=1,
max_tp=3,
init_bankroll=1000,
)
test = backtester.get_results()