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