Skip to content

Commit

Permalink
add : example sample 2
Browse files Browse the repository at this point in the history
  • Loading branch information
alm0ra committed Mar 4, 2022
1 parent ceacb58 commit 0ff2c04
Show file tree
Hide file tree
Showing 6 changed files with 6,010 additions and 0 deletions.
20 changes: 20 additions & 0 deletions examples/sample2/README.md
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 added examples/sample2/__init__.py
Empty file.
5,938 changes: 5,938 additions & 0 deletions examples/sample2/data.csv

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions examples/sample2/main.py
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.
34 changes: 34 additions & 0 deletions examples/sample2/signal_generator.py
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)

0 comments on commit 0ff2c04

Please sign in to comment.