When does the PosSizer get a non-null Position
Author: Frank1972
Creation Date: 1/12/2017 5:01 PM
profile picture

Frank1972

#1
I'm writing a custom position sizer and trying to figure out how it works. First here is my setup:

Strategy Code:
CODE:
Please log in to see this code.


What I'm trying to do here is just place an order on the last bar and trigger the position sizing code. The data that I'm using is 1 minute for SPY on 12/12/2016. I gathered this using Fidelity data setting the "Data Range" to start on 12/11/2016 and end on 12/12/2016. Since 12/11/2016 was a Sunday the data downloaded is for 12/12/2016 only.

My PosSizer code:
CODE:
Please log in to see this code.


I place a break point on
CODE:
Please log in to see this code.
line. The bar data for the last bar on 12/12/2016 is
CODE:
Please log in to see this code.


What I am seeing is this:

Set the BuyAtLimit( bar + 1, 226.15) => At the break point in Visual Studio in the Debug Window Locals I see this:
CODE:
Please log in to see this code.


Stack Trace is this:
CODE:
Please log in to see this code.


I press F5 to continue running the code and the break point is hit again. At the break point in Visual Studio in the Debug Window Locals I see this:
CODE:
Please log in to see this code.


Stack Trace is this:
CODE:
Please log in to see this code.


It is at this point that Wealth Lab plays the audio alert signifying an alert. But then the break point is hit again. At the break point in Visual Studio in the Debug Window Locals I see this:
CODE:
Please log in to see this code.


Stack Trace is this:
CODE:
Please log in to see this code.


So I have placed the limit order in the middle of the bar and my position sizer gets called 3 times. Once with the basisPrice equal to the close of the bar, once with the basisPrice equal to the limit price and then again with basisPrice equal to the close of the bar. All three times the currentPos is equal to null. Also the PrintDebug prints "position is null". Because the position is null I am not able to set the RiskStopLevel, because I need a position to do that.

It doesn't matter if I move the BuyAtLimit price to 226.30 (above the high) or to 226.00 (below the low) I get the same pattern.

So with all that, here is my question: The position sizing algorithm that I am trying to write needs to know the RiskStopLevel in order to calculate that number of shares. How can I get this information when BuyAtLimit doesn't return a Position object that I can use to set this? The setup here isn't how I am planning to use in "real trading". I am planning to trade on 1 minute bars. So when my strategy triggers a trade it will call BuyAtLimit on the last bar, this could be at 10:00 AM EST. So I want to be able to calculate the number of shares needed so that the BuyAtLimit alert will have the number of shares based on the RiskStopLevel. Is this possible?

Please let me know if you need any more information to help in reproducing or understanding what I am asking for.

Thanks,

-Frank
profile picture

Eugene

#2
Hi Frank,

QUOTE:
So I have placed the limit order in the middle of the bar and my position sizer gets called 3 times.

If you right click on the chart you should see those "Buy/Short X shares" options. So the unexpected PosSizer calls are made by Wealth-Lab to support this "right-click trading" feature.

QUOTE:
When does the PosSizer get a non-null Position

When the Position is not an Alert. For sizing Alerts, the Position is null.

QUOTE:
How can I get this information when BuyAtLimit doesn't return a Position object that I can use to set this?

Here's a little trick from the Wiki FAQ to help you achieve this: How to pass a value from Strategy to PosSizer for Alert sizing?
profile picture

Frank1972

#3
Eugene,

Thank you for the reply. That makes sense and things are working now!!


-Frank
profile picture

Eugene

#4
Great news!
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).