【EA】双向挂单移动式EA

By 迈达斯之手 at 2017-06-23 • 1人收藏 • 2464人看过

很久以前写的钓鱼EA,今天突然翻到了,发出来纪念一下


/*+------------------------------------------------------------------+
  |                     EAsport.mq4                                  |
  |                     Copyright 2013, waihuituan.                     |
  |                     https://www.waihuituan.com                      ; |
  +------------------------------------------------------------------+*/
#property copyright "Copyright 2013, waihuituan."
#property link      "https://www.waihuituan.com"
/*
#import "MQLDLL.dll"
int GetPI (int n);
#import
*/
/* 用于信号 */
//extern int    幸运数 = 13249 ;
extern int    突破点数 = 110 ;          /* 单位:点 */        
extern int    突破时限 = 8 ; 
//extern bool   乘胜追击 = true ;
//extern int    追击倍数 = 2 ;
extern bool   严格资金管理 = false ;
//extern int    平仓时限 = 7 ;
extern double 基本手 = 0.01 ;
extern int    止损 = 25 ;             /* 单位:点 */
extern int    零风险点 = 50 ;
extern int    移动信号点 = 100 ;
extern int    移动止损 = 50 ;
extern int    最大订单数 = 1 ;         
extern int    反转临界数 = 9 ;         
double 周期最大亏损 = -15.0;    /* 单位:美元 */
int magic = 31415 ;  //区分EA订单和手动订单
bool 允许开仓 = true ;
double spread;    /* 点差 */
//double myMaxLots = 0.0 ;
int 实时订单数 = 0 ;
int 挂单数 = 0 ;
int 挂单类型 = 0 ;
double stoploss = 0 ;
bool 垂钓 = false ;
bool 上钩 = false ;
datetime 下杆时间 = 0 ;
datetime 鱼群到达时间 = 0 ;
int ticketB = -1 ;
int ticketS = -1 ;
//bool noloss = false ;
/* 初始化 */
int init()
{
   showhz(AccountCompany()+"  交易商",5,20) ;
   showhz(AccountServer()+"  服务器",5,40) ;
   showhz("1:"+AccountLeverage()+"  杠杆比",5,60) ; 
   showhz(Symbol()+"  货币对",5,80) ;
   //showhz(DoubleToStr(基本手,2)+"  基本手",5,100) ;
   ismarketopen();
   showtime();
   showhz("==============================",5,132) ;
   iShowInfo();
   //Print(MarketInfo(Symbol(),MODE_MARGINREQUIRED)," ",MarketInfo(Symbol(),MODE_MARGININIT)," ",MarketInfo(Symbol(),MODE_MARGINMAINTENANCE)," ",MarketInfo(Symbol(),MODE_MARGINHEDGED));
   return(0);
}
//datetime cptime = 5;
/* 启动:收到每一个tick时都将调用此函数 */
int start()
{
   int ticket ;
   int TSignal= 0 ;
   datetime orderOpen ;
   //int startime=GetTickCount();
   //观察鱼标
   if (垂钓==true && 上钩 == false && (TimeCurrent() - 下杆时间 > 突破时限)){    //防止震荡行情,暂且收杆
      //ticketB = DeleteSth(ticketB) ;
      //ticketS = DeleteSth(ticketS) ;
      //垂钓 = false ;
      //重新抛竿
      
      OrderSelect(ticketB,SELECT_BY_TICKET,MODE_TRADES) ;
      if (NormalizeDouble(OrderOpenPrice(),Digits)!= NormalizeDouble(Ask+突破点数*Point,Digits)){
         OrderModify(ticketB,Ask+突破点数*Point,0,0,0,CLR_NONE); 
      }  
      OrderSelect(ticketS,SELECT_BY_TICKET,MODE_TRADES) ;
      if (NormalizeDouble(OrderOpenPrice(),Digits)!= NormalizeDouble(Bid-突破点数*Point,Digits)){
         OrderModify(ticketS,Bid-突破点数*Point,0,0,0,CLR_NONE);
      }
      /* 反向
      OrderSelect(ticketS,SELECT_BY_TICKET,MODE_TRADES) ;
      if (NormalizeDouble(OrderOpenPrice(),Digits)!= NormalizeDouble(Ask+突破点数*Point,Digits)){
         OrderModify(ticketS,Ask+突破点数*Point,0,0,0,CLR_NONE); 
      }  
      OrderSelect(ticketB,SELECT_BY_TICKET,MODE_TRADES) ;
      if (NormalizeDouble(OrderOpenPrice(),Digits)!= NormalizeDouble(Bid-突破点数*Point,Digits)){
         OrderModify(ticketB,Bid-突破点数*Point,0,0,0,CLR_NONE); 
      }
      */
      下杆时间 = TimeCurrent() ; 
   }
   
   showtime() ;
   iShowInfo();
   
   //收杆
   if (垂钓==true && 上钩 == true && 实时订单数 == 0) {垂钓 =  false ; 上钩 = false ;}
   
/*   //信息记录
   if (垂钓==true && 上钩 == true && 实时订单数 == 1) {
      int myTicket ;
      
      if (挂单类型==OP_BUY) {
         myTicket = ticketS ;
      }
      else {
         myTicket = ticketB ;
      }
      
      OrderSelect(myTicket,SELECT_BY_TICKET,MODE_TRADES) ;
      
      orderOpen=OrderOpenTime();
      if (noloss != true && TimeCurrent()-orderOpen >= 平仓时限) {    //未实现零亏损且平仓时限到达并且处于盈利状态
         RefreshRates();
         if(OrderType()==OP_BUY)
         OrderClose(myTicket,OrderLots(),Bid, 10,CLR_NONE);
         if(OrderType()==OP_SELL)
         OrderClose(myTicket,OrderLots(),Ask, 10,CLR_NONE);
         
     }
      
      int handle=FileOpen("Log-"+TimeToStr(orderOpen,TIME_DATE)+".csv", FILE_CSV|FILE_WRITE|FILE_READ, ';');
      if(handle>0){
         if (FileSeek(handle, 0, SEEK_END)==true && cptime != (TimeCurrent()-orderOpen)) {                  
                  FileWrite(handle, myTicket,  DoubleToStr(AccountProfit(),2), (TimeCurrent()-orderOpen)); 
                  cptime = TimeCurrent()-orderOpen ;       
         }
         FileClose(handle);
      }   
   }
*/    
   if (ismarketopen() && 允许开仓==true && (实时订单数 < 最大订单数)){   
      TSignal = GetSignal() ;
      switch(TSignal){    
         case 1:  //Buy
            ticket=OrderSend(Symbol(),OP_BUY,基本手,Ask,10,0,0,"EA",magic,0,CLR_NONE);
            stoploss = Bid-止损*Point ;
            break ;
         case 2:  //Sell
            ticket=OrderSend(Symbol(),OP_SELL,基本手,Bid,10,0,0,"EA",magic,0,CLR_NONE);
            stoploss = Ask+止损*Point ;
            break ; 
         case 9:  //垂钓挂单
            
               ticketB=OrderSend(Symbol(),OP_BUYSTOP,基本手,Ask+突破点数*Point,10,0,0,"EA",magic,0,CLR_NONE);
               ticketS=OrderSend(Symbol(),OP_SELLSTOP,基本手,Bid-突破点数*Point,10,0,0,"EA",magic,0,CLR_NONE);
               if (ticketB == -1 || ticketS == -1) {
                  DeleteSth(ticketB) ;
                  DeleteSth(ticketS) ;
                  垂钓 =  false ; 上钩 = false ;
               }
            //ticketB=OrderSend(Symbol(),OP_BUYLIMIT,基本手,Bid-突破点数*Point,10,0,0,"EA",magic,0,CLR_NONE);
            //ticketS=OrderSend(Symbol(),OP_SELLLIMIT,基本手,Ask+突破点数*Point,10,0,0,"EA",magic,0,CLR_NONE);
            下杆时间 = TimeCurrent() ;
            break ;    
         case 0:   
            break ;  //Do nothing
         default: 
            break ;
      }
   } 
   if (垂钓 == true && 挂单数 == 1){    //上钩     
      if (挂单类型==OP_BUY) {   //删除另一个未成交的挂单
         ticketB = DeleteSth(ticketB) ;
         stoploss = Ask+止损*Point ;   //为成交的卖单设置止损位
      }
      if (挂单类型==OP_SELL) {   //删除另一个未成交的挂单
         ticketS = DeleteSth(ticketS) ;
         stoploss = Bid-止损*Point ;  //为成交的买单设置止损位
      }
      鱼群到达时间 = TimeCurrent() ;
      上钩 = true ;
   }
   /* 移动止损 */ 
   trailStop(Symbol(),零风险点,移动信号点,移动止损,MarketInfo(Symbol(),MODE_POINT),TurnAroundS());
   
   iGetError() ;
   //Print(GetTickCount()-startime+" ms") ;
   return(0);
}
int DeleteSth(int ticket)
{
   if (ticket != -1) {
      if (OrderDelete(ticket, CLR_NONE)== true) ticket = -1 ;
      //else ;
   }
   return (ticket) ;
}
double newhigh = 0;
double newlow = 0 ;
int TurnCount = 0 ;
bool TurnAroundS(){
   bool ret = false ;
   
   if (newhigh != 0 && newlow ==0){   
      if ((newhigh - Bid) > 0){
         newhigh = Bid ;
         TurnCount++ ;
      }
   }
   else if (newlow !=0 && newhigh ==0){ 
      if ((newlow - Ask) < 0){
         newlow = Ask ;
         TurnCount++ ;
      }
   }
   if (TurnCount >= 反转临界数){
      TurnCount = 0 ;  
      newhigh = 0 ;
      newlow = 0 ;
      ret = true ;
   }
   return (ret) ;
}
/* 检查订单是否成功 
void Checkfor(int myTicket,double stoploss)
{
   //int slip = 0 ;
   //int handle;
   //datetime orderOpen;
   bool succeed = false ;
   int  try = 0 ;
   
   if(myTicket>0 && OrderSelect(myTicket,SELECT_BY_TICKET,MODE_TRADES)){
      while(!succeed && try<5){   /* 为订单设置止损       
         succeed = OrderModify(myTicket,OrderOpenPrice(),NormalizeDouble(stoploss,MarketInfo(OrderSymbol(),MODE_DIGITS)),OrderTakeProfit(),0,CLR_NONE);          
         if(!succeed){try++;Print("设置止损失败! Retry "+try+" times at "+stoploss);Sleep(200);}
      }
      /*
      slip = MathAbs(OrderOpenPrice()-comp)*MathPow(10,Digits);
      Print("滑点 : ",slip);
      orderOpen=OrderOpenTime();
      handle=FileOpen("Slip-"+TimeToStr(orderOpen,TIME_DATE)+".csv", FILE_CSV|FILE_WRITE|FILE_READ, ';');
      if(handle>0){
         if (FileSeek(handle, 0, SEEK_END)==true)  
            FileWrite(handle, "订单"+myTicket, TimeToStr(orderOpen), "滑点"+slip);
         FileClose(handle);
      }
      
   } 
   else iDisplayInfo("Warning1", "错误:开仓失败!", 3, 5, 5, 9, "Arial", Red);
}
*/
/* 追踪止损 */
void trailStop(string symbol,int BreakEven,int TrailStart,int TrailStop,double point,bool TurnAround){
   int digits=MarketInfo(symbol,MODE_DIGITS);
   double ask = MarketInfo(symbol,MODE_ASK);
   double bid = MarketInfo(symbol,MODE_BID);
   bool succeed = false ;
   int  try = 0 ;
   
  if(OrdersTotal()>0){
    for(int i=0;i<OrdersTotal();i++){
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()<=OP_SELL && OrderMagicNumber()==magic && OrderSymbol()==symbol){
        /* 空白单设置止损 */
        if(OrderStopLoss()==0&&stoploss>0){
            while(!succeed && try<10){   /* 为订单设置止损 */      
              succeed = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(stoploss,digits),OrderTakeProfit(),0,CLR_NONE);          
              if(!succeed){
               if(OrderType()==OP_BUY){
                  stoploss = stoploss - 10*Point ;
               }else {
                  stoploss = stoploss + 10*Point ;
               }
               try++;
               Print("设置止损失败! Retry "+try+" times at "+stoploss);
               Sleep(200);
              }
            }
        }
        if(OrderType()==OP_BUY){
          /* 盈利单零亏损控制 */
          if (OrderStopLoss()!= OrderOpenPrice()){
            if( NormalizeDouble((bid-OrderOpenPrice()),digits)>=NormalizeDouble(BreakEven*point,digits) ){
             if((OrderStopLoss()-OrderOpenPrice())<0){
                Changestop(OrderOpenPrice());
                
                /*if (乘胜追击==true){
                   OrderSend(Symbol(),OP_BUY,基本手*追击倍数,Ask,10,0,0,"EA",magic,0,CLR_NONE);
                   stoploss = Bid-止损*Point ;
                   乘胜追击=false ;
                }*/
             }
            }
          }
          /* 移动止损控制 */
          if( NormalizeDouble((bid-OrderOpenPrice()),digits)>=NormalizeDouble(TrailStart*point,digits)){
              if(NormalizeDouble(OrderStopLoss(),digits)<NormalizeDouble(bid-TrailStop*point,digits)){
                Changestop(bid-TrailStop*point);
                newhigh = bid ;
                newlow = 0 ;
              }
          }
          /* 市场反转控制*/
          if (TurnAround == true){
               Changestop(bid-(MarketInfo(symbol,MODE_STOPLEVEL)+5)*point);
          } 
        }else{
          /* 盈利单零亏损控制 */
          if (OrderStopLoss()!= OrderOpenPrice()){
             if( NormalizeDouble((OrderOpenPrice()-ask),digits)>=NormalizeDouble(BreakEven*point,digits)){
               if(((OrderOpenPrice()-OrderStopLoss())<0)){
                Changestop(OrderOpenPrice());
                
                /*if (乘胜追击==true){
                   OrderSend(Symbol(),OP_SELL,基本手*追击倍数,Bid,10,0,0,"EA",magic,0,CLR_NONE);
                   stoploss = Ask + 止损*Point ;
                   乘胜追击 = false ;
                }*/
               }
            } 
          }  
          /* 移动止损控制 */
          if( NormalizeDouble((OrderOpenPrice()-ask),digits)>=NormalizeDouble(TrailStart*point,digits)){       
              if( (NormalizeDouble(OrderStopLoss(),digits) > NormalizeDouble(ask+TrailStop*point,digits))){
                Changestop(ask+TrailStop*point);
                newlow = ask ;
                newhigh = 0 ;
              }
          } 
          /* 市场反转控制  */ 
          if (TurnAround == true){
               Changestop(ask+(MarketInfo(symbol,MODE_STOPLEVEL)+5)*point);
          } 
        }     
      }
    }  
  }
}
void Changestop(double BASE)
{
   double BaseTemp=NormalizeDouble(BASE,Digits);
   if (NormalizeDouble(OrderStopLoss(),Digits)!=NormalizeDouble(BaseTemp,Digits))
      OrderModify(OrderTicket(),OrderOpenPrice(),BaseTemp,OrderTakeProfit(),0,CLR_NONE) ;
}
/*
函    数:计算交易信号
输入参数:
输出参数:0-无信号
          1-买入开仓信号
          2-卖出开仓信号
          3-BuyLimit信号
          4-SellLimit信号
          5-BuyStop信号
          6-SellStop信号
          7-买入单平仓信号
          8-卖出单平仓信号
          9-垂钓挂单信号
算    法:
*/
//int OpenN = 0 ;
int GetSignal()
{
   int myReturn = 0;//预定义返回变量
   
   if (垂钓==false){
      myReturn = 9 ;
      垂钓=true ;
   }
   /*
   if ((GetPI(幸运数+OpenN))%2 == 1)
       myReturn = 1 ;
   else
       myReturn = 2 ;
   
   OpenN++ ;
   */ 
   return(myReturn);
}
   
bool ismarketopen()
{
   bool ret = false ;
       
   if (IsExpertEnabled() == true && IsTradeAllowed()==true){
      iDisplayInfo("MarketStatus","【交易正常】",1,1,120,10,"Arial",White);
      ret = true ;
   }
   else {
      iDisplayInfo("MarketStatus","【暂停交易】",1,1,120,10,"Arial",Gray);
      ret = false ;
   }  
   
   return (ret) ; 
}
void showtime()
{
   iDisplayInfo("Time",TimeToStr(MarketInfo(Symbol(),MODE_TIME),TIME_DATE|TIME_SECONDS),1,60,1,10,"Arial",White);
}
   
/*
函    数:显示交易信息
输入参数:
输出参数:
算    法:
*/
datetime 星期一 = 0 ;
bool timeflag = false ;
void iShowInfo()
   {   
      //定义统计变量
      int BuyHistoryOrders,SellHistoryOrders,ProfitHistoryOrders,HistoryOrderTotal;//买入历史订单、卖出历史订单、盈利历史订单、历史订单总数
      int WinHistory,LossHistory; //历史盈利/亏损单累计
      double TotalHistoryLots;//历史交易总手数
      double TotalHistoryProfit,TotalHistoryLoss;//盈利总数、亏损总数变量
      
      if(DayOfWeek()==1 && timeflag == false) {
        星期一 = TimeCurrent() ;
        timeflag = true ;
      /*  
      int handle=FileOpen("Debug"+".csv", FILE_CSV|FILE_WRITE|FILE_READ, ';');
      if(handle>0){
         if (FileSeek(handle, 0, SEEK_END)==true)  
            FileWrite(handle, TimeToStr(星期一));
         FileClose(handle);
      }
      */
      }else if (DayOfWeek()!= 1 && timeflag == true){
        timeflag = false ;
      }
      spread = MarketInfo(Symbol(), MODE_SPREAD); //当前点差   
      
      //遍历当前订单,计算相关信息
      int TradeTotal = OrdersTotal() ;
      实时订单数 = 0 ;
      挂单数 = 0 ;
      for (int cnt=0;cnt<=TradeTotal-1;cnt++){
         if (OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)){
         if (OrderSymbol()==Symbol()){
            if (OrderType()==OP_BUY || OrderType()==OP_SELL){
            实时订单数++ ;
            }
            if (OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT ){
            挂单类型 = OP_BUY ;
            挂单数++ ;
            }
            if (OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT ){
            挂单类型 = OP_SELL ;
            挂单数++ ;
            }
         }
         }
      }
      
      //遍历历史订单,计算相关信息
      HistoryOrderTotal=OrdersHistoryTotal(); //统计历史订单总数
      for (cnt=0;cnt<=HistoryOrderTotal-1;cnt++)
         {
            if (OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY)){
               if (OrderSymbol()==Symbol() && OrderCloseTime()>=星期一){
                  if (OrderType()==OP_BUY)   BuyHistoryOrders++;
                  if (OrderType()==OP_SELL)  SellHistoryOrders++;
                  if (OrderType()==OP_BUY || OrderType()==OP_SELL){
                     if (OrderProfit()>=0){
                        WinHistory++;
                        TotalHistoryProfit=TotalHistoryProfit+OrderProfit();
                     }else {
                        LossHistory++;
                        TotalHistoryLoss=TotalHistoryLoss+OrderProfit();
                     }
                  }
                  TotalHistoryLots=TotalHistoryLots+OrderLots();
               }
            }
         }
      /*   
      double myWinRate = 50.0 ;   
      double myOdds = -1 ;
      double myKelly = 0.1 ; //默认凯利值为10%
      
      
      if (HistoryOrderTotal!=0) myWinRate=(WinHistory*1.00)/(HistoryOrderTotal*1.00)*100; //胜率变量,int类型转double类型
      if (TotalHistoryProfit !=0 && WinHistory != 0 && LossHistory != 0 && TotalHistoryLoss !=0) myOdds=(TotalHistoryProfit/WinHistory)/(-TotalHistoryLoss/LossHistory); //计算赔率=平均盈利/平均亏损
      
      myKelly=((myOdds+1)*(myWinRate/100)-1)/myOdds;
     
      if (myKelly<0) myKelly=-myKelly;*/
      
      double 余额 = AccountBalance() ;
      
      if (严格资金管理 == true)  基本手 = MathCeil(余额/125)*0.01 ;
      else                       基本手 = MathCeil(余额/30)*0.01;
      
      if (基本手>0.01) 基本手 = 基本手 - 0.01 ;
         
      if (严格资金管理 == true) 周期最大亏损 = 余额*(-0.1) ;
      else 周期最大亏损 = 基本手*(-1000)   ;
               
      //if (myKelly<1) myMaxLots= AccountBalance()*myKelly/MarketInfo(Symbol(), MODE_MARGINREQUIRED); 
      //myMaxLots = iLotsOptimized(AccountBalance(), 20) ; 
      
      /*持仓警告
      double myLots;
      for (cnt=0;cnt<OrdersTotal();cnt++)
         {
            if (OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
               {
                  myLots=myLots+OrderLots();
               }
         }
      iDisplayInfo("statusword","  当前状况",1,5,190,10,"Arial",White);
      if (myLots>myMaxLots){
         iDisplayInfo("ccstatus","超限",1,77,190,10,"Arial",Red);
         允许开仓 = false ;
      }
      else {
         iDisplayInfo("ccstatus","安全",1,77,190,10,"Arial",LimeGreen);
         允许开仓 = true ;
      }
      */
      iDisplayInfo("kellynum",DoubleToStr(基本手,2)+"手"+"  开仓单位",1,5,150,10,"Arial",White);
      //iDisplayInfo("adviced",DoubleToStr(myMaxLots,2)+"手"+"  最大开仓",1,5,170,10,"Arial",White);
      
      iDisplayInfo("crtylword","  周期盈利",1,5,210,10,"Arial",White);
      if ((TotalHistoryProfit+TotalHistoryLoss)>=0){
         iDisplayInfo("crtprofits",DoubleToStr(TotalHistoryProfit+TotalHistoryLoss,3)+"$",1,77,210,10,"Arial",LimeGreen);
         允许开仓 = true ;
      }
      else {
         iDisplayInfo("crtprofits",DoubleToStr(TotalHistoryProfit+TotalHistoryLoss,3)+"$",1,77,210,10,"Arial",Red);
         
         if ( (TotalHistoryProfit+TotalHistoryLoss) <= (周期最大亏损) ) 允许开仓 = false ;
         else 允许开仓 = true ;
      }     
      
      if (spread < 50) iDisplayInfo("spdc",DoubleToStr(spread/10,1)+" pips",3,5,20,30,"Arial",LimeGreen);
      else iDisplayInfo("spdc",DoubleToStr(spread/10,1)+" pips",3,5,20,30,"Arial",Red);
      
      //if (Bid!=0 && Point!=0) {
         //if (严格资金管理 == true) 止损 = MathCeil((余额*0.02)/((MarketInfo(Symbol(),MODE_LOTSIZE)*Point/Bid)*基本手)) ; 
         //else    止损 = MathCeil((余额*0.04)/(((MarketInfo(Symbol(),MODE_LOTSIZE)*Point*Bid)*基本手))) ;
         
      //}
      //Print(周期最大亏损,",",止损,",",(MarketInfo(Symbol(),MODE_LOTSIZE)*Point));     
      return(0);
   }
   
