Thứ bảy, 13/03/2021 | 14:57 GMT+7
Code So ví dụ tương đương Tradingview - Pine Script và Amibroker - Afl
Nếu bạn biết Pine Script, muốn tham khảo thêm code tương đương của Amibroker hoặc ngược lại, thì đây là các ví dụ tương ứng để bạn dễ tiếp cận. Tradingview Pine script có vẻ dễ hiểu , có nhiều tài liệu hơn so với AFL amibroker. Đoạn code sau thực thi takeprofit và stoplost với AFL và PineScript tương đương nhauTake profit 2%
Stop Lost 1%
BUY, SELL khi giao cắt khi MACD, Signal...
Code PineScript
//@version=4 //Refer: congnghe.pro strategy("st MACD stoplost, take profit long,short", overlay=false) inFastEma = input(12), inSlowEma = input(26), inSignal = input(9), inEMA200 = input(150, step=10) fastMA = ema(close, inFastEma) slowMA = ema(close, inSlowEma) macd = fastMA - slowMA signal = sma(macd, inSignal) ema200 = ema(close, inEMA200) ntp = input(2, type=input.float, step = 0.1) // 2.6 và 0.1 t?t nh?t cho vn30 nsl = input(1, type=input.float, step = 0.1) // 2.6 và 0.1 t?t nh?t cho vn30 plot(macd, color=color.blue) plot(signal, color=color.red) hline(0, '0 Line', color=color.white) //LONG: MACD crosses above signal line // and MACD crossover is below the zero line // and market is above 200 Moving Average //macd < 0 and , and (close > ema200 and open > ema200) if crossover(macd, signal) and strategy.position_size <= 0 strategy.entry(id="EL", long =true) //SHORT: market is below the 200 //moving average and MACD line crosses below the signal line //and the MACD crossover is above the zero line //macd > 0 and, and (close < ema200 and open < ema200) if crossunder(macd, signal) and strategy.position_size >= 0 strategy.entry(id="ES", long=false) //Stop lost when <= 1%, take benifit when >= 1.5% if strategy.position_size > 0 strategy.exit(id="XL", stop=strategy.position_avg_price * (1 - nsl/100), limit=strategy.position_avg_price * (1 + ntp/100)) //Stop lost when <= 1%, take benifit when >= 1.5% if strategy.position_size < 0 strategy.exit(id="XS", stop=strategy.position_avg_price * (1 + nsl/100), limit=strategy.position_avg_price * (1 - ntp/100))
Code tương đương Amibroker
//Refer: congnghe.pro _SECTION_BEGIN("TestMACD stoplost, take profit"); r1 = Param( "Fast avg", 12, 2, 200, 1 ); r2 = Param( "Slow avg", 26, 2, 200, 1 ); r3 = Param( "Signal avg", 9, 2, 200, 1 ); r1 = Optimize( "Fast avg", r1 , 2, 250, 1 ); r2 = Optimize( "Slow avg", r2 , 2, 200, 1 ); r3 = Optimize( "Signal avg", r3 , 2, 200, 1 ); nTP = Param( "TP avg", 2, 0.1, 10, 0.1 ); nSL = Param( "SL avg", 1, 0.1, 10, 0.1 ); nTP = Optimize( "TP avg", nTP, 0.1, 10, 0.1 ); nSL = Optimize( "SL avg", nSL, 0.1, 10, 0.1 ); ema1 = EMA(C, 150); Plot( ml = MACD(r1, r2), StrFormat(_SECTION_NAME()+"(%g,%g)", r1, r2), ParamColor("MACD color", colorRed ), ParamStyle("MACD style") ); Plot( sl = Signal(r1,r2,r3), "Signal" + _PARAM_VALUES(), ParamColor("Signal color", colorBlue ), ParamStyle("Signal style") ); Plot( ml-sl, "MACD Histogram", ParamColor("Histogram color", colorBlack ), styleNoTitle | ParamStyle("Histogram style", styleHistogram | styleNoLabel, maskHistogram ) ); //Buy = Cover = Cross(ml, sl) AND C > ema1 AND O > ema1; //Sell = Short = Cross(sl, ml) AND C < ema1 AND O < ema1; Buy = Cover = Cross(ml, sl); Sell = Short = Cross(sl, ml); Buy = ExRem( Buy, Sell); Sell = ExRem(Sell, Buy); Cover = ExRem(Cover, Short); Short = ExRem(Short, Cover); PlotShapes( Buy * shapeUpArrow + Sell * shapeDownArrow, IIf( Buy, colorGreen, colorRed ) ); ApplyStop(stopTypeLoss,stopModePercent,nSl,True); ApplyStop(stopTypeProfit,stopModePercent,nTp,True); _SECTION_END();
Kết quả so sánh
Khung 1h BTC sàn Binance,
02 bên kết quả report không hoàn toàn như nhau, do cách tính chi tiết của 2 công cụ
Nhưng cũng có nhiều điểm tương đồng, ví dụ như hình vẽ dưới đây
Sự khác biệt này có thể dẫn đến kết quả trade sẽ không hoàn toàn tương đương
Các tin trước
Cài đặt mongodb trên Ubuntu 18.04 source 2020-10-09 Cấu hình remote-access cho mongodb trên Ubuntu 18.04 2020-10-08 Cài đặt anaconda python trên Ubuntu 18 2020-10-03 Tạo user mới và Phân quyền với Mysql 2020-09-29 Cài đặt LAMP (Apache MariaDb Php) trên CENTOS 8 2020-09-28 Cách cài đặt máy chủ Apache trên Centos 8 2020-09-28 Tổng quan sử dụng SSH, làm việc với SSH Client - Server 2020-09-28 Cách cài đặt SSH Key trên Centos 8, đăng nhập không cần password! 2020-09-27 Cài đặt bảo mật cơ bản cho Centos 8 2020-09-27 Lập lịch với Crontab trên Centos 8 2020-09-27