Customized PosSizer with zero quantity of buy alerts
Author: llhk
Creation Date: 6/25/2012 4:11 AM
profile picture

llhk

#1
I have created my own possizer and using tag to specify the size of position each time I enter, but I found that it is fine in strategy testing and trade details, but when i checked it on the alert, i could only find all buy alerts in zero quantity.

strategy code:
CODE:
Please log in to see this code.



Possizer:
CODE:
Please log in to see this code.
profile picture

Eugene

#2
Check this FAQ out:

FAQ | Miscellaneous, Extensions > How to pass a value from Strategy to PosSizer for Alert sizing?

It contains both an explanation of your error and a workaround.
profile picture

llhk

#3
I read it before, but i don't understand what it mean, does it mean i have no hope to get my quantity in alerts for next day buy before market opening?

QUOTE:

How to pass a value from Strategy to PosSizer for Alert sizing?

Problem:

You're using currentPos.Tag to size positions in a PosSizer which works only if currentPos is not null i.e. not for Alerts.



profile picture

Eugene

#4
Additionally, in some scenarios Convert.ToDouble(currentPos.Tag) may generate a FormatException when you inadvertently pass an integer number to it e.g. LastPosition.Tag = 2 while a double value is expected. This may require special handling (for instance, by wrapping in a try/catch block).
profile picture

Eugene

#5
QUOTE:
I read it before, but i don't understand what it mean,

It means that when currentPos == null, Wealth-Lab is sizing an Alert, so the Position does not exist yet. Therefore you can't consume its properties (including the .Tag property), so the usage of Position.Tag in this particular case is inappropriate and you should achieve the same goal by using Bars.Tag (when sizing an Alert). And when currentPos != null (sizing the backtest trades), you can continue using your existing Position.Tag logic.

Please carefully read that FAQ answer to its end, and follow the external source that it suggests to review (the QuickRef), and you'll find the complete solution:
QUOTE:
For a complete code snippet, refer to the Bars.Tag example in the Wealth-Lab QuickRef.

profile picture

llhk

#6
First, Eugene, thanks for your patience and guidance towards the Bars.Tag example.

I got your meaning if I am correct, you means I have to write 2 separate codes, one for backtest, another for getting daily alerts, right?

I have changed my code like this
CODE:
Please log in to see this code.


however, it still gives me buy alerts with quantity in zero.
profile picture

Eugene

#7
QUOTE:
however, it still gives me buy alerts with quantity in zero.

Of course, because you still have to teach your PosSizer to recognize the Bars.Tag. The changes are required to both your Strategy and the PosSizer, just as you will find explained above.

In PosSizer (pseudocode):
CODE:
Please log in to see this code.
profile picture

llhk

#8
According to your code, I have rewritten my own, but still can't do so.

Backtesting is fine, but not for alerts

Code for backtesting:
In strategy code:
CODE:
Please log in to see this code.


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



Code for alert:
In strategy code:
CODE:
Please log in to see this code.


In PosSizer:
Same as above

is there anything I missed? Thanks in advance.
profile picture

Eugene

#9
What does the Visual Studio debugger show?
Have you already stepped through the executing code?
profile picture

Eugene

#10
For me it works correctly.
profile picture

llhk

#11
Yeah, it works correctly now, thanks for your help!
profile picture

llhk

#12
however, it won't have any sell alerts as it never actually buy in and so no holdings for sell alert to generate out.
profile picture

Eugene

#13
That's different question. For example?
profile picture

llhk

#14
because my code for generating the alerts are using the technique like the following

CODE:
Please log in to see this code.


it doesn't actually make any entry in the testing, but buy alerts. Since no holding in my portfolio, it will not trigger any sell signal (alerts) in the "alert" tab. I am now using my backtesting code to generate the sell signal instead.

However, another problem comes out, that is my strategy is single trade for each symbol, without any holding records in the portfolio, it will generate another buy signal once the criteria meets but in fact i might already have this symbol bought. It is still ok for me if not too much alerts giving out, only see if there is any advice for this issue.

Thanks~
profile picture

Eugene

#15
In fact, there's no need to modify the entry logic anyhow. Just assign a value to both Bars.Tag and Position.Tag at the same time and make the decision in the PosSizer in a try/catch block (just as you're already doing).
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).