// Alice3a: Portfolio trading /////////////////// 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 run() { set(PARAMETERS+FACTORS+LOGFILE+TESTNOW+PLOTNOW); StartDate = 2010; // further back due to WFO EndDate = 2020; // fixed simulation period BarPeriod = 60; // 1 hour bars LookBack = 4*500; // needed for FisherN() Capital = 2000; if(Train) Detrend = TRADES; NumWFOCycles = 10; // activate WFO NumCores = -1; // multicore training (Zorro S only) ReTrainDays = 147; if(ReTrain) { UpdateDays = -1; // update price data from the server SelectWFO = -1; // select the last cycle for re-optimization reset(FACTORS); } // portfolio loop while(asset(loop("EUR/USD"))) while(algo(loop("CNTR"))) { // method 1: invest 1% of the balance in any trade //Margin = 0.01 * (Capital + ProfitClosed); // method 2: invest 50% of Optimal-F //Margin = 0.5 * OptimalF * (Capital + ProfitClosed); // method 3: invest the square root of the component profit Margin = 0.5 * OptimalF * Capital * sqrt(1 + ProfitClosed/Capital); if(Algo == "CNTR") tradeCounterTrend(); } }