// Workshop 4: Trend Trading ///////////////////
#include <profile.c>
var MMI(vars Data,int TimePeriod)
//credits to jcl
{
var m = Median(Data,TimePeriod);
int i, nh=0, nl=0;
for(i=1; i<TimePeriod; i++) {
if(Data[i] > m && Data[i] > Data[i-1])
nl++;
else if(Data[i] < m && Data[i] < Data[i-1])
nh++;
}
return 100.*(nl+nh)/(TimePeriod-1);
}
function run()
{
//StartDate = 2002;
//EndDate = 2008;
vars Price = series(price());
vars Trend = series(LowPass(Price,500));
//vars Trendindicator = series(KAMA(Trend,500));
vars Signals = series(0);
Stop = 4*ATR(100);
/*
if(valley(Trend)) {
Signals[0] = 1;
if(Sum(Signals+1,4) == 0) // no signals in the previous 3 bars?
enterLong();
exitShort(); // close opposite position
} else if(peak(Trend)) {
Signals[0] = 1;
if(Sum(Signals+1,4) == 0)
enterShort();
exitLong();
}
*/
//vars Hursts = series(Hurst(Price,100));
var HurstExponent = Hurst(Price,100);
/*
if(HurstExponent > 0.5) {
if(valley(Trend))
enterLong();
else if(peak(Trend))
enterShort();
} else {
exitShort();
exitLong();
}
*/
/*if(valley(Trend)) {
if(HurstExponent > 5) // no signals in the previous 3 bars?
enterLong();
exitShort(); // close opposite position
} else if(peak(Trend)) {
if(HurstExponent > 5)
enterShort();
exitLong();
}*/
vars Meanness = series(MMI(Price,200));
vars Filter = series(LowPass(Meanness,500));
if( valley(Trend) ){
exitShort(); // close opposite position
if(falling(Filter))
enterLong();
} else if( peak(Trend) ) {
exitLong();
if(falling(Filter))
enterShort();
}
/*MAMA(Price,0.05,0.05);
vars MAMAs = series(rMAMA);
vars FAMAs = series(rFAMA);*/
/*if( crossUnder(FAMAs,MAMAs) ){
exitShort(); // close opposite position
if(falling(Filter))
enterLong();
} else if( crossOver(FAMAs,MAMAs) ) {
exitLong();
if(falling(Filter))
enterShort();
}*/
//vars FilterShannon = series(LowPass(series(ShannonGain(Price,500)),500));
/*vars FilterShannon = series(ShannonGain(Price,500));
if( valley(Trend) ){
exitShort(); // close opposite position
if(crossOver(FilterShannon,0))
enterLong();
} else if( peak(Trend) ) {
exitLong();
if(crossUnder(FilterShannon,0))
enterShort();
}*/
/*BBands(Price,30,2,2,MAType_MAMA);
plot("price",Price[0],MAIN|LINE,BLACK);
plot("Bollinger1",rRealUpperBand,BAND1,0x000000CC);
plot("Bollinger2",rRealLowerBand,BAND2,0x800000FF);
plot("Trend",Trend[0],LINE,RED);
plot("Filter",Filter[0],NEW|LINE,BLACK);
plot("Hurst",HurstExponent*100,NEW|LINE,BLACK);
plot("Shannon",FilterShannon[0],NEW|LINE,BLACK);*/
/*plot("price",Price[0],MAIN|LINE,BLACK);
plot("lowpass",Trend,LINE,RED);
plot("kama",kavg,LINE,BLUE);*/
//PlotWidth = 600;
//PlotHeight1 = 300;
// plotTradeProfile(50);
set(LOGFILE); // log all trades
}