Hi guys
I’ve been trying to put together a “dynamic” exit strategy which can be applied across strategies and products. I’ve been helped by a few of you so far (Nicolas on the calculation of the Value Areas and Henry on the difficult bit of the actual exit). The discussion about the exit is in the following link so I won’t repeat it here:
http://www.prorealcode.com/topic/how-to-code-target-and-stop/
The exit is almost there although I’m still in discussion with PRT as I’ve spotted a few anomalies which we’re trying to rectify. I’ve just been testing it in demo and encountered a problem with it over buying (and on another occasion over selling). It seems to happen when there is a partially executed entry. I’ve looked at the order list and clicked on the order status history.
I’ve pasted below the order tickets. Long entry 3 Dax:
Date
Side
Name
Status
Qty
Qty exec
Price
17:15:01
Buy
DAX30 Only1216
Transmitted
3
17:15:01
Buy
DAX30 Only1216
Pending
3
17:15:01
Buy
DAX30 Only1216
Partially executed
3
2
11,185.50
17:15:01
Buy
DAX30 Only1216
Executed
3
1
11,186.00
As a separate order (or ticket) is the following:
Date
Side
Name
Status
Qty
Qty exec
Price
17:15:01
Buy
DAX30 Only1216
Transmitted
2
17:15:01
Buy
DAX30 Only1216
Pending
2
17:15:01
Buy
DAX30 Only1216
Executed
2
2
11,185.50
The result is that I buy 3 lots as I wanted, but then also an additional 2 lots = net position of 5. However the extra 2 lots are “naked” and unwanted (i.e. they do not fit with the exit targets and are not covered with the stops).
A similar thing happened previously on a sell order however in this instance the system partially executed 1 lot out of three to begin, and then over bought by 1 lot so the position was short 4.
I’m pasting the code below. The entry strategy sucks and doesn’t work. I’m not interested in this at the moment. As I said I’m just trying to get the exit sorted into a transferable “product” which can then be used by myself and anyone as they want.
Have any of you come across a similar problem / can you see where I’m going wrong? As always, any help much appreciated.
Thanks
Jon
// Excluding around Brexit referendum AND NFP
IF ((Year = 16) AND (Month = 6) AND (Day = 20 OR Day = 21 OR Day = 22 OR Day = 23 OR Day = 24 OR Day = 27 OR Day = 28 OR Day = 29 OR Day = 30)) OR (Day<8 AND DayOfWeek=5) THEN
TradingDay = 0
ELSE
TradingDay = 1
ENDIF
// Conditions to enter long positions
c1 = (close > open)
c2 = (TradingDay = 1)
//Caluculation of Value Areas
mean = (Dhigh(0)+Dlow(0))/2
valuezone = (DHigh(0)-DLow(0)) * 0.68
//lowerVA = mean-valuezone/2
upperVA = mean+valuezone/2
c3 = (close > upperVA)
indicator2 = ExponentialAverage[20](close)
indicator3 = ExponentialAverage[40](close)
c4 = (indicator2 > indicator3)
//Position sizing
IF ((DayOfWeek=1) OR (DayOfWeek=3)) THEN
PosSize = 6
ELSE
PosSize = 3
ENDIF
//Price targets & stops
Tar1 = 10
Tar2 = 20
FxStp = 10
TrStp = 3
IF c1 AND c2 AND c3 AND c4 THEN
BUY PosSize SHARES AT MARKET
sell (PosSize/3) share at close+(Tar1*pointsize) limit
sell (PosSize/3) share at close+(Tar2*pointsize) limit
set stop ploss FxStp
ENDIF
// Stops and targets
If LongOnMarket THEN
if countoflongshares=PosSize then
sell (PosSize/3) share at tradeprice+(Tar1*pointsize) limit
sell (PosSize/3) share at tradeprice+(Tar2*pointsize) limit
elsif countoflongshares=((PosSize/3) * 2) then
sell (PosSize/3) share at tradeprice(2)+(Tar2*pointsize) limit
sell at tradeprice(2) stop
set stop ploss 0
elsif countoflongshares=(PosSize/3) then
set stop ptrailing TrStp
ENDIF
ENDIF