// Downloaded From https://www.WiseStockTrader.com
// Set title
Title = "T+4 " + " " + FullName() + " " + Date() + ": C (" + C + ") - O (" + O + ") - H (" + H + ") - L (" + L + ")";

// Set parameter variables
MinClose = Param("Min Close", 2, 1, 300, 1);
MinVolume = Param("Min Volume", 50000, 10000, 100000000, 10000);

// Calculate T+4 values
prevT4 = AMA2(Close, 1, 0);

dT4 = IIf(Close > Ref(Max(Max(High, Ref(High, -3)), Max(Ref(High, -1), Ref(High, -2))), -1),
         Min(Min(Low, Ref(Low, -3)), Min(Ref(Low, -1), Ref(Low, -2))),
         IIf(Close < Ref(Min(Min(Low, Ref(Low, -3)), Min(Ref(Low, -1), Ref(Low, -2))), -1),
             Max(Max(High, Ref(High, -3)), Max(Ref(High, -1), Ref(High, -2))),
             prevT4
            )
        );
at4 = Cross(Close, dT4);
bt4 = Cross(dT4, Close);
state = IIf(BarsSince(at4) < BarsSince(bt4), 1, 0);
Buyt4 = state > Ref(state, -1);
Sellt4 = state < Ref(state, -1);
Noaction = state == Ref(state, -1);
col = IIf(state == 1, 51, IIf(state == 0, 4, 1));

// Calculate technical indicators
currentRSI = RSI(12);
prevRSI = Ref(currentRSI, -1);
CondClose = Close >= MinClose;
CondVolume = MA(Volume, 20) >= MinVolume;
PrevClose = Ref(Close, -1);
Price = Prec((Close - PrevClose) * 100 / PrevClose, 2);
LongMa = MA(Close, 105);

// Ma Sell Condition
shortMa = MA(C, 9);
downTrend = Close < LongMa;
upTrend = Close >= LongMa;

MaSellCond = PrevClose < shortMa AND C < PrevClose AND state == 1;

// RSI Buy Cond
RsiBuyCond = prevRSI < 28 AND currentRSI >= prevRSI;

// RSI Sell Condition
RsiSellCond = prevRSI > 72 AND currentRSI <= prevRSI;

// Calculate buy/sell signals
Buy = ((upTrend AND BuyT4) OR (downTrend AND BuyT4)) AND CondClose AND CondVolume;
Sell = Sellt4 OR MaSellCond;
Filter = Buy OR Sell;

TradingText = IIf(Buy, 1, 0);
colorTradingBackground = IIf(Buy, colorLime, colorRed);

// Add columns to the output table
AddTextColumn(IcbID(1), "Sector");
AddTextColumn(SectorID(1), "Nhom");
AddTextColumn(MarketID(1), "Market");
AddColumn(TradingText, "Trading", 1, colorWhite, colorTradingBackground);
AddColumn(Price, "Close");
AddColumn(Prec(GetFnData("EPS") * 1000, 0), "EPS");
AddColumn(Prec(Close / GetFnData("EPS"), 1), "P/E");
AddColumn(Prec(Close / GetFnData("BookValuePerShare"), 1), "P/B");
AddColumn(Prec(Close / GetFnData("SalesPerShare"), 1), "P/S");
AddColumn(Prec(GetFnData("SalesPerShare") * 1000, 2), "Sales/Share");
AddColumn(Prec(GetFnData("BookValuePerShare") * 1000, 2), "BV/Share");
AddColumn(Prec(GetFnData("ReturnOnEquity") , 2), "ROE");
AddColumn(Prec(GetFnData("ReturnOnAssets") , 2), "ROA");
AddColumn(Prec(GetFnData("SharesOut") / 1000000 , 3), "KLCP");
AddColumn(Prec(GetFnData("SharesOut") * C / 1000000, 0), "Von hoa");
AddColumn(currentRSI, "RSI");

SetSortColumns(3, 18, -2);

Plot(C, "", col, styleBar);

PlotShapes(shapeUpArrow * RsiBuyCond, colorBlue, 0, L);
PlotShapes(shapeUpArrow * Buy, colorLime, 0, L);

PlotShapes(shapeDownArrow * RsiSellCond, colorPink, 0, H);
PlotShapes(shapeDownArrow * MaSellCond, colorYellow, 0, H);
PlotShapes(shapeDownArrow * Sellt4, colorRed, 0, H);

// MA
_SECTION_BEGIN("Short MA");
Periods = Param("Periods", 9, 2, 300, 1, 10 );
Plot( MA( Close, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorGreen ), ParamStyle("Style", styleDashed)); 
_SECTION_END();

_SECTION_BEGIN("Medium MA");
Periods = Param("Periods", 20, 2, 300, 1, 10 );
Plot( MA( Close, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorRed ), ParamStyle("Style") ); 
_SECTION_END();

_SECTION_BEGIN("Long MA");
Periods = Param("Periods", 105, 2, 300, 1, 10 );
Plot( MA( Close, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorYellow ), ParamStyle("Style", styleThick) ); 
_SECTION_END();