有您的持续关注
我们会做得更好

EA指定货币对和订单方向到达某个盈利值全平仓的写法



extern double yingli=0;//设定盈利值

//+——————————————————————+
//| Expert initialization function                                   |
//+——————————————————————+
int OnInit()
{
//—

//—
return(INIT_SUCCEEDED);
}
//+——————————————————————+
//| Expert deinitialization function                                 |
//+——————————————————————+
void OnDeinit(const int reason)
{
//—

}
//+——————————————————————+
//| Expert tick function                                             |
//+——————————————————————+
void OnTick()
{
//—

if (Profit_total_buy() >= yingli)   iCloseOrders(“Buy”);
if (Profit_total_sell()>= yingli)   iCloseOrders(“Sell”);

}
//+——————————————————————+

//—
double Profit_total_buy()
{
double Profit=0;
for(int i=0;i<=OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if( OrderSymbol()!=Symbol())                         continue;
if(OrderType()==OP_BUY)
Profit=Profit+OrderProfit();
}
return(Profit);
}

//—
double Profit_total_sell()
{
double Profit=0;
for(int i=0;i<=OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if( OrderSymbol()!=Symbol())                         continue;
if(OrderType()==OP_SELL)
Profit=Profit+OrderProfit();
}
return(Profit);
}

//—
void iCloseOrders(string myType)
{
if(OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES)==false) return;
//———————–
if (myType==”Buy”)//平掉所有多头订单
{
for(int i=OrdersTotal()-1;i>=0;i–)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if( OrderSymbol()!=Symbol()) continue;
//— check order type
if(OrderType()==OP_BUY)
{
if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,White))
Print(“OrderClose error “,GetLastError());
continue;
}
}
}
//———————–
if (myType==”Sell”)//平掉所有空头订单
{
for(int i=OrdersTotal()-1;i>=0;i–)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if( OrderSymbol()!=Symbol()) continue;
//— check order type
if(OrderType()==OP_SELL)
{
if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Red))
Print(“OrderClose error “,GetLastError());
continue;
}
}
}
}

如您喜欢此文章请点下面分享按钮↴峰汇在线 » EA指定货币对和订单方向到达某个盈利值全平仓的写法
上一篇:
下一篇:
分享到:更多 ()