#define H1 (60/BarPeriod)
#define H24 (1440/BarPeriod)
#define W1 (7200/BarPeriod)
//var LastWeekClose;
//#define CancelOtherTrades AssetVar[0];
int myTMF() { //if stopped out, set pending at W1close in opp direction
var LastWeekClose = AssetVar[1];
if (TradeIsLong and TradeIsStop) {
//enter at last close
TradeEntryLimit = LastWeekClose;
TradeStopLimit = 30*PIP;
enterShort(myTMF);
}
if (TradeIsShort and TradeIsStop) {
TradeEntryLimit = LastWeekClose;
TradeStopLimit = 30*PIP;
enterLong(myTMF);
}
if(TradeIsPending and AssetVar[0] == 1) {
return 1;
}
if(TradeIsOpen) {
AssetVar[0] = 1;
ThisTrade->manage = 0; // terminate the TMF
}
return 0;
}
function frameAlign(BOOL Event){
// Let time frame start when Event == true
// f.i. frameAlign(hour() == 0); aligns to midnight
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)) AssetVar[0] = 0;
// CancelOtherTrades = 0;
TimeFrame = W1;
frameAlign(dow() == 7 and hour() == 23); //align weekly chart to a UTC open time of Sunday 2200
vars closeW1 = series(priceClose());
AssetVar[1] = 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); }
Entry = closeW1[0] - 50*PIP;
if (NumOpenShort == 0) enterShort(myTMF);
}
if (NumPendingLong + NumPendingShort == 0) AssetVar[0] = 0;
if (dow() == exitDay and hour() == exitHour) {exitLong("*"); exitShort("*");}
plot("closeW1", closeW1, MAIN, RED);
}