错误 4756

 

我的 ea 出现错误 4756

你能帮我吗谢谢

//+------------------------------------------------------------------+
//|                                                    ErlyBird6.mq5 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link       "http://www.mql5.com"
#property version   "1.00"

string name, oldnameHTop,oldnameHBottom,oldnameVstart,oldnameVstop;

input int       MA_Period= 8 ;       // Moving Average Period
input int       StopLoss= 100 ;       // Stop Loss
input int       TakeProfit= 100 ;   // Take Profit
input int       Trigger= 5 ;
input double    Lot= 1 ;
input int       EA_Magic= 12345 ;   // EA Magic Number


int maHandle;   // handle for our Moving Average indicator
int     Sommerzeit     =   87 ;   // DayOfYear Beginn Sommerzeit
int     Winterzeit     =   297 ;   // DayOfYear Beginn Winterzeit
int   TimeSelect;
int STP, TKP;   // To be used for Stop Loss & Take Profit values


double maVal[]; // Dynamic array to hold the values of Moving Average for each bars
double hg[],lw[],p_close;
double High[],  Low[];
double Top,Bottom;

   MqlRates rates[];
   MqlDateTime Time;

 MqlTick latest_price;     // To be used for getting recent/latest price quotes
 MqlTradeRequest mrequest;   // To be used for sending our trade requests
 MqlTradeResult mresult;     // To be used to get our trade results
     // Initialization of mrequest structure
  

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+


