Skip to content

Issue 1: Inconsistent Data Usage in SMACrossover Strategy #1

@PrinceSajjadHussain

Description

@PrinceSajjadHussain

Title: SMACrossover strategy uses only first column, potentially ignoring other assets

Body:
The SMACrossover strategy in backtesting_tool.py uses only the first column of the target.universe DataFrame (data = target.universe[target.universe.columns[0]]). This means that the SMA and EMA calculations are based solely on the price data of the first cryptocurrency in the combined data, regardless of which assets are in the universe. This is likely not the intended behavior for a crossover strategy designed to be applied across multiple assets. It would always assign 0 or 1 to all assets depending on the first asset's crossover. The intention was probably to apply the crossover on each available asset.
To fix this, the strategy needs to iterate through each asset in the universe and calculate the SMA and EMA crossover independently for each one, assigning weights accordingly. Each asset is a column of the target.universe dataframe.

class SMACrossover(bt.Algo):
    def __call__(self, target):
        weights = {}
        for asset in target.universe.columns:
            data = target.universe[asset]  # Use the first column
            sma_50 = data.rolling(window=50).mean()
            ema_200 = data.ewm(span=200).mean()
            signal = (sma_50 > ema_200).astype(int).iloc[-1] #get latest crossover signal
            weights[asset] = signal
        target.temp['weights'] = weights
        return True

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions