Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asset.Monthly().Open and asset.Monthly().Close does not work the way you would think #17

Open
mathewsk opened this issue Jul 18, 2023 · 1 comment

Comments

@mathewsk
Copy link

I found something strange in the TuringTrader.org. I am using the latest version,
I am not sure if this is a bug in the API or a problem with my understanding.
Whenever you have some time, if you would please let me know your thoughts, I would appreciate it!

var asset = Asset(ETF.SPY);

On SimDate of Jan 31, 2020, if I do
asset.Monthly().Open[0] I get 309.3909221661874
asset.Monthly().Close[0] I get 304.40472412109375
I thought that using asset.Monthly().Open[0], I would get the opening price of the month January, 2020 (306.1172745036964) instead of 309.3909221661874

If I print the daily Open and Close each of the month of Jan, 2020 I get this

1-2-2020 daily open=306.1172745036964 daily close=307.3756408691406
1-3-2020 daily open=303.8654391035394 daily close=305.0481262207031
1-6-2020 daily open=303.2316099598491 daily close=306.2120056152344
1-7-2020 daily open=305.625314816108 daily close=305.3509521484375
1-8-2020 daily open=305.5496066207918 daily close=306.9783020019531
1-9-2020 daily open=308.59624078058306 daily close=309.0598449707031
1-10-2020 daily open=309.6653735147707 daily close=308.1704406738281
1-13-2020 daily open=308.81386449573563 daily close=310.28985595703125
1-14-2020 daily open=309.8357155822098 daily close=309.8168029785156
1-15-2020 daily open=309.7220834882384 daily close=310.516845703125
1-16-2020 daily open=311.9456106028352 daily close=313.09991455078125
1-17-2020 daily open=313.83783393783926 daily close=314.0743713378906
1-21-2020 daily open=313.080927001925 daily close=313.4593811035156
1-22-2020 daily open=314.3488436155879 daily close=313.497314453125
1-23-2020 daily open=312.8254812932716 daily close=313.8567810058594
1-24-2020 daily open=314.5380256021345 daily close=311.0656433105469
1-27-2020 daily open=305.6347152551311 daily close=306.07940673828125
1-28-2020 daily open=307.5554559419741 daily close=309.28692626953125
1-29-2020 daily open=310.6966045755457 daily close=309.0313720703125
1-30-2020 daily open=306.8931082857648 daily close=310.0343322753906
1-31-2020 daily open=309.3909221661874 daily close=304.40472412109375

@fbertram
Copy link
Owner

TimeSeriesAsset.Monthly() indeed simply returns the last OHLCV bar of the month - which is not what most users would want or expect. This is a known issue and mentioned here:

// BUGBUG: this returns the last OHLCV bar of each weekly period

For the time being, users can work around the issue like this using TimeSeriesFloat.Monthly():

       SimLoop(() =>
        {
            var daily = Asset(ETF.SPY);

#if false
// this doesn't work as expected
var monthly = daily.Monthly();

            Output.WriteLine("{0:MM/dd/yyyy}: daily: o={1:C2}, c={2:C2}, monthly: o={3:C2}, c={4:C2}",
                SimDate, daily.Open[0], daily.Close[0], monthly.Open[0], monthly.Close[0]);

#else
// this is a suitable workaround
var monthlyOpen = daily.Open.Monthly(1);
var monthlyClose = daily.Close.Monthly(0);

            Output.WriteLine("{0:MM/dd/yyyy}: daily: o={1:C2}, c={2:C2}, monthly: o={3:C2}, c={4:C2}",
                SimDate, daily.Open[0], daily.Close[0], monthlyOpen[0], monthlyClose[0]);

#endif
});
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants