Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How can len(context.portfolio.positions) be fractional?

Just curious, I'm tracking number of positions with record(numPositions=len(context.portfolio.positions)), but sometimes it is fractional. How should this be interpreted?

3 responses

Matt,

The chart probably can't display the quantity of positions at the frequency you set it to record. The result is likely that it is taking the average of 2 or more counts of your position quantity, leading to the fractions. Try scheduling a function to run once per day (or week) and record the quantity of positions in that function.

It's a good question and I can explain it. What Frank said is in the right direction and just depends on the length of the run.

Due to chart smoothing, feeding integers to the chart, the values sometimes show floating point numbers. After the backtest, move the chart sliders to zoom in on an area, and when the display area is small enough, the chart will redraw with less smoothing eventually to only the actual-value original integers.

By the same token, with a slow run for example, watching the chart, one can sometimes notice spikes that are then redrawn as the run continues so that they disappear. Zooming in, they will reappear.

So the chart is a general idea while more precision is available when needed with extra effort.

Also, more generally: If multiple values are being recorded each day, the only one recorded is the last value of the day because daily is its max resolution. Charting maximums can sometimes be useful.

Ah, makes perfect sense. Thanks!