nitishkarvekar commited on
Commit
30d191f
·
verified ·
1 Parent(s): 98f4839

Update backtest_engine.py

Browse files
Files changed (1) hide show
  1. backtest_engine.py +13 -7
backtest_engine.py CHANGED
@@ -1,23 +1,29 @@
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)
 
 
 
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)