Create backtest_engine.py
Browse files- backtest_engine.py +23 -0
backtest_engine.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
|
| 3 |
+
def backtest(df):
|
| 4 |
+
|
| 5 |
+
wins = 0
|
| 6 |
+
trades = 0
|
| 7 |
+
|
| 8 |
+
for i in range(60, len(df)-1):
|
| 9 |
+
|
| 10 |
+
today = df.iloc[i]
|
| 11 |
+
tomorrow = df.iloc[i+1]
|
| 12 |
+
|
| 13 |
+
if today["Close"] > df["Close"].rolling(20).mean().iloc[i]:
|
| 14 |
+
|
| 15 |
+
trades += 1
|
| 16 |
+
|
| 17 |
+
if tomorrow["Close"] > today["Close"]:
|
| 18 |
+
wins += 1
|
| 19 |
+
|
| 20 |
+
if trades == 0:
|
| 21 |
+
return 0
|
| 22 |
+
|
| 23 |
+
return round((wins/trades)*100,2)
|