客服热线:18391752892

MT4 均线卖出自动交易系统,最好带30点止损

   日期:2019-01-12     浏览:969    
马上注册,结交更多好友,下载更多资源

您需要 登录 才可以下载或查看,没有帐号?注册 MT4   5日均线金叉15日均线买入, 5日均线死叉15日均线卖出自动交易系统,最好带30点止损,100点止赢
请不要用此类语言,这里都是真诚的朋友,你真心求教,大家会帮你的...

//+------------------------------------------------------------------+
//|                     MA_Crossover_expert_v1.mq4     |
//|                                Copyright ?2005    |
//|       Copyright ?2005, Jason Robinson (jnrtrading)         |
//|              http://www.jnrtading.co.uk               |
//|    Written by MrPip from idea of Jason Robinson             |                                    |
//+------------------------------------------------------------------+
#property copyright Copyright ?2005, Jason Robinson (jnrtrading)
#property link      http:/strategybuilderfx.com/
#include stdlib.mqh

        // Change to true to allow print

//+---------------------------------------------------+
//|Account functions                        |
//+---------------------------------------------------+
extern bool AccountIsMini = false;     // Change to true if trading mini account
//+---------------------------------------------------+
//|Money Management                       |
//+---------------------------------------------------+
extern bool MoneyManagement = true; // Change to false to shutdown money management controls.
                         // Lots = 1 will be in effect and only 1 lot will be open regardless of equity.
extern double Riskpercent = 5;     // Change to whatever percent of equity you wish to risk.
extern double DecreaseFactor = 3;   // Used to decrease lot size after a loss is experienced to protect equity. Recommended not to change.
extern double StopLoss = 35;     // Maximum pips willing to lose per position.
extern double TrailingStop = 0;   // Change to whatever number of pips you wish to trail your position with.
extern double Margincutoff = 800;   // Expert will stop trading if equity level decreases to that level.
extern double Maxtrades = 10;      // Total number of positions on all currency pairs. You can change this number as you wish.
//+---------------------------------------------------+
//|Profit controls                         |
//+---------------------------------------------------+
extern int TakeProfit = 100;        // Maximum profit level achieved.
extern int Slippage = 10;       // Possible fix for not getting filled or closed   
//+---------------------------------------------------+
//|Indicator Variables                     |
//| Change these to try your own system           |
//| or add more if you like                   |
//+---------------------------------------------------+
extern int FasterMode = 3; //0=sma, 1=ema, 2=smma, 3=lwma, 4 = lsma
extern int FastMAPeriod =   5;
extern int SlowerMode = 1; //0=sma, 1=ema, 2=smma, 3=lwma, 4 = lsma
extern int SlowMAPeriod =   15;

//---- filter parameters


extern double Lots = 1;          // standard lot size.
extern bool Turnon = true;        // Turns expert on, change to false and no trades will take place even if expert is enabled.

//+---------------------------------------------------+
//|General controls                       |
//+---------------------------------------------------+
string OrderText =
double lotMM;
int TradesInThisSymbol;
datetime LastTime;
double Sl;
double Tr;
bool FlatMarket;
int NumBuys, NumSells;          // Number of buy and sell trades in this symbol
bool first_time = true;         // Used to check if first call to SilverTrend


//+------------------------------------------------------------------+
//+------------------------------------------------------------------------+
//| LSMA - Least Squares Moving Average function calculation           |
//| LSMA_In_Color Indicator plots the end of the linear regression line    |
//+------------------------------------------------------------------------+

double LSMA(int Rperiod, int shift)
{
   int i;
   double sum;
   int length;
   double lengthvar;
   double tmp;
   double wt;

   length = Rperiod;

   sum = 0;
   for(i = length; i = 1 ; i--)
   {
   lengthvar = length + 1;
   lengthvar /= 3;
   tmp = 0;
   tmp = ( i - lengthvar)*Close[length-i+shift];
   sum+=tmp;
    }
    wt = sum*6/(length*(length+1));
   
    return(wt);
}

