int TrailingStop(var range )
{
if(TradeIsLong and TradeIsOpen and TradeProfit > 0)
TradeStopLimit = max(TradeStopLimit,LL(range));
else if(TradeIsShort and TradeIsOpen and TradeProfit > 0)
TradeStopLimit = min(TradeStopLimit,HH(range));
return 0;
}
function run()
{
set(PARAMETERS+FACTORS);
AssetList = "AssetsOandaEUR100extended.csv";
BarPeriod = 60; // 1 hour bars
LookBack = 1800;
NumYears = 10;
NumWFOCycles = 10;
Hedge = 2;
Weekend = 2;
StartWeek = 10400; // start Monday 4 am
EndWeek = 51900; // end Friday 7 pm
if(Train) {
Detrend = PRICES;
NumSampleCycles = 4; }
Capital = slider(1,4000,0,7000,"Capital","initial captial");
Margin = 0.5 * OptimalF * Capital;
int i=0;
for(i=0; Assets[i]; i++)
{asset(Assets[i]);
i++;}
var Threshold = optimize(1,0.25,3,0.25);
int r = optimize(20,1,20,2);
ccyReset();
string Name;
while(Name = (loop(Assets)))
{
if(assetType(Name) != FOREX)
continue; // Currency pairs only
asset(Name);
vars Prices = series(priceClose());
ccySet(ROC(Prices,1)); // store price change as strength
}
// get currency pairs with highest and lowest strength difference
string Best = ccyMax(), Worst = ccyMin();
asset(Best);
vars Prices_b = series(priceClose());
vars roc_b = series(ROC(Prices_b,1));
vars zscore_b = series(zscore(roc_b[0],1800));
asset(Worst);
vars Prices_w = series(priceClose());
vars roc_w = series(ROC(Prices_w,1));
vars zscore_w = series(zscore(roc_w[0],1800));
static char OldBest[8], OldWorst[8]; // static for keeping contents between runs
if(*OldBest && !strstr(Best,OldBest)) { // new strongest asset?
asset(OldBest);
exitLong();
if(zscore_b[0] > Threshold) {
asset(Best);
Stop = 2*ATR(20);
enterLong(TrailingStop,r);
}
}
if(*OldWorst && !strstr(Worst,OldWorst)) { // new weakest asset?
asset(OldWorst);
exitShort();
if(zscore_w[0] < -Threshold) {
asset(Worst);
Stop = 2*ATR(20);
enterShort(TrailingStop,r);
}
}
// store previous strongest and weakest asset names
strcpy(OldBest,Best);
strcpy(OldWorst,Worst);
}