Python Backtrader RSI ZeroDivisionError: деление с плавающей запятой на нольPython

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Python Backtrader RSI ZeroDivisionError: деление с плавающей запятой на ноль

Сообщение Anonymous »

Код: Выделить всё

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.")
При запуске программы (инициализация AT RSI) она обнаруживает сообщение об ошибке ZeroDivisionError: деление с плавающей запятой на ноль.
Данные верны, начиная с РСИ. В чем может быть проблема? Я пробовал много вещей, отлаживал, записывал. Но лог внутри RSI тоже не работал, значит проблема где-то в RSI.

Подробнее здесь: https://stackoverflow.com/questions/790 ... on-by-zero
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»