//+------------------------------------------------------------------+
//| CheckExitCondition                               |
//| Check if any exit condition is met                      |
//+------------------------------------------------------------------+
bool CheckExitCondition(string TradeType)
{
   bool YesClose;
   double fasterMAnow, slowerMAnow, fasterMAprevious, slowerMAprevious, fasterMAafter, slowerMAafter;
   
   YesClose = false;
   if (FasterMode == 4)
   {
     fasterMAnow = LSMA(FastMAPeriod, 2);
     fasterMAprevious = LSMA(FastMAPeriod,3);
     fasterMAafter = LSMA(FastMAPeriod, 1);
   }
   else
   {
     fasterMAnow = iMA(NULL, 0, FastMAPeriod, 0, FasterMode, PRICE_CLOSE, 2);
     fasterMAprevious = iMA(NULL, 0, FastMAPeriod, 0, FasterMode, PRICE_CLOSE, 3);
     fasterMAafter = iMA(NULL, 0, FastMAPeriod, 0, FasterMode, PRICE_CLOSE, 1);
   }
   if (SlowerMode == 4)
   {
     slowerMAnow = LSMA(SlowMAPeriod, 2);
     slowerMAprevious = LSMA(SlowMAPeriod,3);
     slowerMAafter = LSMA(SlowMAPeriod, 1);
   }
   else
   {
     slowerMAnow = iMA(NULL, 0, SlowMAPeriod, 0, SlowerMode, PRICE_CLOSE, 2);
     slowerMAprevious = iMA(NULL, 0, SlowMAPeriod, 0, SlowerMode, PRICE_CLOSE, 3);
     slowerMAafter = iMA(NULL, 0, SlowMAPeriod, 0, SlowerMode, PRICE_CLOSE, 1);
   }
   if (TradeType == BUY )    // Check for cross down
   {
     if ((fasterMAnow slowerMAnow) (fasterMAprevious slowerMAprevious) (fasterMAafter slowerMAafter))
     {
      YesClose = true;
     }
   }
   if (TradeType == SELL )   // Check for cross up
   {
     if ((fasterMAnow slowerMAnow) (fasterMAprevious slowerMAprevious) (fasterMAafter slowerMAafter))
     {
      YesClose =true;
     }
   }
       
//+------------------------------------------------------------------+
//| Friday Exits                                   |
//+------------------------------------------------------------------+

//   if(DayOfWeek()==5 Hour() =20) YesClose = true;

   return (YesClose);
}

//+------------------------------------------------------------------+
//| CheckEntryCondition                                |
//| Check if entry condition is met                        |
//+------------------------------------------------------------------+
bool CheckEntryCondition(string TradeType)
{
   bool YesTrade;
   double fasterMAnow, slowerMAnow, fasterMAprevious, slowerMAprevious, fasterMAafter, slowerMAafter;
   
   YesTrade = false;
   if (FasterMode == 4)
   {
     fasterMAnow = LSMA(FastMAPeriod, 2);
     fasterMAprevious = LSMA(FastMAPeriod,3);
     fasterMAafter = LSMA(FastMAPeriod, 1);
   }
   else
   {
     fasterMAnow = iMA(NULL, 0, FastMAPeriod, 0, FasterMode, PRICE_CLOSE, 2);
     fasterMAprevious = iMA(NULL, 0, FastMAPeriod, 0, FasterMode, PRICE_CLOSE, 3);
     fasterMAafter = iMA(NULL, 0, FastMAPeriod, 0, FasterMode, PRICE_CLOSE, 1);
   }
   if (SlowerMode == 4)
   {
     slowerMAnow = LSMA(SlowMAPeriod, 2);
     slowerMAprevious = LSMA(SlowMAPeriod,3);
     slowerMAafter = LSMA(SlowMAPeriod, 1);
   }
   else
   {
     slowerMAnow = iMA(NULL, 0, SlowMAPeriod, 0, SlowerMode, PRICE_CLOSE, 2);
     slowerMAprevious = iMA(NULL, 0, SlowMAPeriod, 0, SlowerMode, PRICE_CLOSE, 3);
     slowerMAafter = iMA(NULL, 0, SlowMAPeriod, 0, SlowerMode, PRICE_CLOSE, 1);
   }
   if (TradeType == BUY ) // Check for cross up
   {
     if ((fasterMAnow slowerMAnow) (fasterMAprevious slowerMAprevious) (fasterMAafter slowerMAafter))
     {
      YesTrade = true;
     }
   }
   if (TradeType == SELL )   // Check for cross down
   {
     if ((fasterMAnow slowerMAnow) (fasterMAprevious slowerMAprevious) (fasterMAafter slowerMAafter))
     {
      YesTrade =true;
     }
   }
   return (YesTrade);
}
 
