No trades for EuroStoxx50 ; consistent trades for Google (same code ; all data from yfinance). Any idea? #1114
Replies: 1 comment
-
Problem solved.
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I start with backtesting.py and I’m lost.
I have the following code to test a very simple strategy as a first program (Buy when MACD becomes positive; Sell when MACD becomes negative).
The code gives consistent results for Google but I don’t get any transactions for EuroStoxx50 while the conditions are met (see 'trades_control' DataFrame: there should be a purchase on 2010-06-01 for example).
The format of dates and data seems identical between Google and Eurostoxx so I have no explanation for this difference in result.
Does anyone understand what the problem is?
Here is the code:
#!/usr/bin/env python3
import pandas as pd
import pandas_ta as ta
import numpy as np
import yfinance as yf
import backtesting
from backtesting import Backtest
from backtesting import Strategy
from backtesting.lib import crossover
from backtesting.test import GOOG
Step 1. Data
# . E : Euro Stoxx 50
# . G : Google
Data_choice = 'E'
if Data_choice == "E":
ESTX50 = yf.download('^STOXX50E',
start="2010-01-01",
end="2024-02-07",
progress=True)
elif Data_choice == "G":
# data = GOOG
Google = yf.download('GOOG',
start="2010-01-01",
end="2024-02-07",
progress=True)
Step 2. Strategy definition
class MACD_crossing_zero(Strategy):
Step 3. Backtest parameters
bt = Backtest(data, MACD_crossing_zero, cash=1_000, commission=.001,
trade_on_close=False, exclusive_orders=True)
Step 4. Backtest running
stats = bt.run()
trades = stats['_trades'].to_string()
Step 5. Backtest control
macd_control = ta.macd(data.Close, fast=12, slow=26, signal=9)
macdl = macd_control.iloc[:, 0]
macdh = macd_control.iloc[:, 1]
macds = macd_control.iloc[:, 2]
# Concatenate MACD histogram and Trades in the same pd DataFrame
# (ie with the same calendar to check decision consistency)
trades_control0 = pd.DataFrame(stats['_trades'])
trades_control0.set_index('EntryTime', inplace=True)
trades_control = pd.concat([macdh,trades_control0], axis=1)
trades_control = trades_control.fillna('')
trades_control = trades_control.replace({np.nan: None})
trades_control = trades_control.replace({np.nan: ""})
=> Visual inspection at the end:
. trades
. trades_control
Beta Was this translation helpful? Give feedback.
All reactions