I've been attempting to build various position sizing rules for an EWMA crossover trend strategy but I've been having issues where the backtest does not produce any results. To try and figure out the issue I decided to go through a process of elimination to identify the issue with my sizing rules.
To that end, I copied the Moving Average Crossover strategy from the Quantrocket Usage Guide but changed it to an Exponential Weighted Moving Average instead of the SMA. It's identical except for the change to EWMA and I'm using a StaticUniverse that I use regularly without issue. I also ensured that there was a decay_rate included for the EWMAs and used the example decay_rate from API guide but the backtest is producing no returns. As a result, I'm thinking the issue may not be with the various sizing rule strategies I've been building
Any assistance would be appreciated. I've pasted the EWMA version of the crossover strategy I copied from the Usage Guide and where I'm receiving an error indicating no returns were produced below:
import zipline.api as algo
from zipline.pipeline import Pipeline, EquityPricing
from zipline.pipeline.factors import ExponentialWeightedMovingAverage
from zipline.pipeline.filters import StaticUniverse
BUNDLE = "usstock-1min"
def initialize(context: algo.Context):
"""
Create a pipeline containing the moving averages and
schedule the rebalance function to run each trading
day 30 minutes after the open.
"""
context.target_value = 50000
pipe = Pipeline(
columns={
"long_mavg": ExponentialWeightedMovingAverage(
inputs=[EquityPricing.close],
window_length=128, decay_rate=.08),
"short_mavg": ExponentialWeightedMovingAverage(
inputs=[EquityPricing.close],
window_length=32, decay_rate=.08)
},
initial_universe=StaticUniverse("global-macro1"))
algo.attach_pipeline(pipe, "mavgs")
algo.schedule_function(
rebalance,
algo.date_rules.every_day(),
algo.time_rules.market_open(minutes=30))
def before_trading_start(context: algo.Context, data: algo.BarData):
"""
Gather today's pipeline output.
"""
context.mavgs = algo.pipeline_output("mavgs")
def rebalance(context: algo.Context, data: algo.BarData):
"""
Buy the assets when their short moving average is above the
long moving average.
"""
for asset in context.mavgs.index:
short_mavg = context.mavgs.short_mavg.loc[asset]
long_mavg = context.mavgs.long_mavg.loc[asset]
if short_mavg > long_mavg:
algo.order_target_value(asset, context.target_value)
elif short_mavg < long_mavg:
algo.order_target_value(asset, 0)