import backtrader as bt
import pandas as pd
# Load data
file_path = r'C:\forex\python\data\EURUSD1min-v2.xlsx'
data = pd.read_excel(file_path)
# Handle date and time in separate columns
data['Datetime'] = pd.to_datetime(data['Datetime'], format='%Y.%m.%d. %H:%M')
data.set_index('Datetime', inplace=True)
print("Data loaded, total entries:", len(data))
# Backtrader data format
class PandasData(bt.feeds.PandasData):
params = (
('datetime', None),
('open', ''),
('high', ''),
('low', ''),
('close', ''),
('volume', None),
('openinterest', None),
)
# RSI-based strategy
class RSIStrategy(bt.Strategy):
params = (
('rsi_period', 14),
('rsi_upper', 70),
('rsi_lower', 30),
)
def __init__(self):
self.rsi = bt.indicators.RSI_SMA(self.data.close, period=self.params.rsi_period)
print("RSI indicator initialized.")
def next(self):
if len(self.data.close) < self.params.rsi_period:
print("Not enough data for RSI calculation.")
return
current_rsi = self.rsi[0]
current_close = self.data.close[0]
if pd.isna(current_rsi) or current_rsi == 0:
print("Invalid or zero RSI:", current_rsi)
return
print(f"Close: {current_close}, RSI: {current_rsi}")
# Load data into Backtrader
data_feed = PandasData(dataname=data)
print("Data converted to Backtrader format.")
# Create Backtrader cerebro and add strategy
cerebro = bt.Cerebro()
cerebro.adddata(data_feed)
cerebro.addstrategy(RSIStrategy)
print("Strategy added to Cerebro.")
# Run the strategy
cerebro.run()
print("Execution finished.")
< /code>
Когда программа запускается, (при инициализации RSI) она встречается с сообщением об ошибке ZerodivisionError: разделение плавания на ноль < /p>
Данные правильны, начиная с RSI. В чем может быть проблема? Я пробовал много вещей, отладка, журнал. Но журнал внутри RSI также не работал, поэтому проблема заключается где -то с RSI.
Подробнее здесь: https://stackoverflow.com/questions/790 ... on-by-zero
Python Backtrader RSI ZerodivisionError: Float Division от Zero ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Python Backtrader RSI ZeroDivisionError: деление с плавающей запятой на ноль
Anonymous » » в форуме Python - 0 Ответы
- 25 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Python Backtrader RSI ZeroDivisionError: деление с плавающей запятой на ноль
Anonymous » » в форуме Python - 0 Ответы
- 27 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Python Backtrader RSI ZeroDivisionError: деление с плавающей запятой на ноль
Anonymous » » в форуме Python - 0 Ответы
- 17 Просмотры
-
Последнее сообщение Anonymous
-