Back

Standalone Average & Relative Volume Calculator Class

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.
Update Backtest








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;
}

}

}
0

After some brainstorming, I think it would be best to add a function in the standalone that takes the current volume as input and adds it to the day total. Then use the date to determine when a day ends and add the daily total to the array.
0

Hey James! That is a good start. If you're convinced to make it a stand alone class I agree with your last comment its probably easiest to keep it separate. You would check when the date changes, and output the previous day's volume.

In your main class you could have an array/queue which remembers your previous volumes, and removes elements from the end of the queue once it is X-days long.

If you'd consider using the tools in QuantConnect you could use the built in consolidator classes -- they would do all the hard work for you, and you could just use the output. An example of this would be the "How do I Use Consolidators?" in the University which builds daily bars from the TradeBar data.

You could also use internal components of QuantConnect for the Fixed length queue. In QC its called a "RollingWindow" -- which you could use like so:
// Save 10 samples, of data type double
var window = new RollingWindow(10);
0

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.


If you just want a running average of the daily volume you can easily accomplish it like this:
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) { ... }
1

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.


Haha, I thought there might be a simpler way. Thanks for the suggestions, I'll try them tonight.
0

Update Backtest





0

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.


Loading...

This discussion is closed