Mã Supertrend V3.0 AFL với Cảnh báo Mua và Bán
Siêu xu hướng V3.0
là phiên bản nâng cấp của Supertrend Indicator. Logic giao dịch vẫn giữ nguyên tuy nhiên rất nhiều lỗi đã được loại bỏ và một số tính năng được thêm vào trong phiên bản hiện tại. Nhiều vấn đề cảnh báo về Âm thanh và Cửa sổ bật lên liên tiếp đã được giải quyết. Bây giờ bạn sẽ chỉ nhận được một cảnh báo bằng âm thanh và cửa sổ bật lên bất cứ khi nào có tín hiệu mua hoặc bán xuất hiện.
Các tính năng đã thêm
1)Cảnh báo âm thanh và cửa sổ bật lên dựa trên quét trong Amibroker như hiển thị bên dưới. Nếu bây giờ bạn đã bật Quét theo thời gian thực, bạn sẽ thấy một cửa sổ bật lên xuất hiện cho các đoạn mã được quét phù hợp.
- Chỉ báo siêu xu hướng với
Bảng điều khiển 5 giao dịch gần đây nhất
Các tính năng hiện có khác
-
Bảng điều khiển giao dịch.
-
Chỉ báo không sơn lại.
-
Tính năng thời gian còn lại cảnh báo sự kết thúc của nến đang chạy hiện tại.
Mã AFL của Amibroker Supertrend V3.0
Nếu bạn là người mới bắt đầu giao dịch, hãy theo dõi video này để biết thêm về siêu xu hướng
Liên quan
/* Done by Rajandran R */
/* Author of www.marketcalls.in */
/* Date : 10th Dec 2019 */
_SECTION_BEGIN("New Supertrend V3.0");
global trend, TrendUp, TrendDown;
function supertrend(Factor, Pd)
{
//local scope
Up=(H+L)/2+(Factor*ATR(Pd));
Dn=(H+L)/2-(Factor*ATR(Pd));
iATR=ATR(Pd);
TrendUp=TrendDown=Null; //Final Trailingstop array for both Buy and Short Signal
trend[0]=1; // 1 - buy continuing and (-1) short is continuing
changeOfTrend=0; //transition from buy to sell or sell to buy
flag=flagh=0;
for(i=1;i<BarCount;i++)
{
TrendUp[i] = Null;
TrendDown[i] = Null;
trend[i]=1;
if (Close[i]>Up[i-1]) { // if close is breaking the upper channel
trend[i]=1; //positive trend started or ongoing
if (trend[i-1] == -1) changeOfTrend = 1; //change in trend from negative to positive
}//if loop closed
else if (Close[i]<Dn[i-1]) { //if the candle is closing below the down channel
trend[i]=-1; //negative trend started or ongoing
if (trend[i-1] == 1) changeOfTrend = 1; //change in trend from positive to negative
}
else if (trend[i-1]==1) { // buy continues
trend[i]=1;
changeOfTrend = 0; //existing trend continues
}
else if (trend[i-1]==-1) { //sell continues
trend[i]=-1;
changeOfTrend = 0; //existing trend continues
}
if (trend[i]<0 && trend[i-1]>0) { //transition from buy to sell
flag=1; //signal just changed from buy to sell signal
}
else {
flag=0;
}
if (trend[i]>0 && trend[i-1]<0) { //transition froms sell to buy
flagh=1; //signal just changed from sell to buy signal
}
else {
flagh=0;
}
//remodifying my channel so that it behaves like a stoploss
if (trend[i]>0 && Dn[i]<Dn[i-1]){ //if current trend is positive down channel is my trailing stoploss
Dn[i]=Dn[i-1]; // keeping the downchannel flat whenver down channel is going down. Here want to trail the downchannel only if the down channel value increases compared to prev downchannel
}
if (trend[i]<0 && Up[i]>Up[i-1]) //if the current trend is negative upper channel as our trailing stoploss
{ Up[i]=Up[i-1]; //keeping the up channel flat whenver up channel value increases. Here want to trail the upperchannel - trail downwards. if the upper channel value is decrated
}
if (flag==1) //transition from buy to sell;
{ Up[i]=(H[i]+L[i])/2+(Factor*iATR[i]); //resetting the upper channel when fresh sell signal happens. upper channel will act like a short TSL
}
if (flagh==1) //sell to buy
{ Dn[i]=(H[i]+L[i])/2-(Factor*iATR[i]); //resetting the down channel fresh uptrend (buy signal happens) lower channel will act like a long TSL
}
if (trend[i]==1) {
TrendUp[i]=Dn[i]; //remodified down channel
if (changeOfTrend == 1) { //negative trend to positive trend
TrendUp[i-1] = TrendDown[i-1]; //transition/swithcover in Trailing stops
changeOfTrend = 0;
}
}
else if (trend[i]==-1) {
TrendDown[i]=Up[i]; //remodified up channel
if (changeOfTrend == 1) {
TrendDown[i-1] = TrendUp[i-1]; //transition/swithcover in Trailing stops
changeOfTrend = 0;
}
}
}//for loop
}
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 Candles AND X-Axis
SetChartOptions(0, chartShowArrows | chartShowDates); //x-Axis will be plottted
Plot(Close,"Close",colorDefault,GetPriceStyle() | styleNoTitle);
Multipier=Param("Factor",3,1,10,0.1);
Period=Param("ATR Periods",10,1,100,1);
colorup = ParamColor("Up Color",colorGreen);
colordown = ParamColor("Down Color",colorRed);
supertrend(Multipier,Period);
//Plot Trailing Stoploss
Plot(TrendUp,"Trend",colorup,styleThick);
Plot(TrendDown,"Down",colordown,styleThick);
//Plot(up,"upper",colorRed,styleLine | styleThick);
//Plot(Dn,"Lower",colorgreen,styleLine | styleThick);
Buy = trend == 1;
Sell = trend == -1;
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
Short = Sell;
Cover = Buy;
//Non Repainting - Signals happens only after the close of the current ( 1 bar delay is introduced while making non repainting)
//Intialize trade delay to zero to avoid any over-riding from external parameters -
SetTradeDelays(0,0,0,0);
//Generate Signals on Closing Basis - Non Repainting and Signals delayed by 1 bar
Buy = Ref(Buy,-1);
Sell = Ref(Sell,-1);
Short = Ref(Short,-1);
Cover = Ref(Cover,-1);
buycontinue = Flip(Buy,Sell);
shortcontinue = Flip(Short,Cover);
//Buy at Next Bar Open, Trade Delay is already given at
BuyPrice=ValueWhen(Buy,Open);
SellPrice=ValueWhen(Sell,Open);
ShortPrice=ValueWhen(Short,Open);
CoverPrice=ValueWhen(Cover,Open);
/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-60);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-70);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-65);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=60);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=70);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-65);
for( i = 0; i < BarCount; i++ )
{
if( Buy[i] ) PlotText("Buy",i,L[i],colorWhite,colorLime,-40);
if( Short[i] ) PlotText("Short",i,H[i],colorWhite,colorRed,40);
//if( Sell[i] ) PlotText( "Selln@" + C[ i ], i, H[ i ]+dist[i], colorRed, colorYellow );
}
TrendSL=IIf(trend==1,TrendUp,TrendDown);
for(i=BarCount-1;i>1;i--)
{
if(Buy[i] == 1)
{
entry = open[i];
sig = 1; //flag contains 1 when buy is valid
sl = TrendSL[i]; //initial stop for buy
tar1 = entry + (entry * .0050);
tar2 = entry + (entry * .0092);
tar3 = entry + (entry * .0179);
bars = i;
i = 0;
}
if(Sell[i] == 1)
{
sig = -1; //flag contains -1 wjhen sell is valid
entry = open[i];
sl = TrendSL[i]; //initial stop for the sell
tar1 = entry - (entry * .0050);
tar2 = entry - (entry * .0112);
tar3 = entry - (entry * .0212);
bars = i; // i - holds the value of bar index, computing the barindex value when the signal happened
i = 0;
}
}//for loop
Offset = 20;
Clr = IIf(sig == 1, colorLime, colorRed);
ssl = IIf(bars == BarCount-1, TrendSL[BarCount-1], Ref(TrendSL, -1));
sl = ssl[BarCount-1];
//plot target lines
Plot(LineArray(bars-Offset, tar1, BarCount-1, tar1,1), "", Clr, styleLine|styleDots, Null, Null, Offset);
Plot(LineArray(bars-Offset, tar2, BarCount-1, tar2,1), "", Clr, styleLine|styleDots, Null, Null, Offset);
Plot(LineArray(bars-Offset, tar3, BarCount-1, tar3,1), "", Clr, styleLine|styleDots, Null, Null, Offset);
_SECTION_END();
_SECTION_BEGIN("Signal Dashboard");
messageboard = ParamToggle("Message Board","Show|Hide",1);
upcolor = ParamColor("Up Color",colorBlue);
dncolor = ParamColor("Down Color",colorRed);
if (messageboard == 1 )
{
GfxSelectFont( "Tahoma", 13, 100 );
GfxSetBkMode( 1 );
GfxSetTextColor( colorWhite );
//Dashboard color changes dynamically according to the signals continuation
color = IIf(buycontinue, upcolor, IIf(shortcontinue, dncolor, Null));
GfxSelectSolidBrush(SelectedValue(color));
pxHeight = Status( "pxchartheight" ) ;
xx = Status( "pxchartwidth");
Left = 1100;
width = 310;
x = 5;
x2 = 360;
y = pxHeight;
sigstatus = WriteIf(buycontinue,"Buy Signal @ ","Sell Signal @");
GfxSelectPen( colorGreen, 1); // broader color
GfxRoundRect( x, y - 98, x2, y , 7, 7 ) ;
GfxTextOut( ( "Marketcalls - Supertrend"),13,y-100);
GfxTextOut( (" "),27,y-100);
if(SelectedValue(Buycontinue))
{
GfxTextOut( ("Last " + sigstatus + " Signal came " + BarsSince(Buy) * Interval()/60 + " mins ago"), 13, y-80) ; // The text format location
GfxTextOut( ("" + sigstatus + " : " + ValueWhen(Buy,BuyPrice)), 13, y-60);
GfxTextOut( ("Profit/Loss : " + NumToStr(Close-ValueWhen(Buy,BuyPrice),1.2)), 13, y-22);
}
if(SelectedValue(Shortcontinue))
{
GfxTextOut( ("Last " + sigstatus + " Signal came " + BarsSince(short) * Interval()/60 + " mins ago"), 13, y-80) ; // The text format location
GfxTextOut( ("" + sigstatus + " : " + ValueWhen(Short,ShortPrice)), 13, y-60);
GfxTextOut( ("Current P/L : " + NumToStr(ValueWhen(Short,ShortPrice)-Close,1.2)), 13, y-22);
}
GfxTextOut( ("Trailing SL : " + Ref(TrendSL,-1) + " (" + WriteVal(IIf(sig == -1,entry-sl,sl-entry), 2.2) + ")"), 13, y-40);
}
_SECTION_END();
_SECTION_BEGIN("Top Dashboard");
X0 = 10;
Y0 = 20;
procedure DrawData (Text, x1, y1, x2, y2, colorFrom, colorTo)
{
GfxSetOverlayMode(0);
GfxSelectFont("Verdana", 8.5, 700);
GfxSetBkMode(1);
GfxGradientRect(x1, y1, x2, y2, colorFrom, colorTo);
GfxDrawText(Text, x1, y1, x2, y2, 32|1|4|16);
}
DrawData (Name(), X0, Y0, X0+150, Y0+20, colorGrey40, colorblack);
DrawData (Date(), X0+155, Y0, X0+320, Y0+20, colorGrey40, colorblack);
DrawData ("Open : " + Open, X0+325, Y0, X0+450, Y0+20, colorGrey40, colorblack);
DrawData ("Close : " + Close, X0+455, Y0, X0+580, Y0+20, colorGrey40, colorblack);
DrawData ("High : " + High, X0+585, Y0, X0+710, Y0+20, colorGrey40, colorblack);
DrawData ("Low : " + Low, X0+715, Y0, X0+840, Y0+20, colorGrey40, colorblack);
_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.