Simple Coding Help - No Promise.

Thanks casoni.

I want to add one more condition for buy and sell in the above code as "AND if the bar closes above EMA(34) for buy and closes below of EMA(34) for sell. I tried the below code but it is giving error. Please help in correcting this.

Buy = Cross(C,trailarray) AND Cross(C,EMA(34));
Sell = Cross(trailarray,C) AND Cross(EMA(34),C);
 

pratapvb

Well-Known Member
Thanks casoni.

I want to add one more condition for buy and sell in the above code as "AND if the bar closes above EMA(34) for buy and closes below of EMA(34) for sell. I tried the below code but it is giving error. Please help in correcting this.

Buy = Cross(C,trailarray) AND Cross(C,EMA(34));
Sell = Cross(trailarray,C) AND Cross(EMA(34),C);
should be EMA(C, 34)
 

casoni

Well-Known Member
Thanks casoni.

I want to add one more condition for buy and sell in the above code as "AND if the bar closes above EMA(34) for buy and closes below of EMA(34) for sell. I tried the below code but it is giving error. Please help in correcting this.

Buy = Cross(C,trailarray) AND Cross(C,EMA(34));
Sell = Cross(trailarray,C) AND Cross(EMA(34),C);

//originally Posted by tools&tradingideas [ Edward.Pottasch ]
function vstop_func(tr)
{
trailArray[ 0 ] = C[ 0 ]; // initialize
for( i = 1; i < BarCount; i++ )
{
prev = trailArray[ i - 1 ];

if (C[ i ] > prev AND C[ i - 1 ] > prev)
{
trailArray[ i ] = Max(prev,C[ i ] - tr[ i ]);
}
else if (C[ i ] < prev AND C[ i - 1 ] < prev)
{
trailArray[ i ] = Min(prev,C[ i ] + tr[ i ]);
}
else if (C[ i ] > prev)
{
trailArray[ i ] = C[ i ] - tr[ i ];
}
else
{
trailArray[ i ] = C[ i ] + tr[ i ];
}
}
return trailArray;
}

per = Param("per",20, 5, 150, 1);
mult = Param("mult",2, 1, 4, 0.05);
tr = mult * ATR(per);
trailArray = vstop_func(tr);
//trailArray = Ref(trailArray,-1);

SetChartBkColor( ParamColor("ColorBG", ColorRGB( 0, 0, 0 ) ) );
GraphXSpace = 5;
SetChartOptions(0, chartShowDates);
Plot(IIf(trailArray > C,trailArray,Null),"\ntrailShort",ParamColor("Colo rTrailShort",ColorRGB(255,0,0)),styleStaircase);
Plot(IIf(trailArray < C,trailArray,Null),"\ntrailLong",ParamColor("Color TrailLong",ColorRGB(0,255,0)),styleStaircase);
Plot( C, "\nCandle",colorWhite, styleCandle );

// added
xx=EMA(C,34);
Plot(xx,"Ema34:",3,1);

//----------- 1
Buy1 = C>xx AND Cross(C,trailarray);
Sell1 = C<xx AND Cross(trailarray,C);
PlotShapes(Buy1*shapesmallUpTriangle+Sell1*shapesmallDownTriangle,IIf(Buy1,colorBlue,colorYellow),0,IIf(Buy1,L,H)) ;

//------------ 2
Buy2 = C>xx AND C>trailarray;
Sell2 = C<xx AND C<trailarray;
buy2=ExRem(buy2,sell2);
sell2=ExRem(sell2,buy2);
PlotShapes(Buy2*shapeUparrow+Sell2*shapeDownarrow,IIf(Buy2,colorgreen,colorred),0,IIf(Buy2,L-30,H+30)) ;

//------------ 3
Buy3 = Cross(C,trailarray) AND Cross(C,xx);
Sell3 = Cross(trailarray,C) AND Cross(xx,C);
PlotShapes(Buy3*shapeuparrow+Sell3*shapeDownarrow,IIf(Buy3,colorpalegreen,colororange),0,IIf(Buy3,L-40,H+40)) ;

Thank you
 
