Thư viện AFL
Thư viện AFL
AlphaTrend - Mã AFL của Amibroker để quản lý thị trường đi ngang AlphaTrend - Mã AFL của Amibroker để quản lý thị trường đi ngang

AlphaTrend - Mã AFL của Amibroker để quản lý thị trường đi ngang AlphaTrend - Mã AFL của Amibroker để quản lý thị trường đi ngang

Chiến lược theo xu hướng rất đơn giản và thú vị có thể quản lý thị trường đi ngang ở mức độ lớn hơn khi đi theo xu hướng. Gần đây tôi lấy cảm hứng từ lập trình viên Pinescript, ông KivancOzbilgic và với chỉ báo của ông

Xu hướng Alpha trong Tradingview

và chuyển đổi chiến lược cho người dùng Amibroker.

Alphatrend là gì?

Alpha Trend cố gắng giải quyết những vấn đề đó như:

1-Để giảm thiểu mức dừng lỗ và khắc phục tình trạng thị trường đi ngang.

2-Để có tín hiệu MUA/BÁN chính xác hơn trong điều kiện thị trường có xu hướng.

3- Có mức hỗ trợ và kháng cự đáng kể.

4- Tập hợp các chỉ báo từ các danh mục khác nhau tương thích với nhau và tạo ra sự kết hợp có ý nghĩa về động lượng, xu hướng, độ biến động, khối lượng và điểm dừng lỗ.

Mục đích của Xu hướng Alpha:

1- Hoạt động giống như một chỉ báo chết giống như tổ tiên của nó Magic Trendin đi ngang các điều kiện thị trường và không đưa ra nhiều tín hiệu sai.

2- Với một đường khác có 2 thanh lệch so với đường ban đầu, Alpha Trend có tín hiệu MUA và BÁN từ các điểm giao nhau của chúng.

Tr

Quy tắc quảng cáo

MUA khi đường AlphaTrend vượt lên trên đường bù 2 thanh của nó

BÁN khi đường Alpha Trend cắt xuống dưới đường bù 2 thanh của nó

Đường xu hướng Alpha

-đóng vai trò là mức hỗ trợ khi xu hướng tăng xảy ra ở mức 0,5*

Khoảng cách ATR (hệ số mặc định) từ các giá trị thấp của thanh

-ngược lại, đóng vai trò là mức kháng cự khi xu hướng giảm xảy ra theo sau

Khoảng cách 0,5*ATR (hệ số mặc định) từ các giá trị cao của thanh và đóng vai trò là điểm dừng lỗ theo sau

Các đường Alpha Trend càng thẳng thì càng có nhiều hỗ trợ và kháng cự trở nên mạnh mẽ hơn.

Alphatrend – Mã AFL của Amibroker

Liên quan

Mã nguồn (.afl)
//Alphatrend - Trend Follow Strategy
//Coded by Rajandran R - Founder - Marketcalls / Creator - OpenAlgo
//Website : www.openalgo.in / www.marketcalls.in
//Code Inspired from Tradingview - KivancOzbilgic
//Tradingview URL : https://tradingview.com/script/o50NYLAZ-AlphaTrend/

//Alpha Trend tries to solve those problems such as:

//1-To minimize stop losses and overcome sideways market conditions.
//2-To have more accurate BUY/SELL signals during trending market conditions.
//3- To have significant support and resistance levels.
//4- To bring together indicators from different categories that are compatible with each other and make a meaningful combination regarding momentum, trend, volatility , volume and trailing stop loss.


_SECTION_BEGIN("AlphaTrend");

SetBarsRequired(sbrAll,sbrAll);

coeff = Param("Multiplier",0.5,0.1,1,0.1);
AP = Param("Common Period", 20, 1, 100, 1);

iATR = MA(ATR(1),AP);

src = Close;

showsignalsk = True;
novolumedata = False;

upT = Low - iATR * coeff;
downT = High + iATR * coeff;

hlc3= (H+L+C)/3;

rs = RSIa(Close,14);
mf = MFI(14);

AlphaTrend = 0.0;

//AlphaTrend := (novolumedata ? ta.rsi(src, 14) >= 50 : 
//				ta.mfi(hlc3, 14) >= 50) ? upT < nz(AlphaTrend[1]) ? nz(AlphaTrend[1]) : upT :
//				 downT > nz(AlphaTrend[1]) ? nz(AlphaTrend[1]) : downT


