Article-14782-MQL5-Trailing.../TrailingBySAR_02.mq5

244 lines
23 KiB
MQL5
Raw Permalink Normal View History

2026-03-22 16:52:40 +07:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| TrailingBySAR_02.mq5 |
//| Copyright 2024, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
2026-03-22 17:00:47 +07:00
#property copyright "Copyright 2024, MetaQuotes Ltd."
2026-03-22 16:52:40 +07:00
#property link "https://www.mql5.com"
#property version "1.00"
#define SAR_DATA_INDEX 1 // 10@, A :>B>@>3> ?>;CG05< 40==K5 Parabolic SAR
#include <Trade\Trade.mqh> // 70<5=8< B>@3>2K5 DC=:F88 <5B>40<8 !B0=40@B=>9 81;8>B5:8
//--- input parameters
input ENUM_TIMEFRAMES InpTimeframeSAR = PERIOD_CURRENT; // Parabolic SAR Timeframe
input double InpStepSAR = 0.02; // Parabolic SAR Step
input double InpMaximumSAR = 0.2; // Parabolic SAR Maximum
input ulong InpMagic = 123; // Magic Number
//--- global variables
int ExtHandleSAR=INVALID_HANDLE; // EM=4; Parabolic SAR
double ExtStepSAR=0; // H03 Parabolic SAR
double ExtMaximumSAR=0; // <0:A8<C< Parabolic SAR
CTrade ExtTrade; // M:75<?;O@ :;0AA0 B>@3>2KE >?5@0F89
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- CAB0=02;8205< 2 >1J5:B B>@3>2>3> :;0AA0 <038G5A:89 =><5@
ExtTrade.SetExpertMagicNumber(InpMagic);
//--- CAB0=02;8205< ?0@0<5B@K Parabolic SAR 2 4>?CAB8<KE ?@545;0E
ExtStepSAR =(InpStepSAR<0.0001 ? 0.0001 : InpStepSAR);
ExtMaximumSAR=(InpMaximumSAR<0.0001 ? 0.0001 : InpMaximumSAR);
//--- ?@8 >H81:5 A>740=8O 8=48:0B>@0 2K2>48< A>>1I5=85 2 6C@=0; 8 2KE>48< A >H81:>9 87 OnInit
ExtHandleSAR =iSAR(Symbol(), InpTimeframeSAR, ExtStepSAR, ExtMaximumSAR);
if(ExtHandleSAR==INVALID_HANDLE)
{
PrintFormat("Failed to create iSAR(%s, %s, %.3f, %.2f) handle. Error %d",
Symbol(), TimeframeDescription(InpTimeframeSAR), ExtStepSAR, ExtMaximumSAR, GetLastError());
return(INIT_FAILED);
}
//--- CA?5H=>
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- 5A;8 =5 =>2K9 10@ - CE>48< 87 >1@01>BG8:0
if(!IsNewBar())
return;
if(MQLInfoInteger(MQL_TESTER))
{
//--- ?>;CG05< 40==K5 Parabolic SAR A 10@>2 1 8 2
double sar1=GetSARData(SAR_DATA_INDEX);
double sar2=GetSARData(SAR_DATA_INDEX+1);
//--- 5A;8 AB@C:BC@0 F5= 70?>;=5=0 8 40==K5 >B Parabolic SAR ?>;CG5=K
MqlTick tick={};
if(SymbolInfoTick(Symbol(), tick) && sar1!=EMPTY_VALUE && sar2!=EMPTY_VALUE)
{
//--- 5A;8 Parabolic SAR =0 10@5 1 =865 F5=K Bid, 0 =0 10@5 2 2KH5 - >B:@K205< 4;8==CN ?>78F8N
if(sar1<tick.bid && sar2>tick.bid)
ExtTrade.Buy(0.1);
//--- 5A;8 Parabolic SAR =0 10@5 1 2KH5 F5=K Ask, 0 =0 10@5 2 =865 - >B:@K205< :>@>B:CN ?>78F8N
if(sar1>tick.ask && sar2<tick.ask)
ExtTrade.Sell(0.1);
}
}
//--- B@0;8< AB>?K ?>78F89 ?> Parabolic SAR
TrailingStopBySAR(InpMagic);
}
//+------------------------------------------------------------------+
//| TradeTransaction function |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction& trans,
const MqlTradeRequest& request,
const MqlTradeResult& result)
{
if(trans.type==TRADE_TRANSACTION_DEAL_ADD)
TrailingStopBySAR(InpMagic);
}
//+------------------------------------------------------------------+
//| >72@0I05B >?8A0=85 B09<D@59<0 |
//+------------------------------------------------------------------+
string TimeframeDescription(const ENUM_TIMEFRAMES timeframe)
{
return(StringSubstr(EnumToString(timeframe==PERIOD_CURRENT ? Period() : timeframe), 7));
}
//+------------------------------------------------------------------+
//| >72@0I05B 2@5<O >B:@KB8O 10@0 ?> 8=45:AC B09<A5@88 |
//+------------------------------------------------------------------+
datetime TimeOpenBar(const int index)
{
datetime array[1];
ResetLastError();
if(CopyTime(NULL, PERIOD_CURRENT, index, 1, array)!=1)
{
PrintFormat("%s: CopyTime() failed. Error %d", __FUNCTION__, GetLastError());
return 0;
}
return array[0];
}
//+------------------------------------------------------------------+
//| >72@0I05B D;03 >B:@KB8O =>2>3> 10@0 B09<A5@88 |
//+------------------------------------------------------------------+
bool IsNewBar(void)
{
static datetime time_prev=0;
datetime bar_open_time=TimeOpenBar(0);
if(bar_open_time==0)
return false;
if(bar_open_time!=time_prev)
{
time_prev=bar_open_time;
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| >72@0I05B 40==K5 Parabolic SAR A C:070==>3> 8=45:A0 B09<A5@88 |
//+------------------------------------------------------------------+
double GetSARData(const int index)
{
double array[1];
ResetLastError();
if(CopyBuffer(ExtHandleSAR, 0, index, 1, array)!=1)
{
PrintFormat("%s: CopyBuffer() failed. Error %d", __FUNCTION__, GetLastError());
return EMPTY_VALUE;
}
return array[0];
}
//+------------------------------------------------------------------+
//| >72@0I05B @07<5@ StopLevel 2 ?C=:B0E |
//+------------------------------------------------------------------+
int StopLevel(const int spread_multiplier)
{
int spread =(int)SymbolInfoInteger(Symbol(), SYMBOL_SPREAD);
int stop_level=(int)SymbolInfoInteger(Symbol(), SYMBOL_TRADE_STOPS_LEVEL);
return(stop_level==0 ? spread * spread_multiplier : stop_level);
}
//+------------------------------------------------------------------+
//| #=825@A0;L=0O DC=:F8O B@59;8=30 AB>?0 ?> 7=0G5=8N F5=K StopLoss |
//+------------------------------------------------------------------+
void TrailingStopByValue(const double value_sl, const long magic=-1, const int trailing_step_pt=0, const int trailing_start_pt=0)
{
//--- AB@C:BC@0 F5=
MqlTick tick={};
//--- 2 F8:;5 ?> >1I5<C :>;8G5AB2C >B:@KBKE ?>78F89
int total=PositionsTotal();
for(int i=total-1; i>=0; i--)
{
//--- ?>;CG05< B8:5B >G5@54=>9 ?>78F88
ulong pos_ticket=PositionGetTicket(i);
if(pos_ticket==0)
continue;
//--- ?>;CG05< A8<2>; 8 <038: ?>78F88
string pos_symbol = PositionGetString(POSITION_SYMBOL);
long pos_magic = PositionGetInteger(POSITION_MAGIC);
//--- ?@>?CA:05< ?>78F88, =5 A>>B25BAB2CNI85 D8;LB@C ?> A8<2>;C 8 <038:C
if((magic!=-1 && pos_magic!=magic) || pos_symbol!=Symbol())
continue;
//--- 5A;8 F5=K ?>;CG8BL =5 C40;>AL - 84Q< 40;55
if(!SymbolInfoTick(Symbol(), tick))
continue;
//--- ?>;CG05< B8? ?>78F88, F5=C 5Q >B:@KB8O 8 C@>25=L StopLoss
ENUM_POSITION_TYPE pos_type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
double pos_open=PositionGetDouble(POSITION_PRICE_OPEN);
double pos_sl =PositionGetDouble(POSITION_SL);
//--- 5A;8 CA;>28O 4;O <>48D8:0F88 StopLoss ?>4E>4OB - <>48D8F8@C5< AB>? ?>78F88
if(CheckCriterion(pos_type, pos_open, pos_sl, value_sl, trailing_step_pt, trailing_start_pt, tick))
ExtTrade.PositionModify(pos_ticket, value_sl, PositionGetDouble(POSITION_TP));
}
}
//+------------------------------------------------------------------+
//|@>25@O5B :@8B5@88 <>48D8:0F88 StopLoss ?>78F888 8 2>72@0I05B D;03|
//+------------------------------------------------------------------+
bool CheckCriterion(ENUM_POSITION_TYPE pos_type, double pos_open, double pos_sl, double value_sl,
int trailing_step_pt, int trailing_start_pt, MqlTick &tick)
{
//--- 5A;8 AB>? ?>78F88 8 C@>25=L AB>?0 4;O <>48D8:0F88 @02=K - 2>72@0I05< false
if(NormalizeDouble(pos_sl-value_sl, Digits())==0)
return false;
double trailing_step = trailing_step_pt*Point(); // ?5@52>48< H03 B@0;0 2 F5=C
double stop_level = StopLevel(2)*Point(); // ?5@52>48< C@>25=L StopLevel A8<2>;0 2 F5=C
int pos_profit_pt = 0; // ?@81K;L ?>78F88 2 ?C=:B0E
//--- 2 7028A8<>AB8 >B B8?0 ?>78F88 ?@>25@O5< CA;>28O 4;O <>48F8:0F88 StopLoss
switch(pos_type)
{
//--- 4;8==0O ?>78F8O
case POSITION_TYPE_BUY :
pos_profit_pt=int((tick.bid-pos_open)/Point()); // @0AAG8BK205< ?@81K;L ?>78F88 2 ?C=:B0E
if(tick.bid-stop_level>value_sl // 5A;8 F5=0 8 >B;>65==K9 >B =5Q C@>25=L StopLevel 2KH5 C@>2=O StopLoss (A>1;N45=0 48AB0=F8O ?> StopLevel)
&& pos_sl+trailing_step<value_sl // 5A;8 C@>25=L StopLoss 2KH5, G5< H03 B@0;0, >B;>65==K9 >B B5:CI53> StopLoss ?>78F88
&& (trailing_start_pt==0 || pos_profit_pt>trailing_start_pt) // 5A;8 B@0;8< ?@8 ;N1>9 ?@81K;8 8;8 ?@81K;L ?>78F88 2 ?C=:B0E 1>;LH5 7=0G5=8O =0G0;0 B@59;8=30 - 2>72@0I05< true
)
return true;
break;
//--- :>@>B:0O ?>78F8O
case POSITION_TYPE_SELL :
pos_profit_pt=int((pos_open-tick.ask)/Point()); // @0AAG8BK205< ?@81K;L ?>78F88 2 ?C=:B0E
if(tick.ask+stop_level<value_sl // 5A;8 F5=0 8 >B;>65==K9 >B =5Q C@>25=L StopLevel =865 C@>2=O StopLoss (A>1;N45=0 48AB0=F8O ?> StopLevel)
&& (pos_sl-trailing_step>value_sl || pos_sl==0) // 5A;8 C@>25=L StopLoss =865, G5< H03 B@0;0, >B;>65==K9 >B B5:CI53> StopLoss ?>78F88 8;8 C ?>78F88 5IQ =5 CAB0=>2;5= StopLoss
&& (trailing_start_pt==0 || pos_profit_pt>trailing_start_pt) // 5A;8 B@0;8< ?@8 ;N1>9 ?@81K;8 8;8 ?@81K;L ?>78F88 2 ?C=:B0E 1>;LH5 7=0G5=8O =0G0;0 B@59;8=30 - 2>72@0I05< true
)
return true;
break;
//--- ?> C<>;G0=8N 25@=Q< false
default: break;
}
return false;
}
//+------------------------------------------------------------------+
//| $C=:F8O B@59;8=30 StopLoss ?> 8=48:0B>@C Parabolic SAR |
//+------------------------------------------------------------------+
void TrailingStopBySAR(const long magic=-1, const int trailing_step_pt=0, const int trailing_start_pt=0)
{
//--- ?>;CG05< 7=0G5=85 Parabolic SAR A ?5@2>3> 10@0 B09<A5@88
double sar=GetSARData(SAR_DATA_INDEX);
//--- 5A;8 40==K5 ?>;CG8BL =5 C40;>AL - CE>48<
if(sar==EMPTY_VALUE)
return;
//--- 2K7K205< C=825@A0;L=CN DC=:F8N B@0;0 A C:070=85< F5=K StopLoss, ?>;CG5==>9 >B Parabolic SAR
TrailingStopByValue(sar, magic, trailing_step_pt, trailing_start_pt);
}
//+------------------------------------------------------------------+