mt4 开仓函数

来源:互联网 发布:mac做视频的免费软件 编辑:程序博客网 时间:2024/05/01 03:53
开即时仓
    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);




平仓封装函数
iCloseOrders("BUY");//平仓所有多单
iCloseOrders("SELL");//平仓所有空单
iCloseOrders("LOSS");//平仓所有亏损单
iCloseOrders("PROFIT");//平仓所有盈利单
iCloseOrders("ALL");//平仓所有单

  int iCloseOrders(string type){int orders_index;if (OrderSelect(OrdersTotal()-1,SELECT_BY_POS)==false) {return(0);}   int total = OrdersTotal();   if (type=="ALL"){for(orders_index=total-1;orders_index>=0;orders_index--){   Print("index : "+orders_index + " , total : " +total);if(OrderSelect(orders_index,SELECT_BY_POS)==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 ) {bool res = OrderDelete(OrderTicket(),NULL);Print("index : "+orders_index +" , ticket : "+OrderTicket() + " , type : " + OrderType() +" , res : "+res );}}}if (type=="GUADAN"){for(orders_index = total-1; orders_index >= 0;orders_index--){ Print("index : "+orders_index + " , total : " +total);if(OrderSelect(orders_index,SELECT_BY_POS)==false) {continue;}else if (OrderType()==OP_BUYLIMIT  ||OrderType()==OP_SELLLIMIT  || OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP ) {bool res = OrderDelete(OrderTicket(),NULL);   Print("index : "+orders_index +" , ticket : "+OrderTicket() + " , type : " + OrderType() +" , res : "+res );}}}if (type=="BUY")//平掉所有多头订单{for(orders_index=total-1;orders_index>=0;orders_index--){ Print("index : "+orders_index + " , total : " +total);if(OrderSelect(orders_index,SELECT_BY_POS)==false) {continue;}else if (OrderType()==OP_BUY) {bool res = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);Print("index : "+orders_index +" , ticket : "+OrderTicket() + " , type : " + OrderType() +" , res : "+res );}}}if (type=="SELL")//平掉所有空头订单{for(orders_index=OrdersTotal();orders_index>=0;orders_index--){ Print("index : "+orders_index + " , total : " +total);if(OrderSelect(orders_index,SELECT_BY_POS)==false){continue;}else if (OrderType()==OP_SELL){bool res = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);Print("index : "+orders_index +" , ticket : "+OrderTicket() + " , type : " + OrderType() +" , res : "+res );}}}if (type=="PROFIT")//平掉所有盈利订单{for(orders_index=total-1;orders_index>=0;orders_index--){ Print("index : "+orders_index + " , total : " +total);if(OrderSelect(orders_index,SELECT_BY_POS)==false) {continue;}else if (OrderProfit()>0) {bool res = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);Print("index : "+orders_index +" , ticket : "+OrderTicket() + " , type : " + OrderType() +" , res : "+res );}}}if (type=="LOSS"){for(orders_index=total-1;orders_index>=0;orders_index--){ Print("index : "+orders_index + " , total : " +total);if(OrderSelect(orders_index,SELECT_BY_POS)==false){continue;}else if (OrderProfit()<0) {bool res = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);Print("index : "+orders_index +" , ticket : "+OrderTicket() + " , type : " + OrderType() +" , res : "+res );}}}return 0;}


0 0