Hey guys, forgive my noobiness but I have really only coded for hobbies and it's been some time. I am trying to create a standalone relative volume calculator class that would calc the daily average volume over a user specified period of days, and then check the current volume to see if the stock is being traded more frequently than normal. I am a little confused as to how it should be done, my current thought was:
-create the class in your main, and then every time you get data, pass it to the relativeVolume class
-wait until end of day to calculate daily volume
- fill an array of length(period in days) with today's volume
-once you hit the period, change index to beginning of array, then generate the average volume over the period
I'm a little stuck on how to pass the data in and have the class updating in sync with the main. I attached the code I have now, it's mainly a shell, but I wanted some input on if it is fundamentally sound or not.
James T
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; } } }
James T
Jared Broad
// 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.
Michael Handschuh
var averageVolume = SMA("SPY", 14, Resolution.Daily, x => ((TradeBar)x).Volume;
This will produce a 14 day SMA of the daily volumes. You can then compare today's volume with that using:if (data["SPY"].Volume > averageVolume) { ... }
James T
Ariel Salazar
Is there a way to do this in Python on QC?
Jack Simonson
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
Â
James T
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!