\\第四天课程,编写,设置止盈输入框,盈利大于多少自动平仓及手动平仓。
int OnInit()
{
//--- create timer
//Print(Symbol());
Panel("order_panel",20,20,206,62,10,clrGray);
Button("_button1",22,22,100,30,"设置盈利值",9,clrRed,clrWheat);
Input("profit_text",124,22,100,30,"0.1",9,clrRed,clrWhite);
Button("hand_button",22,55,100,30,"手动平仓",9,clrRed,clrWheat);
Button("auto_button",124,55,100,30,"自动平仓",9,clrRed,clrWheat);
//Button("clear_button",50,100,100,30,"一键平仓",10,clrRed,clrWheat);
//Input("profit_text",50,150,100,30,"3.3",10,clrRed,clrWhite);
EventSetTimer(1);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- destroy timer
EventKillTimer();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
if(HandOrAuto == false)return;
ClearAllOrder();
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
//Print("Hello!");
}
//+------------------------------------------------------------------+
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam){//按键处理事件
string _strProfit ;
switch(id){
case CHARTEVENT_OBJECT_CLICK:
if(sparam=="hand_button"){
HandOrAuto = false;
ClearAllOrder();
}
else if(sparam=="auto_button"){
HandOrAuto = true;
}
break;
case CHARTEVENT_OBJECT_ENDEDIT: //修改盈利值
_strProfit = ObjectGetString(0,"profit_text",OBJPROP_TEXT);
_dblProfit = StringToDouble(_strProfit);
Print("val=",_dblProfit);
break;
}
}
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__,": 创建按钮失败! Error code = ",GetLastError());
return(false);
}
ObjectSetInteger(0,name,OBJPROP_CORNER,0) ;
ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);
ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y);
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 Input(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_EDIT,0,0,0))
{
Print(__FUNCTION__,": 创建文本框失败! Error code = ",GetLastError());
return;
}
ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);
ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y);
ObjectSetInteger(0,name,OBJPROP_XSIZE,width);
ObjectSetInteger(0,name,OBJPROP_YSIZE,height);
ObjectSetString(0,name,OBJPROP_FONT,"Arial");
ObjectSetInteger(0,name,OBJPROP_FONTSIZE,font_size);
ObjectSetInteger(0,name,OBJPROP_ALIGN,ALIGN_CENTER);
ObjectSetInteger(0,name,OBJPROP_READONLY,false);
ObjectSetInteger(0,name,OBJPROP_COLOR,clr);
ObjectSetInteger(0,name,OBJPROP_BGCOLOR,bclr);
ObjectSetString(0,name,OBJPROP_TEXT,text);
return;
}
void Panel(string name,int x,int y ,int width,int height,int font_size,color bclr)
{
ObjectCreate(0,name,OBJ_EDIT,0,0,0);
ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);
ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y);
ObjectSetInteger(0,name,OBJPROP_BGCOLOR,bclr);
ObjectSetInteger(0,name,OBJPROP_SELECTED,true);
ObjectSetInteger(0,name,OBJPROP_BACK,false);
ObjectSetInteger(0,name,OBJPROP_XSIZE,width);
ObjectSetInteger(0,name,OBJPROP_XSIZE,width);
ObjectSetInteger(0,name,OBJPROP_YSIZE,height);
ObjectSetString(0,name,OBJPROP_FONT,"Arial");
ObjectSetInteger(0,name,OBJPROP_FONTSIZE,font_size);
ObjectSetString(0,name,OBJPROP_TEXT,"");
ObjectSetInteger(0,name,OBJPROP_COLOR,bclr);
ObjectSetInteger(0,name,OBJPROP_BORDER_COLOR,clrKhaki);
}
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(OrderSymbol(),MODE_BID);
double buy_price=MarketInfo(OrderSymbol(),MODE_ASK);
double order_profit=OrderProfit(); //订单盈利值
int order_type = OrderType(); //订单交易类型
double ss = OrderLots(); //订单交易手数
int order_num = OrderTicket(); //订单号
switch(order_type){
case OP_BUY:
if(order_profit>=_dblProfit){
result = OrderClose(order_num,ss,sell_price,3,Yellow);
if(result== false){
Print(__FUNCTION__,": 买单平仓失败! Error code = ",GetLastError());
}
else{Print("买单平仓成功,盈利=",order_profit);}
}
else{
Print("设定盈利值=",_dblProfit,"<实际盈利=",order_profit,"平仓取消!");
}
break;
case OP_SELL:
if(order_profit>=_dblProfit){
result = OrderClose(order_num,ss,buy_price,3,Yellow);
if(result== false){
Print(__FUNCTION__,": 卖单平仓失败! Error code = ",GetLastError());
}
else{Print("卖单平仓成功,盈利=",order_profit);}
}
else{
Print("设定盈利值=",_dblProfit,"<实际盈利=",order_profit,"平仓取消!");
}
break;
}
}
}
} |