I've simplified the strategy so I can publish the script below. I made a few changes already and seems the TMF works better now. However, the TrailLock function remains a mystery to me - it simply doesnt execute at (in my case) 90% of the max profit of a trade. Do you have any advice for that too?
// ============== TRADER SCRIPT UPL, simplified for bug fixing ==============
int TMFLong()
{
Stop = priceLow(1)-Spread;
Trail = 2.9*ATR(1);
bool Cond_LongStop = priceHigh(0)<=priceHigh(1)<=priceHigh(2);
if(TradeResult>Trail) TrailLock = 90;
if(Cond_LongStop and TradeResult>Trail){
// exitLong();
return 1;
}
return 0;
}
int TMFShort()
{
Stop = priceHigh(1)+Spread;
Trail = 2.9*ATR(1);
bool Cond_ShortStop = priceLow(0)>=priceLow(1)>=priceLow(2);
if(TradeResult>Trail) TrailLock = 90;
if(Cond_ShortStop and TradeResult>Trail){
// exitShort();
return 1;
}
return 0;
}
// ============== DEFINE GLOBAL VARIABLES ==============
static bool initRun = true;
static int startHour = 8; // 8 is standard
static int endHour = 22; // 22 is standard
static string autoTradesCSV = "C:\\PROJECTS AND COMPANIES\\Project Watchdog\\results\\autoTrades.csv";
set(TICKS);
function run()
{
// ============== DEFINE LOCAL VARIABLES FOR EACH ITERATION ==============
char output[400];
var currEMA,valSRoC,stochDVal,deltaD0,CBand;
bool isLookBackPeriod,noConsolidation,withinVolatility, canTrade,closingHour;
double val;
var* Prices = series(price());
val = Prices[0];
//=================TRADING PARAMETERS ==============
BarPeriod = 2;
BarOffset = 0;
StartDate = 20130730;
EndDate = 20130731;
StartWeek = 10600; // start Monday 6 am
EndWeek = 52200; // end Friday 10 pm
Weekend = 3; // log off during the weekend
// ============== DEFINE STATIC VARIABLES ==============
static bool SRoC_Short_Prev = false;
static bool SRoC_Long_Prev = false;
// ============== INDICATORS ==============
// ============== SRoC ==============
currEMA = EMA(Prices,13);
var* EMAVals = series(currEMA);
valSRoC = (EMAVals[0]- EMAVals[21])*100/EMAVals[21];
var* SRoC = series(valSRoC);
// ============== INDICATOR ANALYSIS ==============
// ============== SRoC Analysis ==============
var DELTASROC_THRESH = 0.00;
var deltaSRoC_Curr = SRoC[0]-SRoC[1];
bool SRoC_Cond_Long = deltaSRoC_Curr>DELTASROC_THRESH;
bool SRoC_Cond_Short = deltaSRoC_Curr<-DELTASROC_THRESH;
// ============== SET TRADING HOURS ==============
isLookBackPeriod = (year()*10000+month()*100+day()<StartDate);
canTrade = workday and lhour(CET,0)>=startHour and lhour(CET,0)<endHour and !(isLookBackPeriod);
bool Cond_LongEntry = priceHigh(0)<priceHigh(1);
bool Cond_ShortEntry = priceLow(0)>priceLow(1);
// ============== PLOT LONG SHORT DOTS AND CALCULATE LONG / SHORT POSITIONS ==============
EntryTime = 1;
if(Cond_LongEntry and canTrade and SRoC_Cond_Long and NumOpenLong==0 and NumPendingLong==0){
Entry = priceHigh(0)+Spread;
enterLong(TMFLong);
}
if(Cond_ShortEntry and canTrade and SRoC_Cond_Short and NumOpenShort==0 and NumPendingShort==0){
Entry = priceLow(0)-Spread;
enterShort(TMFShort);
}
// ============== CLOSE ALL POSITIONS AT END OF THE DAY AND OUTPUT P&L ==============
closingHour = lhour(CET,0)==endHour-1 and minute()==60-2*BarPeriod and (!isLookBackPeriod);
if(closingHour){
closingHour = false;
exitLong();
exitShort();
sound("optimize.wav");
}
}C:\\PROJECTS AND COMPANIES\\Project Watchdog\\results\\autoTrades.csv
Description: Here are 2 examples where TrailLock = 90 obviously doesn't execute at 90% of intra-bar profit...