Last edited:
//originally Posted by tools&tradingideas [ Edward.Pottasch ]
function vstop_func(tr)
{
trailArray[ 0 ] = C[ 0 ]; // initialize
for( i = 1; i < BarCount; i++ )
{
prev = trailArray[ i - 1 ];

if (C[ i ] > prev AND C[ i - 1 ] > prev)
{
trailArray[ i ] = Max(prev,C[ i ] - tr[ i ]);
}
else if (C[ i ] < prev AND C[ i - 1 ] < prev)
{
trailArray[ i ] = Min(prev,C[ i ] + tr[ i ]);
}
else if (C[ i ] > prev)
{
trailArray[ i ] = C[ i ] - tr[ i ];
}
else
{
trailArray[ i ] = C[ i ] + tr[ i ];
}
}
return trailArray;
}

per = Param("per",20, 5, 150, 1);
mult = Param("mult",2, 1, 4, 0.05);
tr = mult * ATR(per);
trailArray = vstop_func(tr);
//trailArray = Ref(trailArray,-1);

SetChartBkColor( ParamColor("ColorBG", ColorRGB( 0, 0, 0 ) ) );
GraphXSpace = 5;
SetChartOptions(0, chartShowDates);
Plot(IIf(trailArray > C,trailArray,Null),"\ntrailShort",ParamColor("Colo rTrailShort",ColorRGB(255,0,0)),styleStaircase);
Plot(IIf(trailArray < C,trailArray,Null),"\ntrailLong",ParamColor("Color TrailLong",ColorRGB(0,255,0)),styleStaircase);
Plot( C, "\nCandle",colorWhite, styleCandle );

// added
xx=EMA(C,34);
Plot(xx,"Ema34:",3,1);

//----------- 1
Buy1 = C>xx AND Cross(C,trailarray);
Sell1 = C<xx AND Cross(trailarray,C);
PlotShapes(Buy1*shapesmallUpTriangle+Sell1*shapesmallDownTriangle,IIf(Buy1,colorBlue,colorYellow),0,IIf(Buy1,L,H)) ;

//------------ 2
Buy2 = C>xx AND C>trailarray;
Sell2 = C<xx AND C<trailarray;
buy2=ExRem(buy2,sell2);
sell2=ExRem(sell2,buy2);
PlotShapes(Buy2*shapeUparrow+Sell2*shapeDownarrow,IIf(Buy2,colorgreen,colorred),0,IIf(Buy2,L-30,H+30)) ;

//------------ 3
Buy3 = Cross(C,trailarray) AND Cross(C,xx);
Sell3 = Cross(trailarray,C) AND Cross(xx,C);
PlotShapes(Buy3*shapeuparrow+Sell3*shapeDownarrow,IIf(Buy3,colorpalegreen,colororange),0,IIf(Buy3,L-40,H+40)) ;

Thank you
Thanks Casoni for quick response. Very helpful. This is how the Banknifty chart looks like

 
//originally Posted by tools&tradingideas [ Edward.Pottasch ]
function vstop_func(tr)
{
trailArray[ 0 ] = C[ 0 ]; // initialize
for( i = 1; i < BarCount; i++ )
{
prev = trailArray[ i - 1 ];

if (C[ i ] > prev AND C[ i - 1 ] > prev)
{
trailArray[ i ] = Max(prev,C[ i ] - tr[ i ]);
}
else if (C[ i ] < prev AND C[ i - 1 ] < prev)
{
trailArray[ i ] = Min(prev,C[ i ] + tr[ i ]);
}
else if (C[ i ] > prev)
{
trailArray[ i ] = C[ i ] - tr[ i ];
}
else
{
trailArray[ i ] = C[ i ] + tr[ i ];
}
}
return trailArray;
}

per = Param("per",20, 5, 150, 1);
mult = Param("mult",2, 1, 4, 0.05);
tr = mult * ATR(per);
trailArray = vstop_func(tr);
//trailArray = Ref(trailArray,-1);

SetChartBkColor( ParamColor("ColorBG", ColorRGB( 0, 0, 0 ) ) );
GraphXSpace = 5;
SetChartOptions(0, chartShowDates);
Plot(IIf(trailArray > C,trailArray,Null),"\ntrailShort",ParamColor("Colo rTrailShort",ColorRGB(255,0,0)),styleStaircase);
Plot(IIf(trailArray < C,trailArray,Null),"\ntrailLong",ParamColor("Color TrailLong",ColorRGB(0,255,0)),styleStaircase);
Plot( C, "\nCandle",colorWhite, styleCandle );