//+------------------------------------------------------------------+
//| Check if filters allow trades                         |
//| Return 0 for flat market                           |
//| return 1 for enough juice for trading                    |
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//| expert start function                             |
//+------------------------------------------------------------------+
int start()
 {
//----

   bool ExitBuy, ExitSell, YesTrade;
   
     int MagicNumber = 3000 + func_Symbol2Val(Symbol())*100 + func_Timeframe_Const2Val(Period());

   string setup= ma_crossover_expert_v1 + Symbol() + _ + func_Timeframe_Val2String(func_Timeframe_Const2Val(Period()));

// Check for input parameter errors



//+------------------------------------------------------------------+
//| Check for Open Position                             |
//+------------------------------------------------------------------+

   ExitBuy = CheckExitCondition( BUY
   ExitSell = CheckExitCondition( SELL
   HandleOpenPositions(MagicNumber, ExitBuy, ExitSell);
   
// Check if any open positions were not closed

   TradesInThisSymbol = CheckOpenPositions(MagicNumber);
   
//+------------------------------------------------------------------+
//| Check if OK to make new trades                       |
//+------------------------------------------------------------------+


   if(AccountFreeMargin() Margincutoff) {
   return(0);}
   
// only allow 1 trade per Symbol

   if(TradesInThisSymbol 0) {
   return(0);}
   if(CurTime() LastTime) {
   return(0);}

// Money Management
// Moved after open positions are closed for more available margin
   
   if(MoneyManagement)
   {
   lotMM = LotsOptimized(MagicNumber);
   }
   else {
   lotMM = Lots; // Change mm to 0 if you want the Lots parameter to be in effect
   }
   
   OrderText = //Must be blank before going into the main section
   
   
     if(CheckEntryCondition( BUY ))
     {
            OrderText = BUY
            if (StopLoss 0) {
           Sl = Ask-StopLoss*Point;
            } else {
           Sl=0;
            }
            if (TakeProfit == 0)
             Tr = 0;
            else
             Tr = Ask+TakeProfit*Point;
     }

   
     if(CheckEntryCondition( SELL ) )
     {
            OrderText = SELL
            if (StopLoss 0) {
           Sl = Bid+StopLoss*Point;
            } else {
           Sl = 0;
            }
            if (TakeProfit == 0)
             Tr = 0;
            else
             Tr = Bid-TakeProfit*Point;
     }
   if(OrderText != TradesInThisSymbol == 0 Turnon)
   {

       LastTime = DoTrades(OrderText,setup,MagicNumber, lotMM,Sl,Tr,CurTime());
     return(0);
   }
//----
   return(0);
 }

//+------------------------------------------------------------------+
//| Functions beyond this point should not need to be modified      |
//| Eventually will be placed in include file                 |
//+------------------------------------------------------------------+



//+-------------------------------------------+
//| DoTrades module cut from start         |
//| No real changes                 |
//+-------------------------------------------+
datetime DoTrades(string OrdText, string SetupStr,int MagicNum,double lotM, double SSl, double TTr, datetime LstTime)
{
   double Min_OrderPrice;
   int err,tries;
   double dtries;
   int ticket;
   datetime lsttim;

   lsttim = LstTime;
   if(OrdText == BUY )
   {
      Min_OrderPrice=MinOrderPrice(OP_BUY, MagicNum);
      if (Min_OrderPrice 0 Min_OrderPrice =Ask*1.05) {
        Print( Buy too expensive = MinOrderPrice= + Min_OrderPrice +  Ask= + Ask);
      } else {
       tries = 0;
       while (tries 3)
       {
         ticket = OrderSend(Symbol(),OP_BUY,lotM,Ask,Slippage,SSl,TTr,SetupStr,MagicNum,0,Green);
         lsttim += 12;
         if (ticket = 0) {
            tries++;
         } else tries = 3;
       }
       if(ticket =0) {
         err = GetLastError();
         alert( Error opening BUY order [ + SetupStr + ]: ( + err + ) + ErrorDescription(err));
                  }
       return(lsttim);
      }
   }
   else if(OrdText == SELL )
   {
      Min_OrderPrice=MinOrderPrice(OP_SELL, MagicNum);
      if (Min_OrderPrice 0 Min_OrderPrice =Bid) {
        Print( Sell too expensive MinOrderPrice= + Min_OrderPrice +  Bid= + Bid);
      } else {
        tries = 0;
        while (tries 3)
        {
         ticket = OrderSend(Symbol(),OP_SELL,lotM,Bid,Slippage,SSl,TTr,SetupStr,MagicNum,0,Red);
         lsttim += 12;
         if (ticket = 0)
         {
         tries++;
          }else tries = 3;
       }
         
         
        if(ticket =0) {
          err = GetLastError();
          alert( Error opening Sell order [ + SetupStr + ]: ( + err + ) + ErrorDescription(err));
                 }
       return(lsttim);
     }
    }
    return(lsttim);
}

//+------------------------------------------------------------------+
//| Check Open Position Controls                          |
//+------------------------------------------------------------------+
 
int CheckOpenPositions(int MagicNumbers)
{
   int cnt, total, NumPositions;
   int NumBuyTrades, NumSellTrades;   // Number of buy and sell trades in this symbol
   
   NumBuyTrades = 0;
   NumSellTrades = 0;
   total=OrdersTotal();
   for(cnt=0;cnt total;cnt++)
   {
     if ( OrderSelect (cnt, SELECT_BY_POS) == false ) continue;
     if ( OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumbers) continue;
     
     if((OrderType() == OP_BUY || OrderType() == OP_BUYSTOP) (OrderSymbol()==Symbol()))
        {
          NumBuyTrades++;
        }
         
     if((OrderType() == OP_SELL || OrderType() == OP_SELLSTOP) (OrderSymbol()==Symbol()))
        {
          NumSellTrades++;
        }
         
   }
   NumPositions = NumBuyTrades + NumSellTrades;
   return (NumPositions);
 }


//+------------------------------------------------------------------+
//| Handle Open Positions                             |
//| Check if any open positions need to be closed or modified     |
//| Three attempts are made to close or modify               |
//+------------------------------------------------------------------+
int HandleOpenPositions(int MagicNum, bool BuyExit, bool SellExit)
{
   int cnt, total;
   int CloseCnt, err;
   
   total=OrdersTotal();
   for(cnt=0;cnt total;cnt++)
   {
     if ( OrderSelect (cnt, SELECT_BY_POS) == false ) continue;
     if ( OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNum) continue;
     
     if((OrderType() == OP_BUY || OrderType() == OP_BUYSTOP) (OrderSymbol()==Symbol()))
        {
         
         if (BuyExit)
         {
   // try to close 3 Times
     
         CloseCnt = 0;
         while (CloseCnt 3)
         {
            if (!OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Violet))
            {
           err=GetLastError();
           Print(CloseCnt, Error closing order : ( , err , ) + ErrorDescription(err));
           if (err 0) CloseCnt++;
            }
            else
            {
           CloseCnt = 3;
            }
           }
          }
          else
          {
         if(TrailingStop 0)
           {           
           if(Bid-OrderOpenPrice() Point*TrailingStop)
           {
             if(OrderStopLoss() Bid-Point*TrailingStop)
             {
               CloseCnt=0;
               while (CloseCnt 3)
               {
               if (!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Aqua))
               {
                 err=GetLastError();
                 if (err 0) CloseCnt++;
               }
               else CloseCnt = 3;
               }
             }
           }
         }
          }
        }

     if((OrderType() == OP_SELL || OrderType() == OP_SELLSTOP) (OrderSymbol()==Symbol()))
        {
         if (SellExit)
         {
         
   // try to close 3 Times
     
           CloseCnt = 0;
           while (CloseCnt 3)
           {
           if (!OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Violet))
           {
             err=GetLastError();
             Print(CloseCnt, Error closing order : ,cnt, ( , err , ) + ErrorDescription(err));
             if (err 0) CloseCnt++;
           }
           else CloseCnt = 3;
           }
          }
          else
          {
 
   // try to modify 3 Times
     
           if(TrailingStop 0) 
           {           
             if((OrderOpenPrice()-Ask) (Point*TrailingStop))
             {
              if(OrderStopLoss()==0.0 || OrderStopLoss() (Ask+Point*TrailingStop))
              {
               CloseCnt = 0;
               while (CloseCnt 3)
               {
               if (!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Aqua))
               {
                  err=GetLastError();
                  if (err 0) CloseCnt++;
               }
               else CloseCnt = 3;
               }
              }
             }
            }
          }
      }
 }
}

   
//+------------------------------------------------------------------+
//| Calculate optimal lot size                           |
//+------------------------------------------------------------------+

