// The ZEN Indicator v4
void run(){
set(PLOTNOW,LOGFILE,TESTNOW,PARAMETERS);
// StartDate = 20180101;
EndDate = 20181231;
// BarMode = BR_WEEKEND+BR_SLEEP;
BarPeriod = 60;
TimeFrame = 24; // frameSync(24);
LookBack = 100;
MaxLong = MaxShort = 1;
// var day = 24;
// if (Train) Detrend = TRADES;
// NumWFOCycles = 10;
// NumCores = -1;
// asset(loop("EUR/USD", "USD/JPY", "GBP/USD","USD/CHF", "USD/CAD", "AUD/USD", "EUR/NZD"));
asset("SPY");
// if(is(INITRUN)){
// assetHistory(asset, FROM_STOOQ);
// }
vars Prices = series(price());
vars Highs = series(priceHigh());
vars Lows = series(priceLow());
// while(asset(loop("EUR/USD", "USD/JPY", "GBP/USD","USD/CHF", "USD/CAD", "AUD/USD", "EUR/NZD"))){
static var lowReg;
static var highReg;
// var threshold = -(optimize(0.001, 0.000, 0.01, 0.001));
var threshold = 0;
print(TO_LOG,"\n#### LOWS: %f %f %f %f %f", Lows[0], Lows[1], Lows[2], Lows[3], Lows[4]);
print(TO_LOG,"\n#### HIGHS: %f %f %f %f %f", Highs[0], Highs[1], Highs[2], Highs[3], Highs[4]);
if(Lows[0] > Lows[1] and Lows[1] > Lows[2] and Lows[2] > Lows[3] and Lows[3] > Lows[4]){
print(TO_LOG,"\n#### \n#### UPTREND \n#### \n####");
// print(TO_LOG,"\n#### \n#### Lows: %f \n#### \n####", Lows);
lowReg = LinearReg(Lows, 5) +threshold;
plot("lowReg",lowReg,MAIN,GREEN);
}else if(Highs[0] < Highs[1] and Highs[1] < Highs[2] and Highs[2] < Highs[3]and Highs[3] < Highs[4]){
print(TO_LOG,"\n#### \n#### DOWNTREND \n#### \n####");
highReg = LinearReg(Highs, 5) -threshold;
plot("highReg",highReg,MAIN,RED);
}
if(Lows[0] < lowReg){
enterLong();
// lowReg = -999999999;
// highReg = 999999999;
}else if(Highs[0] > highReg){
enterShort();
// lowReg = -999999999;
// highReg = 999999999;
}
// if(crossOver(Lows, trendLine)){
// enterLong();
// }else if(crossUnder(Highs, trendLine)){
// enterShort();
// }
// }
}