Trong bài này chúng ta sẽ áp dụng Stop Loss/ Take Profit [SL/TP] trong EA mà chúng ta đã phát triển trong bài Viết chương trình Simple Buy EA. SL/TP là biện pháp thường được sử dụng trong trading nhằm hạn chế rủi ro cũng như thu về lợi nhuận mong muốn. Ví dụ khi ta mở một vị thế Long, mục tiêu lãi của chúng ta là 50 pips và trong trường hợp rủi ro chúng ta không muốn lỗ quá 20 pips thì ta sẽ đặt SL= 20 pips và TP= 50 pips.
Với Stop loss, chúng ta có thể chia thành hai loại chính là Hard-SL và Rule based SL. Hard-SL là SL mà ta thiết lập giá trị SL cố định, không thay đổi theo thời gian còn Rule based SL là giá trị SL có thể thay đổi tùy theo tiêu chuẩn của chúng ta [ví dụ như SL thay đổi theo đường SMA, ATR,..]. Một dạng Rule based SL phổ biến thường dùng là Trailing Stop [giá trị SL di chuyển theo giá thực tế ở một khoảng cách nhất định].
Để viết chương trình EA sử dụng SL/TP, chúng ta sẽ sử dụng lại code đã được trình bày trong bài Viết chương trình Simple Buy EA và bổ sung thêm SL/TP cũng như Trailing Stop. Trong phần Setting, ta định nghĩa thêm một số biến cần thiết như sau:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | //+------------------------------------------------------------------+ //|SLTP_EA.mq5 | //|Copyright 2020, MetaQuotes Software Corp. | //| //www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2020, MetaQuotes Software Corp." #property link"//www.mql5.com" #property version "1.00" #include // Define variables CTrade tradeController; double currentAsk; input int short_sma=10, medium_sma=51; int short_smaHandle, medium_smaHandle; double short_smaData[], medium_smaData[]; input int fixed_SL=50, fixed_TP=80, trailing_ST=30; // The value in pips input bool useTrailingSL=true; // True if we want to use Trailing stop double SL_price, TP_price, TrailingST_price; // The real price |
Lưu ý:
Ở đây ta thiết lập giá trị fixed_SL, fixed_TP và trailing_ST ở đơn vị pip và ta sẽ tiến hành đổi sang giá trị giao dịch thực trong phần OnTick
Giá trị short_sma và medium_sma được thay đổi là 10 và 51 theo như kết quả testing trình bày trong bài Tối ưu hóa Expert Advisors
Đối với phần OnInit và DeInit thì ta vẫn giữ nguyên code như trong bài bài Viết chương trình Simple Buy EA.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit[] { //---Get handle for sma indicators short_smaHandle= iMA[_Symbol,_Period, short_sma,0,MODE_SMA, PRICE_CLOSE]; medium_smaHandle= iMA[_Symbol,_Period,medium_sma,0,MODE_SMA, PRICE_CLOSE]; if[short_smaHandle current SL if[TrailingST_price>PositionGetDouble[POSITION_SL]] { tradeController.PositionModify[_Symbol,TrailingST_price,0]; Print["Trailing Stop moved from ",PositionGetDouble[POSITION_SL]," to ", TrailingST_price]; } } } |
Ở đây, ta chỉ kích hoạt Trailing Stop khi giá trị của Trailing Stop cao hơn giá trị mà chúng ta đã đặt mua để đảm bảo khi đóng lệnh với Trailing Stop ta vẫn sẽ thu được một khoản lợi nhuận nhất định. Ngoài ra, khi ta đã kích hoạt Trailing Stop thì không sử dụng Hard TP, do đó ta thiết lập giá trị này bằng 0.
Như vậy, full code của chương trình EA sử dụng SL/TP và Trailing Stop như sau:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | //+------------------------------------------------------------------+ //|SLTP_EA.mq5 | //|Copyright 2020, MetaQuotes Software Corp. | //| //www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2020, MetaQuotes Software Corp." #property link"//www.mql5.com" #property version "1.00" #include // Define variables CTrade tradeController; double currentAsk; input int short_sma=10, medium_sma=51; int short_smaHandle, medium_smaHandle; double short_smaData[], medium_smaData[]; input int fixed_SL=50, fixed_TP=80, trailing_ST=30; // The value in pips input bool useTrailingSL=true; // True if we want to use Trailing stop double SL_price, TP_price, TrailingST_price; // The real price //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit[] { //---Get handle for sma indicators short_smaHandle= iMA[_Symbol,_Period, short_sma,0,MODE_SMA, PRICE_CLOSE]; medium_smaHandle= iMA[_Symbol,_Period,medium_sma,0,MODE_SMA, PRICE_CLOSE]; if[short_smaHandle current SL if[TrailingST_price>PositionGetDouble[POSITION_SL]] { tradeController.PositionModify[_Symbol,TrailingST_price,0]; Print["Trailing Stop moved from ",PositionGetDouble[POSITION_SL]," to ", TrailingST_price]; } } } } // Only open a new position if there is no existing position if[PositionSelect[_Symbol]==false] { // Open long position if SMA20 cut SMA 50 from below if[short_smaData[0]>medium_smaData[0] && short_smaData[1]
Chủ Đề |