LEAN is the open source
algorithmic trading engine powering QuantConnect. Founded in 2013 LEAN has been built by a
global community of 80+ engineers and powers more than a dozen hedge funds today.
Alpha League Competition: $1,000 Weekly Prize Pool
Qualifying Alpha Streams Reentered Weekly Learn
more
I have a question, I would like to use quant connect in helping with writing a paper but I am a little confused as to how to use the system. I was hoping you could help me. I am looking for information such as average liquidity in the stock, average price per trade per month, average amount of trades in the stock per month which were done using an algo and average value(profit) generated by the stock per month. I'm not very good at coding so any help given would be much appreciated
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.
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.
// Popular Securities Property Values:
Securities["IBM"].HasData // Security has data
.Invested // Have holdings
.LocalTime // Time on the asset exchange
.Holdings // Portfolio object
.Exchange // Exchange information
.FeeModel; // Fee model setter
If you want to calculate some statistics at the end of the algorithm, you need to add the OnEndOfAlgorithm event handler in your algorithm:
public override void OnEndOfAlgorithm()
{
// Your statistics calculation here
}
Here are examples on how to calculate some of the statistics you mentioned:
public override void OnEndOfAlgorithm()
{
foreach (var security in Securities.Values)
{
var ordersByMonth = Transactions
// Get orders of security
.GetOrders(x => x.Symbol == security.Symbol)
// Group by month
.GroupBy(x => x.Time.Month);
foreach (var month in ordersByMonth)
{
// Amount of trades in the stock per month
Log(security.Symbol + " Month: " + month.Key +
" Orders Count: " + month.Count());
// Average price per trade per month
Log(security.Symbol + " Month: " + month.Key +
" Orders Avg Price: " + month.Average(x => x.Price));
}
}
}
We can help you code other statistics if you provide the formulas.
Best regards
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.
Paul deauna
89
,
Cheers!
0
Mike Vaysburd
78
,
HI,
Thanks for that. Would it be possible to instead of using algorithms to test for value traded in a specific stock, to instead test for value traded in say BATS exchange. So exchanges instead of stocks?
Thanks
0
Alexandre Catarino
,
Hi Mark,
Let me complete the answer above first: In order to
average liquidity in the stock,
average price per trade per month,
average amount of trades in the stock per month
average value(profit) generated by the stock per month.
You will need to save each stock volume and profits. You can use the following class to do just that:
class StatisticsData
{
public Symbol Symbol { get; private set; }
public DateTime Time { get; private set; }
public decimal Volume { get; private set; }
public decimal Profit { get; private set; }
public StatisticsData(Symbol symbol, DateTime time, long volume, decimal profit)
{
Symbol = symbol;
Time = time;
Volume = volume;
Profit = profit;
}
}
And save this information in a list:
private List<StatisticsData> _statisticsData =
new List<StatisticsData>();
public void OnData(TradeBars data)
{
foreach (var item in data.Values)
{
_statisticsData.Add(
new StatisticsData(
item.Symbol,
item.Time,
item.Volume,
Portfolio[item.Symbol].Profit));
}
}
Orders are recorded in the Transactions class.
Then, OnEndOfAlgorithm, you can use Linq to sort and group the fields to compute your statistics:
public override void OnEndOfAlgorithm()
{
var monthCount = new Dictionary<int, int>();
for (var date = StartDate; date < EndDate;
date = date.AddMonths(1))
{
if (!monthCount.ContainsKey(date.Month))
{
monthCount.Add(date.Month, 0);
}
monthCount[date.Month]++;
}
foreach (var security in Securities.Values)
{
var averageProfitPerMonth = monthCount
.ToDictionary(x => x.Key, y => 0m);
var averageLiquidyPerMonth = monthCount
.ToDictionary(x => x.Key, y => 0m);
var averageTradesPerMonth = monthCount
.ToDictionary(x => x.Key, y => 0m);
var averagePricePerTradesPerMonth = monthCount
.ToDictionary(x => x.Key, y => 0m);
// Profit per month
// Since Profit a cumulative property in QuantConnect
// we need to calculate the daily profit
var profits = new Dictionary<DateTime, decimal>();
var cumprofits = _statisticsData
.Where(x => x.Symbol == security.Symbol)
.Select(x => new { x.Time, x.Profit }).ToArray();
for (var i = cumprofits.Count() - 1; i > 0; i--)
{
profits.Add(cumprofits[i].Time,
cumprofits[i].Profit - cumprofits[i - 1].Profit);
}
// Group the daily profit per month
var monthlyProfit = profits
.OrderBy(x => x.Key)
.GroupBy(x => x.Key.Month);
// A rough estimation of liquidity, you can simply use an average of daily volume over N days
// http://quant.stackexchange.com/questions/19407/how-do-i-calculate-approximate-equity-liquidity
var monthlyLiquidity = _statisticsData
.Where(x => x.Symbol == security.Symbol)
.GroupBy(x => x.Time.Month);
var ordersByMonth = Transactions
// Get orders of security
.GetOrders(x => x.Symbol == security.Symbol)
// Group by month
.GroupBy(x => x.Time.Month);
foreach (var item in ordersByMonth)
{
// Amount of trades in the stock per month
averageTradesPerMonth[item.Key] =
item.Count() / monthCount[item.Key];
// Average price per trade per month
averagePricePerTradesPerMonth[item.Key] =
item.Average(x => x.Price);
}
for (var i = 1; i < 12; i++)
{
Log(security.Symbol + "statistics. Month = " + i + ":");
Log("averageProfit: " + averageProfitPerMonth[i]);
Log("averageLiquidy: " + averageLiquidyPerMonth[i]);
Log("averageTrades: " + averageTradesPerMonth[i]);
Log("averagePricePerTrades: " +
averagePricePerTradesPerMonth[i]);
}
}
}
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.
Mike Vaysburd
78
,
Wow that is quite the load of information. So I would just place say AAPL in every place you have "stock"?
on the back of my second question can I do the above for say an exchange, like BATS or NYSE?
Many thanks
0
Alexandre Catarino
,
Hi Mike,
No, the snippet above would be applied to all the securities you added in your universe using AddSecurity method in Initialize.
In theory, you can make a study of a particular exchange data, since our tick data has a property with the name of the exchange the tick belongs to. However it is a lot of information and we have memory limitation.
If you have the data, you can set up Lean locally and do it.
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.
Mike Vaysburd
78
,
Hi Alex,
Is it possible for me to backtest foreign stocks as well? Such as Direct Energie, which is a French stock?
Thanks,
0
Alexandre Catarino
,
Currently, QuantConnect does not support foreign stocks. We can backtest CFDs of foreign indexes such as Europe 50 (EU50EUR), France 40 (FR40EUR) or Swiss 20 (CH20CHF) and futures of foreign indexes soon.
Alternatively, if you have the data (at your harddrive or from a website), you can import it.
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.
Mike Vaysburd
78
,
Unfortunately I don't have the data. Do you know of any place I could backtest or get said data?
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...
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!
You do not have enough QC Credit to send this award, get a QC Credit Pack
here.
Award Sent Successfully
Thank you for giving back to the community.
Processing...
Choose a Credit Pack
Payment Confirmation
QuantConnect Credit (QCC) can be applied to your cloud-invoices or
gifted to others in the community with Community Awards in recognition of their contributions.
Community Awards highlight the awesome work your fellow community members are doing and inspires
high quality algorithm contributions to the community. Select an option below to add
QuantConnect Credit to your account:
$5
500 Credit Points
 
$10
1,000 Credit Points
 
$18
2,000 Credit Points
10% bonus
$45
5,000 Credit Points
10% bonus
03/23XXXX XXXX XXXX 0592
We will charge your default organization credit card on file, click
here to update.