EA学习第三节课;编写一个按钮,实现,单击,一键平仓功能。
//创建按钮:
bool Button(string name, int x,int y,int width,int height,string text,int font_size,color clr,color bclr)
{
ResetLastError();
if(!ObjectCreate(0,name,OBJ_BUTTON,0,0,0))
{
Print(__FUNCTION__,": failed to create the button! Error code = ",GetLastError());
return(false);
}
ObjectSetInteger(0,name,OBJPROP_CORNER,0) ;
ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);
ObjectSetInteger(0,name,OBJPROP_XSIZE,width);
ObjectSetInteger(0,name,OBJPROP_YSIZE,height);
ObjectSetString(0,name,OBJPROP_TEXT,text);
ObjectSetString(0,name,OBJPROP_FONT,"Arial");
ObjectSetInteger(0,name,OBJPROP_COLOR,clr);
ObjectSetInteger(0,name,OBJPROP_FONTSIZE,font_size);
ObjectSetInteger(0,name,OBJPROP_BORDER_COLOR,clrNONE);
ObjectSetInteger(0,name,OBJPROP_BGCOLOR,bclr);
return(true);
}
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam){
if(id == CHARTEVENT_OBJECT_CLICK){
if(sparam=="clear_button"){
ClearAllOrder();
}
}
}
//平仓代码。
void ClearAllOrder()
{
int i=0;
bool result=true;
Print("总单量=",OrdersTotal());
for(i=OrdersTotal();i>=0;i--){
if(OrderSelect(i,SELECT_BY_POS)){
double sell_price=MarketInfo(Symbol(),MODE_BID);
double buy_price=MarketInfo(Symbol(),MODE_ASK);
int order_type = OrderType();
//Print(OrderSymbol());
double ss = OrderLots();
//Print(order_type);
//Print(sell_price);
//Print(ss);
int order_num = OrderTicket();
switch(order_type){
case OP_BUY:
result = OrderClose(order_num,ss,sell_price,3,Yellow);
//Print("买单平仓");
if(result== false){
Print(__FUNCTION__,": 买单平仓失败! Error code = ",GetLastError());
}
else{Print("买单平仓成功");}
break;
case OP_SELL:
//Print("卖单平仓");
result = OrderClose(order_num,ss,buy_price,3,Yellow);
if(result== false){
Print(__FUNCTION__,": 卖单平仓失败! Error code = ",GetLastError());
}
else{Print("卖单平仓成功");}
break;
}
}
}
} |