-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
6,010 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# sample 2 | ||
|
||
## EMA Cross Strategy | ||
|
||
first you can run signal generator | ||
it will generate a out out data set call `final_dataset.csv` | ||
|
||
```bash | ||
python signal_generator.py | ||
``` | ||
|
||
`Notice: you may need to install TA-LIB library` | ||
|
||
## backtest generated signals | ||
|
||
```bash | ||
python main.py | ||
``` | ||
|
||
and result will save in `result` directory |
Empty file.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from signal_backtester import SignalBacktester | ||
|
||
|
||
|
||
dataset_address = "./final_dataset.csv" | ||
|
||
backtest = SignalBacktester(dataset=dataset_address, | ||
strategy='two_side_sl_tp_reversed', | ||
cash=1000, | ||
commission=0.0005, | ||
percent_of_portfolio=99, | ||
stop_loss=1, | ||
take_profit=10, | ||
trailing_stop=3, | ||
output_path='./result' # path of result files | ||
) | ||
|
||
backtest.run() |
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import talib # notice you can install talib manually | ||
import pandas as pd | ||
|
||
def cross_EMA_signals(df, fast_period, slow_period): | ||
"""_summary_ | ||
Args: | ||
df (_type_): _description_ | ||
fast_period (_type_): _description_ | ||
slow_period (_type_): _description_ | ||
""" | ||
signal = [0] * len(df) | ||
|
||
df['fast'] = talib.EMA(df.Close, timeperiod=fast_period) | ||
df['slow'] = talib.EMA(df.Close, timeperiod=slow_period) | ||
for idx in range(len(df)): | ||
if idx > slow_period: | ||
|
||
if df.iloc[idx - 1].fast < df.iloc[idx - 1].slow and df.iloc[idx].fast > df.iloc[idx].slow: | ||
# buy signal | ||
signal[idx] = 2 | ||
|
||
if df.iloc[idx - 1].fast > df.iloc[idx - 1].slow and df.iloc[idx].fast < df.iloc[idx].slow: | ||
# sell signal | ||
signal[idx] = 1 | ||
|
||
df['signal'] = signal | ||
df.to_csv('./final_dataset.csv') | ||
|
||
|
||
df = pd.read_csv('./data.csv') | ||
|
||
if __name__ == "__main__": | ||
cross_EMA_signals(df, 15, 30) |