I wrote some code to estimate and compare the tax efficiency of an algorithm. See the attached code. You will see 2 additional fields as shown below:
Please note:
- I use LIFO (last in first out).
- My unrealized gain is different from QuantConnect's. Wondering this can be attributed to LIFO and timing of the calculations (end of the month vs end of the day)
I am sharing it here with the following in mind:
- The community can benefit from it.
- The community can suggest ideas on how to improve it further, For example,:
- Current implementation requires code insertions in multiple places in your algo. Is there way to simplify that?
- The community can share updated and improved versions of the code.
- QuantConnect team gets inspiration from this and incorporates. tax efficiency in their core platform as well as alpha marketplace,
Best,
Manoj
Derek Melchin
Hi Manoj,
Thanks for sharing this with the community! The values for the unrealized gains are about $250K apart because we need to recalculate its value at the end of the backtest.
See the attached backtest for reference. With this change, the values are only $7K apart. The remaining discrepancy can be a result of the built-in calculation incorporating other costs into its formula, including spread costs and commissions. Refer to the source code here.
Best,
Derek Melchin
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.
Manoj Agarwala
Derek,
Thanks for the inputs on how to bridge the discrepancy. Does this mean that Lean also uses LIFO for calculating the unrealized gains?
My current implementation requires the plugging of CalculateTaxes at 10 different places in QCAlgorithm. Wondering if CalculateTaxes can directly subscribe to data (splits/dividends) and order events reducing the number of places where it needs to be plugged in QCAlgorithm? If yes, sample code will be helpful.
Derek Melchin
Hi Minoj,
When calculating the unrealized profit for a given security, LEAN doesn't use LIFO or FIFO. The cost of the holdings is simply the average purchase price of the holdings. See the attached backtest for reference.
To subscribe to splits and dividends, we could add
to the OnData method.
Best,
Derek Melchin
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.
Manoj Agarwala
Derek,
I am already handling the splits and dividends in the OnData method. My current implementation looks as follows:
class UncoupledMultidimensionalAutosequencers(QCAlgorithm):
def Initialize(self):
if calculate_taxes:
self.calculateTaxes = CalculateTaxes()
self.calculateTaxes.MyInitialize(self)
def OnData(self, slice):
if calculate_taxes:
self.calculateTaxes.OnData(slice)
This is cumbersome as I need to insert these lines in every algorithm. I was hoping to simplify the implementation by subscribing to data in calculateTaxes.MyInitialize method. Does such an API exist?
Derek Melchin
Hi Manoj,
We can't subscribe a class to receive updates when the `OnData`, `OnOrderEvent`, and `OnEndOfAlgorithm` methods are called without adding the `if` statements above. To avoid adding the `if` statements, the QCAlgorithm base class would need to be extended to reference the CalculateTaxes class.
Best,
Derek Melchin
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.
Manoj Agarwala
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!