如何在条件满足时进行多笔交易?
How can I take multiple trades when condition is satisfied?
我制作了一个 Expert Advisor(请参阅下面的代码)。不过一次只需要一笔交易。有多种情况下,交易进入的条件不止一次得到满足,但一次只能进行一笔交易。如何在满足入场条件时进行新交易并在满足退出条件时退出所有交易?
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);
}
您的代码使用 TotalOrdersCount()
函数检查是否至少有一笔未结交易。您需要删除该功能,或将其隐藏或编辑以允许进行多笔交易。目前 ea 检查智能交易系统使用相同的幻数(可能不同的符号)打开了多少交易,并且仅当打开的交易数量为零时才检查 long/short 规则:参见行 if( TotalOrdersCount()==0 )
.
如果您需要找到最新开启的交易 - 更新 TotalOrdersCount
并有时间成为一个全局变量,或者通过引用传递它。
int totalOrdersCount(datetime &lastOpenTrade)
{
int result=0; datetime last=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(!OrderSelect(i,SELECT_BY_POS))continue;
if(OrderMagic()!=InpMagicNumber)continue;
if(OrderSymbol()!=_Symbol)continue;
result++;
if(OrderOpenTime()>last)last=OrderOpenTime();
}
lastOpenTrade=last;
return(result);
}
我制作了一个 Expert Advisor(请参阅下面的代码)。不过一次只需要一笔交易。有多种情况下,交易进入的条件不止一次得到满足,但一次只能进行一笔交易。如何在满足入场条件时进行新交易并在满足退出条件时退出所有交易?
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);
}
您的代码使用 TotalOrdersCount()
函数检查是否至少有一笔未结交易。您需要删除该功能,或将其隐藏或编辑以允许进行多笔交易。目前 ea 检查智能交易系统使用相同的幻数(可能不同的符号)打开了多少交易,并且仅当打开的交易数量为零时才检查 long/short 规则:参见行 if( TotalOrdersCount()==0 )
.
如果您需要找到最新开启的交易 - 更新 TotalOrdersCount
并有时间成为一个全局变量,或者通过引用传递它。
int totalOrdersCount(datetime &lastOpenTrade)
{
int result=0; datetime last=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(!OrderSelect(i,SELECT_BY_POS))continue;
if(OrderMagic()!=InpMagicNumber)continue;
if(OrderSymbol()!=_Symbol)continue;
result++;
if(OrderOpenTime()>last)last=OrderOpenTime();
}
lastOpenTrade=last;
return(result);
}