這段代碼有多處錯誤,望大神指正.
//+------------------------------------------------------------------+
//| 時間機器.mq4 |
//| Copyright 2016, MetaQuotes Software Corp. |
//| Discover new MetaTrader 5 opportunities with MQL5 community and services |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link "Discover new MetaTrader 5 opportunities with MQL5 community and services"
#property version "1.00"
#include <CNamedPipes.mqh>
CNamedPipe pipe;
extern double 價差 = 40;
extern double 平台傭金和=4;
extern double 頭寸 = 0.01;
extern int 風險比 = 9;
extern int 訂單持有幾分鐘= 10;
extern int 訂單最大價格變化= 70;
extern double 最大頭寸 = 30.0;
extern double 最大倉位比=0.5;
extern string 後綴 = "m";
extern string 註解="指引者";
extern int 止盈 = 0;
extern int 止損 = 0;
extern int 單量 = 3;
extern int magic=10;
string s[28];
double b[28];
double a[28];
double c[28];
double j[28];
MqlTick price[28],send,get;
int 持倉量[28];
double 第二平台凈值;
double 第二平台保證金;
double 開倉額度;
double 開倉開關;
bool 鎖,魔術開;
datetime 最後時間;
bool closepipe=false;
bool 連接正常;
double ddd;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create timer
s[0]="EURUSD"+後綴; s[1]="GBPUSD"+後綴; s[2]="AUDCAD"+後綴; s[3]="AUDCHF"+後綴; s[4]="AUDJPY"+後綴;
s[5]="AUDNZD"+後綴; s[6]="AUDUSD"+後綴; s[7]="CADCHF"+後綴; s[8]="CHFJPY"+後綴; s[9]="CADJPY"+後綴;
s[10]="EURAUD"+後綴; s[11]="EURCAD"+後綴; s[12]="EURCHF"+後綴; s[13]="EURGBP"+後綴; s[14]="EURJPY"+後綴;
s[15]="EURNZD"+後綴; s[16]="GBPAUD"+後綴; s[17]="GBPCAD"+後綴; s[18]="GBPCHF"+後綴; s[19]="GBPJPY"+後綴;
s[20]="GBPNZD"+後綴; s[21]="NZDCAD"+後綴; s[22]="NZDCHF"+後綴; s[23]="NZDJPY"+後綴; s[24]="NZDUSD"+後綴;
s[25]="USDCHF"+後綴; s[26]="USDCAD"+後綴; s[27]="USDJPY"+後綴;
按鈕("測試開倉",180,60,160,20,CORNER_RIGHT_LOWER,clrOrangeRed,clrBlack);
按鈕("顯示報價",180,100,160,20,CORNER_RIGHT_LOWER,clrOrangeRed,clrBlack);
if(pipe.Create(456)==true)
Print("管道建立,等待連接。。。");
if(pipe.Connect()==true)
Print("管道已連接");
EventSetMillisecondTimer(15);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- destroy timer
EventKillTimer();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
bool tickReceived;
int i=0;
while(GlobalVariableCheck("gvar0")==false)
{
i=0;
while(true)
{
do
{
uint a=GetTickCount();
int count = ArraySize(s);
鎖=false;
讀取報價();
pipe.ReadTick();
魔術匹配();
掉線檢查();
第二平台凈值=pipe.incoming.bid;
第二平台保證金=pipe.incoming.ask;
計算差價();
計算倉位();
同步開倉();
關閉鏈接();
正常平倉();
顯示("af"," 第二賬戶凈值|第二賬戶保證金 |頭寸| 延遲毫秒數|連接狀態",0,400,20,8,"Arial",clrCornsilk);
uint.b=GetTickCount();
顯示("adf",DoubleToString(第二平台凈值)+" "+DoubleToString(第二平台保證金)+" "+頭寸+" "+(b-a)+" "+連接正常,0,400,60,8,"Arial",clrCornsilk);
if(tickReceived==false)
{
if(kernel32::GetLastError()==ERROR_BROKEN_PIPE)
{
Print("客戶端從管道斷開 "+pipe.GetPipeName());
pipe.Disconnect();
closepipe=true;
break;
}
}
}
while(tickReceived==true);
}
if(IsStopped()||closepipe)
pipe.Close();
}
}
//+------------------------------------------------------------------+
void 顯示(string LableName,//標籤名稱,如果顯示多個文本,名稱不能相同
string LableDoc, //文本內容
int Corner, //確定坐標原點0-左上角1-右上角2-左下角3-右下角-1-不顯示
int LableX, //定義橫坐標單位像素
int LableY, //定義縱坐標單位像素
int DocSize, //文本字型大小
string DocStyle, //文本字體
color DocColor) //文本顏色
{
int cnt = 0;
if(Corner == -1) return;
for(cnt=0;cnt<ObjectsTotal();cnt++)
if(ObjectName(cnt)==LableName)
break;//如果有對象名稱,退出循環
if(cnt==ObjectsTotal())
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 按鈕(string name,int XX,int YX,int XL,int YL,int WZ,color A,color B)
{
if(ObjectFind(0,name)==-1)
{
ObjectCreate(0,name,OBJ_BUTTON,0,0,0);
ObjectSetInteger(0,name,OBJPROP_XDISTANCE,XX);
ObjectSetInteger(0,name,OBJPROP_YDISTANCE,YX);
ObjectSetInteger(0,name,OBJPROP_XSIZE,XL);
ObjectSetInteger(0,name,OBJPROP_YSIZE,YL);
ObjectSetString(0,name,OBJPROP_TEXT,name);
ObjectSetString(0,name,OBJPROP_FONT,"微軟雅黑");
ObjectSetInteger(0,name,OBJPROP_FONTSIZE,8);
ObjectSetInteger(0,name,OBJPROP_CORNER,WZ);
}
if(ObjectGetInteger(0,name,OBJPROP_STATE)==1)
{
ObjectSetInteger(0,name,OBJPROP_COLOR,A);
ObjectSetInteger(0,name,OBJPROP_BGCOLOR,B);
}
else
{
ObjectSetInteger(0,name,OBJPROP_COLOR,B);
ObjectSetInteger(0,name,OBJPROP_BGCOLOR,A);
}
}
void 讀取報價()
{
int count = ArraySize(s);
for(int n=0;n<count;n++)
{
pipe.ReadTick();
b[n]=pipe.incoming.bid;
a[n]=pipe.incoming.ask;
}
}
bool 開關()
{
按鈕("開關",180,30,160,20,CORNER_RIGHT_LOWER,clrOrangeRed,clrBlack);
if(ObjectGetInteger(0,"開關",OBJPROP_STATE)==1)
bool 開關=true;
else
開關=false;
return(開關);
}
void 顯示報價()
{
顯示("aD"," 貨幣-A平台價格-B平台價格-差價-距離開單值-持倉量 ",0,100,20,8,"Arial",clrCornsilk);
int count = ArraySize(s);
for(int n=0;n<count;n++)
{
顯示("ac"+s[n],s[n]+" "+b[n]+" "+MarketInfo(s[n],MODE_BID)+" "+c[n]+" "+j[n]+" "+持倉量[n],0,100,10*n+40,8,"Arial",clrBlanchedAlmond);
}
}
void 計算差價()
{
int count = ArraySize(s);
for(int n=0;n<count;n++)
{
ddd=MarketInfo(s[n],MODE_POINT);
if(ddd==0)
{
ddd=0.00001;
b[n]=MarketInfo(s[n],MODE_BID);
}
c[n]= (MarketInfo(s[n],MODE_BID)-b[n])/ddd;
j[n]=MarketInfo(s[n],MODE_SPREAD)+MathAbs(b[n]-a[n])/ddd+平台傭金和+價差;
}
}
int 計算倉位()
{
if (AccountEquity()>第二平台凈值)
開倉額度=第二平台凈值;
else
開倉額度=AccountEquity();
//最大三單
int cnt = 0;
int count = ArraySize(s);
for(int n=0;n<count;n++)
{
持倉量[n]=0;
for (cnt = OrdersTotal()-1 ; cnt >= 0 ; cnt--)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
{
if(OrderSymbol()==s[n])
持倉量[n]=持倉量[n]+1;
}
}
}
if ((開倉額度*最大倉位比>=AccountMargin())&&(開倉額度*最大倉位比>=第二平台保證金))
開倉開關=3;
else
開倉開關=0;
if (風險比 > 0) {
頭寸 = NormalizeDouble(開倉額度 / 風險比 / 1000.0, 2);
if (頭寸 < MarketInfo(Symbol(),MODE_MINLOT)) 頭寸 = MarketInfo(Symbol(), MODE_MINLOT);
if (頭寸 > MarketInfo(Symbol(), MODE_MAXLOT)) 頭寸 = MarketInfo(Symbol(), MODE_MAXLOT);
}
if (頭寸 > 最大頭寸) 頭寸 = 最大頭寸;
return (0);
}
void 同步開倉()
{
//同步開倉:
int count = ArraySize(s);
for(int n=0;n<count;n++)
{//開始循環
if(( c[n]>=j[n] ||測試開倉()) && 開倉開關==3&&持倉量[n]<=單量 && !鎖&&魔術開&&連接正常&&周五檢查() )
//if(開關() && 開倉開關==3&&持倉量[n]<=單量 && !鎖&&魔術開&&連接正常 )
{
鎖=true;
send.ask=1;//OPEN_BUY
send.volume=magic;
send.bid=頭寸;
//計算滑店延遲
uint open=GetTickCount();
double openprice=MarketInfo(s[n],MODE_BID);
int 成功空=OrderSend(s[n],OP_SELL,頭寸,MarketInfo(s[n],MODE_BID),100,0,0,n,magic,0,Red);
if (成功空>=0)
{
magic=magic+1;
最後時間=TimeCurrent();
Print("開空單成功。滑點:"+(OrderOpenPrice()-openprice)+"延時毫秒:"+open);
鎖=true;
}
}
//else if( 開關()&& 開倉開關==3 &&持倉量[n]<=單量 &&!鎖&&魔術開 )
else if( c[n]<=-j[n]&& 開倉開關==3 &&持倉量[n]<=單量 &&!鎖&&魔術開 &&連接正常 &&周五檢查())
{
鎖=true;
send.ask=2;//OPEN_SELL
send.volume=magic;
send.bid=頭寸;
//計算滑店延遲
open=GetTickCount();
openprice=MarketInfo(s[n],MODE_ASK);
int 成功多=OrderSend(s[n],OP_BUY,頭寸,MarketInfo(s[n],MODE_ASK),100,0,0,n,magic,0,White);
if (成功多>=0)
{
magic=magic+1;
最後時間=TimeCurrent();
鎖=true;
Print("開多單成功。滑點:"+(OrderOpenPrice()-openprice)+"延時毫秒:"+open);
}
}
else
{
send.ask=0;//OPEN_SELL
send.volume=magic;
send.bid=頭寸;
}
pipe.WriteTick(send);
}
//循環結束
}
bool 關閉鏈接()
{
if(ObjectGetInteger(0,"顯示報價",OBJPROP_STATE)==1)
{
顯示報價();
}
return(0);
}
bool 測試開倉()
{
if(ObjectGetInteger(0,"測試開倉",OBJPROP_STATE)==1)
{
bool 測試=true;
}
else
測試=false;
return(測試);
}
void 正常平倉()//持倉分鐘和價格變化
{
double ticket;
int cnt = 0;
int 平倉碼=-1;
//檢索平倉單
for (cnt = OrdersTotal()-1 ; cnt >= 0 ; cnt--)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
{
if((TimeCurrent()-OrderOpenTime()>=訂單持有幾分鐘*60 )|| (OrderProfit()>=訂單最大價格變化*OrderLots())|| (OrderProfit()<=-訂單最大價格變化*OrderLots()))
{
int comm=StrToInteger(OrderComment());
if((OrderType()==OP_BUY&& c[comm]>=0)||(OrderType()==OP_SELL&& c[comm]<=0))
{
平倉碼=OrderMagicNumber();
break;
}
}
}
}
//平倉
if (平倉碼>0)
{
send.ask=平倉碼;
//pipe.WriteANSI(IntegerToString(平倉碼));
for (cnt = OrdersTotal()-1 ; cnt >= 0 ; cnt--)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
{
if(OrderMagicNumber()==平倉碼)
{
//買的漲單
if (OrderType()==OP_BUY )
{
ticket=OrderClose(OrderTicket(),OrderLots(), MarketInfo(OrderSymbol(),MODE_BID), 100, Red);
}
//買的空單
if (OrderType()==OP_SELL)
{
ticket=OrderClose(OrderTicket(),OrderLots(), MarketInfo(OrderSymbol(),MODE_ASK), 100, Blue);
}
}
}
}
}
else
send.ask=-1;// pipe.WriteANSI(IntegerToString("-1"));
pipe.WriteTick(send);
return 1;
}
void 魔術匹配()
{
int 匹配=pipe.incoming.volume;
if(匹配==magic)
{
魔術開=true;
}
else
{
魔術開=false;
if(匹配<magic&& TimeCurrent()-最後時間>=60 )
{
魔術平倉(magic);
}
}
if (OrdersTotal()==0)magic=10;
}
int 魔術平倉(int 魔術)
{
if (魔術>0)
{
double ticket;
int cnt = 0;
for (cnt = OrdersTotal()-1 ; cnt >= 0 ; cnt--)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
{
if(OrderMagicNumber()==魔術)
{
//買的漲單
if (OrderType()==OP_BUY )
{
ticket=OrderClose(OrderTicket(),OrderLots(), MarketInfo(OrderSymbol(),MODE_BID), 200, Red);
}
//買的空單
if (OrderType()==OP_SELL)
{
ticket=OrderClose(OrderTicket(),OrderLots(), MarketInfo(OrderSymbol(),MODE_ASK), 200, Blue);
}
}
}
}
}
return 1;
}
void 掉線檢查()
{int co=pipe.incoming.last;
if(IsConnected()&&co=="1")
{
連接正常=true;
}
else
連接正常=false;
return;
}
bool 周五檢查()
{
bool open=true;
if(DayOfWeek()==5&& Hour()>=15)open=false;
return(open);
}
推薦閱讀:
※樂尚商城任意密碼找回
※如何少寫代碼,快速開發一個應用?
※AI 代碼長啥樣?
※clang-format 格式宏代碼引起的 Bug