开即时仓
开仓封装函数,调用方法
iOpenOrders("SELL",0.1,25,40);//开空仓单,0.1手,止损25点,止盈40点
iOpenOrders("BUY",0.1,25,40);
开挂单仓
OP_BUYSTOP
平仓封装函数
int res = OrderSend(Symbol(),OP_SELL,0.1,Bid,3,0,0,"OP_SELL",0,0,Green);
Print("OP_SELL: " + res);
res = OrderSend(Symbol(),OP_BUY,0.1,Ask,3,0,0,"OP_BUY",0,0,Red);
Print("OP_BUY: " + res);
开仓封装函数,调用方法
iOpenOrders("SELL",0.1,25,40);//开空仓单,0.1手,止损25点,止盈40点
iOpenOrders("BUY",0.1,25,40);
void iOpenOrders(string type,double myLots,int myLossStop,int myTakeProfit)
{
double point_real = Point*10;
int mySPREAD=MarketInfo(Symbol(),MODE_SPREAD);//获取市场滑点
//Print("mySPREAD : "+mySPREAD);
double BuyLossStop=Ask-myLossStop*point_real;
double BuyTakeProfit=Ask+myTakeProfit*point_real;
double SellLossStop=Bid+myLossStop*point_real;
double SellTakeProfit=Bid-myTakeProfit*point_real;
if (myLossStop<=0)//如果止损参数为0
{
BuyLossStop=0;
SellLossStop=0;
}
if (myTakeProfit<=0)//如果止赢参数为0
{
BuyTakeProfit=0;
SellTakeProfit=0;
}
if (type=="BUY")
{
int ticket = OrderSend(Symbol(),OP_BUY,myLots,Ask,mySPREAD,BuyLossStop,BuyTakeProfit);
Print("ticket : "+ticket );
}
if (type=="SELL")
{
int ticket = OrderSend(Symbol(),OP_SELL,myLots,Bid,mySPREAD,SellLossStop,SellTakeProfit);
Print("ticket : "+ticket );
}
}
开挂单仓
OP_BUYSTOP
int res = OrderSend(Symbol(),OP_BUYSTOP,0.1,Ask + 3,3,0,0,"OP_SELL",0,0,Green);
Print("OP_SELL: " + res);
平仓封装函数
//+------------------------------------------------------------------+
//| ButtonClickExpert.mq5 |
//| Copyright 2009, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
string buttonOpenBuyID="OpenBuy";
string buttonCloseBuyID="CloseBuy";
string buttonOpenSellID="OpenSell";
string buttonCloseSellID="CloseSell";
string buttonCloseID="Close";
string buttonCloseSellGuaID="CloseSellGua";
string buttonCloseBuyGuaID="CloseBuyGua";
string buttonCloseProfitID="Profit";
string buttonCloseLossID="Loss";
string labellotID="labellotID";
string labelstoplossID="labelstoplossID";
string labelprofitID="labelprofitID";
string lotID="lotID";
string stoplossID="stoplossID";
string profitID="profitID";
enum ORDER_T{
ORDER_ALL,
ORDER_BUY_GUA,
ORDER_SELL_GUA,
ORDER_BUY,
ORDER_SELL,
ORDER_LOSS,
ORDER_PROFIT
} ;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
void creatButton(string buttonID, string label ,int x ,int y ,int xsize,int ysize){
ObjectCreate(0,buttonID,OBJ_BUTTON,0,100,100);
ObjectSetInteger(0,buttonID,OBJPROP_COLOR,clrWhite);
ObjectSetInteger(0,buttonID,OBJPROP_BGCOLOR,clrGray);
ObjectSetInteger(0,buttonID,OBJPROP_XDISTANCE,x);
ObjectSetInteger(0,buttonID,OBJPROP_YDISTANCE,y);
ObjectSetInteger(0,buttonID,OBJPROP_XSIZE,xsize);
ObjectSetInteger(0,buttonID,OBJPROP_YSIZE,ysize);
ObjectSetString(0,buttonID,OBJPROP_FONT,"Arial");
ObjectSetString(0,buttonID,OBJPROP_TEXT,label);
ObjectSetInteger(0,buttonID,OBJPROP_FONTSIZE,10);
ObjectSetInteger(0,buttonID,OBJPROP_SELECTABLE,0);
}
int OnInit()
{
//--- Create a button to send custom events
int btxsize = 50;int btysize=30;
int btx1=20;int btx2=80;int btx3=140;int btx4=220;
int bty1=50;int bty2=80;
creatButton(buttonOpenBuyID,"开多单",btx1,bty1,btxsize,btysize);
creatButton(buttonCloseBuyID,"平多单",btx2,bty1,btxsize,btysize);
creatButton(buttonCloseBuyGuaID,"平多挂",btx3,bty1,btxsize,btysize);
creatButton(buttonCloseProfitID,"平盈利单",btx4,bty1,btxsize+30,btysize);
creatButton(buttonOpenSellID,"开空单",btx1,bty2,btxsize,btysize);
creatButton(buttonCloseSellID,"平空单",btx2,bty2,btxsize,btysize);
creatButton(buttonCloseSellGuaID,"平空挂",btx3,bty2,btxsize,btysize);
creatButton(buttonCloseLossID,"平亏损单",btx4,bty2,btxsize+30,btysize);
creatButton(buttonCloseID,"全部平仓",btx1,bty2+60,btxsize+30,btysize);
int textx1=20;int texty1=30;
ObjectCreate(0,labellotID,OBJ_LABEL,0,0,0);
ObjectSetString(0,labellotID,OBJPROP_TEXT,"手数");
ObjectSetInteger(0,labellotID,OBJPROP_XDISTANCE,textx1);
ObjectSetInteger(0,labellotID,OBJPROP_YDISTANCE,texty1);
ObjectSetInteger(0,labellotID,OBJPROP_COLOR,clrRed);
ObjectCreate(0,lotID,OBJ_EDIT,0,0,0);
ObjectSetString(0,lotID,OBJPROP_TEXT,"0.1");
ObjectSetInteger(0,lotID,OBJPROP_XDISTANCE,textx1+30);
ObjectSetInteger(0,lotID,OBJPROP_YDISTANCE,texty1);
ObjectCreate(0,labelstoplossID,OBJ_LABEL,0,0,0);
ObjectSetString(0,labelstoplossID,OBJPROP_TEXT,"止损");
ObjectSetInteger(0,labelstoplossID,OBJPROP_XDISTANCE,textx1+90);
ObjectSetInteger(0,labelstoplossID,OBJPROP_YDISTANCE,texty1);
ObjectSetInteger(0,labelstoplossID,OBJPROP_COLOR,clrRed);
ObjectCreate(0,stoplossID,OBJ_EDIT,0,0,0);
ObjectSetString(0,stoplossID,OBJPROP_TEXT,"10");
ObjectSetInteger(0,stoplossID,OBJPROP_XDISTANCE,textx1+120);
ObjectSetInteger(0,stoplossID,OBJPROP_YDISTANCE,texty1);
ObjectCreate(0,labelprofitID,OBJ_LABEL,0,0,0);
ObjectSetString(0,labelprofitID,OBJPROP_TEXT,"止盈");
ObjectSetInteger(0,labelprofitID,OBJPROP_XDISTANCE,textx1+180);
ObjectSetInteger(0,labelprofitID,OBJPROP_YDISTANCE,texty1);
ObjectSetInteger(0,labelprofitID,OBJPROP_COLOR,clrGreen);
ObjectCreate(0,profitID,OBJ_EDIT,0,0,0);
ObjectSetString(0,profitID,OBJPROP_TEXT,"10");
ObjectSetInteger(0,profitID,OBJPROP_XDISTANCE,textx1+210);
ObjectSetInteger(0,profitID,OBJPROP_YDISTANCE,texty1);
/*
ObjectCreate(0,labelstoplossID,OBJ_LABEL,0,100,100);
ObjectSetString(0,labelstoplossID,OBJ_LABEL,"止损");
ObjectCreate(0,stoplossID,OBJ_EDIT,0,textx1,texty1);
ObjectCreate(0,profitID,OBJ_EDIT,0,textx1,texty1);
ObjectCreate(0,labelprofitID,OBJ_LABEL,0,100,100);
ObjectSetString(0,labelprofitID,OBJ_LABEL,"止盈");
*/
/*
//--- Create a label for displaying information
ObjectCreate(0,labelID,OBJ_LABEL,0,100,100);
ObjectSetInteger(0,labelID,OBJPROP_COLOR,clrRed);
ObjectSetInteger(0,labelID,OBJPROP_XDISTANCE,100);
ObjectSetInteger(0,labelID,OBJPROP_YDISTANCE,50);
ObjectSetString(0,labelID,OBJPROP_FONT,"Trebuchet MS");
ObjectSetString(0,labelID,OBJPROP_TEXT,"No information");
ObjectSetInteger(0,labelID,OBJPROP_FONTSIZE,20);
ObjectSetInteger(0,labelID,OBJPROP_SELECTABLE,0);
*/
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
ObjectsDeleteAll();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
}
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
//--- Check the event by pressing a mouse button
if(id==CHARTEVENT_OBJECT_CLICK)
{
string s= ObjectGetString(0,profitID,OBJPROP_TEXT);
int profit = StrToInteger(s);
s= ObjectGetString(0,stoplossID,OBJPROP_TEXT);
int stoploss = StrToInteger(s);
s= ObjectGetString(0,lotID,OBJPROP_TEXT);
double lot = StrToDouble(s);
string clickedChartObject=sparam;
//--- If you click on the object with the name buttonID
if(clickedChartObject==buttonOpenBuyID)
{
Print("buttonOpenBuyID ");
openOrders(ORDER_BUY,lot,stoploss,profit);
}
else if(clickedChartObject==buttonOpenSellID)
{
Print("buttonOpenSellID ");
openOrders(ORDER_SELL,lot,stoploss,profit);
}
else if(clickedChartObject==buttonCloseBuyID)
{
Print("buttonCloseBuyID ");
closeOrders(ORDER_BUY);
}
else if(clickedChartObject==buttonCloseSellID)
{
Print("buttonCloseSellID ");
closeOrders(ORDER_SELL);
}
else if(clickedChartObject==buttonCloseBuyGuaID)
{
Print("buttonCloseBuyGuaID ");
closeOrders(ORDER_BUY_GUA);
}
else if(clickedChartObject==buttonCloseSellGuaID)
{
Print("buttonCloseSellGuaID ");
closeOrders(ORDER_SELL_GUA);
}
else if(clickedChartObject==buttonCloseID)
{
Print("buttonCloseID ");
closeOrders(ORDER_ALL);
}
else if(clickedChartObject==buttonCloseProfitID)
{
Print("buttonCloseProfitID ");
closeOrders(ORDER_PROFIT);
}
else if(clickedChartObject==buttonCloseLossID)
{
Print("buttonCloseLossID ");
closeOrders(ORDER_LOSS);
}
}
//--- Check the event belongs to the user events
}
void openOrders(ORDER_T type,double myLots,int myLossStop,int myTakeProfit)
{
double point_real = Point*10;
int mySPREAD=MarketInfo(Symbol(),MODE_SPREAD);//获取市场滑点
//Print("mySPREAD : "+mySPREAD);
double BuyLossStop=Ask-myLossStop*point_real;
double BuyTakeProfit=Ask+myTakeProfit*point_real;
double SellLossStop=Bid+myLossStop*point_real;
double SellTakeProfit=Bid-myTakeProfit*point_real;
if (myLossStop<=0)//如果止损参数为0
{
BuyLossStop=0;
SellLossStop=0;
}
if (myTakeProfit<=0)//如果止赢参数为0
{
BuyTakeProfit=0;
SellTakeProfit=0;
}
switch(type){
case ORDER_BUY:
int ticket = OrderSend(Symbol(),OP_BUY,myLots,Ask,mySPREAD,BuyLossStop,BuyTakeProfit);
Print("ticket : "+ticket );
break;
case ORDER_SELL:
ticket = OrderSend(Symbol(),OP_SELL,myLots,Bid,mySPREAD,SellLossStop,SellTakeProfit);
Print("ticket : "+ticket );
break;
}
}
int closeOrders(ORDER_T type) {
int orders_index;
if (OrderSelect(OrdersTotal()-1,SELECT_BY_POS)==false)
{
return(0);
}
int tick[256]={0};
int total = OrdersTotal();
for(int i=0;i<total;i++){
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
tick[i]=OrderTicket();
Print(tick[i]);
}
switch(type)
{
case ORDER_ALL:
{
for(orders_index=0;orders_index < total;orders_index++)
{
Print("index : "+orders_index + " , total : " +total);
if(OrderSelect(tick[orders_index],SELECT_BY_TICKET)==false)
{
continue;
}
else if(OrderType()==OP_BUY || OrderType()==OP_SELL )
{
bool res = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);
Print("index : "+orders_index +" , ticket : "+OrderTicket() + " , type : " + OrderType() +" , res : "+res );
continue;
}
else if(OrderType()==OP_BUYLIMIT ||
OrderType()==OP_SELLLIMIT ||
OrderType()==OP_BUYSTOP ||
OrderType()==OP_SELLSTOP )
{
res = OrderDelete(OrderTicket(),NULL);
Print("index : "+orders_index +" , ticket : "+OrderTicket() + " , type : " + OrderType() +" , res : "+res );
}
}
}
break;
case ORDER_BUY_GUA:
{
for(orders_index=0;orders_index < total;orders_index++)
{
Print("index : "+orders_index + " , total : " +total);
if(OrderSelect(tick[orders_index],SELECT_BY_TICKET)==false)
{
continue;
}
else if (OrderType()==OP_BUYLIMIT ||
OrderType()==OP_BUYSTOP )
{
res = OrderDelete(OrderTicket(),NULL);
Print("index : "+orders_index +" , ticket : "+OrderTicket() + " , type : " + OrderType() +" , res : "+res );
}
}
}
break;
case ORDER_SELL_GUA:
{
for(orders_index=0;orders_index < total;orders_index++)
{
Print("index : "+orders_index + " , total : " +total);
if(OrderSelect(tick[orders_index],SELECT_BY_TICKET)==false)
{
continue;
}
else if ( OrderType()==OP_SELLLIMIT ||
OrderType()==OP_SELLSTOP )
{
res = OrderDelete(OrderTicket(),NULL);
Print("index : "+orders_index +" , ticket : "+OrderTicket() + " , type : " + OrderType() +" , res : "+res );
}
}
}
break;
case ORDER_BUY:
{
for(orders_index=0;orders_index < total;orders_index++)
{
Print("index : "+orders_index + " , total : " +total);
if(OrderSelect(tick[orders_index],SELECT_BY_TICKET)==false)
{
continue;
}
else if (OrderType()==OP_BUY)
{
res = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);
Print("index : "+orders_index +" , ticket : "+OrderTicket() + " , type : " + OrderType() +" , res : "+res );
}
}
}
break;
case ORDER_SELL:
{
for(orders_index=0;orders_index < total;orders_index++)
{
Print("index : "+orders_index + " , total : " +total);
if(OrderSelect(tick[orders_index],SELECT_BY_TICKET)==false)
{
continue;
}
else if (OrderType()==OP_SELL)
{
res = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);
Print("index : "+orders_index +" , ticket : "+OrderTicket() + " , type : " + OrderType() +" , res : "+res );
}
}
}
break;
case ORDER_PROFIT:
{
for(orders_index=0;orders_index < total;orders_index++)
{
Print("index : "+orders_index + " , total : " +total);
if(OrderSelect(tick[orders_index],SELECT_BY_TICKET)==false)
{
continue;
}
else if (OrderProfit()>0)
{
res = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);
Print("index : "+orders_index +" , ticket : "+OrderTicket() + " , type : " + OrderType() +" , res : "+res );
}
}
}
break;
case ORDER_LOSS:
{
for(orders_index=0;orders_index < total;orders_index++)
{
Print("index : "+orders_index + " , total : " +total);
if(OrderSelect(tick[orders_index],SELECT_BY_TICKET)==false)
{
continue;
}
else if (OrderProfit()<0)
{
res = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);
Print("index : "+orders_index +" , ticket : "+OrderTicket() + " , type : " + OrderType() +" , res : "+res );
}
}
}
break;
}
return 0;
}