int OnInit ()
  {


//--- Get the handle for Moving Average indicator
   maHandle= iMA ( _Symbol , _Period ,MA_Period, 0 , MODE_EMA , PRICE_CLOSE );
//--- What if handle returns Invalid Handle
   if ( maHandle< 0 )
     {
       Alert ( "Error Creating Handles for indicators - error: " , GetLastError (), "!!" );
       return (- 1 );
     }



//--- Let us handle currency pairs with 5 or 3 digit prices instead of 4
   STP = StopLoss;
   TKP = TakeProfit;
   if ( _Digits == 5 || _Digits == 3 )
     {
      STP = STP* 10 ;
      TKP = TKP* 10 ;
     }
   

   return ( 0 );
  }

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit ( const int reason)
  {
//--- destroy timer
   EventKillTimer ();
      
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick ()
  {
   // Do we have enough bars to work with
   if ( Bars ( _Symbol , _Period )< 60 ) // if total bars is less than 60 bars
     {
       Alert ( "We have less than 60 bars, EA will now exit!!" );
       return ;
     }  

// We will use the static Old_Time variable to serve the bar time.
// At each OnTick execution we will check the current bar time with the saved one.
// If the bar time isn't equal to the saved time, it indicates that we have a new tick.

   static datetime Old_Time;
   datetime New_Time[ 1 ];
   bool IsNewBar= false ;

// copying the last bar time to the element New_Time[0]
   int copied= CopyTime ( _Symbol , _Period , 0 , 1 ,New_Time);
   if (copied> 0 ) // ok, the data has been copied successfully
     {
       if (Old_Time!=New_Time[ 0 ]) // if old time isn't equal to new bar time
        {
         IsNewBar= true ;   // if it isn't a first call, the new bar has appeared
         if ( MQL5InfoInteger ( MQL5_DEBUGGING )) Print ( "We have new bar here " ,New_Time[ 0 ], " old time was " ,Old_Time);
         Old_Time=New_Time[ 0 ];             // saving bar time
        }
     }
   else
     {
       Alert ( "Error in copying historical times data, error =" , GetLastError ());
       ResetLastError ();
       return ;
     }

//--- EA should only check for new trade if we have a new bar
   if (IsNewBar== false )
     {
       return ;
     }
 
//--- Do we have enough bars to work with
   int Mybars= Bars ( _Symbol , _Period );
   if (Mybars< 60 ) // if total bars is less than 60 bars
     {
       Alert ( "We have less than 60 bars, EA will now exit!!" );
       return ;
     } 
  
     ArraySetAsSeries (rates, true );
     copied= CopyRates ( _Symbol , 0 , 0 , 60 ,rates);
   
     TimeToStruct (rates[ 0 ].time, Time);
     if (  Time.day_of_year>=Sommerzeit &&   Time.day_of_year<=Winterzeit) TimeSelect= 11 ; else TimeSelect= 10 ;  
     
     if (  Time.hour==TimeSelect &&  Time.min== 0 )   //draw vertical line at TimeSelect
          {
             ObjectDelete ( 0 ,oldnameVstart);  
            name = "VerticalStart" + TimeToString (rates[ 0 ].time, TIME_DATE|TIME_SECONDS);
             ObjectCreate ( 0 ,name, OBJ_VLINE , 0 ,rates[ 0 ].time, 0 ); 
             ObjectSetInteger ( 0 ,name, OBJPROP_COLOR , clrRed );
            oldnameVstart= name;       
          }
      
    
         if (  Time.hour==TimeSelect+ 5 &&  Time.min== 0 )   //draw vertical line at TimeSelect+5
           {
            
             ObjectDelete ( 0 ,oldnameVstop);      
            name = "VerticalEnd" + TimeToString (rates[ 0 ].time, TIME_DATE|TIME_SECONDS);
             ObjectCreate ( 0 , name, OBJ_VLINE , 0 ,rates[ 0 ].time, 0 );
             ObjectSetInteger ( 0 , name, OBJPROP_COLOR , clrDarkViolet );
            oldnameVstop= name;   
            
             ArraySetAsSeries (hg, true );
             ArraySetAsSeries (lw, true );
           
             CopyHigh ( _Symbol , _Period , TimeCurrent (), 5 ,hg);
             CopyLow ( _Symbol , _Period , TimeCurrent (), 5 ,lw);
       
             
             Top = NormalizeDouble (rates[ ArrayMaximum (hg, 0 , WHOLE_ARRAY )].high, _Digits );  
             Bottom = NormalizeDouble (rates[ ArrayMinimum (lw, 0 , WHOLE_ARRAY )].low, _Digits );
          
           
             // draw horizontal line at the top of  5 last  candles 
             ObjectDelete ( 0 , oldnameHTop);
            name = "HorizontalTop" + TimeToString (rates[ 0 ].time, TIME_DATE|TIME_SECONDS);
        
             ObjectCreate ( 0 ,name, OBJ_HLINE , 0 ,rates[ 0 ].time,Top);
             ObjectSetInteger ( 0 ,name , OBJPROP_COLOR , clrGreenYellow );
            oldnameHTop= name;
         
         
             // draw horizontal line at the bottom of  5 last  candles 
             ObjectDelete ( 0 ,oldnameHBottom);
            name = "HorizontalBottom" + TimeToString (rates[ 0 ].time, TIME_DATE|TIME_SECONDS);
        
             ObjectCreate ( 0 ,name, OBJ_HLINE , 0 ,rates[ 0 ].time,Bottom);
             ObjectSetInteger ( 0 ,name , OBJPROP_COLOR , clrCrimson );
            oldnameHBottom= name;
 
     ZeroMemory (mrequest);       
         
 // the MA-8 values arrays
   ArraySetAsSeries (maVal, true );
 
   if ( CopyBuffer (maHandle, 0 , 0 , 3 ,maVal)< 0 )
     {
       Alert ( "Error copying Moving Average indicator buffer - error:" , GetLastError ());
       ResetLastError ();
       return ;
     }
 
 
 
 //--- Get the last price quote using the MQL5 MqlTick Structure
   if (! SymbolInfoTick ( _Symbol ,latest_price))
     {
       Alert ( "Error getting the latest price quote - error:" , GetLastError (), "!!" );
       return ;
     }
 
 
/**********************************           BUY POSITION    ************************************************/             
 
   //--- Declare bool type variables to hold our Buy Conditions
   // bool Buy_Condition_1=(rates[0].close > Top);  
     //bool Buy_Condition_1=(latest_price.ask >Top);
    
    
         // any opened Buy position?
         if (isTrade_BUY())
           {
             Alert ( "We already have a Buy Position!!!" );
             return ;     // Don't open a new Buy Position
           }
         ZeroMemory (mrequest);
         ZeroMemory (mresult); 
         mrequest.action = TRADE_ACTION_PENDING ;   // immediate order execution
         mrequest.price = NormalizeDouble (Top+Trigger* _Point , _Digits );           // latest ask price
         mrequest.sl = NormalizeDouble (latest_price.ask - STP* _Point , _Digits ); // Stop Loss
         mrequest.tp = NormalizeDouble (latest_price.ask + TKP* _Point , _Digits ); // Take Profit
       //  mrequest.sl = 0;
       //  mrequest.tp =0;
         mrequest.symbol = _Symbol ;                                             // currency pair
         mrequest.volume = Lot;                                                 // number of lots to trade
         mrequest.magic = EA_Magic;                                             // Order Magic Number
         mrequest.type = ORDER_TYPE_BUY_STOP ;                                   // Buy Order
         mrequest.type_filling = ORDER_FILLING_RETURN ;                             // Order execution type
         mrequest.deviation= 100 ;                                                 // Deviation from current price
         //--- send order
         OrderSend (mrequest,mresult);
       // get the result code
         if (mresult.retcode== 10009 || mresult.retcode== 10008 ) //Request is completed or order placed
           {
             Alert ( "A Buy order has been successfully placed with Ticket#:" ,mresult.order, "!!" );
           }
         else
           {
             Alert ( "The Buy order request could not be completed -error:" , GetLastError ());
             ResetLastError ();           
             return ;
           }
   
      
 /**********************************          SELL POSITION    ************************************************/             
         if (isTrade_SELL())
           {
             Alert ( "We already have a Sell position!!!" );
             return ;     // Don't open a new Sell Position
           }
         ZeroMemory (mrequest);
         ZeroMemory (mresult); 
         mrequest.action= TRADE_ACTION_PENDING ;                               // immediate order execution
         mrequest.price = NormalizeDouble (Bottom-Trigger* _Point , _Digits );             // latest Bid price
         mrequest.sl = NormalizeDouble (latest_price.bid + STP* _Point , _Digits ); // Stop Loss
         mrequest.tp = NormalizeDouble (latest_price.bid - TKP* _Point , _Digits ); // Take Profit
         mrequest.symbol = _Symbol ;                                           // currency pair
         mrequest.volume = Lot;                                               // number of lots to trade
         mrequest.magic = EA_Magic;                                           // Order Magic Number
         mrequest.type= ORDER_TYPE_SELL_STOP ;                                     // Sell Order
         mrequest.type_filling = ORDER_FILLING_RETURN ;                           // Order execution type
         mrequest.deviation= 100 ;                                             // Deviation from current price
         //--- send order
         OrderSend (mrequest,mresult);
         // get the result code
         if (mresult.retcode== 10009 || mresult.retcode== 10008 ) //Request is completed or order placed
           {
             Alert ( "A Sell order has been successfully placed with Ticket#:" ,mresult.order, "!!" );
           }
         else
           {
             Alert ( "The Sell order request could not be completed -error:" , GetLastError ());
             ResetLastError ();
             return ;
           }
          
          
           }

  DeleteAllOrdersPending();
   
    }
   
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer ()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Trade function                                                   |
//+------------------------------------------------------------------+
void OnTrade ()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
  {
//---
   double ret= 0.0 ;
//---

//---
   return (ret);
  }
//+------------------------------------------------------------------+
bool isTrade_BUY()
  {
   if ( PositionSelect ( _Symbol )== true && PositionGetInteger ( POSITION_TYPE )== POSITION_TYPE_BUY ) // we have an opened position
       {
         return ( true );
        }
  
    
   else 
     {
     return ( false );
     }
  
  }
/////////////////////////////////////////////////////////////////////////////////////


bool isTrade_SELL()
  {
   if ( PositionSelect ( _Symbol )== true && PositionGetInteger ( POSITION_TYPE )== POSITION_TYPE_SELL ) // we have an opened position
       {
         return ( true );
        }
  
    
   else 
     {
     return ( false );
     }
  
  }
  
////////////////////////////////////////////////////////////////////////////////////// 
 void DeleteAllOrdersPending()
   {
    
     if (isTrade_SELL()||isTrade_BUY())
     {  
    
     int i;
   // in this loop we're checking all pending orders
       for (i= 0 ;i< OrdersTotal ();i++)
         {
         // choosing each order and getting its ticket
         ulong ticket= OrderGetTicket (i);
           // processing orders with "our" symbols only
           if ( OrderGetString ( ORDER_SYMBOL )== Symbol ())
             {
             // processing Buy Stop orders
             if ( OrderGetInteger ( ORDER_TYPE )== ORDER_TYPE_BUY_STOP )
                {
                 mrequest.action= TRADE_ACTION_REMOVE ;
                 // putting the ticket number to the structure
                 mrequest.order=ticket;
                 // sending request to trade server
                 OrderSend (mrequest,mresult);
                }
        
         // processing Sell Stop orders
             if ( OrderGetInteger ( ORDER_TYPE )== ORDER_TYPE_SELL_STOP )
               {
               // we will delete this pending order
               mrequest.action= TRADE_ACTION_REMOVE ;
               // putting the ticket number to the structure
               mrequest.order=ticket;
               // sending request to trade server
               OrderSend (mrequest,mresult);
                    
               }
           } 
       } //boucle for
      } //if
    }    
 
dan5:

我的EA出现错误4756

你能帮助我吗 谢谢

2013.03.10 11:19:18 2012.01.04 15:00:00买入止损 1.00 EURUSD at 1.30505 sl: 1.28375 tp: 1.30375[无效的停止]
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Order Properties - Documentation on MQL5
 

CopyHigh(_Symbol,_Period,TimeCurrent(),5,hg);

Top = NormalizeDouble(rates[ArrayMaximum(hg,0,WHOLE_ARRAY)].high,_Digits); 

- 被误解 设计。
最大双数值中选择 ,并 使用 该值 代替 整数 索引

 
dan5:

我的EA出现错误4756

你能帮助我吗,谢谢

错误4756是 "交易请求发送失败"。如果你在发送订单时出现错误,请检查MqlTradeResult的返回代码。

         //--- send order
         OrderSend(mrequest,mresult);
         // get the result code
         if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
           {
            Alert("An order has been successfully placed with Ticket#:",mresult.order,"!!");
           }
         else
           {
            Alert("The order request could not be completed -error:",GetLastError()," with trade return code ",mresult.retcode);
            ResetLastError();
            return;
           }

根据上面Konstantin83,你有无效止损的错误,这意味着你必须在SymbolInfoIntegerSYMBOL_TRADE_STOPS_LEVEL和SYMBOL_TRADE_FREEZE_LEVEL 之外下挂单

 
phi.nuts:

错误4756是 "交易请求发送失败"。如果你在发送订单时出现错误,检查MqlTradeResult的返回代码。

根据上面Konstantin83,你有无效止损的错误,这意味着你必须在SymbolInfoIntegerSYMBOL_TRADE_STOPS_LEVEL和SYMBOL_TRADE_FREEZE_LEVEL 之外下挂单

对于ECN类型的经纪商,是否也会返回无效的止损?
 
RaptorUK:
对于ECN类型的经纪商,无效的止损也会被返回吗?
当然,你知道答案是否定的。但为什么这么问?
 
phi.nuts:
当然,你知道答案是否定的。但为什么这么问?
Why do you assume I know the answer is no ? Invalid stops(error 130) is returned on mql4 when SL or TP are sent with OrderSend() to an ECN Broker so I was asking if the same is true with mql5. What error is returned ?
Documentation on MQL5: Standard Constants, Enumerations and Structures / Codes of Errors and Warnings / Compilation Errors
Documentation on MQL5: Standard Constants, Enumerations and Structures / Codes of Errors and Warnings / Compilation Errors
  • www.mql5.com
Standard Constants, Enumerations and Structures / Codes of Errors and Warnings / Compilation Errors - Documentation on MQL5
 
RaptorUK:
为什么你认为我知道答案是否定的? 当SL或TP用OrderSend()发送到ECN经纪商时,无效止损(错误130)在mql4上被返回,所以我在问mql5是否也是如此。 什么错误被返回?
真的吗? 这很有趣。我以后要检查一下;D.
 
phi.nuts:
真的吗? 这很有趣。我稍后要检查它;D.
如果策略测试器的行为类似于经纪商返回的结果,那么当为一个ENUM_SYMBOL_TRADE_EXECUTION交易所执行或市场执行 的符号发送SL和TP时,似乎会被忽略,并且不会返回错误。 因此,与mql4相比,事情有很大不同。
 
RaptorUK:
If the Strategy Tester behaviour is similar to what would be returned by a Broker then it seems that SL & TP are ignored when sent for a Symbol whoseENUM_SYMBOL_TRADE_EXECUTION isExchange execution orMarket execution and no error is returned. So things are quite different compared to mql4.

我还发现了一些关于无效止损的问题。 我有一个简单的EA,它在进行交易时带有SL和TP,除非符号是交易所或市场执行,那么它在发送交易时没有设置SL和TP,然后向(TRADE_ACTION_SLTP)发送第二个请求以设置SL和TP。

它在策略测试器中工作正常,所以今天我在模拟账户上试了一下,我一直得到无效止损(错误10016)。 所以我检查了止损水平和冻结水平,它们都是0,我尝试了各种水平的SL和TP,没有任何效果。我可以为现有的头寸手动设置相同的SL和TP,没有任何问题 ... ...所以我添加了一个测试,只在可以选择头寸的情况下放置SL和TP ... ...不再有无效止损,也不再有TRADE_ACTION_SLTP交易请求 :-(

所以我在完成没有TP和SL的初始交易请求和随后发送TP和SL的交易请求之间添加了这段代码 ..

         SelectRetryCount = 1;
         if(SetTPandSL)
            {
            while(!PositionSelect(_Symbol) && SelectRetryCount < 10)
               {
               Sleep(SelectRetryCount * 100); // sleep for SelectRetryCount * 100 mS
               SelectRetryCount++;
               }
            }

如果初始交易请求成功,SetTPandSL被设置为真,如果不成功,就没有必要尝试设置TP和SL。选择头寸,如果失败,会发生100毫秒的睡眠,然后重新选择,如果失败,会发生200毫秒的睡眠,等等,最多尝试9次(共4.5秒)。

我真的不知道我在用mql5做什么,我真的只是在尝试让一些东西工作,希望边做边学......我在这里发现的是正常行为吗?我认为一旦初始交易请求返回10009 - TRADE_RETCODE_DONE,我就可以发送请求来设置TP和SL,是不是这样?

 
RaptorUK:

我还发现了一些关于无效止损的问题。 我有一个简单的EA,它用SL和TP进行交易,除非符号是交易所或市场执行,然后它在没有设置SL和TP的情况下发送交易,然后向(TRADE_ACTION_SLTP)发送第二个请求以设置SL和TP。

它在策略测试器中工作正常,所以今天我在模拟账户上试了一下,我一直得到无效止损(错误10016)。 所以我检查了止损水平和冻结水平,它们都是0,我尝试了各种水平的SL和TP,没有任何效果。我可以为现有的头寸手动设置相同的SL和TP,没有任何问题 ... ...所以我添加了一个测试,只在可以选择头寸的情况下放置SL和TP ... ...不再有无效止损,也不再有TRADE_ACTION_SLTP交易请求 :-(

所以我在完成没有TP和SL的初始交易请求和随后发送TP和SL的交易请求之间添加了这段代码 ..

如果初始交易请求成功,SetTPandSL被设置为真,如果不成功,就没有必要尝试设置TP和SL。选择头寸,如果失败,会发生100毫秒的睡眠,然后重新选择,如果失败,会发生200毫秒的睡眠,等等,最多9次尝试(总共4.5秒)。

我真的不知道我在用mql5做什么,我真的只是在尝试让一些东西工作,希望边做边学......我在这里发现的是正常行为吗?我认为一旦最初的交易请求返回10009 - TRADE_RETCODE_DONE,我就可以发送请求来设置TP和SL,是不是这样?

你用什么函数,类/方法来发送/修改你的订单?