/*
函    数:金额转换手数
输入参数:mySymbol:货币对  
          myFunds:资金基数
输出参数:
算    法:
double iFundsToHands(string mySymbol,double myFunds)
   {
      double myLots=myFunds/MarketInfo(mySymbol, MODE_MARGINREQUIRED);//换算可开仓手数
      myLots=MathRound(myLots/MarketInfo(mySymbol, MODE_MINLOT))*MarketInfo(Symbol(), MODE_MINLOT);//手数整形
      if (myLots<MarketInfo(Symbol(), MODE_MINLOT)){
         myLots = MarketInfo(Symbol(), MODE_MINLOT);
      }
      return(myLots);
   }
*/
/*
函    数:资金风险管理
输入参数:myFunds:资金基数,myCapitalRisk:风险比例
输出参数:在指定的风险比例下,允许操作的最大的下单量
算    法:
double iLotsOptimized(double myFunds, double myCapitalRisk)
   {
      double myMargin=myFunds*myCapitalRisk/100-AccountMargin();//可用保证金额度
      double myLots=myMargin*AccountLeverage()/MarketInfo(Symbol(), MODE_LOTSIZE)/(MarketInfo(Symbol(), MODE_MARGINREQUIRED)*2);//换算可R
      myLots=MathRound(myLots/MarketInfo(Symbol(), MODE_MINLOT))*MarketInfo(Symbol(), MODE_MINLOT);//开仓量整形
      if (myLots<=0)
         {
            myLots=0;
         }
      return(myLots);
   }
*/  
/*
函    数:在屏幕上显示文字标签
输入参数:string LableName 标签名称,如果显示多个文本,名称不能相同
          string LableDoc 文本内容
          int Corner 文本显示角
          int LableX 标签X位置坐标
          int LableY 标签Y位置坐标
          int DocSize 文本字号
          string DocStyle 文本字体
          color DocColor 文本颜色
输出参数:在指定的位置(X,Y)按照指定的字号、字体及颜色显示指定的文本
*/
void showhz(string str,int x,int y) 
{
   iDisplayInfo(str,str,1,x,y,10,"Arial",White);
}
void iDisplayInfo(string LableName,string LableDoc,int Corner,int LableX,int LableY,int DocSize,string DocStyle,color DocColor)
{
      if (Corner == -1) return(0);
      
      /* 初始化文字对象 */
      
      if(ObjectFind(LableName)== -1) ObjectCreate(LableName, OBJ_LABEL, 0, 0, 0);
      
      ObjectSetText(LableName, LableDoc, DocSize, DocStyle,DocColor);
      ObjectSet(LableName, OBJPROP_CORNER, Corner);
      ObjectSet(LableName, OBJPROP_XDISTANCE, LableX);
      ObjectSet(LableName, OBJPROP_YDISTANCE, LableY);
}
   
