using QuantConnect.Algorithm.Framework.Alphas;
using QuantConnect.Algorithm.Framework.Portfolio;
using QuantConnect.Algorithm.Framework.Risk;
using QuantConnect.Algorithm.Framework.Selection;
using QuantConnect.Data.UniverseSelection;
namespace QuantConnect.Algorithm.CSharp
{
public class MovingAverageCrossoverBot : QCAlgorithm
{
private const string Symbol = "AAPL"; // Replace with the desired symbol
private readonly RollingWindow<TradeBar> _priceWindow = new RollingWindow<TradeBar>(20);
public override void Initialize()
{
SetStartDate(2022, 1, 1);
SetCash(100000);
AddEquity(Symbol);
}
public override void OnData(Slice data)
{
if (!data.ContainsKey(Symbol)) return;
_priceWindow.Add(data[Symbol]);
if (_priceWindow.IsReady)
{
var fastMa = _priceWindow.Select(bar => bar.Close).Average();
var slowMa = _priceWindow.Skip(10).Select(bar => bar.Close).Average();
if (fastMa > slowMa && Portfolio[Symbol].Quantity <= 0)
{
SetHoldings(Symbol, 1.0);
}
else if (fastMa < slowMa && Portfolio[Symbol].Quantity > 0)
{
Liquidate(Symbol);
}
}
Mia Alissi
The code you provided is a simple moving average crossover strategy implemented in C#. It uses a rolling window of 20 bars to calculate two moving averages: a fast one over the entire window and a slow one over the last 10 bars. When the fast moving average crosses above the slow one, it goes long on the AAPL stock. When the fast moving average crosses below the slow one, it liquidates the AAPL stock.
If you want to implement this strategy in Python, you can use the following code:
This Python code does the same thing as your C# code. It uses a rolling window to keep track of the last 20 bars of the AAPL stock. It calculates the fast and slow moving averages and makes trading decisions based on their crossover.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Mouhamad Abushaqra
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!