Moving Average Crossover Backtest with a Ten Percent Stop
Summary
This strategy example processes historical daily equity bars one at a time, updating a backtest and recording buy and sell markers for later charting. After enough bars have accumulated, it calculates 20 period and 30 period moving averages. A bullish crossover triggers a long entry when no position is held; a bearish crossover exits an existing position. The example sizes an entry using the available account value divided by the closing price.
It also checks whether the bar’s low reaches a stop level set at 90 percent of the average entry price. On a stop, it calculates the exit loss using that threshold and closes the position. The script plots both the account value curve and signals over the price chart. This is an illustrative implementation rather than evidence of profitability: it provides no reported performance results and does not describe fees, slippage, position constraints, or how intrabar price paths are handled. Trades are modeled using bar data, which limits the precision of execution assumptions.
Key ideas
- The strategy enters long when the 20 period moving average crosses above the 30 period average.
- It exits on a bearish crossover when a long position is open.
- Entry quantity is based on account value divided by the current closing price.
- A stop is triggered when the bar low reaches 90 percent of the average entry price.
- The example plots account value and trade signals but reports no performance evidence.
Tags
From a private course collection; the original is not published.