Hello quants, here's a hopefully interesting question.
So, LEAN uses the “cost averaging accounting method”, which determines the cost of your holdings by taking a weighted average of the sum of all your BUY prices. (Ref: https://www.quantconnect.com/docs/v2/writing-algorithms/portfolio/key-concepts#03-Cost-Averaging-Accounting). It could really be called AverageBuyPrice, because In this logic any SELL operations are not accounted for.
However, I would like to find a metric – and I believe it must exists, hence my question – to account for sales too.
Let's make an example of how QC averagePrice will printout:
buy +10 stocks @ 5 usd : averagePrice = ( 10*5 ) / 10 = 5
buy +2 @ 4 : averagePrice = ( 10*5 + 2*4 ) / 12 = 4.83
sell -2 @ 4.5 : averagePrice = ( 10*5 + 2*4 ) / 12 = 4.83 # we completely ignore this sale
buy +2 @ 4 : averagePrice = ( 10*5 + 2*4 + 2*4 ) / 12 = 5.5
Intuitively speaking, how can it be that my averagePrice is now higher than the value I ever bought the stock at (which was at 5)?
I am trying to find a metric that includes the sales, however I am not an economist, and don't know if there is a terminology for this.
Look at the following now:
buy +10 stocks @ 5 usd : averagePrice = ( 10*5 ) / 10 = 5
buy +2 @ 4 : averagePrice = ( 10*5 + 2*4 ) / 12 = 4.83
sell -2 @ 4.5 : averagePrice = ( 10*5 + 2*4 - 2*4.5 ) / 10 = 4.9 # this time we consider this sale
buy +2 @ 4 : averagePrice = ( 10*5 + 2*4 - 2*4.5 + 2*4 ) / 12 = 4.75
Again, intuitively, this makes more sense:
- we sold stocks and made a profit over the second operation, since now we have less stocks that are at a lower value than 5, the average price should go up as it does
- and then we bought again at a lower price, lowering the average to 4.75. To me this makes more sense than 5.5.
I hope I make sense. This sounds terribly silly and I fear I am overseeing something very obvious.
Hence why I'd like to ask people here.
Paul
Paulduring
AH! Ignore the above, ( 10*5 + 2*4 + 2*4 ) / 14 = 4.71 :)
Paulduring
It would be nice to have AverageBuyPrice (buys only), averageSellPrice (sells only), AveragePrice (buys & sells).
I can calculate it myself of course (hopefully not like the above silly mistake haha).
Derek Melchin
Hi Paulduring,
AveragePrice already reflects the buys and sells. To track the average buy price and average sell price, we can monitor the order fills in the OnOrderEvent method.
Best,
Derek Melchin
Want to invest in QuantConnect as we build the Linux of quant finance? Checkout our Wefunder campaign to join the revolution.
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.
Paulduring
Yes I am using onOrder now and I am printing it: but averagePrice IMHO tracks only buys since its value stays the exact same after you sell something. This is why is confusing, it should be really called averageBuyPrice. And IMHO the api should also offer a averageSellPrice. Do you think this could be added?
Derek Melchin
Hi Paulduring,
We updated the documentation to elaborate on the AveragePrice property. Indeed, it represents both buys and sells, and works for both long and short trades. Please refer to Cost Averaging Accounting.
Best,
Derek Melchin
Want to invest in QuantConnect as we build the Linux of quant finance? Checkout our Wefunder campaign to join the revolution.
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.
Paulduring
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!