根据SAR移动止损的EA SARTrailingSystem.mq5-MT5 EA-峰汇在线
诚信为本
量力而为
当前位置:峰汇在线 > MetaTrader 5 > MT5 EA > 正文

根据SAR移动止损的EA SARTrailingSystem.mq5

SARTrailingSystem EA可以开启随机建仓模式。移动止损根据SAR信号运行。

源码:

//+------------------------------------------------------------------+
//|                                            SARTrailingSystem.mq5 |
//|                                        Copyright 2016, Oschenker |
//+------------------------------------------------------------------+
#property copyright  "Copyright 2016, Oschenker"
#property link       "http://www.125808047.com"
#property version    "1.00"

input int            TimerTime=300;        // Timer frequency (sec)
input int            StopLoss = 100;       // Initial Stop-loss (points)
input double         AFStep = 0.02;       // SAR acceleration factor increment step
input double         AFCap = 0.2;         // SAR acceleration factor max. value
input bool           RandomTrade = true;  // Random trade toggle

int                  TradeValue;
long                 StopLevel;
datetime             BarTime;
double               StopLossLevel;
double               EP;
double               AF;
double               delta;
ENUM_POSITION_TYPE   Trend;

bool                 ChangeFlag;
bool                 TimerFlag = false;

MqlTradeRequest      Request = {0};
MqlTradeResult       Results = {0};
MqlRates             Price[];
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   StopLevel = SymbolInfoInteger(Symbol(), SYMBOL_TRADE_STOPS_LEVEL);
   MathSrand(int(TimeLocal())); 
   if(EventSetTimer(TimerTime)) Print("Timer Setup to ", TimerTime, " sec.");
   else Print("Timer Error ", GetLastError());  
   Comment("www.125808047.com"); 
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   EventKillTimer();
   ChartSetString( 0, CHART_COMMENT, "");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(CopyRates(Symbol(), 0, 0, 1, Price) > 0)
     {
      if(PositionSelect(Symbol()))
         {
          if(PositionGetDouble(POSITION_SL, StopLossLevel))
            {
             if(EP == 0) EP = Price[0].close;
             if(AF == 0) AF = AFStep;
             if(BarTime != (datetime)SeriesInfoInteger(Symbol(), 0, SERIES_LASTBAR_DATE))
                 {
                  BarTime = (datetime)SeriesInfoInteger(Symbol(), 0, SERIES_LASTBAR_DATE);
                  Trend = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
                  switch(Trend)
                     {
                      case POSITION_TYPE_BUY:
                           if(Price[0].high > EP)
                              {
                               AF = fmin(AFCap, AF + AFStep);
                               EP = Price[0].high;
                              }
                           delta += AF * (EP - StopLossLevel);
                           ChangeFlag = false;
                           if(delta > _Point)
                              {
                               StopLossLevel += delta;
                               delta = 0;
                               ChangeFlag = (Price[0].close - StopLossLevel) > StopLevel * _Point;
                              }
                           break;
                      case POSITION_TYPE_SELL:
                           if(Price[0].low < EP)
                                 {
                                  AF = fmin(AFCap, AF + AFStep);
                                  EP = Price[0].low;
                                 }
                           delta += AF * (StopLossLevel - EP);
                           ChangeFlag = false;
                           if(delta >  _Point)
                              {
                               StopLossLevel -= delta;
                               delta = 0;
                               ChangeFlag = (StopLossLevel - Price[0].close) > StopLevel * _Point;
                              }
                           break;
                     }
                  if(ChangeFlag)
                    {
                     Request.action = TRADE_ACTION_SLTP;
                     Request.symbol = Symbol();
                     Request.sl = StopLossLevel;
                     Request.position = PositionGetInteger(POSITION_TICKET);
                     if(OrderSend(Request, Results)) Print("Stop Loss has changed", StopLossLevel);
                    }
                 }
            }
          else   Print("Position has no SL");
         }
      else
         {
          if(RandomTrade && TimerFlag)
           {
            ZeroMemory(Request);
            Request.action = TRADE_ACTION_DEAL;
            Request.symbol = Symbol();
            Request.volume = 1;
            Request.deviation = 3;
            if(rand() < 16384)
               {
                Request.price = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
                Request.sl = SymbolInfoDouble(Symbol(), SYMBOL_BID) - _Point * StopLoss;
                Request.type = ORDER_TYPE_BUY;
                Trend = POSITION_TYPE_BUY;
               }
            else
               {
                Request.price = SymbolInfoDouble(Symbol(), SYMBOL_BID);
                Request.sl = SymbolInfoDouble(Symbol(), SYMBOL_ASK) + _Point * StopLoss;
                Request.type = ORDER_TYPE_SELL;
                Trend = POSITION_TYPE_SELL;
               }
            if(CheckMargin(Symbol(), Request.volume, Request.type, Request.price))
               {
                if(OrderSend(Request, Results))
                  {
                   Print("Trade is successful");
                   AF = AFStep;
                   EP = 0;
                   delta = 0;
                   TimerFlag = 0;
                  }
               }
           }
         }
     }
  }
//+------------------------------------------------------------------+
//| Expert Check Margin function                                     |
//+------------------------------------------------------------------+
bool CheckMargin(string          symb,
                 double          lots,
                 ENUM_ORDER_TYPE type,
                 double          price)
  {
   double margin,free_margin=AccountInfoDouble(ACCOUNT_MARGIN_FREE);
   if(!OrderCalcMargin(type, symb, lots, price, margin))
        {
         Print("Error in ",__FUNCTION__," code=",GetLastError()); 
         return(false);
        }
   if(margin > free_margin)
        {
         Print("Not enough money for ",EnumToString(type)," ",lots," ",symb," Error code=",GetLastError());
         return(false);
        }
   return(true);
  }
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
  {
   double ret=0.0;
   return(ret);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
   TimerFlag = true;
  }
//+------------------------------------------------------------------+
打赏
版权所有转载请注明标题及链接:峰汇在线 » 根据SAR移动止损的EA SARTrailingSystem.mq5
上一篇:
下一篇:
分享到: 更多 (0)

如您觉得此文不错请扫码打赏!

支付宝扫一扫打赏

微信扫一扫打赏