Update backtest_engine.py
Browse files- backtest_engine.py +13 -7
backtest_engine.py
CHANGED
|
@@ -1,23 +1,29 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
|
| 3 |
-
|
|
|
|
| 4 |
|
| 5 |
wins = 0
|
| 6 |
trades = 0
|
| 7 |
|
| 8 |
-
for i in range(
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
tomorrow = df.iloc[i+1]
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
trades += 1
|
| 16 |
|
| 17 |
-
if
|
| 18 |
wins += 1
|
| 19 |
|
| 20 |
if trades == 0:
|
| 21 |
return 0
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
|
| 3 |
+
|
| 4 |
+
def backtest_strategy(df):
|
| 5 |
|
| 6 |
wins = 0
|
| 7 |
trades = 0
|
| 8 |
|
| 9 |
+
for i in range(200, len(df) - 1):
|
| 10 |
+
|
| 11 |
+
ma20 = df["Close"].rolling(20).mean().iloc[i]
|
| 12 |
|
| 13 |
+
price_today = df["Close"].iloc[i]
|
|
|
|
| 14 |
|
| 15 |
+
price_next = df["Close"].iloc[i + 1]
|
| 16 |
+
|
| 17 |
+
if price_today > ma20:
|
| 18 |
|
| 19 |
trades += 1
|
| 20 |
|
| 21 |
+
if price_next > price_today:
|
| 22 |
wins += 1
|
| 23 |
|
| 24 |
if trades == 0:
|
| 25 |
return 0
|
| 26 |
|
| 27 |
+
winrate = (wins / trades) * 100
|
| 28 |
+
|
| 29 |
+
return round(winrate, 2)
|