/*
函    数:获取最后错误信息
输入参数:数值
输出参数:错误信息
算    法:
*/
void iGetError()
{
   int myLastErrorMub=GetLastError();
   string myLastErrorStr="";
   if(myLastErrorMub!=0)
      {
         switch (myLastErrorMub)
            {
               case 0:myLastErrorStr="错误代码:"+0+"没有错误返回";break;
               case 1:myLastErrorStr="错误代码:"+1+"没有错误返回但结果不明";break;
               case 2:myLastErrorStr="错误代码:"+2+"一般错误";break;
               case 3:myLastErrorStr="错误代码:"+3+"无效交易参量";break;
               case 4:myLastErrorStr="错误代码:"+4+"交易服务器繁忙";break;
               case 5:myLastErrorStr="错误代码:"+5+"客户终端旧版本";break;
               case 6:myLastErrorStr="错误代码:"+6+"没有连接服务器";break;
               case 7:myLastErrorStr="错误代码:"+7+"没有权限";break;
               case 8:myLastErrorStr="错误代码:"+8+"请求过于频繁";break;
               case 9:myLastErrorStr="错误代码:"+9+"交易运行故障";break;
               case 64:myLastErrorStr="错误代码:"+64+"账户禁止";break;
               case 65:myLastErrorStr="错误代码:"+65+"无效账户";break;
               case 128:myLastErrorStr="错误代码:"+128+"交易超时";break;
               case 129:myLastErrorStr="错误代码:"+129+"无效价格";break;
               case 130:myLastErrorStr="错误代码:"+130+"无效停止";break;
               case 131:myLastErrorStr="错误代码:"+131+"无效交易量";break;
               case 132:myLastErrorStr="错误代码:"+132+"市场关闭";break;
               case 133:myLastErrorStr="错误代码:"+133+"交易被禁止";break;
               case 134:myLastErrorStr="错误代码:"+134+"资金不足";break;
               case 135:myLastErrorStr="错误代码:"+135+"价格改变";break;
               case 136:myLastErrorStr="错误代码:"+136+"开价";break;
               case 137:myLastErrorStr="错误代码:"+137+"经纪繁忙";break;
               case 138:myLastErrorStr="错误代码:"+138+"重新开价";break;
               case 139:myLastErrorStr="错误代码:"+139+"定单被锁定";break;
               case 140:myLastErrorStr="错误代码:"+140+"只允许看涨仓位";break;
               case 141:myLastErrorStr="错误代码:"+141+"过多请求";break;
               case 145:myLastErrorStr="错误代码:"+145+"因为过于接近市场,修改否定";break;
               case 146:myLastErrorStr="错误代码:"+146+"交易文本已满";break;
               case 147:myLastErrorStr="错误代码:"+147+"时间周期被经纪否定";break;
               case 148:myLastErrorStr="错误代码:"+148+"开单和挂单总数已被经纪限定";break;
               case 149:myLastErrorStr="错误代码:"+149+"当对冲备拒绝时,打开相对于现有的一个单置";break;
               case 150:myLastErrorStr="错误代码:"+150+"把为反FIFO规定的单子平掉";break;
               case 4000:myLastErrorStr="错误代码:"+4000+"没有错误";break;
               case 4001:myLastErrorStr="错误代码:"+4001+"错误函数指示";break;
               case 4002:myLastErrorStr="错误代码:"+4002+"数组索引超出范围";break;
               case 4003:myLastErrorStr="错误代码:"+4003+"对于调用堆栈储存器函数没有足够内存";break;
               case 4004:myLastErrorStr="错误代码:"+4004+"循环堆栈储存器溢出";break;
               case 4005:myLastErrorStr="错误代码:"+4005+"对于堆栈储存器参量没有内存";break;
               case 4006:myLastErrorStr="错误代码:"+4006+"对于字行参量没有足够内存";break;
               case 4007:myLastErrorStr="错误代码:"+4007+"对于字行没有足够内存";break;
               case 4008:myLastErrorStr="错误代码:"+4008+"没有初始字行";break;
               case 4009:myLastErrorStr="错误代码:"+4009+"在数组中没有初始字串符";break;
               case 4010:myLastErrorStr="错误代码:"+4010+"对于数组没有内存";break;
               case 4011:myLastErrorStr="错误代码:"+4011+"字行过长";break;
               case 4012:myLastErrorStr="错误代码:"+4012+"余数划分为零";break;
               case 4013:myLastErrorStr="错误代码:"+4013+"零划分";break;
               case 4014:myLastErrorStr="错误代码:"+4014+"不明命令";break;
               case 4015:myLastErrorStr="错误代码:"+4015+"错误转换(没有常规错误)";break;
               case 4016:myLastErrorStr="错误代码:"+4016+"没有初始数组";break;
               case 4017:myLastErrorStr="错误代码:"+4017+"禁止调用DLL ";break;
               case 4018:myLastErrorStr="错误代码:"+4018+"数据库不能下载";break;
               case 4019:myLastErrorStr="错误代码:"+4019+"不能调用函数";break;
               case 4020:myLastErrorStr="错误代码:"+4020+"禁止调用智能交易函数";break;
               case 4021:myLastErrorStr="错误代码:"+4021+"对于来自函数的字行没有足够内存";break;
               case 4022:myLastErrorStr="错误代码:"+4022+"系统繁忙 (没有常规错误)";break;
               case 4050:myLastErrorStr="错误代码:"+4050+"无效计数参量函数";break;
               case 4051:myLastErrorStr="错误代码:"+4051+"无效参量值函数";break;
               case 4052:myLastErrorStr="错误代码:"+4052+"字行函数内部错误";break;
               case 4053:myLastErrorStr="错误代码:"+4053+"一些数组错误";break;
               case 4054:myLastErrorStr="错误代码:"+4054+"应用不正确数组";break;
               case 4055:myLastErrorStr="错误代码:"+4055+"自定义指标错误";break;
               case 4056:myLastErrorStr="错误代码:"+4056+"不协调数组";break;
               case 4057:myLastErrorStr="错误代码:"+4057+"整体变量过程错误";break;
               case 4058:myLastErrorStr="错误代码:"+4058+"整体变量未找到";break;
               case 4059:myLastErrorStr="错误代码:"+4059+"测试模式函数禁止";break;
               case 4060:myLastErrorStr="错误代码:"+4060+"没有确认函数";break;
               case 4061:myLastErrorStr="错误代码:"+4061+"发送邮件错误";break;
               case 4062:myLastErrorStr="错误代码:"+4062+"字行预计参量";break;
               case 4063:myLastErrorStr="错误代码:"+4063+"整数预计参量";break;
               case 4064:myLastErrorStr="错误代码:"+4064+"双预计参量";break;
               case 4065:myLastErrorStr="错误代码:"+4065+"数组作为预计参量";break;
               case 4066:myLastErrorStr="错误代码:"+4066+"刷新状态请求历史数据";break;
               case 4067:myLastErrorStr="错误代码:"+4067+"交易函数错误";break;
               case 4099:myLastErrorStr="错误代码:"+4099+"文件结束";break;
               case 4100:myLastErrorStr="错误代码:"+4100+"一些文件错误";break;
               case 4101:myLastErrorStr="错误代码:"+4101+"错误文件名称";break;
               case 4102:myLastErrorStr="错误代码:"+4102+"打开文件过多";break;
               case 4103:myLastErrorStr="错误代码:"+4103+"不能打开文件";break;
               case 4104:myLastErrorStr="错误代码:"+4104+"不协调文件";break;
               case 4105:myLastErrorStr="错误代码:"+4105+"没有选择定单";break;
               case 4106:myLastErrorStr="错误代码:"+4106+"不明货币对";break;
               case 4107:myLastErrorStr="错误代码:"+4107+"无效价格";break;
               case 4108:myLastErrorStr="错误代码:"+4108+"无效定单编码";break;
               case 4109:myLastErrorStr="错误代码:"+4109+"不允许交易";break;
               case 4110:myLastErrorStr="错误代码:"+4110+"不允许长期";break;
               case 4111:myLastErrorStr="错误代码:"+4111+"不允许短期";break;
               case 4200:myLastErrorStr="错误代码:"+4200+"定单已经存在";break;
               case 4201:myLastErrorStr="错误代码:"+4201+"不明定单属性";break;
               case 4202:myLastErrorStr="错误代码:"+4202+"定单不存在";break;
               case 4203:myLastErrorStr="错误代码:"+4203+"不明定单类型";break;
               case 4204:myLastErrorStr="错误代码:"+4204+"没有定单名称";break;
               case 4205:myLastErrorStr="错误代码:"+4205+"定单坐标错误";break;
               case 4206:myLastErrorStr="错误代码:"+4206+"没有指定子窗口";break;
               case 4207:myLastErrorStr="错误代码:"+4207+"定单一些函数错误";break;
               case 4250:myLastErrorStr="错误代码:"+4250+"错误设定发送通知到队列中";break;
               case 4251:myLastErrorStr="错误代码:"+4251+"无效参量- 空字符串传递到SendNotification()函数";break;
               case 4252:myLastErrorStr="错误代码:"+4252+"无效设置发送通知(未指定ID或未启用通知)";break;
               case 4253:myLastErrorStr="错误代码:"+4253+"通知发送过于频繁";break;
            }
      Print(myLastErrorStr);
      }
  
   return ;
}
int deinit()
{
   ObjectDelete(AccountCompany()+"  交易商");
   ObjectDelete(AccountServer()+"  服务器");
   ObjectDelete("1:"+AccountLeverage()+"  杠杆比");
   ObjectDelete(Symbol()+"  货币对");
   //ObjectDelete(DoubleToStr(基本手,2)+"  基本手");
   ObjectDelete("==============================");
   ObjectDelete("MarketStatus");
   ObjectDelete("Time");
   ObjectDelete("Warning1");
   ObjectDelete("ccstatus");
   ObjectDelete("statusword");
   ObjectDelete("kellynum");
   ObjectDelete("adviced");
   ObjectDelete("crtylword");
   ObjectDelete("crtprofits");
   ObjectDelete("spdc");
   //ObjectDelete();
   return(0);
}


1 个回复 | 最后更新于 2018-01-18
2018-01-18   #1

看看怎么样交易的

登录后方可回帖

Loading...