double LotsOptimized(int Mnr)
 {
   double lot=Lots;
   int    orders=HistoryTotal();   // history orders total
   int    losses=0;             // number of losses orders without a break
   int    tolosses=0;
//---- select lot size
   lot=NormalizeDouble(MathFloor(AccountFreeMargin()*Riskpercent/10000)/10,1);
   
//---- calculate number of losses orders without a break
   if(DecreaseFactor 0)
   {
     for(int i=orders-1;i i--)
     {
       if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print( Error in history! break; }
       if(OrderSymbol()!=Symbol() || OrderType() OP_SELL || OrderMagicNumber()!=Mnr) continue;
       //----
       if(OrderProfit() 0) break;
       if(OrderProfit() 0) losses++;
     }
     for(i=orders-1;i i--)
     {
       if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print( Error in history! break; }
       if(TimeDay(OrderCloseTime()) != TimeDay(CurTime())) continue;
       //----
       if(OrderProfit() 0) tolosses++;
     }
     if(losses 1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
   }
 
 // lot at this point is number of standard lots
 
// if (Debug) Print ( Lots in LotsOptimized : ,lot);
 
 // Check if mini or standard Account
 
 if(AccountIsMini)
 {
    lot = MathFloor(lot);
    lot = lot / 10;
   
// Use at least 1 mini lot

   if(lot 0.1) lot=0.1;

 }else
 {
    if (lot 1.0) lot = 1.0;
    if (lot 100) lot = 100;
 }

   return(lot);
 }

//+------------------------------------------------------------------+
//| Time frame interval appropriation function                |
//+------------------------------------------------------------------+

int func_Timeframe_Const2Val(int Constant ) {
   switch(Constant) {
     case 1: // M1
       return(1);
     case 5: // M5
       return(2);
     case 15:
       return(3);
     case 30:
       return(4);
     case 60:
       return(5);
     case 240:
       return(6);
     case 1440:
       return(7);
     case 10080:
       return(8);
     case 43200:
       return(9);
   }
}

//+------------------------------------------------------------------+
//| Time frame string appropriation function                      |
//+------------------------------------------------------------------+

string func_Timeframe_Val2String(int Value ) {
   switch(Value) {
     case 1: // M1
       return( PERIOD_M1
     case 2: // M1
       return( PERIOD_M5
     case 3:
       return( PERIOD_M15
     case 4:
       return( PERIOD_M30
     case 5:
       return( PERIOD_H1
     case 6:
       return( PERIOD_H4
     case 7:
       return( PERIOD_D1
     case 8:
       return( PERIOD_W1
     case 9:
       return( PERIOD_MN1
       default:
              return( undefined + Value);
   }
}

int func_Symbol2Val(string symbol) {
     if(symbol== AUDCAD ) {
            return(1);
     } else if(symbol== AUDJPY ) {
            return(2);
     } else if(symbol== AUDNZD ) {
            return(3);
     } else if(symbol== AUDUSD ) {
            return(4);
     } else if(symbol== CHFJPY ) {
            return(5);
     } else if(symbol== EURAUD ) {
            return(6);
     } else if(symbol== EURCAD ) {
            return(7);
     } else if(symbol== EURCHF ) {
            return(8);
     } else if(symbol== EURGBP ) {
            return(9);
     } else if(symbol== EURJPY ) {
            return(10);
     } else if(symbol== EURUSD ) {
            return(11);
     } else if(symbol== GBPCHF ) {
            return(12);
     } else if(symbol== GBPJPY ) {
            return(13);
     } else if(symbol== GBPUSD ) {
            return(14);
     } else if(symbol== NZDUSD ) {
            return(15);
     } else if(symbol== USDCAD ) {
            return(16);
     } else if(symbol== USDCHF ) {
            return(17);
     } else if(symbol== USDJPY ) {
            return(18);
     } else {
            Comment( unexpected Symbol
            return(0);
     }
}

//+------------------------------------------------------------------+
//| Average price efficiency function                      |
//+------------------------------------------------------------------+

double MinOrderPrice(int OType, int OMagicNumber) {
double MinPrice;

   if (OrderType()==OP_BUY) {
     MinPrice=1000000;
   } else {
     MinPrice=0;
   }

   for(int cnt=0;cnt OrdersTotal();cnt++)
   {
     OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
     
     if(OrderType()==OType OrderSymbol()==Symbol() OrderMagicNumber()==OMagicNumber) {
       if (OrderType()==OP_BUY) {
         if (OrderOpenPrice() MinPrice) {
           MinPrice=OrderOpenPrice();
         }
       } else {
         if (OrderOpenPrice() MinPrice) {
           MinPrice=OrderOpenPrice();
         }
       }
     }
   }
   if (MinPrice==1000000) MinPrice=0;
   return(MinPrice);
   
}


//+------------------------------------------------------------------+复制代码
外汇交易有很大的风险性,本站所有资源均来自网络,请选择使用,如若出现亏损,本站不承担任何责任!


特别提示:本信息由相关企业自行提供,真实性未证实,仅供参考。请谨慎采用,风险自负。


0相关评论
相关行情
推荐行情
点击排行