How would I implement a TMF that:
- Cancels other pending orders when one is hit
- When a trade is stopped out, sets a new pending order at last week's close in the opposite direction to the stopped trade
The strategy is an implementation of something I read on the web. Its rules: At the market open after the weekend, the set pending orders 50 pips above and below last week's close. Set a stop loss of 30 pips, no profit target. If stopped out, enter a new pending order in the opposite direction at last week's close with a stop loss of 30 pips. Hold the position until late in Friday's trading.
Its a simple strategy and one that I don't expect to work, still I would like to get the TMF working as this is something that I have yet to grasp fully.
My code for the strategy is below. The issues that I see at the moment are that the opposite pending order isn't cancelled when it should and that new trades are not entered at the previous week's close. I'm wondering if I am using the TradeVars inappropriately? I've plagiarised code that I found in the manual and the forums for performing similar functions.
Any advice much appreciated!
Cheers
#define H1 (60/BarPeriod)
#define H24 (1440/BarPeriod)
#define W1 (7200/BarPeriod)
#define LastWeekClose TradeVar[0]
#define CancelOtherTrades TradeVar[1]
int myTMF() { //if stopped out, set pending at W1close in opp direction
if (TradeIsLong and TradeIsStop) {
//enter at last close
Entry = LastWeekClose;
Stop = 30*PIP;
enterShort(myTMF);
}
if (TradeIsShort and TradeIsStop) {
Entry = LastWeekClose;
Stop = 30*PIP;
enterLong(myTMF);
}
if(TradeIsPending and CancelOtherTrades == 1)
return 1;
if(TradeIsOpen) {
CancelOtherTrades = 1;
ThisTrade->manage = 0; // terminate the TMF
}
return 0;
}
function frameAlign(BOOL Event){
TimeFrame = 1;
vars Num = series(0); // use a series for storing Num between calls
Num[0] = Num[1]+1; // count Num up once per bar
if(!Event)
TimeFrame = 0; // continue current time frame
else {
TimeFrame = -Num[0]; // start a new time frame
Num[0] = 0; // reset the counter
}
}
function run() {
set(TICKS); // normally needed for TMF
Weekend = 3; // don't run TMF at weekend
StartDate = 20141201;
EndDate = 20141231; //preserve 2015 data for OOS testing
BarPeriod = 60;
if (is(INITRUN)) CancelOtherTrades = 0;
TimeFrame = W1;
frameAlign(dow() == 7 and hour() == 23);
vars closeW1 = series(priceClose());
LastWeekClose = closeW1[0];
TimeFrame = H1;
int exitDay = 5;
int exitHour = 16;
if (!(dow() == exitDay and hour() >= exitHour)) {
Entry = closeW1[0] + 50*PIP;
Stop = 30*PIP;
if (NumOpenLong == 0) { enterLong(myTMF, LastWeekClose); }
Entry = closeW1[0] - 50*PIP;
if (NumOpenShort == 0) enterShort(myTMF, LastWeekClose);
}
if (dow() == exitDay and hour() == exitHour) {exitLong("*"); exitShort("*");}
plot("closeW1", closeW1, MAIN, RED);
}