2

我发现了这个错误,我不确定原因(语法类似于 C)

该功能应该用卖单关闭买单,但我得到.
error 3: invalid trade parameters

是否有一些我设置错误的参数?

我之前已经成功打电话,OrderClose()所以我猜OrderSend()是正确的。

void closeBuySell(int buyTicket){
   double buyPrice = OrderClosePrice();
   //Print("total ",OrdersTotal()," ",buyTickets," ",sellTickets);
   for(int Counter=0; Counter<OrdersTotal(); Counter++){ //search for a matching sell
      if(OrderSelect(Counter,SELECT_BY_POS) && OrderType()==OP_SELL && OrderOpenPrice()>buyPrice){
         Print("CLOSING BUYSELL ",buyTicket," ",OrderTicket()," ",OrderOpenPrice(),">",buyAsk);
         if(OrderCloseBy(buyTicket,OrderTicket(),Red)){ buyTickets--; sellTickets--; }
         else Print(ErrorDescription(GetLastError()));
         break;
      }
   }
}

int buyTickets=0;
int sellTickets=0;
bool up = false;
void OnTick(){
   double FastMA = iMA(NULL,0,FastMAPeriod,0,0,0,0);
   double SlowMA = iMA(NULL,0,SlowMAPeriod,0,0,0,0);
   if(FastMA>SlowMA && !up){ up=true;
       if(buyTickets<15 && OrderSend(Symbol(),OP_BUY,.1,Ask,40,0,0))
           buyTickets++;
   }
   if(FastMA<SlowMA && up){ up=false;
       if(sellTickets<15 && OrderSend(Symbol(),OP_SELL,.1,Bid,40,0,0))
           sellTickets++;
   }

   for(int Counter=0; Counter<OrdersTotal(); Counter++) //ordercloseby buy with sell
      if(OrderSelect(Counter,SELECT_BY_POS) && OrderType()==OP_BUY)
          closeBuySell(OrderTicket());
}

输出:

...after
13:42  practice1 EURUSD,M5: invalid trade parameters
13:42  practice1 EURUSD,M5: OrderCloseBy error 3
13:42  practice1 EURUSD,M5: CLOSING BUYSELL 17 8 1.086>1.086
13:40  practice1 EURUSD,M5: invalid trade parameters
13:40  practice1 EURUSD,M5: OrderCloseBy error 3
13:40  practice1 EURUSD,M5: CLOSING BUYSELL 23 1 1.0858>1.0855
13:40  practice1 EURUSD,M5: invalid trade parameters
13:40  practice1 EURUSD,M5: OrderCloseBy error 3
13:40  practice1 EURUSD,M5: CLOSING BUYSELL 22 1 1.0858>1.0855
00:05  practice1 EURUSD,M5: sell orders:12
00:05  practice1 EURUSD,M5: open #12 sell limit 0.10 EURUSD at 1.08598 ok
00:05  practice1 EURUSD,M5: sell orders:11
00:05  practice1 EURUSD,M5: open #11 sell limit 0.10 EURUSD at 1.08599 ok
00:05  practice1 EURUSD,M5: sell orders:10
00:05  practice1 EURUSD,M5: open #10 sell limit 0.10 EURUSD at 1.08600 ok
...before
4

1 回答 1

1

是的,需要更改一些细节:

一个公平的通知,请注意,MQL4不是语言 - 简而言之 -确实不是Ca ,而是a ,编译模式默默地改变了代数表达式中运算符的优先级(!!)(如果不是由括号“机械地”强制执行)是模糊地由几乎看不见的指令控制,并且适用类似的危险差异。stringstringstruct#property strict

一个小问题,所谓的市价单不应该有到期参数设置,这些是立即“在市场”执行的,所以到期时间域-地平线对这些没有意义。

db.POOL在访问函数之前,您似乎忘记了一个强制性步骤——OrderSelect()调用:

int buyTickets  = 0;
int sellTickets = 0;
int mn          = 0;

void OnTick(){

     double FastMA = iMA( NULL, 0, FastMAPeriod, 0, MODE_SMA, PRICE_CLOSE, 0 );
     double SlowMA = iMA( NULL, 0, SlowMAPeriod, 0, MODE_SMA, PRICE_CLOSE, 0 );

     if (  FastMA >  SlowMA &&  buyTickets <  15 ){  buy( .1 ); Print(  "buy orders:",  buyTickets ); }
     if (  FastMA <  SlowMA && sellTickets <  15 ){ sell( .1 ); Print( "sell orders:", sellTickets ); }

     for ( int Counter = 0; Counter <  OrdersTotal(); Counter++ )
     {   OrderSelect( Counter, SELECT_BY_POS );   // *MUST* .SELECT() 1st
         if (  OrderType()      == OP_BUY         //         to access Order*() funcs
            && OrderCloseTime() == 0              // *!CLOSED* only
            && FastMA           <  SlowMA         // aPriceDOMAIN condition holds
            )  closeBuySell( OrderTicket() );     // closeBuySell()
     }
}

void buy( const double LotSize ){
     int    Slippage       = GetSlippage( Symbol(), 4 ); // ~ 40
     double StopLoss       = 0;
     double TakeProfit     = 0;
     int    MagicNumber    = mn++;
     int    ExpirationDate = TimeCurrent() + PERIOD_M1 * 60 * 24;
     int    buyTicket      = OrderSend( Symbol(),
                                        OP_BUY,
                                        LotSize,
                                        Ask,
                                        Slippage,
                                        StopLoss,
                                        TakeProfit,
                                        "Buy Order",
                                        MagicNumber,
                                        ExpirationDate, // --------------
                                        Green
                                        );
     if (  buyTicket == -1 ) Print( "OrderSend Error:", ErrorDescription( GetLastError() ) );
     else  buyTickets++;
}

void sell( const double LotSize ){
     int    Slippage       = GetSlippage( Symbol(), 4 );
     double StopLoss       = 0;
     double TakeProfit     = 0;
     int    MagicNumber    = mn++;
     int    ExpirationDate = TimeCurrent() + PERIOD_M1 * 60 * 24;
     int    sellTicket     = OrderSend( Symbol(),
                                        OP_SELL,
                                        LotSize,
                                        Bid,
                                        Slippage,
                                        StopLoss,
                                        TakeProfit,
                                        "Sell Order",
                                        MagicNumber,
                                        ExpirationDate, // --------------
                                        Green
                                        );
     if (  sellTicket == -1 ) Print( "OrderSend Error:", ErrorDescription( GetLastError() ) );
     else  sellTickets++;
}

void closeBuySell( const int buyTicket ){
     OrderSelect( buyTicket, SELECT_BY_TICKET );
     double buyAsk = OrderClosePrice();
  // Print( "total ", OrdersTotal(), " ", buyTickets, " ", sellTickets );
     for ( int Counter = 0; Counter < OrdersTotal(); Counter++ ){ // search for a matching sell
           OrderSelect( Counter, SELECT_BY_POS );
           if (  OrderType()      == OP_SELL
              && OrderCloseTime() == 0            // *!CLOSED" only
              && OrderOpenPrice() >  buyAsk
              ){
              Print( "CLOSING BUYSELL ", buyTicket, " ", OrderTicket(), " ", OrderOpenPrice(), ">", buyAsk );
              if (  OrderCloseBy( buyTicket, OrderTicket(), Red ) ){
                     buyTickets--;
                    sellTickets--;
              }
              else  Print( ErrorDescription( GetLastError() ) );
              break;
           }
     }
}
于 2016-06-05T16:02:58.760 回答