I am playing around with the code a bit to increase my understanding. I am trying to program the option to enter a long trade if SMA 5
crosses over SMA 14 with a shift of 3 (with optional LWMA filter) and check of MACD before position is opened. Exit long when there is a crossUnder. Opposite for short. The function below ultimately gives me an error message 111. Can anybody direct me as to where I need to look to resolve the issue (or how I actually shift an array)? Tnx, Norbert

function run()
{

BarPeriod = 60; // 4 hour bars
LookBack = 500; // maximum indicator time period
set(PARAMETERS); // generate and use optimized parameters
StartDate = 2010;
NumWFOCycles = 10;
if(ReTrain) {
UpdateDays = -1;
SelectWFO = -1;
}

// calculate the buy/sell signal with optimized parameters
vars SMA5;
vars SMA14;
vars LWMA_240;
vars LWMA_800;
vars MACD;
vars Price = series(price());
vars High = series(priceHigh());
vars Low = series(priceLow());
vars Close = series(priceClose());

SMA5 = series(SMA(Price, 5));
SMA14 = series(SMA(Price, 14),3);
LWMA_240 = series(WMA(Price, 240));
LWMA_800 = series(WMA(Price, 800));


bool useLWMA_240=false;
bool useLWMA_800=false;
bool cbLWMA_240=false;
bool cbLWMA_800=false;
bool csLWMA_240=false;
bool csLWMA_800=false;
bool cbSMA=false;
bool csSMA=false;
bool cbMACD=false;
bool csMACD=false;

useLWMA_240=optimize(false,false,true);
useLWMA_800=optimize(false,false,true);

if(useLWMA_240){
cbLWMA_240 = Price > LWMA_240;
csLWMA_240 = Price < LWMA_240;
if(useLWMA_800){
cbLWMA_800 = Price > LWMA_800;
csLWMA_800 = Price < LWMA_800;
} else {
csLWMA_800= true;
cbLWMA_800= true;
}
} else {
cbLWMA_240= true;
cbLWMA_800= true;
csLWMA_240= true;
csLWMA_800= true;
}

MACD[0] = EMA(Price,12)-EMA(Price,26);
cbMACD = MACD[0] > 0;
csMACD = MACD[0] < 0;

// buy and sell
// Stop = optimize(4,2,10) * ATR(100);
// Trail = optimize(4,2,10)* ATR(100);


if (crossOver(SMA5,SMA14) and NumOpenShort > 0) {
exitShort();
}
if (crossUnder(SMA5,SMA14) && NumOpenLong > 0) {
exitLong();
}

if (crossOver(SMA5,SMA14) && cbMACD && cbLWMA_240 && cbLWMA_800) {
if (!NumOpenLong)
enterLong();
} else if (crossUnder(SMA5,SMA14) && csMACD && csLWMA_240 && csLWMA_800) {
if (!NumOpenShort)
enterShort();
}


// plot("SMA5",SMA5,NEW,BLUE);
// plot("SMA14",SMA14[3],NEW,BLUE);
// PlotWidth = 600;
// PlotHeight1 = 300;
}

Last edited by NRW; 01/16/19 12:49.