无法关闭我在 MetaTrader Terminal 4 Strategy Tester 中的未平仓交易
Can't close my open trades in MetaTrader Terminal 4 Strategy Tester
我正在尝试编写我的 EA 代码,如果我的关闭条件得到满足,我希望我的 EA 关闭所有未平仓交易(可能有超过 1 个未平仓交易)。
这是我关闭交易部分的代码,当我通过策略测试器 运行 它时,我注意到它没有关闭我的交易。
Total=0; // Amount of orders
for(int i=1; i<=OrdersTotal(); i++) // Loop through orders
{
if (OrderSelect(i-1,SELECT_BY_POS)==true) // If there is the next one
{ // Analyzing orders:
if (OrderSymbol()!=Symb)continue; // Another security
Total++; // Counter of market orders
}
}
while(true) // Loop of closing orders
{
if (OrderType()==0 && Close_Buy_Condition==true) // Order Buy is opened & Close Buy Condition is true
{
for (c=Total-1; c>=0; c--)
{
RefreshRates(); // Refresh rates
Ans=OrderClose(OrderTicket(),Lot,Bid,Slippage); // Closing Buy
}
}
if (OrderType()==1 && Close_Sell_Condition==true) // Order Sell is opened & Close Sell Condition is true
{
for (d=Total-1; d>=0; d--)
{
RefreshRates(); // Refresh rates
Ans=OrderClose(OrderTicket(),Lot,Ask,Slippage); // Closing Sell
}
}
break; // Exit while
}
我不知道您在哪里设置 Ask
、Bid
和 Lot
变量的值。但是,当您遍历未平仓头寸时,它们必须改变。
您可以尝试使用此功能平仓:
bool CloseAllPositions()
{
double total;
int cnt;
while(OrdersTotal()>0)
{
// Close pending orders first, you can remove this section if you don't want pending orders to be deleted
total=OrdersTotal();
for(cnt=total-1; cnt>=0; cnt--)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
{
switch(OrderType())
{
case OP_BUYLIMIT :OrderDelete(OrderTicket()); break;
case OP_SELLLIMIT :OrderDelete(OrderTicket()); break;
case OP_BUYSTOP :OrderDelete(OrderTicket()); break;
case OP_SELLSTOP :OrderDelete(OrderTicket()); break;
}
}
}
// then close opened orders
total=OrdersTotal();
for(cnt=total-1; cnt>=0; cnt--)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
{
switch(OrderType())
{
case OP_BUY:
if (Close_Buy_Condition==true) {
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,Violet);
}
break;
case OP_SELL:
if (Close_Sell_Condition==true) {
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,Violet);
}
break;
}
}
}
}
return true;
}
编辑:
如果您根本不想处理挂单,请使用此代码代替 OrdersTotal()
:
int GetNumOpenPositions() {
int total = OrdersTotal();
double OpenBuyOrders = 0, OpenSellOrders = 0, PendBuys =0, PendSells =0;
for( i=0;i<total;i++)
{
OrderSelect(i, SELECT_BY_POS );
if ( OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
int type = OrderType();
if (type == OP_BUY ) {OpenBuyOrders=OpenBuyOrders+1;}
if (type == OP_SELL ) {OpenSellOrders=OpenSellOrders+1;}
if (type == OP_BUYSTOP ) {PendBuys=PendBuys+1;}
if (type == OP_SELLSTOP ) {PendSells=PendSells+1;}
}
}
return (OpenBuyOrders + OpenSellOrders);
}
则总订单数为
我正在尝试编写我的 EA 代码,如果我的关闭条件得到满足,我希望我的 EA 关闭所有未平仓交易(可能有超过 1 个未平仓交易)。
这是我关闭交易部分的代码,当我通过策略测试器 运行 它时,我注意到它没有关闭我的交易。
Total=0; // Amount of orders
for(int i=1; i<=OrdersTotal(); i++) // Loop through orders
{
if (OrderSelect(i-1,SELECT_BY_POS)==true) // If there is the next one
{ // Analyzing orders:
if (OrderSymbol()!=Symb)continue; // Another security
Total++; // Counter of market orders
}
}
while(true) // Loop of closing orders
{
if (OrderType()==0 && Close_Buy_Condition==true) // Order Buy is opened & Close Buy Condition is true
{
for (c=Total-1; c>=0; c--)
{
RefreshRates(); // Refresh rates
Ans=OrderClose(OrderTicket(),Lot,Bid,Slippage); // Closing Buy
}
}
if (OrderType()==1 && Close_Sell_Condition==true) // Order Sell is opened & Close Sell Condition is true
{
for (d=Total-1; d>=0; d--)
{
RefreshRates(); // Refresh rates
Ans=OrderClose(OrderTicket(),Lot,Ask,Slippage); // Closing Sell
}
}
break; // Exit while
}
我不知道您在哪里设置 Ask
、Bid
和 Lot
变量的值。但是,当您遍历未平仓头寸时,它们必须改变。
您可以尝试使用此功能平仓:
bool CloseAllPositions()
{
double total;
int cnt;
while(OrdersTotal()>0)
{
// Close pending orders first, you can remove this section if you don't want pending orders to be deleted
total=OrdersTotal();
for(cnt=total-1; cnt>=0; cnt--)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
{
switch(OrderType())
{
case OP_BUYLIMIT :OrderDelete(OrderTicket()); break;
case OP_SELLLIMIT :OrderDelete(OrderTicket()); break;
case OP_BUYSTOP :OrderDelete(OrderTicket()); break;
case OP_SELLSTOP :OrderDelete(OrderTicket()); break;
}
}
}
// then close opened orders
total=OrdersTotal();
for(cnt=total-1; cnt>=0; cnt--)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
{
switch(OrderType())
{
case OP_BUY:
if (Close_Buy_Condition==true) {
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,Violet);
}
break;
case OP_SELL:
if (Close_Sell_Condition==true) {
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,Violet);
}
break;
}
}
}
}
return true;
}
编辑:
如果您根本不想处理挂单,请使用此代码代替 OrdersTotal()
:
int GetNumOpenPositions() {
int total = OrdersTotal();
double OpenBuyOrders = 0, OpenSellOrders = 0, PendBuys =0, PendSells =0;
for( i=0;i<total;i++)
{
OrderSelect(i, SELECT_BY_POS );
if ( OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
int type = OrderType();
if (type == OP_BUY ) {OpenBuyOrders=OpenBuyOrders+1;}
if (type == OP_SELL ) {OpenSellOrders=OpenSellOrders+1;}
if (type == OP_BUYSTOP ) {PendBuys=PendBuys+1;}
if (type == OP_SELLSTOP ) {PendSells=PendSells+1;}
}
}
return (OpenBuyOrders + OpenSellOrders);
}
则总订单数为