for(i=1;i<BarCount;i++)
{
if(novolumedata){
	Alphatrend[i] = rs[i]>= 50;

}//end novolumedata
else if(mf[i] >= 50){

if(upT[i] < Alphatrend[i-1])
{
Alphatrend[i] = Alphatrend[i-1] ;
}
else{

Alphatrend[i] = upT[i];

}

}

else
{
if(downT[i] > Alphatrend[i-1])
{
Alphatrend[i] = Alphatrend[i-1] ;
}
else 
{
Alphatrend[i] = downT[i];
}

}//end else 



}//end for loop

color = IIf(AlphaTrend>Ref(AlphaTrend,-2), colorGreen, IIf(AlphaTrend<Ref(AlphaTrend,-2), colorred, 
		IIf(Ref(AlphaTrend,-1) > Ref(AlphaTrend,-3), colorblue, colorYellow)));


Buy = Cross(Alphatrend,Ref(Alphatrend,-2));
Sell = Cross(Ref(AlphaTrend,-2),AlphaTrend);


Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

Short = Sell;
Cover = Buy;


//Non Repainting  - Signals happens only after the close of the current
Buy = Ref(Buy,-1);
Sell = Ref(Sell,-1);
Short = Ref(Short,-1);
Cover = Ref(Cover,-1);


BuyPrice = ValueWhen(Buy,Open);
sellPrice = ValueWhen(sell,Open);
shortPrice = ValueWhen(Short,Open);
coverPrice = ValueWhen(cover,Open);

buycontinue = Flip(Buy,Sell);
color1 = IIf(buycontinue,colorGreen,colorRed);

cloud = ParamToggle("Cloud Enabled","TRUE|False",0);
	
if(cloud)
{
PlotOHLC(AlphaTrend,AlphaTrend,Ref(AlphaTrend,-2),Ref(AlphaTrend,-2), "AlphaTrend",color, styleCloud|styleNoLabel);		
}
else
{
Plot(Alphatrend,"Alphatrend",color1,styleLine | styleThick);
}

/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);                      
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);                      
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

PlotShapes(Sell * shapestar, colorBrightGreen, 0, High, 12);
PlotShapes(Cover * shapestar, colorRed, 0, Low, -12);

_SECTION_END();


_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", color1, styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();



_SECTION_BEGIN("Trading Dashboard");

dashboard = ParamToggle("Show Dashboard","SHOW|HIDE",1);

if(dashboard)
{

fontsize = Param("Font Size",14,8,40,1);

GfxSelectFont("BOOK ANTIQUA", fontsize, 400);
GfxSetTextColor(colorWhite);
GfxSetBkMode(1); //Transparent mode

buycontinue = Flip(Buy,Sell);
sellcontinue = Flip(Sell,Buy);

color = IIf(buycontinue, colorGreen, IIf(sellcontinue, colorRed, colorGrey40));

GfxSelectPen(colorWhite); //pen -> draw outline of the shape
GfxSelectSolidBrush(SelectedValue(color));

width = Status("pxchartwidth");
height = Status("pxchartheight");


GfxRoundRect(20,height-150,320,height-30,10,10);

//Trading Signal Status - e.g Buy Signal or Short Signal
//Profit/Loss for Current Running Trade

sigstatus = WriteIf(buycontinue,"Buy Signal", WriteIf(sellcontinue,"Short Signal", "No Trade(Relax)"));
PNL = IIf(buycontinue,Close-BuyPrice, IIf(sellcontinue, ShortPrice-Close, Null));

//variable sigstatus -> string
//variable PNL -> Array

GfxTextOut("AlphaTrend",30,height-130);
GfxTextOut(sigstatus + " came @ : "+ IIf(buycontinue,BuyPrice, IIf(sellcontinue, ShortPrice, Null)),30,height-110);
GfxTextOut("Profit/Loss :"+SelectedValue(Prec(PNL,2)),30, height-90);


}

_SECTION_END();

Hướng dẫn: Copy đoạn mã trên, mở AmiBroker Formula Editor, dán vào và lưu lại với tên tương ứng.