3 registered members (Akow, AndrewAMD, Quad),
733
guests, and 2
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
TMF that cancels pending orders and enters trades
#452309
06/09/15 07:48
06/09/15 07:48
|
Joined: Apr 2014
Posts: 482 Sydney, Australia
boatman
OP
Senior Member
|
OP
Senior Member
Joined: Apr 2014
Posts: 482
Sydney, Australia
|
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);
}
|
|
|
Re: TMF that cancels pending orders and enters trades
[Re: jcl]
#452316
06/09/15 09:09
06/09/15 09:09
|
Joined: Apr 2014
Posts: 482 Sydney, Australia
boatman
OP
Senior Member
|
OP
Senior Member
Joined: Apr 2014
Posts: 482
Sydney, Australia
|
Thanks, that makes sense. I've changed my script to use global variables instead of TradeVars. My script now enters just one trade for the entire simulation period. After it is stopped out, no new trades are entered. Therefore, I think there is an error in the TMF script. Any ideas? Updated script:
#define H1 (60/BarPeriod)
#define H24 (1440/BarPeriod)
#define W1 (7200/BarPeriod)
var LastWeekClose;
int CancelOtherTrades;
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);
|
|
|
Re: TMF that cancels pending orders and enters trades
[Re: jcl]
#452443
06/12/15 03:17
06/12/15 03:17
|
Joined: Apr 2014
Posts: 482 Sydney, Australia
boatman
OP
Senior Member
|
OP
Senior Member
Joined: Apr 2014
Posts: 482
Sydney, Australia
|
For those that are interested, here is my solution to the problem of changing the entry level of the second trade to the weekly close. I used counters and a second TMF. I'm sure there would be a more elegant solution, but this one works. As to the profitability of the system, that is another story altogether. If anyone has any ideas for improving the performance, I'd love to hear them.
#define H1 (60/BarPeriod)
#define H24 (1440/BarPeriod)
#define W1 (7200/BarPeriod)
int myTMF1() {
if (TradeIsEntry) {
AssetVar[2] += 1;
if (TradeIsLong) AssetVar[3] += 1;
if (TradeIsShort) AssetVar[4] += 1;
}
return 0;
}
int myTMF() {
if (TradeIsEntry) {AssetVar[2] = AssetVar[2] + 1;
if (TradeIsLong) AssetVar[3] += 1;
if (TradeIsShort) AssetVar[4] += 1;
}
if (TradeIsLong and TradeIsStop and AssetVar[4] < 1){
enterShort(myTMF);
}
return 0;
if (TradeIsShort and TradeIsStop and AssetVar[3] < 1) {
enterLong(myTMF);
}
return 0;
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|PARAMETERS|FACTORS|LOGFILE);
Weekend = 3; // don't run TMF at weekend
StartDate = 20140901;
EndDate = 20141231; //preserve 2015 data for OOS testing
BarPeriod = 60;
LookBack = 209;
if (is(INITRUN)) { AssetVar[0] = 0; AssetVar[1] = 0; AssetVar[2] = 0; AssetVar[3] = 0; AssetVar[4] = 0; }
TimeFrame = W1;
frameAlign(dow() == 7 and hour() == 23); //align weekly chart to a UTC open time of Sunday 2200 (note it is 2100 in summer)
vars closeW1 = series(priceClose());
AssetVar[1] = closeW1[0];
TimeFrame = H1;
if (dow() == 7 and hour() == 23) { AssetVar[2] = 0; AssetVar[3] = 0; AssetVar[4] = 0; } //reset to zero at start of each week
int exitDay = 5;
int exitHour = 16;
var entryMultiple = 4;//
Stop = 30 *PIP;
Trail =2 * entryMultiple * ATR(168); // optimize(0.5, 0.25, 3.0, 0.25) * entryMultiple * ATR(168); //
if (AssetVar[2] == 0) {
if (!(dow() == exitDay and hour() >= exitHour)) {
Entry = closeW1[0] + entryMultiple*ATR(168);
if (NumOpenLong == 0) enterLong(myTMF1);
Entry = closeW1[0] - entryMultiple*ATR(168);
if (NumOpenShort == 0) enterShort(myTMF1);
}
}
if (AssetVar[2] > 0) {
if (!(dow() == exitDay and hour() >= exitHour)) {
Entry = closeW1[0];
if (NumOpenLong == 0 and AssetVar[3] < 1) { enterLong(myTMF);}
if (NumOpenShort == 0 and AssetVar[4] < 1) { enterShort(myTMF);}
}
}
if (dow() == exitDay and hour() == exitHour) {exitLong("*"); exitShort("*");}
plot("closeW1", closeW1, MAIN, RED);
}
|
|
|
|