满足条件后如何进行多笔交易?

时间:2018-08-24 11:25:31

标签: mql4 algorithmic-trading metatrader4

我已经创建了一个EA交易(请参见下面的代码)。虽然一次只需要一笔交易。在许多情况下,满足多次进入交易的条件,但一次只进行一项交易。每当满足入场条件时如何进行新交易,而当满足出场条件时如何退出所有交易?

extern int MagicNumber=10001;
extern double Lots =0.01;
double StopLoss=0;
double TakeProfit=50;
int TrailingStop=0;
int Slippage=3;

int start()
{
  double MyPoint=Point;
  if(Digits==3 || Digits==5) MyPoint=Point*10;

  double TheStopLoss=0;
  double TheTakeProfit=0;
  if( TotalOrdersCount()==0 ) 
  {
     int result=0;
     if(
     (iStochastic(NULL,PERIOD_M15,5,3,3,MODE_EMA,1,MODE_SIGNAL,0)<iStochastic(NULL,PERIOD_M15,5,3,3,MODE_EMA,1,MODE_MAIN,0))&&
 (iMACD(NULL,PERIOD_M15,12,26,1,PRICE_CLOSE,MODE_MAIN,1)<iMACD(NULL,PERIOD_M15,12,26,1,PRICE_CLOSE,MODE_MAIN,0)) 
     ) // Here is your open buy rule
     {
        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"Test",MagicNumber,0,Blue);
    Alert("BUY OPENED");
    //if(result>0)
    //{
    // TheStopLoss=0;
    // TheTakeProfit=0;
    // if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
    // if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
    // OrderSelect(result,SELECT_BY_TICKET);
    // OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
    //}
    return(0);
 }
 if(
 (iStochastic(NULL,PERIOD_M15,5,3,3,MODE_EMA,1,MODE_SIGNAL,0)>iStochastic(NULL,PERIOD_M15,5,3,3,MODE_EMA,1,MODE_MAIN,0))&&
 (iMACD(NULL,PERIOD_M15,12,26,1,PRICE_CLOSE,MODE_MAIN,1)>iMACD(NULL,PERIOD_M15,12,26,1,PRICE_CLOSE,MODE_MAIN,0)) 
 ) // Here is your open Sell rule
 {
    result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Red);
    Alert("SELL OPENED");
    //if(result>0)
    //{
    // TheStopLoss=0;
    // TheTakeProfit=0;
    // if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
    // if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
    // OrderSelect(result,SELECT_BY_TICKET);
    // OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
    //}
    return(0);
     }
  }

  for(int cnt=0;cnt<OrdersTotal();cnt++)
      {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
   if(OrderType()<=OP_SELL &&   
     OrderSymbol()==Symbol() &&
     OrderMagicNumber()==MagicNumber 
     )  
    {
     if(OrderType()==OP_BUY)  
       {
          if(
          (iStochastic(NULL,PERIOD_M15,5,3,3,MODE_EMA,1,MODE_SIGNAL,0)>iStochastic(NULL,PERIOD_M15,5,3,3,MODE_EMA,1,MODE_MAIN,0))&&
          (iMACD(NULL,PERIOD_M15,12,26,1,PRICE_CLOSE,MODE_MAIN,1)>iMACD(NULL,PERIOD_M15,12,26,1,PRICE_CLOSE,MODE_MAIN,0))) //here is your close buy rule
          {
               OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
               Alert("BUY CLOSED");
          }
        if(TrailingStop>0)  
          {                 
           if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
             {
              if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                {
                 OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
                 return(0);
                }
             }
          }
       }
     else 
       {
            if(
            (iStochastic(NULL,PERIOD_M15,5,3,3,MODE_EMA,1,MODE_SIGNAL,0)<iStochastic(NULL,PERIOD_M15,5,3,3,MODE_EMA,1,MODE_MAIN,0))&&
            (iMACD(NULL,PERIOD_M15,12,26,1,PRICE_CLOSE,MODE_MAIN,1)<iMACD(NULL,PERIOD_M15,12,26,1,PRICE_CLOSE,MODE_MAIN,0))) // here is your close sell rule
            {
               OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
               Alert("SELL CLOSED");
            }
        if(TrailingStop>0)  
          {                 
           if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
             {
              if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                {
                 OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);
                 return(0);
                }
             }
          }
       }
    }
 }
return(0);
}

int TotalOrdersCount()
{
  int result=0;
  for(int i=0;i<OrdersTotal();i++)
  {
     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
     if (OrderMagicNumber()==MagicNumber) result++;

   }
  return (result);
}

1 个答案:

答案 0 :(得分:1)

您的代码通过使用var list = ctx.Web.Lists.GetByTitle(listName); var view = list.Views.GetByTitle(viewName); ctx.Load(view); ctx.ExecuteQuery(); var qry = new CamlQuery(); qry.ViewXml = String.Format("<View><Query>{0}</Query></View>", view.ViewQuery); var result = list.RenderListData(qry.ViewXml); ctx.ExecuteQuery(); //parse json and print var data = JObject.Parse(result.Value); foreach (var row in data["Row"]) { Console.WriteLine(row["Status"]); } 函数检查是否存在至少一项未平仓交易。您需要删除该功能,或者将其隐藏或编辑以允许多笔交易。目前,ea会检查由具有相同幻数(可能还有不同符号)的专家顾问打开了多少交易,并且仅在未平仓交易数量为零的情况下才检查多头/空头规则:请参阅第TotalOrdersCount()行。

如果您需要查找最新的未平仓交易-更新if( TotalOrdersCount()==0 )并有时间使用全局变量或通过引用传递它。

TotalOrdersCount