Hello,
Would like to have your support on the captioned issue. The issue is exactly like that when I try to compile the following code(part of the strategy code), there was an "error" suggested the variable "firstEntryPrice" was used without value assignment.
The details is that:
CODE:
Please log in to see this code.
So my question is hwo to properly handle the firstEntryPrice so that it can be used in the following if statement.
Thanks in advance
P.S.: due to more that 2 positions are potentially placed, the "LastPosition.EntryPrice" does not fit this case.
Size:
Color:
CODE:
Please log in to see this code.
However, the structure of your script looks suspicious to me. It inherits from the single-position template whereas it's made managing multiple positions. You might want to reveal it for us to check.
Size:
Color:
Hi Eugene,
Thanks a lot. It works now.
You are right. The script inherits from my old single-position strtegy. The trading loop code is as following. Would be great if you can help to check whether there is better solution. This strategy is only for "Short Trading" backtest purpose. The separate "Long Trading" script has the same structure.
CODE:
Please log in to see this code.
Size:
Color:
Proposed refactoring to your code:
CODE:
Please log in to see this code.
Size:
Color:
The code looks more better.Thanks a lot!
BTW, is it the normal way to check the ful set property/method of such as Position object on the Wiki? Cause I fail to find the Position.Allpositions property via the WLD QuickRef.
Size:
Color:
Position.AllPositions is mentioned in every exit method's description (Trading > Sell*, Cover*, Exit*).
Size:
Color:
It's clear now. Thanks
Size:
Color:
Hello Eugene,
One more issue for your support.
Based on the above code, is it possible to define/claim another variable as below?
CODE:
Please log in to see this code.
The idea is to create a variable of the average entry price of all positions ( less than 5 positions), so that it can be used as the condition of the stop order.
Thanks
Size:
Color:
Hello Peng,
Here's one out of possible ways:
CODE:
Please log in to see this code.
Size:
Color:
Thanks for the solution.
Basically, the above multi-position trading idea is based on the firstEntryPrice. If I would change it with the LastActivePosition.EntryPrice of each previous position as showed in the following idea, then there is a chance that three more positions take place on the same bar which is not the original idea at all. What's the problem and possible solution on that?
Thanks
CODE:
Please log in to see this code.
Size:
Color:
A. How about a non-programming solution?
1. "Max Entries Per Day" PosSizer
2.
Position Options PosSizer > Max Pos. Per Day
B. If this is not what you're after (I think it may skip entries), then:
1. Create an integer
counter for number of positions taken on every bar
CODE:
Please log in to see this code.
2. The first thing after the trading loop, you reset that
counter to 0 (each bar)
CODE:
Please log in to see this code.
3. Each entry increments the counter after making sure that a new position has been opened e.g.
CODE:
Please log in to see this code.
4. Ignore subsequent entries if
counter > 1 e.g.
CODE:
Please log in to see this code.
Size:
Color:
Hello Eugene,
Thanks a lot. It seems the solution B is almostly a perfect solution. I just make a slight change on the "counter" condition which should always be "counter<1". Otherwise, there is still the chance of 2 positions with same entryprice on one same bar.
Thanks
Size:
Color:
You're welcome.
Size:
Color: