function run()
{
var FastLen = 9;
var SlowLen = 18;
var ChLen = 12;
var TrailBar = 8;
var ReBars = 15;
var ReEnt = 10;
vars Price = series(priceClose());
vars FastMA = series(SMA(Price, FastLen));
vars SlowMA = series(SMA(Price, SlowLen));
static int Dir;
bool prn = false;
set(LOGFILE);
if(is(INITRUN)) {
Dir = 0;
}
Hedge = 0;
if (crossOver(FastMA, SlowMA) && (NumOpenLong == 0) && (NumPendingLong == 0)) {
EntryTime = ChLen;
enterLong(0, HH(ChLen)*1.0003, LL(TrailBar));
}
if (crossUnder(FastMA, SlowMA) && (NumOpenShort == 0) && (NumPendingShort == 0)) {
EntryTime = ChLen;
enterShort(0, LL(ChLen)*0.9997, HH(TrailBar));
}
for(open_trades) {
if (TradeIsLong && TradeIsOpen) {
exitShort(); //discard pending trades
TradeStopLimit = LL(TrailBar);
}
if (TradeIsShort && TradeIsOpen) {
exitLong(); //discard pending trades
TradeStopLimit = HH(TrailBar);
}
}
if (NumOpenLong > 0) {
Dir = 2;
}
if (NumOpenShort > 0) {
Dir = -2;
}
if (NumOpenTotal+NumPendingTotal==0) {
if (Dir > 1) {
if (prn) printf("\nbeing flat after long, decided to reenter long at %f", HH(ReEnt));
Dir = 1;
EntryTime = ReBars;
enterLong(0, HH(ReEnt), LL(TrailBar));
}
if (Dir < -1) {
if (prn) printf("\nbeing flat after short, decided to reenter short at %f", LL(ReEnt));
Dir = -1;
EntryTime = ReBars;
enterShort(0, LL(ReEnt), HH(TrailBar));
}
}
}