MultiPosition Strategy: How to define and use the EntryPrice of the first position?
Author: lookingbackon
Creation Date: 1/16/2015 1:47 AM
profile picture

lookingbackon

#1
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.
profile picture

Eugene

#2
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.
profile picture

lookingbackon

#3
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.
profile picture

Eugene

#4
Proposed refactoring to your code:

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

lookingbackon

#5
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.
profile picture

Eugene

#6
Position.AllPositions is mentioned in every exit method's description (Trading > Sell*, Cover*, Exit*).
profile picture

lookingbackon

#7
It's clear now. Thanks
profile picture

lookingbackon

#8
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
profile picture

Eugene

#9
Hello Peng,

Here's one out of possible ways:
CODE:
Please log in to see this code.
profile picture

lookingbackon

#10
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.
profile picture

Eugene

#11
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.
profile picture

lookingbackon

#12
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
profile picture

Eugene

#13
You're welcome.
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).