外汇交易辅助EA(编写)

2012-05-04 13:22:35    来源:    作者:
#property copyright "外汇联盟 www.FXunion.com QQ群144033"
#property link      ""

//=============== VARS external
extern int 第一目标点数 = 20;
extern int 第二目标与第一目标距离 = 20;
extern double 分批平仓手数 = 0.2;
//=======================================
extern bool 使用移动止损功能 = true;
extern int 第一次盈利点数 = 10;
extern int 第一次移止损距离开仓价点数 = 0;
extern int 第二次盈利点数 = 30;
extern int 第二次移止损距离开仓价点数 = 10;
//======================================
extern bool 使用最大允许亏损功能 = true;
extern int 最大亏损点数 = 20;
extern int Magic_Number=0;

//=============== VARS internal
int nextTP;
bool sl;
int range = 5;
int multiplier;
// OrderType == 1 is OP_SELL

 


//=============== FUNCTION init
int init()
  {
   sl=0;
   nextTP = 第一目标点数;
   getMaxLoss();
  }
//== end function


//=============== FUNCTION deinit
int deinit()
  {
//----
   sl=0;
   nextTP = 第一目标点数;
//----
   return(0);
  }
//== end function

//========== FUNCTION Start
int start()
  {
//----
   getOpenOrders();
   getSpread();
   //Comment(sl);
//----
   return(0);
  }
//== end function

//========== FUNCTION getPipValue
double type_order_profit(double order_openprice,int ordertype)
{
   double val;
   RefreshRates();
   if(ordertype == 1) val=(NormalizeDouble(order_openprice,Digits) - NormalizeDouble(Ask,Digits));
   else val=(NormalizeDouble(Bid,Digits) - NormalizeDouble(order_openprice,Digits));
   val = val/Point;
   return(val);  
}
//== end function

int getSpread()
{
   int spread=MarketInfo(Symbol(),MODE_SPREAD);
   return(spread);
}


int getMaxLoss()
{
   int calcMaxLoss;
   calcMaxLoss = 最大亏损点数;
   return(calcMaxLoss);
}
//========== FUNCTION getOpenOrders
void getOpenOrders()
{
   int nsl, nsd;
   string mngMagic;
   int totalorders = OrdersTotal();
   for(int j=0; j<totalorders;j++)
     { 
         OrderSelect(j, SELECT_BY_POS, MODE_TRADES);
         if((OrderType() == OP_BUY || OrderType() == OP_SELL) && OrderSymbol() == Symbol()&&(Magic_Number==0 || Magic_Number == OrderMagicNumber()))
         {

            double order_profit=type_order_profit(OrderOpenPrice(),OrderType());
            //int val = OrderProfit()/(OrderLots()*10);
            if(使用最大允许亏损功能 && 最大亏损点数 > 0) killTrade(order_profit,OrderTicket());
            if(使用移动止损功能) checkStops(order_profit,OrderTicket());
            Close_at_Profit(order_profit,OrderTicket());           
           
         }
        
         if(Magic_Number == 0)
         mngMagic = "All "+Symbol()+" trades.";
         else
         mngMagic = "Trades with magic number = "+Magic_Number;
        
        
         if(sl==0)
            {
               nsl = 第一次盈利点数;
               nsd = 第一次移止损距离开仓价点数;
            }
          else
            {
               nsl = 第二次盈利点数;
               nsd = 第二次移止损距离开仓价点数;
            }
         //RefreshRates();
         Comment("Order Open: ",OrderOpenPrice(),
         "\nPip Count: ", order_profit,
         "\nNext Stop Target: ",nsl,
         "\nNext Stop Differential: ", nsd,
         "\nNext TP: ", nextTP,
         "\nSL: ",sl,
         "\nMax Loss: ", getMaxLoss(),
         "\nManaging: ",mngMagic);
        

     } 
}

//========== FUNCTION takeProfit
void Close_at_Profit(int orderprofit, int orderticket)
{
   if(OrderSelect(orderticket, SELECT_BY_TICKET)==true)
   {

      if(orderprofit >= nextTP && orderprofit < (nextTP + 第二目标与第一目标距离))
      {
         if(OrderType()==1)
         {
            if(OrderClose(orderticket, 分批平仓手数, Ask, 3, YellowGreen))
            nextTP+=第二目标与第一目标距离;
            else
            Print("Error closing order : ",GetLastError());
         }
         else
         {
            if(OrderClose(orderticket, 分批平仓手数, Bid, 3, YellowGreen))
            nextTP+=第二目标与第一目标距离;
            else
            Print("Error closing order : ",GetLastError());
         }
             
      }

   }
}

//== end function

 

//========== FUNCTION moveStops
void checkStops(int Orderprofit,int ticket)
{
   if(sl==0 && Orderprofit < 第二次盈利点数)
   {
      if(Orderprofit >= 第一次盈利点数 && Orderprofit < (第二次盈利点数))
      {
         moveStops(ticket, 第一次移止损距离开仓价点数);
      }
   }
   else if(sl==1 || Orderprofit >= 第二次盈利点数)
   {
      if(Orderprofit >= 第二次盈利点数)
      {
         moveStops(ticket,第二次移止损距离开仓价点数);
      }
   }


}

//== end function

//========== FUNCTION moveStops
void moveStops(int orderticket,int TrainingStop)
{
   if(OrderSelect(orderticket, SELECT_BY_TICKET)==true)
   {
      Print("moveStops called ",orderticket, " ",TrainingStop);
      if(OrderType()==1)
      {
      OrderModify(orderticket,OrderOpenPrice(),OrderOpenPrice()-TrainingStop*Point, OrderTakeProfit(),0,Plum);
      sl=1;
      }
      else
      {
      OrderModify(orderticket,OrderOpenPrice(),OrderOpenPrice()+TrainingStop*Point, OrderTakeProfit(),0,Plum);
      sl=1;
      }
   }
  

  
}

//== end function

//========== FUNCTION killTrades
void killTrade(int pips, int ticket)
{
   if(OrderSelect(ticket, SELECT_BY_TICKET)==true)
   {
      if(pips <= -1*getMaxLoss())
      {
         if(OrderType()==1) OrderClose(ticket,OrderLots(),Ask,3,Red);    
         else OrderClose(ticket,OrderLots(),Bid,3,Red);
      }     
   }
}

本文标签: