I plotted out both “short” and the conditions under which the short signal would be triggered. However when the condition’s true, the short signal stays false. As I moved the bar back and forth on the chart, the plots changed. And I only used reference price of O,H,L & C. Can anyone tell me what types of errors in my code might cause this problem?
I also used the exploration interface to dispaly the condition and signal columns. It shows that while the condition’s true, the signal is false.
for (i=0;i<BarCount;i++)
{
if (i==0)
{
Highafterentry[i]=0;
Lowafterentry[i]=0;
Longin[i]=0;
Shortin[i]=0;
Buy[i]=0;
Short[i]=0;
Cover[i]=0;
Sell[i]=0;
}
else
{
if (Buysig[i])// AND !Longin[i-1] AND !Shortin[i-1])
{
Buy[i]=1;
BuyPrice[i]=O[i];
priceatbuy[i]=BuyPrice[i];
Highafterentry[i]=H[i];
Lowafterentry[i]=L[i];
Longin[i]=1;
}
else if (Shortsig[i])// AND !Longin[i-1] AND !Shortin[i-1])
{
Short[i]=1;
ShortPrice[i]=O[i];
priceatshort[i]=ShortPrice[i];
Highafterentry[i]=H[i];
Lowafterentry[i]=L[i];
Shortin[i]=1;
}
else if (Sellsig[i] AND Longin[i-1])
{
Sell[i]=1;
SellPrice[i]=O[i];
Longin[i]=0;
}
else if (Coversig[i] AND Shortin[i-1])
{
Cover[i]=1;
CoverPrice[i]=O[i];
Shortin[i]=0;
}
else if (Longin[i-1])
{
priceatbuy[i]=priceatbuy[i-1];
Highafterentry[i]=Max(H[i],Highafterentry[i-1]);
Lowafterentry[i]=Min(L[i],Lowafterentry[i-1]);
longstop3[i]=(Highafterentry[i-1]>=priceatbuy[i]*(1+0.01*StartPro3) AND Low[i]<= Highafterentry[i-1]-(Highafterentry[i-1]-priceatbuy[i])*0.01*StopPro3);
longstop2[i]=(Highafterentry[i-1]>=priceatbuy[i]*(1+0.01*StartPro2) AND Low[i]<=Highafterentry[i-1]-(Highafterentry[i-1]-priceatbuy[i])*0.01*StopPro2);
longstop1[i]=(Highafterentry[i-1]>=priceatbuy[i]*(1+0.01*StartPro1) AND Low[i]<=Highafterentry[i-1]-(Highafterentry[i-1]-priceatbuy[i])*0.01*StopPro1);
Longstoploss[i]=(Low[i]<=priceatbuy[i]*(1-StopLoss*0.01));
if (Highafterentry[i-1]>=priceatbuy[i]*(1+0.01*StartPro3) AND Low[i]<= Highafterentry[i-1]-(Highafterentry[i-1]-priceatbuy[i])*0.01*StopPro3)
{
Sell[i]=1;
SellPrice[i]=Min(Open[i],Highafterentry[i-1]-(Highafterentry[i-1]-priceatbuy[i])*0.01*StopPro3);
Longin[i]=0;
}
else if (Highafterentry[i-1]>=priceatbuy[i]*(1+0.01*StartPro2) AND Low[i]<=Highafterentry[i-1]-(Highafterentry[i-1]-priceatbuy[i])*0.01*StopPro2)
{
Sell[i]=1;
SellPrice[i]=Min(Open[i],Highafterentry[i-1]-(Highafterentry[i-1]-priceatbuy[i])*0.01*StopPro2);
Longin[i]=0;
}
else if (Highafterentry[i-1]>=priceatbuy[i]*(1+0.01*StartPro1) AND Low[i]<=Highafterentry[i-1]-(Highafterentry[i-1]-priceatbuy[i])*0.01*StopPro1)
{
Sell[i]=1;
SellPrice[i]=Min(Open[i],Highafterentry[i-1]-(Highafterentry[i-1]-priceatbuy[i])*0.01*StopPro1);
Longin[i]=0;
}
else if (Low[i]<=priceatbuy[i]*(1-StopLoss*0.01))
{
Sell[i]=1;
SellPrice[i]=Min(Open[i],priceatbuy[i]*(1-StopLoss*0.01));
Longin[i]=0;
}
else
{
Sell[i]=0;
Longin[i]=1;
}
}
else if (Shortin[i-1])
{
priceatshort[i]=priceatshort[i-1];
Highafterentry[i]=Max(H[i],Highafterentry[i-1]);
Lowafterentry[i]=Min(L[i],Lowafterentry[i-1]);
Shortstop3[i]=( Lowafterentry[i-1]<=priceatshort[i]*(1-0.01*StartPro3) AND High[i]>=Lowafterentry[i-1]+(priceatshort[i]-Lowafterentry[i-1])*0.01*StopPro3);
Shortstop2[i]=( Lowafterentry[i-1]<=priceatshort[i]*(1-0.01*StartPro2) AND High[i]>=Lowafterentry[i-1]+(priceatshort[i]-Lowafterentry[i-1])*0.01*StopPro2);
Shortstop1[i]=( Lowafterentry[i-1]<=priceatshort[i]*(1-0.01*StartPro1) AND High[i]>=Lowafterentry[i-1]+(priceatshort[i]-Lowafterentry[i-1])*0.01*StopPro1);
Shortstoploss[i]=(High[i]>=priceatshort[i]*(1+StopLoss*0.01));
if ( Lowafterentry[i-1]<=priceatshort[i]*(1-0.01*StartPro3) AND High[i]>=Lowafterentry[i-1]+(priceatshort[i]-Lowafterentry[i-1])*0.01*StopPro3)
{
Cover[i]=1;
CoverPrice[i]=Max(Open[i],Lowafterentry[i-1]+(priceatshort[i]-Lowafterentry[i-1])*0.01*StopPro3);
Shortin[i]=0;
}
else if ( Lowafterentry[i-1]<=priceatshort[i]*(1-0.01*StartPro2) AND High[i]>=Lowafterentry[i-1]+(priceatshort[i]-Lowafterentry[i-1])*0.01*StopPro2)
{
Cover[i]=1;
CoverPrice[i]=Max(Open[i],Lowafterentry[i-1]+(priceatshort[i]-Lowafterentry[i-1])*0.01*StopPro2);
Shortin[i]=0;
}
else if ( Lowafterentry[i-1]<=priceatshort[i]*(1-0.01*StartPro1) AND High[i]>=Lowafterentry[i-1]+(priceatshort[i]-Lowafterentry[i-1])*0.01*StopPro1)
{
Cover[i]=1;
CoverPrice[i]=Max(Open[i],Lowafterentry[i-1]+(priceatshort[i]-Lowafterentry[i-1])*0.01*StopPro1);
Shortin[i]=0;
}
else if (High[i]>=priceatshort[i]*(1+StopLoss*0.01))
{
Cover[i]=1;
CoverPrice[i]=Max(Open[i],priceatshort[i]*(1+StopLoss*0.01));
Shortin[i]=0;
}
else
{
Cover[i]=0;
Shortin[i]=1;
}
}
else
{
Shortin[i]=0;
Longin[i]=0;
}
}
}
Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);
SetPositionSize(10,spsShares);
//Plot(Cover,"cover",colorRed);
//Plot(Buysig,"buysig",colorRed);
//Plot(Buy,"buy",colorWhite);
Plot(Shortsig,"shortsig",colorYellow);
Plot(Short,"short",colorBlue);
Filter=1;
AddColumn(barssofartoday,"barsofartoday");
AddColumn(Buysig,"buysig");
AddColumn(Shortsig,"shortsig");
AddColumn(Sellsig,"sellsig");
AddColumn(Coversig,"coversig");
AddColumn(Buy,"buy");
AddColumn(Short,"short");
AddColumn(Sell,"sell");
AddColumn(Cover,"cover");
AddColumn(Longin,"longin");
AddColumn(Shortin,"shortin");