/*
http://www.traderji.com/amibroker/84273-i-can-create-afl-but-need-strategy.html
strategy
chart price as candles,ma5, ma20
display macd,signal,rsi(7) values in legend-title
buy condition
Buy if 1) ma(c,5) cross ma(c,20)
2)macd>signal
3)Rsi(7)>55
4)STOPLOSS =ENTRY - 2*ATR(20) =PLOT AS A LINE
5)TARGET1 ENTRY+2*ATR(20)
TARGET2 =ENTRY+4*ATR(20)
PLOT targets AS DASHED LINES
*/
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();
Plot(RSI(7),"7 D RSI",4,256+styleOwnScale);
Plot(MACD(),"MACD",4,256+styleOwnScale);
Plot(Signal(),"Signal",4,256+styleOwnScale);
MAfast=MA(C,5);MAslow=MA(C,20);
Buy=Cross(MA(C,5),MA(C,20) )AND MACD()>Signal() AND RSI(7)>55;
j=C;
k = Optimize("K",Param("K",2,0.25,5,0.05),0.25,5,0.05) ;
Per=Optimize("atr",Param("atr",20,3,20,1),3,20,1);
f=ATR(20);
rfsctor = WMA(H-L, Per);
revers = k * rfsctor;
Trend = 1;
NW[0] = 0;
for(i = 1; i < BarCount; i++)
{
if(Trend[i-1] == 1)
{
if(j[i] < NW[i-1])
{
Trend[i] = -1;
NW[i] = j[i] + Revers[i];
}
else
{
Trend[i] = 1;
if((j[i] - Revers[i]) > NW[i-1])
{
NW[i] = j[i] - Revers[i];
}
else
{
NW[i] = NW[i-1];
}
}
}
if(Trend[i-1] == -1)
{
if(j[i] > NW[i-1])
{
Trend[i] = 1;
NW[i] = j[i] - Revers[i];
}
else
{
Trend[i] = -1;
if((j[i] + Revers[i]) < NW[i-1])
{
NW[i] = j[i] + Revers[i];
}
else
{
NW[i] = NW[i-1];
}
}
}
}
//===============system================
Plot(NW, "", IIf(Trend == 1, 27, 4), 4);
BuyPrice=C;
ProfitTaker1 = IIf(C>nw,BuyPrice + 2 * ATR(20),Null);
ProfitTaker2 = IIf(C>nw,BuyPrice + 4 * ATR(20),Null);
Plot(Ref(ProfitTaker1,-1), "1st Profit areal", 6, 512+4+32);
Plot(Ref(ProfitTaker2,-1), "1st Profit areal", 6, 512+4+32);
Sell=Cross(nw,C);
PlotShapes(Buy * shapeUpArrow,colorGreen, 0,L, Offset=-10);
PlotShapes(Sell * shapeHollowDownArrow,colorRed, 0,H, Offset=-10);