// added
xx=EMA(C,34);
Plot(xx,"Ema34:",3,1);

//----------- 1
Buy1 = C>xx AND Cross(C,trailarray);
Sell1 = C<xx AND Cross(trailarray,C);
PlotShapes(Buy1*shapesmallUpTriangle+Sell1*shapesmallDownTriangle,IIf(Buy1,colorBlue,colorYellow),0,IIf(Buy1,L,H)) ;

//------------ 2
Buy2 = C>xx AND C>trailarray;
Sell2 = C<xx AND C<trailarray;
buy2=ExRem(buy2,sell2);
sell2=ExRem(sell2,buy2);
PlotShapes(Buy2*shapeUparrow+Sell2*shapeDownarrow,IIf(Buy2,colorgreen,colorred),0,IIf(Buy2,L-30,H+30)) ;

//------------ 3
Buy3 = Cross(C,trailarray) AND Cross(C,xx);
Sell3 = Cross(trailarray,C) AND Cross(xx,C);
PlotShapes(Buy3*shapeuparrow+Sell3*shapeDownarrow,IIf(Buy3,colorpalegreen,colororange),0,IIf(Buy3,L-40,H+40)) ;

Thank you
I am trying to backtest this code from Analysis -->Automatic Analysis but getting the following error message

"Missing buy/sell variable assignments"

Please help.
 
_SECTION_BEGIN("Trailing Stop Loss");
per = Param("Bars",2,2,100,1);
mult = Param("Trailing Loss %",0.4,0.01,10,0.01);
tr = (Ref(C,-PER)*(mult/100));

trailArray[ 0 ] = C[ 0 ]; // initialize
for( i = 1; i < BarCount; i++ )
{
prev = trailArray[ i - 1 ];

if (C[ i ] > prev AND C[ i - 1 ] > prev)
{
trailArray[ i ] = Max(prev,C[ i ] - tr[ i ]);
}
else if (C[ i ] < prev AND C[ i - 1 ] < prev)
{
trailArray[ i ] = Min(prev,C[ i ] + tr[ i ]);
}
else if (C[ i ] > prev)
{
trailArray[ i ] = C[ i ] - tr[ i ];
}
else
{
trailArray[ i ] = C[ i ] + tr[ i ];
}
}
trailArray = Ref(trailArray,-1);

SetChartBkColor( ParamColor("ColorBG", ColorRGB( 0, 0, 0 ) ) );
GraphXSpace = 5;
SetChartOptions(0, chartShowDates);
Plot(trailArray,"\ntrailArray",ParamColor("ColorTr ail",ColorRGB(255,255,255)),styleStaircase);
Plot( C, "\nCandle",colorWhite, styleCandle );
_SECTION_END();
 
Last edited:

casoni

Well-Known Member
I am trying to backtest this code from Analysis -->Automatic Analysis but getting the following error message

"Missing buy/sell variable assignments"

Please help.
Yes..
first you decide which Buy or Sell you want to use ?
i.e you feel comfortable with Buy1 or Buy2 ... [ as Buy3 will signal very less ]
if you are comfortable with Buy1 ans sell1 then
Your code will be

Buy=cover=buy1;
sell=short=sell1;

will work for scan and Backtest

Thank you
 
Yes..
first you decide which Buy or Sell you want to use ?
i.e you feel comfortable with Buy1 or Buy2 ... [ as Buy3 will signal very less ]
if you are comfortable with Buy1 ans sell1 then
Your code will be

Buy=cover=buy1;
sell=short=sell1;

will work for scan and Backtest

Thank you

After modifying the code (as given below), the error is gone.
//------------ 2
Buy2 = C>xx AND C>trailarray;
Sell2 = C<xx AND C<trailarray;
buy2=ExRem(buy2,sell2);
sell2=ExRem(sell2,buy2);

Buy=Cover=Buy2;
Sell=Short=Sell2;

PlotShapes(Buy2*shapeUpArrow+Sell2*shapeDownArrow, IIf(Buy2,colorGreen,colorRed),0,IIf(Buy2,L-30,H+30)) ;


I backtested it for this week on Banknifty and getting only 2 trades in the result. But actually there has been around 13 trades as shown in the chart. Not sure why? Need your help on this please.

 
Last edited:

Similar threads