关于MQL5,如何关闭交易并进入交易

时间:2019-03-13 01:25:46

标签: mql5

在下面的代码中,我想在我放置加号的位置进行卖出之前关闭所有未结买入交易。 我想在所有放置星号的位置开仓之前关闭所有未平仓交易。
请,有人可以帮助我提供代码吗?预先感谢您的帮助

#include <Trade\Trade.mqh>
CTrade trade;

input double MyLotSize = 0.01;

void OnTick()
{
   double myMovingAverageArray1[],myMovingAverageArray2[];
   int movingAverageDefinition1 = iMA (_Symbol,_Period, 20,0,MODE_EMA,PRICE_CLOSE);
   int movingAverageDefinition2 = iMA (_Symbol,_Period, 50,0,MODE_EMA,PRICE_CLOSE);

   ArraySetAsSeries(myMovingAverageArray1,true);
   ArraySetAsSeries(myMovingAverageArray2,true);

   CopyBuffer(movingAverageDefinition1,0,0,3,myMovingAverageArray1);
   CopyBuffer(movingAverageDefinition2,0,0,3,myMovingAverageArray2);

   if((myMovingAverageArray1[0]>myMovingAverageArray2[0])&&(myMovingAverageArray1[1]<myMovingAverageArray2[1]))
   {
      double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
      Comment ("BUY");

***********************************************************   
      trade.Buy(MyLotSize,NULL,Ask,0,(Ask+60 * _Point),NULL);
   }
   if((myMovingAverageArray1[0]<myMovingAverageArray2[0])&&(myMovingAverageArray1[1]>myMovingAverageArray2[1]))
   {
      double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
      Comment ("SELL");

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
      trade.Sell(MyLotSize,NULL,Bid,0,(Bid-60 * _Point),NULL);    
   }
}

1 个答案:

答案 0 :(得分:0)

尝试以下-

void TryToClose(long type){
   if(!PositionSelect(_Symbol))return;
   if(PositionGetInteger(POSITION_TYPE)!=type)return;
   trade.PositionClose(_Symbol);
}

add `TryToClose(POSITION_TYPE_BUY)` before opening a sell order.