int MMI_PERIOD = 250;
int FISHER_PERIOD = 200;
float STOP_DISTANCE = 4;
float MMI_THRESH = 51.5; // 51.5
float FD_THRESH = 1.55; // 1.55
float ENTER_CYCLE_THRESH = 2.4; // 2.4
float EXIT_CYCLE_THRESH = -0.2;
int HoldTrend = 0;
void main() {
HoldTrend = 0;
BarPeriod = 5;
StartDate = 20190101;
EndDate = 20220101;
History = "History\\*m5.t6";
asset("BTCUSD");
set(PLOTNOW);
}
void counterTrend(bool IsActive, vars Prices) {
vars Cycles = series(BandPass(Prices,20,2));
vars Signals = series(FisherN(Cycles,FISHER_PERIOD));
plot("Signals",Signals,NEW,GREEN);
plot("Enter Long",-ENTER_CYCLE_THRESH,0,BLUE);
plot("Exit Long",EXIT_CYCLE_THRESH,0,BLUE);
plot("Enter Short",ENTER_CYCLE_THRESH,0,RED);
plot("Exit Short",-EXIT_CYCLE_THRESH,0,RED);
if(IsActive && HoldTrend == 0) {
if(crossOver(Signals,ENTER_CYCLE_THRESH))
enterShort();
if(crossUnder(Signals,EXIT_CYCLE_THRESH * -1))
exitShort();
if(crossUnder(Signals,ENTER_CYCLE_THRESH * -1))
enterLong();
if(crossOver(Signals,EXIT_CYCLE_THRESH))
exitLong();
}
}
void followTrend(bool IsActive, vars Prices) {
vars Trend = series(Laguerre(Prices,200));
plot("Following",HoldTrend,NEW,RED);
plot("Trend",Trend,MAIN,BLUE);
if(HoldTrend == 1) {
if(peak(Trend) || Prices[0] < Trend[0]) {
exitLong();
HoldTrend = 0;
return;
}
if(valley(Trend) || Prices[0] > Trend[0]) {
exitShort();
HoldTrend = 0;
return;
}
}
if(IsActive) {
if(rising(Trend)) {
enterLong();
HoldTrend = 1;
}
else if(falling(Trend)) {
enterShort();
HoldTrend = 1;
}
}
}
void run() {
LookBack = 800;
if(Train) Detrend = TRADES;
set(PARAMETERS+LOGFILE+TESTNOW+PLOTNOW);
MaxShort = MaxLong = -1;
vars Prices = series(priceClose());
vars FDRaws = series(FractalDimension(Prices,400));
vars FDSmooth = series(SMA(FDRaws,400));
plot("FD Smoothed",FDSmooth,NEW,GREEN);
plot("FD Threshold",FD_THRESH,0,RED);
vars MMIRaws = series(MMI(Prices,MMI_PERIOD));
vars MMISmooth = series(SMA(MMIRaws,MMI_PERIOD));
plot("MMI Smoothed",MMISmooth,NEW,BLUE);
plot("MMI Threshold",MMI_THRESH,0,RED);
Stop = STOP_DISTANCE*ATR(10);
MaxLong = MaxShort = -1;
// bool TradeWithTrend = FDSmooth[0] > FD_THRESH && MMISmooth[0] < MMI_THRESH;
bool TradeCounterTrend = FDSmooth[0] > FD_THRESH;
bool TradeWithTrend = falling(MMISmooth) && falling(FDSmooth) && MMISmooth[0] < MMI_THRESH && !TradeCounterTrend;
if(TradeWithTrend) plot("Follow The Trend",1,NEW,PURPLE);
else plot("Follow The Trend",0,NEW,PURPLE);
followTrend(TradeWithTrend,Prices);
// counterTrend(TradeCounterTrend && !TradeWithTrend,Prices);
}