Back

I need help using this for a paper

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

Thanks,
M

Update Backtest








Hi Mike,

Please checkout the documentation on how to access the information that the algorithm holds on each security on section Securities and Portfolio:

// 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

// Popular Portfolio Property Values:
Portfolio["IBM"].Invested
.IsLong // IsLong, IsShort Holdings.
.Quantity // Shares held.
.UnrealizedProfit; // Holdings profit/loss
.TotalFees // Fees since backtest start
.Price; // Asset price

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.


Cheers!
0

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

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

foreach (var item in monthlyProfit)
{
averageProfitPerMonth[item.Key] =
item.Sum(x => x.Value) / monthCount[item.Key];
}

// 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);

foreach (var item in monthlyLiquidity)
{
averageLiquidyPerMonth[item.Key] =
item.Average(x => x.Volume);
}

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.


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

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.


Hi Alex,

Is it possible for me to backtest foreign stocks as well? Such as Direct Energie, which is a French stock? 

Thanks,

0

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.


Unfortunately I don't have the data. Do you know of any place I could backtest or get said data?

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