初学者的问题 MQL5 MT5 MetaTrader 5 - 页 1403 1...139613971398139914001401140214031404140514061407140814091410...1503 新评论 Vitaly Muzichenko 2022.04.25 08:30 #14021 Marco Nicholas Comment() 来代替同样的Print。 自己尝试真的有那么难吗? Marco Nicholas 2022.04.26 14:09 #14022 Vitaly Muzichenko #:自己尝试真的有那么难吗? 我试过了,很有效。 由于F1内部关于 "switch "的帮助,出现了这样的问题,因为那里只指定了Print。而且我早些时候注意到,void OnTick()中的Comment() 大大降低了代码执行速度。 kduarte 2022.04.26 14:40 #14023 我买了一个指标,但当我安装它时,我得到了MQL5社区的对话框,它告诉我,我的登录名和密码无效,但尽管如此,我正在输入发送到我的电子邮件的凭证,我已经改变了密码,但什么都没有,我已经注册了。 kduarte 2022.04.26 14:41 #14024 有谁能帮助我如何做到这一点? Eleni Anna Branou 2022.04.26 16:14 #14025 kduarte #: 谁能帮助我如何做到这一点? 请确保您使用的是您的kduarte登录,并且您的MQL5账户密码不超过10个字符。 Francois Dominique Izaak Huibregtse 2022.04.27 09:42 #14026 Vladimir Karputov #:你没有以任何方式控制ORDERS,你在代码中检查POSITION的数量和类型。此外,你不正确地使用POSITIONS(如果你的账户类型是对冲,使用 "PositionSelect(_Symbol) "是错误的)。你的算法有严重的设计错误。你应该做什么。1.只在新条形图诞生的时候工作。如果没有新条形图--什么都不做,不要做不必要的动作。2.这一项将在第1项完成后披露。 你好,弗拉基米尔。 再次感谢你的提示。我花了一些时间才弄清楚事情。 根据你的提示,我改变了什么。 - 把开仓和平仓的逻辑改成了对冲版本。 - 我现在用一个新柱子的诞生来进行一些计算/下单的工作。 由于这些改变的应用,订单现在只打开和关闭一次。但是现在我在回测时遇到了一个问题。我得到一个错误,即第121行的数组超出了范围。你能给我指出正确的方向吗?我试着用一个双倍类型的数组来做收盘价和copyClose,但当时还是得到错误。 也为第2项感到兴奋 :) 到目前为止的代码。 黄色的部分我突出了第121行。 void OnTick() { MqlTradeRequest request; MqlTradeResult Result; ZeroMemory(request); //Setting newBar event handler bool newBar = true; int barShift =0; newBar = NewBar.CheckNewBar(_Symbol,_Period); barShift = 1; // Get current market orders ulong buyTicket = 0, sellTicket = 0; for(int i = 0; i < PositionsTotal(); i++) { ulong ticket = PositionGetTicket(i); PositionSelectByTicket(ticket); if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) { buyTicket = ticket; Buy_openend = true; } else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) { sellTicket = ticket; Sell_openend = true; } } if(newBar == true) { double closingprice[]; datetime date_time[]; MqlRates bar[]; ArraySetAsSeries(bar,true); ArrayResize(bar,80); CopyRates(_Symbol,_Period,0,56,bar); double close = bar[1].close; datetime time = bar[1].time; datetime startTime = CreateDateTime(OpentimeHH,OpentimeMM); datetime endTime = CreateDateTime(EndtimeHH,EndtimeMM); if(OpentimeHH <= EndtimeHH) { endTime -= TIME_ADD_DAY; } Comment(close,time, "start time:", startTime, "endtime:", endTime); int bar_index = iBarShift(_Symbol, PERIOD_CURRENT, endTime,true); double closeprev = bar[bar_index].close; //line 121 // Check for trade conditions bool BuyCondition = (close >= closeprev) && (bar[1].time == startTime) && (Buy_openend == false); bool SellCondition = (close <= closeprev) && (bar[1].time == startTime) && (Sell_openend == false); // Open Market buy order if(BuyCondition) { //Close Posible Sell Order if(sellTicket > 0) { PositionSelectByTicket(sellTicket); request.action = TRADE_ACTION_DEAL; request.type = ORDER_TYPE_BUY; request.symbol = _Symbol; request.position = sellTicket; request.volume = PositionGetDouble(POSITION_VOLUME); request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK); request.type_filling = ORDER_FILLING_IOC; request.deviation = 50; bool sent = OrderSend(request,Result); } request.action = TRADE_ACTION_DEAL; request.type = ORDER_TYPE_BUY; request.symbol = _Symbol; request.position = 0; request.volume = TradeVolume; request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK); request.type_filling = ORDER_FILLING_IOC; request.sl = 0; request.tp = 0; request.deviation = 50; bool sent = OrderSend(request,Result); } //Open Market Sell Order if(SellCondition) { //Close posible buy orders if(buyTicket > 0) { PositionSelectByTicket(buyTicket); request.action = TRADE_ACTION_DEAL; request.type = ORDER_TYPE_SELL; request.symbol = _Symbol; request.position = buyTicket; request.volume = PositionGetDouble(POSITION_VOLUME); request.price = SymbolInfoDouble(_Symbol,SYMBOL_BID); request.type_filling = ORDER_FILLING_IOC; request.deviation = 50; bool sent = OrderSend(request,Result); } request.action = TRADE_ACTION_DEAL; request.type = ORDER_TYPE_SELL; request.symbol = _Symbol; request.position = 0; request.volume = TradeVolume; request.price = SymbolInfoDouble(_Symbol,SYMBOL_BID); request.type_filling = ORDER_FILLING_IOC; request.sl = 0; request.tp = 0; request.deviation = 50; bool sent = OrderSend(request,Result); } } } Vladimir Karputov 2022.04.27 09:55 #14027 Divania111 # : 检查操作的结果。 int bar_index = iBarShift ( _Symbol , PERIOD_CURRENT , endTime, true ); Francois Dominique Izaak Huibregtse 2022.04.27 10:01 #14028 Vladimir Karputov #:检查操作的结果。 Omg!非常感谢! Aldi Dwi Juntoro 2022.04.27 18:32 #14029 您好交易者...请帮助如何在 EA 细分专家:细分中将风险百分比更改为手数或修复手数。 //+------------------------------------------------------------------+ //| breakdown(barabashkakvn's edition).mq5 | //| Arist0 | //| arist0.rr@gmail.com | //+------------------------------------------------------------------+ #property copyright "Arist0" #property link "arist0.rr@gmail.com" #property version "1.001" //--- #include <Trade\PositionInfo.mqh> #include <Trade\Trade.mqh> #include <Trade\SymbolInfo.mqh> #include <Trade\AccountInfo.mqh> #include <Trade\OrderInfo.mqh> #include <Expert\Money\MoneyFixedMargin.mqh> CPositionInfo m_position; // trade position object CTrade m_trade; // trading object CSymbolInfo m_symbol; // symbol info object CAccountInfo m_account; // account info wrapper COrderInfo m_order; // pending orders object CMoneyFixedMargin *m_money; //--- input parameters input ushort InpStopLoss = 50 ; // Stop Loss (in pips) input ushort InpTakeProfit = 50 ; // Take Profit (in pips) input ushort InpTrailingStop = 5 ; // Trailing Stop (in pips) input ushort InpTrailingStep = 5 ; // Trailing Step (in pips) input ushort InpMinDistance = 25 ; // Minimum distance input double Risk = 5 ; // Risk in percent for a deal from a free margin input ulong m_magic = 585000550 ; // magic number //--- ulong m_slippage= 10 ; // slippage double ExtStopLoss= 0.0 ; double ExtTakeProfit= 0.0 ; double ExtTrailingStop= 0.0 ; double ExtTrailingStep= 0.0 ; double ExtMinDistance= 0.0 ; double m_adjusted_point; // point value adjusted for 3 or 5 points bool bln_delete_all= false ; datetime dt_last_delete= 0 ; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit () { if (InpTrailingStop!= 0 && InpTrailingStep== 0 ) { string text=( TerminalInfoString ( TERMINAL_LANGUAGE )== "Russian" )? "Трейлинг невозможен: параметр \"Trailing Step\" равен нулю!" : "Trailing is not possible: parameter \"Trailing Step\" is zero!" ; Alert ( __FUNCTION__ , " ERROR! " ,text); return ( INIT_PARAMETERS_INCORRECT ); } if (!m_symbol.Name( Symbol ())) // sets symbol name return ( INIT_FAILED ); RefreshRates(); //--- m_trade.SetExpertMagicNumber(m_magic); m_trade.SetMarginMode(); m_trade.SetTypeFillingBySymbol(m_symbol.Name()); m_trade.SetDeviationInPoints(m_slippage); //--- tuning for 3 or 5 digits int digits_adjust= 1 ; if (m_symbol. Digits ()== 3 || m_symbol. Digits ()== 5 ) digits_adjust= 10 ; m_adjusted_point=m_symbol. Point ()*digits_adjust; ExtStopLoss = InpStopLoss * m_adjusted_point; ExtTakeProfit = InpTakeProfit * m_adjusted_point; ExtTrailingStop= InpTrailingStop * m_adjusted_point; ExtTrailingStep= InpTrailingStep * m_adjusted_point; ExtMinDistance = InpMinDistance * m_adjusted_point; //--- if (m_money!= NULL ) delete m_money; m_money= new CMoneyFixedMargin; if (m_money!= NULL ) { if (!m_money.Init( GetPointer (m_symbol), Period (),m_symbol. Point ()*digits_adjust)) return ( INIT_FAILED ); m_money.Percent(Risk); } else { Print ( __FUNCTION__ , ", ERROR: Object CMoneyFixedMargin is NULL" ); return ( INIT_FAILED ); } //--- bln_delete_all= false ; dt_last_delete= 0 ; //--- return ( INIT_SUCCEEDED ); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit ( const int reason) { //--- if (m_money!= NULL ) delete m_money; } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick () { //--- if (bln_delete_all) { if (IsPendingOrdersExists()) { DeleteAllPendingOrders(); //dt_last_delete=iTime(m_symbol.Name(),Period(),0); return ; } else { bln_delete_all= false ; dt_last_delete= iTime (m_symbol.Name(), PERIOD_D1 , 0 ); } } //--- if (IsPendingOrdersExists() && dt_last_delete< iTime (m_symbol.Name(), PERIOD_D1 , 0 )) { bln_delete_all= true ; return ; } //--- if (!IsPendingOrdersExists()) { if (!RefreshRates()) return ; //--- double price= iHigh (m_symbol.Name(), PERIOD_D1 , 1 )+ExtMinDistance; double sl=(InpStopLoss== 0 )? 0.0 :price-ExtStopLoss; double tp=(InpTakeProfit== 0 )? 0.0 :price+ExtTakeProfit; PendingBuyStop(price,sl,tp); //--- price= iLow (m_symbol.Name(), PERIOD_D1 , 1 )-ExtMinDistance; sl=(InpStopLoss== 0 )? 0.0 :price+ExtStopLoss; tp=(InpTakeProfit== 0 )? 0.0 :price-ExtTakeProfit; PendingSellStop(price,sl,tp); dt_last_delete= iTime (m_symbol.Name(), PERIOD_D1 , 0 ); return ; } //--- Trailing(); } //+------------------------------------------------------------------+ //| TradeTransaction function | //+------------------------------------------------------------------+ void OnTradeTransaction ( const MqlTradeTransaction &trans, const MqlTradeRequest &request, const MqlTradeResult &result) { //--- double res= 0.0 ; int losses= 0.0 ; //--- get transaction type as enumeration value ENUM_TRADE_TRANSACTION_TYPE type=trans.type; //--- if transaction is result of addition of the transaction in history if (type== TRADE_TRANSACTION_DEAL_ADD ) { long deal_ticket = 0 ; long deal_order = 0 ; long deal_time = 0 ; long deal_time_msc = 0 ; long deal_type =- 1 ; long deal_entry =- 1 ; long deal_magic = 0 ; long deal_reason =- 1 ; long deal_position_id = 0 ; double deal_volume = 0.0 ; double deal_price = 0.0 ; double deal_commission = 0.0 ; double deal_swap = 0.0 ; double deal_profit = 0.0 ; string deal_symbol = "" ; string deal_comment = "" ; string deal_external_id = "" ; if ( HistoryDealSelect (trans.deal)) { deal_ticket = HistoryDealGetInteger (trans.deal, DEAL_TICKET ); deal_order = HistoryDealGetInteger (trans.deal, DEAL_ORDER ); deal_time = HistoryDealGetInteger (trans.deal, DEAL_TIME ); deal_time_msc = HistoryDealGetInteger (trans.deal, DEAL_TIME_MSC ); deal_type = HistoryDealGetInteger (trans.deal, DEAL_TYPE ); deal_entry = HistoryDealGetInteger (trans.deal, DEAL_ENTRY ); deal_magic = HistoryDealGetInteger (trans.deal, DEAL_MAGIC ); deal_reason = HistoryDealGetInteger (trans.deal, DEAL_REASON ); deal_position_id = HistoryDealGetInteger (trans.deal, DEAL_POSITION_ID ); deal_volume = HistoryDealGetDouble (trans.deal, DEAL_VOLUME ); deal_price = HistoryDealGetDouble (trans.deal, DEAL_PRICE ); deal_commission = HistoryDealGetDouble (trans.deal, DEAL_COMMISSION ); deal_swap = HistoryDealGetDouble (trans.deal, DEAL_SWAP ); deal_profit = HistoryDealGetDouble (trans.deal, DEAL_PROFIT ); deal_symbol = HistoryDealGetString (trans.deal, DEAL_SYMBOL ); deal_comment = HistoryDealGetString (trans.deal, DEAL_COMMENT ); deal_external_id = HistoryDealGetString (trans.deal, DEAL_EXTERNAL_ID ); } else return ; if (deal_symbol==m_symbol.Name() && deal_magic==m_magic) if (deal_entry== DEAL_ENTRY_IN ) if (deal_type== DEAL_TYPE_BUY || deal_type== DEAL_TYPE_SELL ) DeleteAllPendingOrders(); } } //+------------------------------------------------------------------+ //| Refreshes the symbol quotes data | //+------------------------------------------------------------------+ bool RefreshRates( void ) { //--- refresh rates if (!m_symbol.RefreshRates()) { Print ( "RefreshRates error" ); return ( false ); } //--- protection against the return value of "zero" if (m_symbol.Ask()== 0 || m_symbol.Bid()== 0 ) return ( false ); //--- return ( true ); } //+------------------------------------------------------------------+ //| Pending order of Buy Stop | //+------------------------------------------------------------------+ void PendingBuyStop( double price, double sl, double tp) { sl=m_symbol.NormalizePrice(sl); tp=m_symbol.NormalizePrice(tp); double check_open_long_lot=m_money.CheckOpenLong(m_symbol.Ask(),sl); Print ( "sl=" , DoubleToString (sl,m_symbol. Digits ()), ", CheckOpenLong: " , DoubleToString (check_open_long_lot, 2 ), ", Balance: " , DoubleToString (m_account.Balance(), 2 ), ", Equity: " , DoubleToString (m_account.Equity(), 2 ), ", FreeMargin: " , DoubleToString (m_account.FreeMargin(), 2 )); if (check_open_long_lot== 0.0 ) { Print ( __FUNCTION__ , ", ERROR: method CheckOpenLong returned the value of \"0.0\"" ); return ; } //--- check volume before OrderSend to avoid "not enough money" error (CTrade) double check_volume_lot=m_trade.CheckVolume(m_symbol.Name(),check_open_long_lot,m_symbol.Ask(), ORDER_TYPE_BUY ); if (check_volume_lot!= 0.0 ) { if (check_volume_lot>=check_open_long_lot) { if (m_trade.BuyStop(check_open_long_lot,m_symbol.NormalizePrice(price), m_symbol.Name(),m_symbol.NormalizePrice(sl),m_symbol.NormalizePrice(tp))) { if (m_trade.ResultOrder()== 0 ) { Print ( "#1 Buy Stop -> false. Result Retcode: " ,m_trade.ResultRetcode(), ", description of result: " ,m_trade.ResultRetcodeDescription()); PrintResultTrade(m_trade,m_symbol); } else { Print ( "#2 Buy Stop -> true. Result Retcode: " ,m_trade.ResultRetcode(), ", description of result: " ,m_trade.ResultRetcodeDescription()); PrintResultTrade(m_trade,m_symbol); } } else { Print ( "#3 Buy Stop -> false. Result Retcode: " ,m_trade.ResultRetcode(), ", description of result: " ,m_trade.ResultRetcodeDescription()); PrintResultTrade(m_trade,m_symbol); } } else { Print ( __FUNCTION__ , ", ERROR: method CheckVolume (" , DoubleToString (check_volume_lot, 2 ), ") " , "< method CheckOpenLong (" + DoubleToString (check_open_long_lot, 2 )+ ")" ); return ; } } else { Print ( __FUNCTION__ , ", ERROR: method CheckVolume returned the value of \"0.0\"" ); return ; } //--- } //+------------------------------------------------------------------+ //| Pending order of Sell Stop | //+------------------------------------------------------------------+ void PendingSellStop( double price, double sl, double tp) { sl=m_symbol.NormalizePrice(sl); tp=m_symbol.NormalizePrice(tp); double check_open_short_lot=m_money.CheckOpenShort(m_symbol.Bid(),sl); Print ( "sl=" , DoubleToString (sl,m_symbol. Digits ()), ", CheckOpenLong: " , DoubleToString (check_open_short_lot, 2 ), ", Balance: " , DoubleToString (m_account.Balance(), 2 ), ", Equity: " , DoubleToString (m_account.Equity(), 2 ), ", FreeMargin: " , DoubleToString (m_account.FreeMargin(), 2 )); if (check_open_short_lot== 0.0 ) { Print ( __FUNCTION__ , ", ERROR: method CheckOpenShort returned the value of \"0.0\"" ); return ; } //--- check volume before OrderSend to avoid "not enough money" error (CTrade) double check_volume_lot=m_trade.CheckVolume(m_symbol.Name(),check_open_short_lot,m_symbol.Bid(), ORDER_TYPE_SELL ); if (check_volume_lot!= 0.0 ) { if (check_volume_lot>=check_open_short_lot) { if (m_trade.SellStop(check_open_short_lot,m_symbol.NormalizePrice(price), m_symbol.Name(),m_symbol.NormalizePrice(sl),m_symbol.NormalizePrice(tp))) { if (m_trade.ResultOrder()== 0 ) { Print ( "#1 Sell Stop -> false. Result Retcode: " ,m_trade.ResultRetcode(), ", description of result: " ,m_trade.ResultRetcodeDescription()); PrintResultTrade(m_trade,m_symbol); } else { Print ( "#2 Sell Stop -> true. Result Retcode: " ,m_trade.ResultRetcode(), ", description of result: " ,m_trade.ResultRetcodeDescription()); PrintResultTrade(m_trade,m_symbol); } } else { Print ( "#3 Sell Stop -> false. Result Retcode: " ,m_trade.ResultRetcode(), ", description of result: " ,m_trade.ResultRetcodeDescription()); PrintResultTrade(m_trade,m_symbol); } } else { Print ( __FUNCTION__ , ", ERROR: method CheckVolume (" , DoubleToString (check_volume_lot, 2 ), ") " , "< method CheckOpenShort (" + DoubleToString (check_open_short_lot, 2 )+ ")" ); return ; } } else { Print ( __FUNCTION__ , ", ERROR: method CheckVolume returned the value of \"0.0\"" ); return ; } //--- } //+------------------------------------------------------------------+ //| Print CTrade result | //+------------------------------------------------------------------+ void PrintResultTrade(CTrade &trade,CSymbolInfo &symbol) { Print ( "Code of request result: " + IntegerToString (trade.ResultRetcode())); Print ( "code of request result as a string: " +trade.ResultRetcodeDescription()); Print ( "Deal ticket: " + IntegerToString (trade.ResultDeal())); Print ( "Order ticket: " + IntegerToString (trade.ResultOrder())); Print ( "Volume of deal or order: " + DoubleToString (trade.ResultVolume(), 2 )); Print ( "Price, confirmed by broker: " + DoubleToString (trade.ResultPrice(),symbol. Digits ())); Print ( "Current bid price: " + DoubleToString (symbol.Bid(),symbol. Digits ())+ " (the requote): " + DoubleToString (trade.ResultBid(),symbol. Digits ())); Print ( "Current ask price: " + DoubleToString (symbol.Ask(),symbol. Digits ())+ " (the requote): " + DoubleToString (trade.ResultAsk(),symbol. Digits ())); Print ( "Broker comment: " +trade.ResultComment()); } //+------------------------------------------------------------------+ //| Is pendinf orders exists | //+------------------------------------------------------------------+ bool IsPendingOrdersExists( void ) { for ( int i= OrdersTotal ()- 1 ;i>= 0 ;i--) // returns the number of current orders if (m_order.SelectByIndex(i)) // selects the pending order by index for further access to its properties if (m_order. Symbol ()==m_symbol.Name() && m_order.Magic()==m_magic) return ( true ); //--- return ( false ); } //+------------------------------------------------------------------+ //| Delete all pending orders | //+------------------------------------------------------------------+ void DeleteAllPendingOrders( void ) { for ( int i= OrdersTotal ()- 1 ;i>= 0 ;i--) // returns the number of current orders if (m_order.SelectByIndex(i)) // selects the pending order by index for further access to its properties if (m_order. Symbol ()==m_symbol.Name() && m_order.Magic()==m_magic) m_trade.OrderDelete(m_order.Ticket()); } //+------------------------------------------------------------------+ //| Trailing | //+------------------------------------------------------------------+ void Trailing() { if (InpTrailingStop== 0 ) return ; for ( int i= PositionsTotal ()- 1 ;i>= 0 ;i--) // returns the number of open positions if (m_position.SelectByIndex(i)) if (m_position. Symbol ()==m_symbol.Name() && m_position.Magic()==m_magic) { if (m_position.PositionType()== POSITION_TYPE_BUY ) { if (m_position.PriceCurrent()-m_position.PriceOpen()>ExtTrailingStop+ExtTrailingStep) if (m_position.StopLoss()<m_position.PriceCurrent()-(ExtTrailingStop+ExtTrailingStep)) { if (!m_trade.PositionModify(m_position.Ticket(), m_symbol.NormalizePrice(m_position.PriceCurrent()-ExtTrailingStop), m_position.TakeProfit())) Print ( "Modify " ,m_position.Ticket(), " Position -> false. Result Retcode: " ,m_trade.ResultRetcode(), ", description of result: " ,m_trade.ResultRetcodeDescription()); RefreshRates(); m_position.SelectByIndex(i); PrintResultModify(m_trade,m_symbol,m_position); continue ; } } else { if (m_position.PriceOpen()-m_position.PriceCurrent()>ExtTrailingStop+ExtTrailingStep) if ((m_position.StopLoss()>(m_position.PriceCurrent()+(ExtTrailingStop+ExtTrailingStep))) || (m_position.StopLoss()== 0 )) { if (!m_trade.PositionModify(m_position.Ticket(), m_symbol.NormalizePrice(m_position.PriceCurrent()+ExtTrailingStop), m_position.TakeProfit())) Print ( "Modify " ,m_position.Ticket(), " Position -> false. Result Retcode: " ,m_trade.ResultRetcode(), ", description of result: " ,m_trade.ResultRetcodeDescription()); RefreshRates(); m_position.SelectByIndex(i); PrintResultModify(m_trade,m_symbol,m_position); } } } } //+------------------------------------------------------------------+ //| Print CTrade result | //+------------------------------------------------------------------+ void PrintResultModify(CTrade &trade,CSymbolInfo &symbol,CPositionInfo &position) { Print ( "Code of request result: " + IntegerToString (trade.ResultRetcode())); Print ( "code of request result as a string: " +trade.ResultRetcodeDescription()); Print ( "Deal ticket: " + IntegerToString (trade.ResultDeal())); Print ( "Order ticket: " + IntegerToString (trade.ResultOrder())); Print ( "Volume of deal or order: " + DoubleToString (trade.ResultVolume(), 2 )); Print ( "Price, confirmed by broker: " + DoubleToString (trade.ResultPrice(),symbol. Digits ())); Print ( "Current bid price: " + DoubleToString (symbol.Bid(),symbol. Digits ())+ " (the requote): " + DoubleToString (trade.ResultBid(),symbol. Digits ())); Print ( "Current ask price: " + DoubleToString (symbol.Ask(),symbol. Digits ())+ " (the requote): " + DoubleToString (trade.ResultAsk(),symbol. Digits ())); Print ( "Broker comment: " +trade.ResultComment()); Print ( "Price of position opening: " + DoubleToString (position.PriceOpen(),symbol. Digits ())); Print ( "Price of position's Stop Loss: " + DoubleToString (position.StopLoss(),symbol. Digits ())); Print ( "Price of position's Take Profit: " + DoubleToString (position.TakeProfit(),symbol. Digits ())); Print ( "Current price by position: " + DoubleToString (position.PriceCurrent(),symbol. Digits ())); } //+------------------------------------------------------------------+ Krasa2lka 2022.04.29 05:51 #14030 下午好,专家们,请帮我了解一下这个错误是什么。通过pinconnector将指标从交易视图连接到mt5模拟账户。 我没有收到任何信号。我附上了一张日志屏幕的截图 附加的文件: qwet4y_oozpaj_2022-04-29_q_8.52.54.png 37 kb 1...139613971398139914001401140214031404140514061407140814091410...1503 新评论 您错过了交易机会: 免费交易应用程序 8,000+信号可供复制 探索金融市场的经济新闻 注册 登录 拉丁字符(不带空格) 密码将被发送至该邮箱 发生错误 使用 Google 登录 您同意网站政策和使用条款 如果您没有帐号,请注册 可以使用cookies登录MQL5.com网站。 请在您的浏览器中启用必要的设置,否则您将无法登录。 忘记您的登录名/密码? 使用 Google 登录
自己尝试真的有那么难吗?
自己尝试真的有那么难吗?
我试过了,很有效。
由于F1内部关于 "switch "的帮助,出现了这样的问题,因为那里只指定了Print。而且我早些时候注意到,void OnTick()中的Comment() 大大降低了代码执行速度。
谁能帮助我如何做到这一点?
请确保您使用的是您的kduarte登录,并且您的MQL5账户密码不超过10个字符。
你没有以任何方式控制ORDERS,你在代码中检查POSITION的数量和类型。此外,你不正确地使用POSITIONS(如果你的账户类型是对冲,使用 "PositionSelect(_Symbol) "是错误的)。你的算法有严重的设计错误。
你应该做什么。
1.只在新条形图诞生的时候工作。如果没有新条形图--什么都不做,不要做不必要的动作。
2.这一项将在第1项完成后披露。
你好,弗拉基米尔。
再次感谢你的提示。我花了一些时间才弄清楚事情。
根据你的提示,我改变了什么。
- 把开仓和平仓的逻辑改成了对冲版本。
- 我现在用一个新柱子的诞生来进行一些计算/下单的工作。
由于这些改变的应用,订单现在只打开和关闭一次。但是现在我在回测时遇到了一个问题。我得到一个错误,即第121行的数组超出了范围。你能给我指出正确的方向吗?我试着用一个双倍类型的数组来做收盘价和copyClose,但当时还是得到错误。
也为第2项感到兴奋 :)
到目前为止的代码。
黄色的部分我突出了第121行。
检查操作的结果。
检查操作的结果。
如何在 EA 细分专家:细分中将风险百分比更改为手数或修复手数。
//+------------------------------------------------------------------+ //| breakdown(barabashkakvn's edition).mq5 | //| Arist0 | //| arist0.rr@gmail.com | //+------------------------------------------------------------------+ #property copyright "Arist0" #property link "arist0.rr@gmail.com" #property version "1.001" //--- #include <Trade\PositionInfo.mqh> #include <Trade\Trade.mqh> #include <Trade\SymbolInfo.mqh> #include <Trade\AccountInfo.mqh> #include <Trade\OrderInfo.mqh> #include <Expert\Money\MoneyFixedMargin.mqh> CPositionInfo m_position; // trade position object CTrade m_trade; // trading object CSymbolInfo m_symbol; // symbol info object CAccountInfo m_account; // account info wrapper COrderInfo m_order; // pending orders object CMoneyFixedMargin *m_money; //--- input parameters input ushort InpStopLoss = 50 ; // Stop Loss (in pips) input ushort InpTakeProfit = 50 ; // Take Profit (in pips) input ushort InpTrailingStop = 5 ; // Trailing Stop (in pips) input ushort InpTrailingStep = 5 ; // Trailing Step (in pips) input ushort InpMinDistance = 25 ; // Minimum distance input double Risk = 5 ; // Risk in percent for a deal from a free margin input ulong m_magic = 585000550 ; // magic number //--- ulong m_slippage= 10 ; // slippage double ExtStopLoss= 0.0 ; double ExtTakeProfit= 0.0 ; double ExtTrailingStop= 0.0 ; double ExtTrailingStep= 0.0 ; double ExtMinDistance= 0.0 ; double m_adjusted_point; // point value adjusted for 3 or 5 points bool bln_delete_all= false ; datetime dt_last_delete= 0 ; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit () { if (InpTrailingStop!= 0 && InpTrailingStep== 0 ) { string text=( TerminalInfoString ( TERMINAL_LANGUAGE )== "Russian" )? "Трейлинг невозможен: параметр \"Trailing Step\" равен нулю!" : "Trailing is not possible: parameter \"Trailing Step\" is zero!" ; Alert ( __FUNCTION__ , " ERROR! " ,text); return ( INIT_PARAMETERS_INCORRECT ); } if (!m_symbol.Name( Symbol ())) // sets symbol name return ( INIT_FAILED ); RefreshRates(); //--- m_trade.SetExpertMagicNumber(m_magic); m_trade.SetMarginMode(); m_trade.SetTypeFillingBySymbol(m_symbol.Name()); m_trade.SetDeviationInPoints(m_slippage); //--- tuning for 3 or 5 digits int digits_adjust= 1 ; if (m_symbol. Digits ()== 3 || m_symbol. Digits ()== 5 ) digits_adjust= 10 ; m_adjusted_point=m_symbol. Point ()*digits_adjust; ExtStopLoss = InpStopLoss * m_adjusted_point; ExtTakeProfit = InpTakeProfit * m_adjusted_point; ExtTrailingStop= InpTrailingStop * m_adjusted_point; ExtTrailingStep= InpTrailingStep * m_adjusted_point; ExtMinDistance = InpMinDistance * m_adjusted_point; //--- if (m_money!= NULL ) delete m_money; m_money= new CMoneyFixedMargin; if (m_money!= NULL ) { if (!m_money.Init( GetPointer (m_symbol), Period (),m_symbol. Point ()*digits_adjust)) return ( INIT_FAILED ); m_money.Percent(Risk); } else { Print ( __FUNCTION__ , ", ERROR: Object CMoneyFixedMargin is NULL" ); return ( INIT_FAILED ); } //--- bln_delete_all= false ; dt_last_delete= 0 ; //--- return ( INIT_SUCCEEDED ); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit ( const int reason) { //--- if (m_money!= NULL ) delete m_money; } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick () { //--- if (bln_delete_all) { if (IsPendingOrdersExists()) { DeleteAllPendingOrders(); //dt_last_delete=iTime(m_symbol.Name(),Period(),0); return ; } else { bln_delete_all= false ; dt_last_delete= iTime (m_symbol.Name(), PERIOD_D1 , 0 ); } } //--- if (IsPendingOrdersExists() && dt_last_delete< iTime (m_symbol.Name(), PERIOD_D1 , 0 )) { bln_delete_all= true ; return ; } //--- if (!IsPendingOrdersExists()) { if (!RefreshRates()) return ; //--- double price= iHigh (m_symbol.Name(), PERIOD_D1 , 1 )+ExtMinDistance; double sl=(InpStopLoss== 0 )? 0.0 :price-ExtStopLoss; double tp=(InpTakeProfit== 0 )? 0.0 :price+ExtTakeProfit; PendingBuyStop(price,sl,tp); //--- price= iLow (m_symbol.Name(), PERIOD_D1 , 1 )-ExtMinDistance; sl=(InpStopLoss== 0 )? 0.0 :price+ExtStopLoss; tp=(InpTakeProfit== 0 )? 0.0 :price-ExtTakeProfit; PendingSellStop(price,sl,tp); dt_last_delete= iTime (m_symbol.Name(), PERIOD_D1 , 0 ); return ; } //--- Trailing(); } //+------------------------------------------------------------------+ //| TradeTransaction function | //+------------------------------------------------------------------+ void OnTradeTransaction ( const MqlTradeTransaction &trans, const MqlTradeRequest &request, const MqlTradeResult &result) { //--- double res= 0.0 ; int losses= 0.0 ; //--- get transaction type as enumeration value ENUM_TRADE_TRANSACTION_TYPE type=trans.type; //--- if transaction is result of addition of the transaction in history if (type== TRADE_TRANSACTION_DEAL_ADD ) { long deal_ticket = 0 ; long deal_order = 0 ; long deal_time = 0 ; long deal_time_msc = 0 ; long deal_type =- 1 ; long deal_entry =- 1 ; long deal_magic = 0 ; long deal_reason =- 1 ; long deal_position_id = 0 ; double deal_volume = 0.0 ; double deal_price = 0.0 ; double deal_commission = 0.0 ; double deal_swap = 0.0 ; double deal_profit = 0.0 ; string deal_symbol = "" ; string deal_comment = "" ; string deal_external_id = "" ; if ( HistoryDealSelect (trans.deal)) { deal_ticket = HistoryDealGetInteger (trans.deal, DEAL_TICKET ); deal_order = HistoryDealGetInteger (trans.deal, DEAL_ORDER ); deal_time = HistoryDealGetInteger (trans.deal, DEAL_TIME ); deal_time_msc = HistoryDealGetInteger (trans.deal, DEAL_TIME_MSC ); deal_type = HistoryDealGetInteger (trans.deal, DEAL_TYPE ); deal_entry = HistoryDealGetInteger (trans.deal, DEAL_ENTRY ); deal_magic = HistoryDealGetInteger (trans.deal, DEAL_MAGIC ); deal_reason = HistoryDealGetInteger (trans.deal, DEAL_REASON ); deal_position_id = HistoryDealGetInteger (trans.deal, DEAL_POSITION_ID ); deal_volume = HistoryDealGetDouble (trans.deal, DEAL_VOLUME ); deal_price = HistoryDealGetDouble (trans.deal, DEAL_PRICE ); deal_commission = HistoryDealGetDouble (trans.deal, DEAL_COMMISSION ); deal_swap = HistoryDealGetDouble (trans.deal, DEAL_SWAP ); deal_profit = HistoryDealGetDouble (trans.deal, DEAL_PROFIT ); deal_symbol = HistoryDealGetString (trans.deal, DEAL_SYMBOL ); deal_comment = HistoryDealGetString (trans.deal, DEAL_COMMENT ); deal_external_id = HistoryDealGetString (trans.deal, DEAL_EXTERNAL_ID ); } else return ; if (deal_symbol==m_symbol.Name() && deal_magic==m_magic) if (deal_entry== DEAL_ENTRY_IN ) if (deal_type== DEAL_TYPE_BUY || deal_type== DEAL_TYPE_SELL ) DeleteAllPendingOrders(); } } //+------------------------------------------------------------------+ //| Refreshes the symbol quotes data | //+------------------------------------------------------------------+ bool RefreshRates( void ) { //--- refresh rates if (!m_symbol.RefreshRates()) { Print ( "RefreshRates error" ); return ( false ); } //--- protection against the return value of "zero" if (m_symbol.Ask()== 0 || m_symbol.Bid()== 0 ) return ( false ); //--- return ( true ); } //+------------------------------------------------------------------+ //| Pending order of Buy Stop | //+------------------------------------------------------------------+ void PendingBuyStop( double price, double sl, double tp) { sl=m_symbol.NormalizePrice(sl); tp=m_symbol.NormalizePrice(tp); double check_open_long_lot=m_money.CheckOpenLong(m_symbol.Ask(),sl); Print ( "sl=" , DoubleToString (sl,m_symbol. Digits ()), ", CheckOpenLong: " , DoubleToString (check_open_long_lot, 2 ), ", Balance: " , DoubleToString (m_account.Balance(), 2 ), ", Equity: " , DoubleToString (m_account.Equity(), 2 ), ", FreeMargin: " , DoubleToString (m_account.FreeMargin(), 2 )); if (check_open_long_lot== 0.0 ) { Print ( __FUNCTION__ , ", ERROR: method CheckOpenLong returned the value of \"0.0\"" ); return ; } //--- check volume before OrderSend to avoid "not enough money" error (CTrade) double check_volume_lot=m_trade.CheckVolume(m_symbol.Name(),check_open_long_lot,m_symbol.Ask(), ORDER_TYPE_BUY ); if (check_volume_lot!= 0.0 ) { if (check_volume_lot>=check_open_long_lot) { if (m_trade.BuyStop(check_open_long_lot,m_symbol.NormalizePrice(price), m_symbol.Name(),m_symbol.NormalizePrice(sl),m_symbol.NormalizePrice(tp))) { if (m_trade.ResultOrder()== 0 ) { Print ( "#1 Buy Stop -> false. Result Retcode: " ,m_trade.ResultRetcode(), ", description of result: " ,m_trade.ResultRetcodeDescription()); PrintResultTrade(m_trade,m_symbol); } else { Print ( "#2 Buy Stop -> true. Result Retcode: " ,m_trade.ResultRetcode(), ", description of result: " ,m_trade.ResultRetcodeDescription()); PrintResultTrade(m_trade,m_symbol); } } else { Print ( "#3 Buy Stop -> false. Result Retcode: " ,m_trade.ResultRetcode(), ", description of result: " ,m_trade.ResultRetcodeDescription()); PrintResultTrade(m_trade,m_symbol); } } else { Print ( __FUNCTION__ , ", ERROR: method CheckVolume (" , DoubleToString (check_volume_lot, 2 ), ") " , "< method CheckOpenLong (" + DoubleToString (check_open_long_lot, 2 )+ ")" ); return ; } } else { Print ( __FUNCTION__ , ", ERROR: method CheckVolume returned the value of \"0.0\"" ); return ; } //--- } //+------------------------------------------------------------------+ //| Pending order of Sell Stop | //+------------------------------------------------------------------+ void PendingSellStop( double price, double sl, double tp) { sl=m_symbol.NormalizePrice(sl); tp=m_symbol.NormalizePrice(tp); double check_open_short_lot=m_money.CheckOpenShort(m_symbol.Bid(),sl); Print ( "sl=" , DoubleToString (sl,m_symbol. Digits ()), ", CheckOpenLong: " , DoubleToString (check_open_short_lot, 2 ), ", Balance: " , DoubleToString (m_account.Balance(), 2 ), ", Equity: " , DoubleToString (m_account.Equity(), 2 ), ", FreeMargin: " , DoubleToString (m_account.FreeMargin(), 2 )); if (check_open_short_lot== 0.0 ) { Print ( __FUNCTION__ , ", ERROR: method CheckOpenShort returned the value of \"0.0\"" ); return ; } //--- check volume before OrderSend to avoid "not enough money" error (CTrade) double check_volume_lot=m_trade.CheckVolume(m_symbol.Name(),check_open_short_lot,m_symbol.Bid(), ORDER_TYPE_SELL ); if (check_volume_lot!= 0.0 ) { if (check_volume_lot>=check_open_short_lot) { if (m_trade.SellStop(check_open_short_lot,m_symbol.NormalizePrice(price), m_symbol.Name(),m_symbol.NormalizePrice(sl),m_symbol.NormalizePrice(tp))) { if (m_trade.ResultOrder()== 0 ) { Print ( "#1 Sell Stop -> false. Result Retcode: " ,m_trade.ResultRetcode(), ", description of result: " ,m_trade.ResultRetcodeDescription()); PrintResultTrade(m_trade,m_symbol); } else { Print ( "#2 Sell Stop -> true. Result Retcode: " ,m_trade.ResultRetcode(), ", description of result: " ,m_trade.ResultRetcodeDescription()); PrintResultTrade(m_trade,m_symbol); } } else { Print ( "#3 Sell Stop -> false. Result Retcode: " ,m_trade.ResultRetcode(), ", description of result: " ,m_trade.ResultRetcodeDescription()); PrintResultTrade(m_trade,m_symbol); } } else { Print ( __FUNCTION__ , ", ERROR: method CheckVolume (" , DoubleToString (check_volume_lot, 2 ), ") " , "< method CheckOpenShort (" + DoubleToString (check_open_short_lot, 2 )+ ")" ); return ; } } else { Print ( __FUNCTION__ , ", ERROR: method CheckVolume returned the value of \"0.0\"" ); return ; } //--- } //+------------------------------------------------------------------+ //| Print CTrade result | //+------------------------------------------------------------------+ void PrintResultTrade(CTrade &trade,CSymbolInfo &symbol) { Print ( "Code of request result: " + IntegerToString (trade.ResultRetcode())); Print ( "code of request result as a string: " +trade.ResultRetcodeDescription()); Print ( "Deal ticket: " + IntegerToString (trade.ResultDeal())); Print ( "Order ticket: " + IntegerToString (trade.ResultOrder())); Print ( "Volume of deal or order: " + DoubleToString (trade.ResultVolume(), 2 )); Print ( "Price, confirmed by broker: " + DoubleToString (trade.ResultPrice(),symbol. Digits ())); Print ( "Current bid price: " + DoubleToString (symbol.Bid(),symbol. Digits ())+ " (the requote): " + DoubleToString (trade.ResultBid(),symbol. Digits ())); Print ( "Current ask price: " + DoubleToString (symbol.Ask(),symbol. Digits ())+ " (the requote): " + DoubleToString (trade.ResultAsk(),symbol. Digits ())); Print ( "Broker comment: " +trade.ResultComment()); } //+------------------------------------------------------------------+ //| Is pendinf orders exists | //+------------------------------------------------------------------+ bool IsPendingOrdersExists( void ) { for ( int i= OrdersTotal ()- 1 ;i>= 0 ;i--) // returns the number of current orders if (m_order.SelectByIndex(i)) // selects the pending order by index for further access to its properties if (m_order. Symbol ()==m_symbol.Name() && m_order.Magic()==m_magic) return ( true ); //--- return ( false ); } //+------------------------------------------------------------------+ //| Delete all pending orders | //+------------------------------------------------------------------+ void DeleteAllPendingOrders( void ) { for ( int i= OrdersTotal ()- 1 ;i>= 0 ;i--) // returns the number of current orders if (m_order.SelectByIndex(i)) // selects the pending order by index for further access to its properties if (m_order. Symbol ()==m_symbol.Name() && m_order.Magic()==m_magic) m_trade.OrderDelete(m_order.Ticket()); } //+------------------------------------------------------------------+ //| Trailing | //+------------------------------------------------------------------+ void Trailing() { if (InpTrailingStop== 0 ) return ; for ( int i= PositionsTotal ()- 1 ;i>= 0 ;i--) // returns the number of open positions if (m_position.SelectByIndex(i)) if (m_position. Symbol ()==m_symbol.Name() && m_position.Magic()==m_magic) { if (m_position.PositionType()== POSITION_TYPE_BUY ) { if (m_position.PriceCurrent()-m_position.PriceOpen()>ExtTrailingStop+ExtTrailingStep) if (m_position.StopLoss()<m_position.PriceCurrent()-(ExtTrailingStop+ExtTrailingStep)) { if (!m_trade.PositionModify(m_position.Ticket(), m_symbol.NormalizePrice(m_position.PriceCurrent()-ExtTrailingStop), m_position.TakeProfit())) Print ( "Modify " ,m_position.Ticket(), " Position -> false. Result Retcode: " ,m_trade.ResultRetcode(), ", description of result: " ,m_trade.ResultRetcodeDescription()); RefreshRates(); m_position.SelectByIndex(i); PrintResultModify(m_trade,m_symbol,m_position); continue ; } } else { if (m_position.PriceOpen()-m_position.PriceCurrent()>ExtTrailingStop+ExtTrailingStep) if ((m_position.StopLoss()>(m_position.PriceCurrent()+(ExtTrailingStop+ExtTrailingStep))) || (m_position.StopLoss()== 0 )) { if (!m_trade.PositionModify(m_position.Ticket(), m_symbol.NormalizePrice(m_position.PriceCurrent()+ExtTrailingStop), m_position.TakeProfit())) Print ( "Modify " ,m_position.Ticket(), " Position -> false. Result Retcode: " ,m_trade.ResultRetcode(), ", description of result: " ,m_trade.ResultRetcodeDescription()); RefreshRates(); m_position.SelectByIndex(i); PrintResultModify(m_trade,m_symbol,m_position); } } } } //+------------------------------------------------------------------+ //| Print CTrade result | //+------------------------------------------------------------------+ void PrintResultModify(CTrade &trade,CSymbolInfo &symbol,CPositionInfo &position) { Print ( "Code of request result: " + IntegerToString (trade.ResultRetcode())); Print ( "code of request result as a string: " +trade.ResultRetcodeDescription()); Print ( "Deal ticket: " + IntegerToString (trade.ResultDeal())); Print ( "Order ticket: " + IntegerToString (trade.ResultOrder())); Print ( "Volume of deal or order: " + DoubleToString (trade.ResultVolume(), 2 )); Print ( "Price, confirmed by broker: " + DoubleToString (trade.ResultPrice(),symbol. Digits ())); Print ( "Current bid price: " + DoubleToString (symbol.Bid(),symbol. Digits ())+ " (the requote): " + DoubleToString (trade.ResultBid(),symbol. Digits ())); Print ( "Current ask price: " + DoubleToString (symbol.Ask(),symbol. Digits ())+ " (the requote): " + DoubleToString (trade.ResultAsk(),symbol. Digits ())); Print ( "Broker comment: " +trade.ResultComment()); Print ( "Price of position opening: " + DoubleToString (position.PriceOpen(),symbol. Digits ())); Print ( "Price of position's Stop Loss: " + DoubleToString (position.StopLoss(),symbol. Digits ())); Print ( "Price of position's Take Profit: " + DoubleToString (position.TakeProfit(),symbol. Digits ())); Print ( "Current price by position: " + DoubleToString (position.PriceCurrent(),symbol. Digits ())); } //+------------------------------------------------------------------+
下午好,专家们,请帮我了解一下这个错误是什么。通过pinconnector将指标从交易视图连接到mt5模拟账户。 我没有收到任何信号。我附上了一张日志屏幕的截图