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.
using System;
using System.Collections;
using System.Collections.Generic;
using QuantConnect.Securities;
using QuantConnect.Models;
namespace QuantConnect {
//
// Make sure to change "BasicTemplateAlgorithm" to your algorithm class name, and that all
// files use "public partial class" if you want to split up your algorithm namespace into multiple files.
//
//public partial class BasicTemplateAlgorithm : QCAlgorithm, IAlgorithm
//{
// Extension functions can go here...(ones that need access to QCAlgorithm functions e.g. Debug, Log etc.)
//}
public class RelativeVolume(TradeBars data, String Symbol, int periodDays) //use data from the main, Symbol of the security we want the volume of, and the period of days. i.e. average over 30 days
{
private long averageVolume;
private long relativeVolume;
private long[] runningTotal= new long[periodDays];
private int arrayIndex = 0;
//Debug("testing");
public long getRelativeVolume(){ //should return relative volume to main
if(1>0){ //will change to a condition later
Console.Write("Period not reached."); //warns user if they call the function before sample set is complete
}
return relativeVolume;
}
private long calcRelativeVolume(){ //I think the calculations should occur here, or do I need a main?
return relativeVolume;
}
}
}
// Save 10 samples, of data type double
var window = new RollingWindow(10);
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.
var averageVolume = SMA("SPY", 14, Resolution.Daily, x => ((TradeBar)x).Volume;
if (data["SPY"].Volume > averageVolume) { ... }
Is there a way to do this in Python on QC?
Hi Ariel,
It is! You need to initialize the Simple Moving Average Indicator in Initialize first, and then you can access its value in OnData or related methods
## In Initialize()
self.sma = self.SMA('SPY', 14, Resolution.Daily, Field.Volume)
## Elsewhere
averageVolume = self.sma.Current.Value ## Retrieves the decimal value of 14-day volume SMA
Â
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!