#include <profile.c>
#include <custom.c>
#define INITCAPITAL 10000
#define ASSETLIST "AssetsGP-org"
#define RISK 0.5
static int tradeCount;
static int tradeLots;
static var tradeComm;
static var tradeRoll;
static var tradeSpread;
function tradeCounterTrend()
{
TimeFrame = frameSync(4);
vars Prices = series(price());
vars Cycles = series(BandPass(Prices,30,2));
vars Signals = series(FisherN(Cycles,500));
var Threshold = optimize(1,0.8,1.2,0.1);
LifeTime = 4*optimize(100,50,150,10);
Trail = Stop = optimize(10,4,20,2)*ATR(100);
MaxLong = MaxShort = -1;
var Regime = FractalDimension(Prices,100);
var RegimeThreshold = optimize(1.5,1.3,1.7,0.1);
if(Regime > RegimeThreshold)
{
if(crossUnder(Signals,-Threshold))
enterLong();
else if(crossOver(Signals,Threshold))
enterShort();
}
}
function tradeTrend()
{
TimeFrame = 1;
vars Prices = series(price());
vars Trends = series(Laguerre(Prices,optimize(0.05,0.02,0.15,0.01)));
Stop = optimize(10,4,20,2)*ATR(100);
Trail = 0;
LifeTime = 0;
MaxLong = MaxShort = -1;
var MMI_Period = optimize(300,100,400,100);
vars MMI_Raws = series(MMI(Prices,MMI_Period));
vars MMI_Avgs = series(SMA(MMI_Raws,MMI_Period));
if(falling(MMI_Avgs)) {
if(valley(Trends))
enterLong();
else if(peak(Trends))
enterShort();
}
}
function run()
{
set(PARAMETERS); //From Training
set(FACTORS); //For OptimalF
set(TESTNOW+PLOTNOW);
set(PRELOAD); //reduce load for fetching data from MT4 server
set(NOLOCK); //speed up API access
StartDate = ymd(wdate(NOW) - 356*10); //Instead calculate StartDate -> 356*10 = 10 years
EndDate = Now;
BarPeriod = 60; // 1 hour bars
LookBack = 4*500; // needed for FisherN()
//Set capital - either from tsv file or from slider
SaveMode = SV_ALGOVARS+SV_ALGOVARS2+SV_TRADES+SV_BACKUP+SV_HTML; //Not save slider -> reset via tsv
if(INITCAPITAL)
Capital = slider(1,INITCAPITAL,0,20000,"Capital",""); //Set capital from tsv file
else
Capital = slider(1,10000,0,20000,"Capital",""); //If not set or test/train mode
if(Train) Detrend = TRADES;
NumWFOCycles = 12; // activate WFO
if(Train) NumCores = -2; // multicore training (Zorro S only)
//ReTrainDays = 1; //From WFO test cycles -> 44 weeks * 7 days = 308 days
if(ReTrain) {
UpdateDays = -1; // update price data from the server
SelectWFO = -1; // select the last cycle for re-optimization
reset(FACTORS);
NumCores = -4;
}
// portfolio loop
assetList(ASSETLIST);
//for(listed_assets) {
while(asset(loop("EUR/USD","XAU/USD"))) {
asset(Asset);
//To fix problems with rollover values (far too high!!)
//So set Roll to zero & instead defined Interest variable
RollLong = RollShort = 0;
Interest = 0.015 * 356; //calculated in percent per 10K -> 1%
//RollLong = priceClose()*(1-1/100)*0.03;
//Commission = RollLong = RollShort = Spread = 0;
if(is(FIRSTINITRUN) && !Test)
assetHistory(Asset,1);
while(algo(loop("TRND","CNTR")))
{
Margin = RISK * OptimalF * Capital * sqrt(1 + ProfitTotal/Capital);
if(Algo == "TRND")
tradeTrend();
else if(Algo == "CNTR")
tradeCounterTrend();
}
}
if(is(EXITRUN)){
for(closed_trades) {
tradeCount++;
tradeLots += TradeLots;
tradeComm += abs(TradeCommission)*TradeLots;
tradeRoll += abs(TradeRoll);
tradeSpread += abs(TradeSpread);
}
printf("\nNumber=%d,Lots=%d\nSpread=%.2f,Comm=%.2f,Roll=%.2f",tradeCount,tradeLots/100,tradeSpread,tradeComm/10,tradeRoll);
}
}