File tree Expand file tree Collapse file tree 6 files changed +6010
-0
lines changed
Expand file tree Collapse file tree 6 files changed +6010
-0
lines changed Original file line number Diff line number Diff line change 1+ # sample 2
2+
3+ ## EMA Cross Strategy
4+
5+ first you can run signal generator
6+ it will generate a out out data set call ` final_dataset.csv `
7+
8+ ``` bash
9+ python signal_generator.py
10+ ```
11+
12+ ` Notice: you may need to install TA-LIB library `
13+
14+ ## backtest generated signals
15+
16+ ``` bash
17+ python main.py
18+ ```
19+
20+ and result will save in ` result ` directory
Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change 1+ from signal_backtester import SignalBacktester
2+
3+
4+
5+ dataset_address = "./final_dataset.csv"
6+
7+ backtest = SignalBacktester (dataset = dataset_address ,
8+ strategy = 'two_side_sl_tp_reversed' ,
9+ cash = 1000 ,
10+ commission = 0.0005 ,
11+ percent_of_portfolio = 99 ,
12+ stop_loss = 1 ,
13+ take_profit = 10 ,
14+ trailing_stop = 3 ,
15+ output_path = './result' # path of result files
16+ )
17+
18+ backtest .run ()
Original file line number Diff line number Diff line change 1+ import talib # notice you can install talib manually
2+ import pandas as pd
3+
4+ def cross_EMA_signals (df , fast_period , slow_period ):
5+ """_summary_
6+
7+ Args:
8+ df (_type_): _description_
9+ fast_period (_type_): _description_
10+ slow_period (_type_): _description_
11+ """
12+ signal = [0 ] * len (df )
13+
14+ df ['fast' ] = talib .EMA (df .Close , timeperiod = fast_period )
15+ df ['slow' ] = talib .EMA (df .Close , timeperiod = slow_period )
16+ for idx in range (len (df )):
17+ if idx > slow_period :
18+
19+ if df .iloc [idx - 1 ].fast < df .iloc [idx - 1 ].slow and df .iloc [idx ].fast > df .iloc [idx ].slow :
20+ # buy signal
21+ signal [idx ] = 2
22+
23+ if df .iloc [idx - 1 ].fast > df .iloc [idx - 1 ].slow and df .iloc [idx ].fast < df .iloc [idx ].slow :
24+ # sell signal
25+ signal [idx ] = 1
26+
27+ df ['signal' ] = signal
28+ df .to_csv ('./final_dataset.csv' )
29+
30+
31+ df = pd .read_csv ('./data.csv' )
32+
33+ if __name__ == "__main__" :
34+ cross_EMA_signals (df , 15 , 30 )
You can’t perform that action at this time.
0 commit comments