超短剥头皮EA True_Scalper_Profit_Lock.mq5-MT5 EA-峰汇在线
诚信为本
量力而为
当前位置:峰汇在线 > MetaTrader 5 > MT5 EA > 正文

超短剥头皮EA True_Scalper_Profit_Lock.mq5

True_Scalper_Profit_Lock EA属于超短剥头皮EA,采用的是均线MA结合RSI信号进行交易。

源码:

//+------------------------------------------------------------------+
//|            True Scalper Profit Lock(barabashkakvn's edition).mq5 |
//|         Copyright © 2005, International Federation of Red Cross. |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, International Federation of Red Cross."
#property link      "http://www.125808047.com"

#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>  
#include <Trade\AccountInfo.mqh>
CPositionInfo  m_position;
CTrade         m_trade;
CSymbolInfo    m_symbol;
CAccountInfo   m_account;

input double Lots=1.0;
input int    TakeProfit=44;
input int    StopLoss=90;
input bool   RSIMethodA=false;
input bool   RSIMethodB=true;
input int    RSIValue=50;
input bool   AbandonMethodA=true;
input bool   AbandonMethodB=false;
input int    Abandon=101;
input bool   MoneyManagement=true;
input int    Risk=2;
input int    Slippage=3;
input bool   UseProfitLock=true;
input int    BreakEvenTrigger=25;
input int    BreakEven=3;
input bool   LiveTrading=false;
input bool   AccountIsMini=false;
input int    maxTradesPerPair=1;
input ulong  m_magic=5432;

double lotMM;
bool BuySignal=false;
bool SetBuy=false;
bool SellSignal=false;
bool SetSell=false;

datetime bartime=0;
int      bartick=0;
int      TradeBars=0;

int    handle_iMA_3;
int    handle_iMA_7;
int    handle_iRSI;
ENUM_ACCOUNT_MARGIN_MODE m_margin_mode;
double         m_adjusted_point;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   m_symbol.Name(Symbol());
   if(!RefreshRates())
     {
      Print("Error RefreshRates. Bid=",DoubleToString(m_symbol.Bid(),Digits()),
            ", Ask=",DoubleToString(m_symbol.Ask(),Digits()));
      return(INIT_FAILED);
     }
   m_symbol.Refresh();

   m_trade.SetExpertMagicNumber(m_magic);    // sets magic number

   int digits_adjust=1;
   if(m_symbol.Digits()==3 || m_symbol.Digits()==5)
      digits_adjust=10;
   m_adjusted_point=m_symbol.Point()*digits_adjust;

   handle_iMA_3=iMA(m_symbol.Name(),Period(),3,0,MODE_EMA,PRICE_CLOSE);
   if(handle_iMA_3==INVALID_HANDLE)
     {
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  m_symbol.Name(),
                  EnumToString(Period()),
                  GetLastError());
      return(INIT_FAILED);
     }
   handle_iMA_7=iMA(m_symbol.Name(),Period(),7,0,MODE_EMA,PRICE_CLOSE);
   if(handle_iMA_7==INVALID_HANDLE)
     {
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  m_symbol.Name(),
                  EnumToString(Period()),
                  GetLastError());
      return(INIT_FAILED);
     }
   handle_iRSI=iRSI(m_symbol.Name(),Period(),2,PRICE_CLOSE);
   if(handle_iRSI==INVALID_HANDLE)
     {
      PrintFormat("Failed to create handle of the iRSI indicator for the symbol %s/%s, error code %d",
                  m_symbol.Name(),
                  EnumToString(Period()),
                  GetLastError());
      return(INIT_FAILED);
     }
    Comment("www.125808047.com");
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(UseProfitLock) ProfitLockStop();
   if(AbandonMethodA || AbandonMethodB)
     {
      RunAbandonCheck();
      RunAbandonMethods();
     }
   if(!SetLotsMM())
      return;
   RunOrderTriggerCalculations();
   RunNewOrderManagement();
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool SetLotsMM()
  {
   double MarginCutoff=0.0;
   if(!AccountIsMini)
      MarginCutoff=1000;
   if(AccountIsMini)
      MarginCutoff=100;
   if(m_account.FreeMargin() < MarginCutoff) return(false);
   if(MoneyManagement)
     {
      lotMM=MathCeil(m_account.Balance()*Risk/10000)/10;
      if(lotMM < 0.1) lotMM=Lots;
      if(lotMM > 1.0) lotMM=MathCeil(lotMM);
      if(LiveTrading)
        {
         if(AccountIsMini) lotMM=lotMM*10;
         if(!AccountIsMini && lotMM<1.0) lotMM=1.0;
        }
      if(lotMM>100) lotMM=100;
     }
   else
     {
      lotMM=Lots; 
     }
   return(true);
  }
