// Workshop 5: Counter trend trading, optimized ////////////////
function reverseShort(int MaxTrades)
{
// update the stops and profit targets of open trades
if(Stop > 0) exitShort(0,priceClose()+Stop);
if(TakeProfit > 0) exitShort(0,-(priceClose()-TakeProfit));
// if MaxTrades is not reached, open a new trade
if(NumOpenShort < MaxTrades)
{
// reinvest the square root of profits
//var MarginShort = sqrt(WinShort-LossShort)*OptimalFShort;
//Margin = clamp(MarginShort, 5, 300); //max $300 per trade
enterShort();
}
// otherwise, close all opposite trades
else if(!mode(HEDGING)) exitLong();
return 0;
}
function reverseLong(int MaxTrades)
{
// update the stops and profit targets of open trades
if(Stop > 0) exitLong(0,priceClose()-Stop);
if(TakeProfit > 0) exitLong(0,-(priceClose()+TakeProfit));
// if MaxTrades is not reached, open a new trade
if(NumOpenLong < MaxTrades)
{
// reinvest the square root of profits
//var MarginLong = sqrt(WinLong-LossLong)*OptimalFLong;
//Margin = clamp(MarginLong, 5, 300); //max $300 per trade
enterLong(); }
//otherwise, close all opposite trades
else if(!mode(HEDGING)) exitShort();
return 0;
}
function run()
{
set(PARAMETERS+FACTORS+NFA); // generate and use optimized parameters
BarPeriod = 240; // 4 hour bars
LookBack = 500;
StartDate = 2002;
//EndDate = 2009;
NumWFOCycles = 5; // activate WFO
if(ReTrain) {
UpdateDays = -1; // update price data from the server
SelectWFO = -1; // select the last cycle for re-optimization
}
// portfolio loop
while(asset(loop("EURUSD","AUDCAD","AUDCHF","AUDJPY","AUDNZD","AUDUSD","CADJPY","CHFJPY")))
//"EURAUD","EURCAD","EURCHF","EURDKK","EURGBP","EURJPY","EURNZD","GBPAUD","GBPCAD","GBPCHF",
//"GBPJPY","GBPNZD","GBPUSD","NZDCHF","NZDCHF","NZDUSD","USDCAD","USDCHF","USDJPY")))
{
// calculate the buy/sell signal
vars Price = series(price());
vars DomPeriod = series(DominantPeriod(Price,30));
var LowPeriod = LowPass(DomPeriod,500);
vars HP = series(HighPass(Price,LowPeriod*optimize(1,0.5,2)));
vars Signal = series(Fisher(HP,500));
var Threshold = optimize(1,0.5,2,0.1);
Stop = optimize(4,2,8) * ATR(100);
Trail = 4*ATR(100);
//when training, force all trades so we can get see what would be profitable via OptimalF
if(is(TRAINMODE))
{
if(crossUnder(Signal,-Threshold)) reverseLong(1);
else if(crossOver(Signal,Threshold)) reverseShort(1);
}
//when testing/trading only trade those known profitable via OptimalF
if(is(TESTMODE) || (TRADEMODE))
{
//Allow more long trades for known-more-profitable
if(crossUnder(Signal,-Threshold) && (OptimalFLong >= .15)) reverseLong(3); //known very big win
else if(crossUnder(Signal,-Threshold) && (OptimalFLong >= .1)) reverseLong(2); //known big win
else if(crossUnder(Signal,-Threshold) && (OptimalFLong >= .001)) reverseLong(1); //known win
//Allow more short trades for known-more-profitable
else if(crossOver(Signal,Threshold) && (OptimalFShort >= .15)) reverseShort(3); //known very big win
else if(crossOver(Signal,Threshold) && (OptimalFShort >= .1)) reverseShort(2); //known big win
else if(crossOver(Signal,Threshold) && (OptimalFShort >= .001)) reverseShort(1); //known win
}
} //while loop
PlotWidth = 1000;
PlotHeight1 = 600;
}