Hi all,
AAPL had a 7:1 stock split on June 9, 2014. I set the algorithm to buy 100 shares of AAPL with a MarketOnOpenOrder on June 6 (before the split) and sell it with a MarketOnOpenOrder on June 10 (after the split). The initial cash was set to be $64700, so after buying 100 shares at 646.25, the remaining cash was 75 USD. The cash and AAPL stock position's holding value are displayed by Console.WriteLine(). The result was as follows:
Launching analysis for cbcef023c539813669e96315321da2cc with LEAN Engine v2.5.0.0.12209
06-03 O=634.0000 C=628.6500 Portfolio=64700.0 AAPL=0 Cash=64700.0
06-04 O=628.4700 C=637.5400 Portfolio=64700.0 AAPL=0 Cash=64700.0
06-05 O=637.2800 C=644.8200 Portfolio=64700.0 AAPL=0 Cash=64700.0
Order filled: 2014-06-06 @646.25x100
06-06 O=646.2500 C=647.3500 Portfolio=64810.00000 AAPL=64735.00000 Cash=75.000
06-07 O=650.0000 C=645.5700 Portfolio=64632.00000 AAPL=64557.00000 Cash=75.000
06-09 O=-1 C=-1 Portfolio=65185.190740668303811078475569 AAPL=64464.801500196000 Cash=720.38924047230381107847556861
Warning: all market orders sent using daily data, or market orders sent after hours are automatically converted into MarketOnOpen orders.
06-10 O=92.6900 C=93.7000 Portfolio=66216.689240472303811078475569 AAPL=65496.30000 Cash=720.38924047230381107847556861
Order filled: 2014-06-11 @94.64x-699
06-11 O=94.6400 C=94.2500 Portfolio=66873.749240472303811078475569 AAPL=0 Cash=66873.749240472303811078475569
06-12 O=94.1300 C=93.8600 Portfolio=66873.749240472303811078475569 AAPL=0 Cash=66873.749240472303811078475569
Algorithm Id:(cbcef023c539813669e96315321da2cc) completed in 0.56 seconds at 0k data points per second. Processing total of 67 data points.
Two questions:
1. On 06-09, Open and Close were both -1, meaning there was no AAPL's TradeBar in data.Bars, which was unexpected. The stock was actually traded on that day.
2. Also on 06-09, the cash became 720.38924047230381107847556861 USD instead of 75 USD. This should be incorrect.
謝永德
Forgot to mention:
1. The algorithm is backtested in the cloud.
2. The DataNormalizationMode is Raw.
Varad Kabade
Hi 謝永德,
On 06-09, Open and Close were both -1, meaning there was no AAPL's TradeBar in data.Bars, which was unexpected. The stock was actually traded on that day.
traders of 06-09(00.00) refers to Sunday, and the algorithm sends in the split because the split is not a bar. It's instantaneous. To see the trade data for 06-09, we need to use finer resolution(Hour, Minute) to get the information about trade bars during the day. When data is set to Raw mode, the dividends are paid as cash into your algorithm, and the splits are directly applied to your holding quantity. Thus we see the change in our cash to avoid this set data to default mode(Adjusted). Refer to the attached backtest to see the fixes,
Best,
Varad Kabade
Best,
Varad Kabade
謝永德
Hi Varad,
Thanks for the reply. I see that in your modification, the cash amount remained unchanged for the Adjusted mode. But there was actually no cash dividends for AAPL on 2014-06-09, only stock split. I assume that when there was no dividends, the cash should remains the same for all 4 DataNormalizationModes. Is this correct?
謝永德
Also, see my original backtest with the following calculations by hand:
1 Buy 100 shares at 646.25 on 06-06, that took 64625USD
2 After the split on 06-09, the 100 shares became 699 shares (Is the 1 share difference resulted from rounding error?). The portfolio's cash should remain unchanged.
3 Sell those 699 shares at 94.64 and received 94.64*699=66153.36USD in return. So these 2 trades brought the profit of 66153.36-64625=1528.36
In the Lean's calculation:
1 The cash became 720.3892, which is 720.3892-75=645.3892 more than it should be.
2. The final portfolio value was 66873.7492. The profit was 66873.7492-64700=2173.7492, which is 2173.7492-1528.36=645.3892 USD more than the result calculated by hand.
Compare these results, we can see that the difference solely came from the increase in the cash amount.
Varad Kabade
Hi 謝永德,
In the above algorithm, the AAPL undergoes a split factor of 0.1428572. The algorithm takes the floor of the resultant Holding(692.99=~692), and the difference is seen in our cash which is not observed when we have data in Adjusted mode because it is already calculated/adjusted backward in time. To resolve this, we have created a GitHub issue. Please subscribe to the following for updates.
https://github.com/QuantConnect/Lean/issues/5765
Best,
Varad Kabade
謝永德
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!