//+------------------------------------------------------------------+
//|  RunOrderTriggerCalculations                                     |
//+------------------------------------------------------------------+
bool RunOrderTriggerCalculations()
  {
   bool    RSIPOS=false;
   bool    RSINEG=false;
   double bullMA3=iMAGet(handle_iMA_3,1);
   double bearMA7=iMAGet(handle_iMA_7,1);
   double RSI=iRSIGet(2);
   double RSI2=iRSIGet(1);
   if(RSIMethodA)
     {
      if(RSI>RSIValue && RSI2<RSIValue)
        {
         RSIPOS=true;
         RSINEG=false;
        }
      else RSIPOS=false;
      if(RSI<RSIValue && RSI2>RSIValue)
        {
         RSIPOS=false;
         RSINEG=true;
        }
      else RSINEG=false;
     }
   if(RSIMethodB)
     {
      if(RSI>RSIValue)
        {
         RSIPOS=true;
         RSINEG=false;
        }
      if(RSI<RSIValue)
        {
         RSIPOS=false;
         RSINEG=true;
        }
     }
   if((bullMA3>(bearMA7+m_symbol.Point())) && RSINEG)
     {
      BuySignal=true;
     }
   else BuySignal=false;
   if((bullMA3<(bearMA7-m_symbol.Point())) && RSIPOS)
     {
      SellSignal=true;
     }
   else SellSignal=false;
   return(true);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CalculatePositions()
  {
   int total=0;
   for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of open positions
      if(m_position.SelectByIndex(i))     // selects the position by index for further access to its properties
         if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic)
            total++;
   return(total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void ProfitLockStop()
  {
   if(CalculatePositions()>0)
     {
      if(!RefreshRates())
         return;

      for(int i=PositionsTotal()-1;i>=0;i--)
         if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
            if(m_position.Symbol()==Symbol() && m_position.Magic()==m_magic)
              {
               if(m_position.PositionType()==POSITION_TYPE_BUY)
                 {
                  if(m_symbol.Bid()>=m_position.PriceOpen()+BreakEvenTrigger*m_symbol.Point() && 
                     m_position.PriceOpen()>m_position.StopLoss())
                    {
                     m_trade.PositionModify(m_position.Ticket(),
                                            m_position.PriceOpen()+BreakEven*m_symbol.Point(),
                                            m_position.TakeProfit());
                    }
                 }

               if(m_position.PositionType()==POSITION_TYPE_SELL)
                 {
                  if(m_symbol.Ask()<=m_position.PriceOpen()-BreakEvenTrigger*m_symbol.Point() && 
                     m_position.PriceOpen()<m_position.StopLoss())
                    {
                     m_trade.PositionModify(m_position.Ticket(),
                                            m_position.PriceOpen()-BreakEven*m_symbol.Point(),
                                            m_position.TakeProfit());
                    }
                 }
              }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool RunAbandonCheck()
  {
   if(CalculatePositions()>0)
     {
      if(TradeBars==0 && bartick==0)
        {
         for(int i=PositionsTotal()-1;i>=0;i--)
            if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
               if(m_position.Symbol()==Symbol() && m_position.Magic()==m_magic)
                 {
                  TradeBars=(int)MathFloor(TimeCurrent()-m_position.Time())/60/PeriodSeconds();
                  bartime=iTime(0);
                  bartick=TradeBars;
                 }

        }
      if(bartime!=iTime(0))
        {
         bartime=iTime(0);
         bartick++;
        }
     }
   return(true);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool RunAbandonMethods()
  {
   if(CalculatePositions()>0)
     {
      for(int i=PositionsTotal()-1;i>=0;i--)
         if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
            if(m_position.Symbol()==Symbol() && m_position.Magic()==m_magic)
              {
               if(m_position.PositionType()==POSITION_TYPE_BUY)
                 {
                  if(AbandonMethodA && bartick==Abandon) // force "HEDGE" after abandon
                    {
                     m_trade.PositionClose(m_position.Ticket());
                     SetSell=true;
                     continue;
                    }
                  else if(AbandonMethodB && bartick==Abandon) // indicators decide direction after abandon
                    {
                     m_trade.PositionClose(m_position.Ticket());
                     continue;
                    }
                 }

               if(m_position.PositionType()==POSITION_TYPE_SELL)
                 {
                  if(AbandonMethodA && bartick==Abandon) // force "HEDGE" after abandon
                    {
                     m_trade.PositionClose(m_position.Ticket());
                     SetBuy=true;
                     continue;
                    }
                  else if(AbandonMethodB && bartick==Abandon) // indicators decide direction after abandon
                    {
                     m_trade.PositionClose(m_position.Ticket());
                     continue;
                    }
                 }
              }
     }
   return(true);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void RunNewOrderManagement()
  {
   double  TP,SL;
//---
   if(CalculatePositions()<maxTradesPerPair)
     {
      if(!RefreshRates())
         return;
      if(BuySignal || SetBuy)
        {
         SL=m_symbol.Ask() - StopLoss*m_symbol.Point();
         TP=m_symbol.Ask() + TakeProfit*m_symbol.Point();

         m_trade.Buy(lotMM,NULL,m_symbol.Ask(),SL,TP,"TS-ProfitLock - Long");

         bartick=0;
         if(SetBuy)
            SetBuy=false;

         return;
        }
      if(SellSignal || SetSell)
        {
         SL=m_symbol.Bid() + StopLoss*m_symbol.Point();
         TP=m_symbol.Bid() - TakeProfit*m_symbol.Point();

         m_trade.Sell(lotMM,NULL,m_symbol.Bid(),SL,TP,"TS-ProfitLock - Short");

         bartick=0;
         if(SetSell)
            SetSell=false;

         return;
        }
     }
  }
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data                                 |
//+------------------------------------------------------------------+
bool RefreshRates()
  {
//--- refresh rates
   if(!m_symbol.RefreshRates())
      return(false);
   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
      return(false);
   return(true);
  }
//+------------------------------------------------------------------+
//| Get value of buffers for the iMA                                 |
//+------------------------------------------------------------------+
double iMAGet(int handle,const int index)
  {
   double MA[1];
   ResetLastError();
   if(CopyBuffer(handle,0,index,1,MA)<0)
     {
      PrintFormat("Failed to copy data from the iMA indicator, error code %d",GetLastError());
      return(0.0);
     }
   return(MA[0]);
  }
//+------------------------------------------------------------------+
//| Get value of buffers for the iRSI                                |
//+------------------------------------------------------------------+
double iRSIGet(const int index)
  {
   double RSI[1];
   ResetLastError();
   if(CopyBuffer(handle_iRSI,0,index,1,RSI)<0)
     {
      PrintFormat("Failed to copy data from the iRSI indicator, error code %d",GetLastError());
      return(0.0);
     }
   return(RSI[0]);
  }
//+------------------------------------------------------------------+ 
//| Get Time for specified bar index                                 | 
//+------------------------------------------------------------------+ 
datetime iTime(const int index,string symbol=NULL,ENUM_TIMEFRAMES timeframe=PERIOD_CURRENT)
  {
   if(symbol==NULL)
      symbol=Symbol();
   if(timeframe==0)
      timeframe=Period();
   datetime Time[1];
   datetime time=0;
   int copied=CopyTime(symbol,timeframe,index,1,Time);
   if(copied>0) time=Time[0];
   return(time);
  }
//+------------------------------------------------------------------+
打赏
版权所有转载请注明标题及链接:峰汇在线 » 超短剥头皮EA True_Scalper_Profit_Lock.mq5
上一篇:
下一篇:
分享到: 更多 (0)

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

支付宝扫一扫打赏

微信扫一扫打赏