I am learning afl language.I'm beginners.I wanted to combine the two indicators.But buy sell rules does not work properly.Where can I make mistakes.
if the first order SAR then do not give orders.iif first order stoch then normal.
_SECTION_BEGIN("PSAR xo");
acc = Param("Acceleration factor", 0.1, 0.01, 0.1, 0.01);
acc = Optimize("Acceleration factor", acc, 0.01, 0.1, 0.01);
af_start = Param("Starting AF value", 0.03, 0.01, 0.1, 0.01);
af_start = Optimize("Starting AF value", af_start, 0.01, 0.1, 0.01);
af_max = Param("Maximum AF value", 0.06, 0.01, 0.1, 0.01);
af_max = Optimize("Maximum AF value", af_max, 0.01, 0.1, 0.01);
Ct = Param("Crossover threshold in %", 0, 0, 1, 0.1);
Ct = Optimize("Crossover threshold in %", Ct, 0, 1, 0.1);
Ct1=Ct/100;
IAF = acc;
MaxAF = af_max; // max acceleration
psar = Close; // initialize
psar_temp = Close;
long = 1; // assume long for initial conditions
af = af_start; // starting value of the acelleration factor
ep = Low[ 0 ]; // init extreme point
hp = High [ 0 ];
lp = Low [ 0 ];
for( i = 2; i < BarCount; i++ )
{
if ( long )
{
psar [ i ] = psar [ i-1 ] + af * ( hp - psar [ i-1 ] );
psar_temp [ i ] = psar [ i ] * (1-Ct1);
}
else
{
psar [ i ] = psar [ i-1 ] + af * ( lp - psar [ i-1 ] );
psar_temp [ i ] = psar [ i ] * (1+Ct1);
}
reverse = 0;
//check for reversal
if ( long )
{
if ( Low [ i ] < psar [ i ] * (1-Ct1) )
{
long = 0; reverse = 1; // reverse position to Short
psar [ i ] = hp; // SAR is High point in prev trade
psar_temp [ i ] = hp;
lp = Low [ i ];
af = af_start;
}
}
else
{
if ( High [ i ] > psar [ i ] * (1+Ct1) )
{
long = 1; reverse = 1; //reverse position to long
psar [ i ] = lp;
psar_temp [ i ] = lp;
hp = High [ i ];
af = af_start;
}
}
if ( reverse == 0 )
{
if ( long )
{
if ( High [ i ] > hp )
{
hp = High [ i ];
af = af + IAF;
if( af > MaxAF ) af = MaxAF;
}
if( Low[ i - 1 ] < psar[ i ] ) psar[ i ] = Low[ i - 1 ];
if( Low[ i - 2 ] < psar[ i ] ) psar[ i ] = Low[ i - 2 ];
}
else
{
if ( Low [ i ] < lp )
{
lp = Low [ i ];
af = af + IAF;
if( af > MaxAF ) af = MaxAF;
}
if( High[ i - 1 ] > psar[ i ] ) psar[ i ] = High[ i - 1 ];
if( High[ i - 2 ] > psar[ i ] ) psar[ i ] = High[ i - 2 ];
}
}
}
//Plot( psar, _DEFAULT_NAME(), ParamColor( "Color", colorRed ), styleDots | styleNoLine | styleThick );
//Plot( psar_temp, _DEFAULT_NAME(), ParamColor( "Color", colorRed ), styleDots | styleNoLine | styleThick );
_SECTION_END();
_SECTION_BEGIN("ADX");
range = Param("ADX Period", 13, 12, 25, 1);
range = Optimize("ADX Period", range, 20, 25, 1 );
MYADXFactor = Param("ADX Factor", 15, 12, 20, 1);
//MYADXFactor = Optimize("ADX Factor", MYADXFactor, 15, 20, 1);
MYADX = ADX(range);
_SECTION_END();
_SECTION_BEGIN("Adaptive Stochastic Oscillator");
prd=Param("Periods",106,100,250 ,1 );
Lmin=Param("Min",48,1,200,1);
Lmax=Param("Max",11,1,100,1);
//prd=Optimize("Periods", 10,1,200,2 );
//Lmin=Optimize("Min",7,1,50,1);
//Lmax=Optimize("Max",28,1,50,1);
V1=StDev(C,prd);
V2=HHV(V1,prd);
V3=LLV(V1,prd);
V4=(V1-V3)/(V2-V3);
CurLen=int(Lmin+(Lmax-Lmin)*(1-V4));
Hh=HHV(H,CurLen);
Ll=LLV(L,CurLen);
stoch=(C-Ll)/(Hh-Ll)*100;
_SECTION_BEGIN("Trading signals");
Cond1=stoch>50;
Cond2=Cross(Open, psar_temp);// AND MYADX>MYADXFactor;
Cond3=stoch<50;
Cond4=Cross(psar_temp, Open);
Buy = Cond1 AND Cond2;// AND MYADX>MYADXFactor;
Sell = Cond3 AND Cond4;// AND MYADX>MYADXFactor;
Short = Sell;
Cover = Buy;
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
Short = ExRem(Short,Cover);
Cover = ExRem(Cover,Short);
BuyPrice = ValueWhen(Buy, Close);
ShortPrice = ValueWhen(Short, Close);
CoverPrice = ValueWhen(Cover, Close);
SellPrice = ValueWhen(Sell, Close);
dist = 1.5*ATR(10);
for (i=2; i<BarCount; i++) {
if (Cover) {
//PlotText( "\nCover short: " + CoverPrice, i+1.5, L[ i ]-dist-3, colorLime);
//PlotText( "\n\nProfit: " + (ShortPrice-CoverPrice), i+1.5, L[ i ]-dist-3, colorLime);
} else if (Sell) {
//PlotText( "\nSell bought: " + SellPrice, i+1.5, H[ i ]+dist+5, colorOrange);
//PlotText( "\n\nProfit: " + (SellPrice-BuyPrice), i+1.5, H[ i ]+dist+5, colorOrange);
}
if(Buy) {
//PlotText( "Buy: " + BuyPrice, i+1.5, L[ i ]-dist-3, colorLime);
} else if( Short) {
//PlotText( "Short: " + ShortPrice, i+1.5, H[ i ]+dist+5, colorOrange);
}
}
PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorLime,0);
PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorRed,0);
Plot( stoch, _DEFAULT_NAME(), ParamColor("Color", ColorCycle ) );
Plot( 80, "Overbought", colorBlue, styleDashed|styleNoLabel|styleNoTitle );
Plot( 20, "Oversold", colorBlue, styleDashed|styleNoLabel|styleNoTitle );
Plot( 50, "zero", colorWhite, styleThick|styleNoLabel|styleNoTitle );