mt4函数-开仓,平仓

开即时仓
    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;  
}
  




  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: MT4-DKX多空分水线是一款专为MetaTrader 4平台设计的技术指标扩展文件(.ex4)。该指标的主要作用是帮助交易者判断市场的多空力量变化和趋势反转的时机。 该指标的计算基于市场价格和成交量的综合分析。它通过分析市场的多空力量平衡来判断市场趋势的转折点。在图表上,MT4-DKX多空分水线以分水线的形式呈现,当多头力量高于空头力量时,分水线上升,表示多头趋势;当空头力量高于多头力量时,分水线下降,表示空头趋势。交叉分水线的位置则表示市场趋势发生了反转的可能,提示交易者注意趋势变化。 该指标的应用可以帮助交易者更好地理解市场趋势,并作出相应的交易决策。当分水线上升或下降时,交易者可以借此确认市场的多空力量强弱,并考虑进一步的买入或卖出机会。当分水线交叉时,交易者可以借此判断趋势反转的可能性,以避免错过逆势交易机会。 总结而言,MT4-DKX多空分水线是一款可在MetaTrader 4平台上使用的技术指标,通过对市场多空力量的综合分析来判断市场趋势的转折点。它的应用能够帮助交易者更好地把握市场行情,作出更明智的交易决策。 ### 回答2: MT4-DKX多空分水线是一种基于MetaTrader 4(MT4)平台的技术指标,它的全称是MT4-DKX Bull Bear Separation Line。这个指标旨在帮助交易者观察市场趋势的转变。它通过计算一段时间内的股价波动平均值,并将其以曲线图的形式呈现,以便交易者更加直观地观察市场的多空力量。 在MT4-DKX多空分水线指标中,分水线的主要含义是表示市场空多力量的分割线。当股价在分水线上方运动时,意味着市场处于多头(上涨)状态;而当股价低于分水线时,意味着市场处于空头(下跌)状态。交易者可以根据这个指标的图表变动来判断市场当前的多空态势,从而制定相应的交易策略。 此外,MT4-DKX多空分水线指标还可以配合其他技术指标一起使用,以增强分析和决策的准确性。比如,交易者可以将其与其他趋势指标如移动平均线等进行比较,来进一步确认市场的多空趋势,并推测未来价格的走势。 总之,MT4-DKX多空分水线是一种用于辅助交易决策的技术指标,通过分析股价的均值波动情况,帮助交易者判断市场的多空力量和趋势变化。使用此指标可以更好地把握市场的行情,从而更加科学和有依据地进行交易操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值