//+------------------------------------------------------------------+ //| (ModifyOrder).mq4 | //| Copyright © 2005, komposter | //| mailto:komposterius@mail.ru | //+------------------------------------------------------------------+ #property copyright "Copyright © 2005, komposter" #property link "mailto:komposterius@mail.ru" /* -------------------------------------------------------------- Before running the script is strongly encouraged to consider the following guidance: The script is designed to update / delete / close position / pending order. This requires: 1) Be familiar with the leadership =)! Default! (under the description, start and finish line //+------------------------------------------- --- +), allow the import of external experts from the menu "Tools" -> "Options" -> "advisers" -> "Allow import of external experts" (you need to describe the errors that may occur when you update a warrant) 2) Drag a script on a schedule close to the order / position, which (th) should be modified. 3) Move all the lines at the appropriate levels: - Open_Price_Line (default - white) - the price of opening (ONLY Pending order) - Stop_Loss_Line (red) - The Stop Loss - Take_Profit_Line (green) - the level Teyk Profit - Expiration_Line (yellow) - the time of expiry (ONLY Pending order) To remove the Stop Loss / Take Profit / expiration time - simply delete the appropriate line. To delete a pending order / close position - remove the line Open_Price_Line. 4) When everything is ready, in the window that appears, click "OK." For the termination of the script at any time, you can click "Cancel." If you will find a bug in the code, or the logic of the script, please report to the komposterius@mail.ru */ #include int first = 1; int start() { //+------------------------------------------------------------------+ // Maximum distance of points from the place, which was "released" script before // Opening price of the warrant.For makimalnoy accuracy, use "0" // Then select only the order in the case of a precise hit. int Order_Find_Radius = 100; // Distance between the line Take_Profit / Stop_Loss and in line Open_Price points by default. // If Take_Profit / Stop_Loss will not be used, set 0 int Take_Profit = 300; int Stop_Loss = 300; // The maximum deviation from the requested price (for closing the position) int Slippage = 5; // Time expiry of the warrant, as expressed in the candlelight // For the period of the schedule and H4 Expiration_Shift = 3 times the end will come 12 hours after installation // If it is necessary to standard time for the expiration of all periods of the schedule, enter "0" (without the quotes), and proceed to the next setting // If the time of the expiration of the warrant will not be used, set 0 int Expiration_Shift = 0; // Time expiry of the warrant, expressed in hours // To use this option, you must install Expiration_Shift (see above on 2 lines) "0" (no quotes) // If the time of the expiration of the warrant will not be used, set 0 int Expiration_Shift_H = 0; // Color display orders on schedule color Buy_Color = Lime; //(for orders BUYSTOP and BUYLIMIT) color Sell_Color = Red; //(for orders SELLLIMIT and SELLSTOP) // Color Line: color Open_Price_Line_Color = White; color Stop_Loss_Line_Color = Red; color Take_Profit_Line_Color = Lime; color Expiration_Line_Color = Yellow; //+------------------------------------------------------------------+ double Open_Price_Level, Stop_Loss_Level, Take_Profit_Level; datetime Expiration_Time; int _break, error; double DropPrice = PriceOnDropped(); // Search warrant for ( int x = 0; x <= Order_Find_Radius; x ++ ) { int _OrdersTotal = OrdersTotal(); for ( int z = _OrdersTotal - 1; z >= 0; z -- ) { OrderSelect( z, SELECT_BY_POS, MODE_TRADES ); if ( OrderSymbol() == Symbol() ) { if ( ( DropPrice - OrderOpenPrice() )/Point <= x && ( DropPrice - OrderOpenPrice() )/Point >= 0 ) { _break = 1; break; } if ( ( OrderOpenPrice() - DropPrice )/Point <= x && ( OrderOpenPrice() - DropPrice )/Point >= 0 ) { _break = 1; break; } } } if ( _break == 1 ) { break; } } if ( _break != 1 ) { MessageBox(" Could not find a warrant! \n\n" + "Move the script on a schedule close to the opening price of the warrant, who want to modify", "Getting Started",0x00000000 | 0x00000010 | 0x00040000 ); return(0); } int _OrderType = OrderType(); int _OrderTicket = OrderTicket(); double _OrderOpenPrice = OrderOpenPrice(); double _OrderStopLoss = OrderStopLoss(); double _OrderTakeProfit = OrderTakeProfit(); datetime _OrderExpiration = OrderExpiration(); // Set the initial values: Open_Price_Level = _OrderOpenPrice; if ( _OrderStopLoss > 0 ) { Stop_Loss_Level = _OrderStopLoss; } else { if ( Stop_Loss > 0 ) { if ( _OrderType == OP_BUY || _OrderType == OP_BUYSTOP || _OrderType == OP_BUYLIMIT ) { Stop_Loss_Level = Open_Price_Level - Stop_Loss*Point; } else { Stop_Loss_Level = Open_Price_Level + Stop_Loss*Point; } } } if ( _OrderTakeProfit > 0 ) { Take_Profit_Level = _OrderTakeProfit; } else { if ( Take_Profit > 0 ) { if ( _OrderType == OP_BUY || _OrderType == OP_BUYSTOP || _OrderType == OP_BUYLIMIT ) { Take_Profit_Level = Open_Price_Level + Take_Profit*Point; } else { Take_Profit_Level = Open_Price_Level - Take_Profit*Point; } } } if ( _OrderExpiration > 0 ) { Expiration_Time = _OrderExpiration; } else { if ( _OrderType != OP_BUY && _OrderType != OP_SELL ) { if ( Expiration_Shift > 0 ) { Expiration_Time = CurTime() + Period()*60*Expiration_Shift; } else { if ( Expiration_Shift_H > 0 ) { Expiration_Time = CurTime() + 3600*Expiration_Shift_H; } } } } // Create the lines: if ( first == 1 ) { ObjectCreate( "Open_Price_Line", OBJ_HLINE, 0, 0, Open_Price_Level, 0, 0, 0, 0 ); ObjectSet( "Open_Price_Line", OBJPROP_COLOR, Open_Price_Line_Color ); ObjectSetText( "Open_Price_Line", "Open_Price_Line", 6, "Arial", Open_Price_Line_Color ); if ( Stop_Loss_Level > 0 ) { ObjectCreate( "Stop_Loss_Line", OBJ_HLINE, 0, 0, Stop_Loss_Level, 0, 0, 0, 0 ); ObjectSet( "Stop_Loss_Line", OBJPROP_COLOR, Stop_Loss_Line_Color ); ObjectSetText( "Stop_Loss_Line", "Stop_Loss_Line", 6, "Arial", Stop_Loss_Line_Color ); } if ( Take_Profit_Level > 0 ) { ObjectCreate( "Take_Profit_Line", OBJ_HLINE, 0, 0, Take_Profit_Level, 0, 0, 0, 0 ); ObjectSet( "Take_Profit_Line", OBJPROP_COLOR, Take_Profit_Line_Color ); ObjectSetText( "Take_Profit_Line", "Take_Profit_Line", 6, "Arial", Take_Profit_Line_Color ); } if ( Expiration_Time > 0 ) { ObjectCreate( "Expiration_Line", OBJ_VLINE, 0, Expiration_Time, 0, 0, 0, 0, 0 ); ObjectSet( "Expiration_Line", OBJPROP_COLOR, Expiration_Line_Color ); ObjectSetText( "Expiration_Line", "Expiration_Line", 6, "Arial", Expiration_Line_Color ); } // Output messedzhboksa string Question = "To make changes to move the line at the appropriate level and click \" OK \".\n" + "To abandon the update and complete the work, click 'Cancel'."; int Answer = MessageBox( Question, "Modification of order", 0x00000001 | 0x00000040 | 0x00040000); first = 0; // If pressed, any other than "OK" button - go if ( Answer != 1 ) { deinit(); return(0); } } // Reads the value of facilities and normalize: Open_Price_Level = NormalizeDouble( ObjectGet( "Open_Price_Line", OBJPROP_PRICE1 ), MarketInfo( Symbol(), MODE_DIGITS ) ); Stop_Loss_Level = NormalizeDouble( ObjectGet( "Stop_Loss_Line", OBJPROP_PRICE1 ), MarketInfo( Symbol(), MODE_DIGITS ) ); Take_Profit_Level = NormalizeDouble( ObjectGet( "Take_Profit_Line", OBJPROP_PRICE1 ), MarketInfo( Symbol(), MODE_DIGITS ) ); Expiration_Time = ObjectGet( "Expiration_Line", OBJPROP_TIME1 ); if ( Open_Price_Level == 0 ) { int order; if ( _OrderType == OP_BUY || _OrderType == OP_SELL ) { if ( _OrderType == OP_BUY ) { order = OrderClose( _OrderTicket, OrderLots(), Bid, Slippage, Buy_Color ); } else { order = OrderClose( _OrderTicket, OrderLots(), Ask, Slippage, Sell_Color ); } } else { order = OrderDelete( _OrderTicket ); } if ( order > 0 ) { // If everything is OK, display the log and exit Print( "Order No: ", _OrderTicket, "closed / deleted successfully!"); return(0); } else { // If the error - display a message and exit error = GetLastError(); Print("Error closing / removing! GetLastError =", error, ", ErrorDescription = \" ", ErrorDescription( error ), "\"" ); MessageBox( "Error closing / removing! GetLastError =" + error + ", ErrorDescription = \"" + ErrorDescription( error ) + "\"", "Error closing / removal orders", 0x00000000 | 0x00000010 | 0x00040000 ); return(-1); } return(0); } color _Color = Buy_Color; // Check all the values if ( _OrderType == OP_BUY ) { if ( Bid - Stop_Loss_Level < MarketInfo( Symbol(), MODE_STOPLEVEL )*Point && Stop_Loss_Level != 0 ) { Answer = MessageBox( "Incorrect set Stop_Loss_Line (red line)! \n" + "\n" + "To Buy - the position it should be lower than the Bid. \n" + "The minimum indentation (" + Symbol() + ") - " + DoubleToStr( MarketInfo( Symbol(), MODE_STOPLEVEL ), 0 ) + " items. \n" + "\n\n" + "To start from the beginning of the modification, press the 'Retry \". \n" + "To abandon the update, click \"Cancel\" :" , "Modification orders", 0x00000005 | 0x00000030 | 0x00040000 ); if ( Answer == 4 ) { start(); } deinit(); return(-1); } if ( Take_Profit_Level - Bid < MarketInfo( Symbol(), MODE_STOPLEVEL )*Point && Take_Profit_Level != 0 ) { Answer = MessageBox( "Incorrect set Take_Profit_Line (green line)! \n" + "\n" + "To Buy - the position it should be higher than the Bid. \n" + "The minimum indentation (" + Symbol() + ") - " + DoubleToStr( MarketInfo( Symbol(), MODE_STOPLEVEL ), 0 ) + "items. \n" + "\n\n" + "To start from the beginning of the modification, press the 'Retry \". \n" + "To abandon the update, click 'Cancel'.", "Modification orders", 0x00000005 | 0x00000030 | 0x00040000 ); if ( Answer == 4 ) { start(); } deinit(); return(-1); } } if ( _OrderType == OP_BUYSTOP || _OrderType == OP_BUYLIMIT ) { if ( Open_Price_Level - Stop_Loss_Level < MarketInfo( Symbol(), MODE_STOPLEVEL )*Point && Stop_Loss_Level != 0 ) { Answer = MessageBox( "Incorrect set Stop_Loss_Line (red line)! \n" + "\n" + "To Buy, BuyLimit and BuyStop - orders must be below the line Open_Price_Line. \n" + "The minimum indentation (" + Symbol () + ") -" + DoubleToStr (MarketInfo (Symbol (), MODE_STOPLEVEL), 0) + "items. \n" + "\n\n" + "To start from the beginning of the modification, press the 'Retry \". \n" + "To abandon the update, click 'Cancel'.", "Modification orders", 0x00000005 | 0x00000030 | 0x00040000 ); if ( Answer == 4 ) { start(); } deinit(); return(-1); } if ( Take_Profit_Level - Open_Price_Level < MarketInfo( Symbol(), MODE_STOPLEVEL )*Point && Take_Profit_Level != 0 ) { Answer = MessageBox( "Incorrect set Take_Profit_Line (green line)! \n" + "\n" + "To Buy, BuyLimit and BuyStop - orders must be above the line Open_Price_Line. \n" + "The minimum indentation (" + Symbol () + ") -" + DoubleToStr (MarketInfo (Symbol (), MODE_STOPLEVEL), 0) + "items. \n" + "\n\n" + "To start from the beginning of the modification, press the 'Retry \". \n" + "To abandon the update, click 'Cancel'.", "Modification orders", 0x00000005 | 0x00000030 | 0x00040000 ); if ( Answer == 4 ) { start(); } deinit(); return(-1); } if ( _OrderType == OP_BUYSTOP ) { if ( Open_Price_Level - Bid < MarketInfo( Symbol(), MODE_STOPLEVEL )*Point ) { Answer = MessageBox( "Incorrect set Open_Price_Line (white line)! \n" + "\n" + "For BuyStop - order it should be above the current price. \n" + "The minimum indentation (" + Symbol () + ") -" + DoubleToStr (MarketInfo (Symbol (), MODE_STOPLEVEL), 0) + "items. \n" + "\n\n" + "To start from the beginning of the modification, press the 'Retry \". \n" + "To abandon the update, click 'Cancel'.", "Modification orders", 0x00000005 | 0x00000030 | 0x00040000 ); if ( Answer == 4 ) { start(); } deinit(); return(-1); } } if ( _OrderType == OP_BUYLIMIT ) { if ( Bid - Open_Price_Level < MarketInfo( Symbol(), MODE_STOPLEVEL )*Point ) { Answer = MessageBox( "Incorrect set Open_Price_Line (white line)! \n" + "\n" + "For BuyLimit - order it should be lower than current prices. \n" + "The minimum indentation (" + Symbol () + ") -" + DoubleToStr (MarketInfo (Symbol (), MODE_STOPLEVEL), 0) + "items. \n" + "\n\n" + "To start from the beginning of the modification, press the 'Retry \". \n" + "To abandon the update, click 'Cancel'.", "Modification orders", 0x00000005 | 0x00000030 | 0x00040000 ); if ( Answer == 4 ) { start(); } deinit(); return(-1); } } } if ( _OrderType == OP_SELL ) { _Color = Sell_Color; if ( Stop_Loss_Level - Ask < MarketInfo( Symbol(), MODE_STOPLEVEL )*Point && Stop_Loss_Level != 0 ) { Answer = MessageBox( "Incorrect set Stop_Loss_Line (red line)! \n" + "\n" + "To Sell - the position it should be higher than Ask. \n" + "The minimum indentation (" + Symbol () + ") -" + DoubleToStr (MarketInfo (Symbol (), MODE_STOPLEVEL), 0) + "items. \n" + "\n\n" + "To start from the beginning of the modification, press the 'Retry \". \n" + "To abandon the update, click 'Cancel'.", "Modification orders", 0x00000005 | 0x00000030 | 0x00040000 ); if ( Answer == 4 ) { start(); } deinit(); return(-1); } if ( Ask - Take_Profit_Level < MarketInfo( Symbol(), MODE_STOPLEVEL )*Point && Take_Profit_Level != 0 ) { Answer = MessageBox( "Incorrect set Take_Profit_Line (green line)! \n" + "\n" + "To Sell - the position it should be lower than the Ask. \n" + "The minimum indentation (" + Symbol () + ") -" + DoubleToStr (MarketInfo (Symbol (), MODE_STOPLEVEL), 0) + "items. \n" + "\n\n" + "To start from the beginning of the modification, press the 'Retry \". \n" + "To abandon the update, click 'Cancel'.", "Modification orders", 0x00000005 | 0x00000030 | 0x00040000 ); if ( Answer == 4 ) { start(); } deinit(); return(-1); } } if ( _OrderType == OP_SELLLIMIT || _OrderType == OP_SELLSTOP ) { _Color = Sell_Color; if ( Stop_Loss_Level - Open_Price_Level < MarketInfo( Symbol(), MODE_STOPLEVEL )*Point && Stop_Loss_Level != 0 ) { Answer = MessageBox( "Incorrect set Stop_Loss_Line (red line)! \n" + "\n" + "To Sell, SellLimit and SellStop - orders must be above the line Open_Price_Line. \n" + "The minimum indentation (" + Symbol () + ") -" + DoubleToStr (MarketInfo (Symbol (), MODE_STOPLEVEL), 0) + "items. \n" + "\n\n" + "To start from the beginning of the modification, press the 'Retry \". \n" + "To abandon the update, click 'Cancel'.", "Modification orders", 0x00000005 | 0x00000030 | 0x00040000); if ( Answer == 4 ) { start(); } deinit(); return(-1); } if ( Open_Price_Level - Take_Profit_Level < MarketInfo( Symbol(), MODE_STOPLEVEL )*Point && Take_Profit_Level != 0 ) { Answer = MessageBox( "Incorrect set Take_Profit_Line (green line)! \n" + "\n" + "To Sell, SellLimit and SellStop - orders must be below the line Open_Price_Line. \n" + "The minimum indentation (" + Symbol () + ") -" + DoubleToStr (MarketInfo (Symbol (), MODE_STOPLEVEL), 0) + "items. \n" + "\n\n" + "To start from the beginning of the modification, press the 'Retry \". \n" + "To abandon the update, click 'Cancel'.", "Modification orders", 0x00000005 | 0x00000030 | 0x00040000); if ( Answer == 4 ) { start(); } deinit(); return(-1); } if ( _OrderType == OP_SELLLIMIT ) { if ( Open_Price_Level - Ask < MarketInfo( Symbol(), MODE_STOPLEVEL )*Point ) { Answer = MessageBox( "Incorrect set Open_Price_Line (white line)! \n" + "\n" + "For SellLimit - order it should be lower than current prices. \n" + "The minimum indentation (" + Symbol () + ") -" + DoubleToStr (MarketInfo (Symbol (), MODE_STOPLEVEL), 0) + "items. \n" + "\n\n" + "To start from the beginning of the modification, press the 'Retry \". \n" + "To abandon the update, click 'Cancel'.", "Modification orders", 0x00000005 | 0x00000030 | 0x00040000); if ( Answer == 4 ) { start(); } deinit(); return(-1); } } if ( _OrderType == OP_SELLSTOP ) { if ( Ask - Open_Price_Level < MarketInfo( Symbol(), MODE_STOPLEVEL )*Point ) { Answer = MessageBox( "Incorrect set Open_Price_Line (white line)! \n" + "\n" + "For SellStop - order it should be above the current price. \n" + "The minimum indentation (" + Symbol () + ") -" + DoubleToStr (MarketInfo (Symbol (), MODE_STOPLEVEL), 0) + "items. \n" + "\n\n" + "To start from the beginning of the modification, press the 'Retry \". \n" + "To abandon the update, click 'Cancel'.", "Modification orders", 0x00000005 | 0x00000030 | 0x00040000); if ( Answer == 4 ) { start(); } deinit(); return(-1); } } } if ( _OrderType == OP_BUY || _OrderType == OP_SELL ) { if ( Open_Price_Level != _OrderOpenPrice ) { Answer = MessageBox( "You can not move Open_Price_Line (white line) to have open positions! \n" + "\n\n" + "To set the line in the initial position and start from the beginning of the modification, press the 'Retry \". \n" + "To abandon the update, click 'Cancel'.", "Modification orders", 0x00000005 | 0x00000030 | 0x00040000); if ( Answer == 4 ) { ObjectSet( "Open_Price_Line", OBJPROP_PRICE1, _OrderOpenPrice ); start(); } deinit(); Print (_OrderOpenPrice); return(-1); } if ( Expiration_Time != 0 ) { Answer = MessageBox( "You can not install Expiration_Line (yellow line) to have open positions! \n" + "\n\n" + "To remove the line and start from the beginning of the modification, press the 'Retry \". \n" + "To abandon the update, click 'Cancel'.", "Modification orders", 0x00000005 | 0x00000030 | 0x00040000); if ( Answer == 4 ) { ObjectDelete( "Expiration_Line" ); start(); } deinit(); return(-1); } } else { if ( Expiration_Time <= CurTime() && Expiration_Time != 0 ) { Answer = MessageBox( "Incorrect set Expiration_Line (yellow line)! \n" + "\n" + "The expiry date of warrants may not be the last time! \n" + "\n\n" + "To start from the beginning of the modification, press the 'Retry \". \n" + "To abandon the update, click 'Cancel'.", "Modification orders", 0x00000005 | 0x00000030 | 0x00040000); if ( Answer == 4 ) { start(); } deinit(); return(-1); } } // Display §Ú§ß§æ§å of the request and try to modify the o Print( "OrderTicket=",_OrderTicket, ",_OrderType=",_OrderType, ",Open_Price_Level=",Open_Price_Level, ",Stop_Loss_Level=", Stop_Loss_Level, ",Take_Profit_Level=", Take_Profit_Level, ",Expiration_Time=", Expiration_Time, ",_Color=", _Color ); int ordermodify = OrderModify( _OrderTicket, Open_Price_Level, Stop_Loss_Level, Take_Profit_Level, Expiration_Time, _Color ); if ( ordermodify > 0 ) { // If everything is OK, display the log and exit OrderPrint(); Print( "Order No£º", _OrderTicket, "modified successfully!"); return(0); } // If the error - display a message and exit error = GetLastError(); Print("Failed to update! GetLastError =", error, ", ErrorDescription = \"", ErrorDescription( error ), "\"" ); MessageBox( "Failed to update! GetLastError = " + error + ", ErrorDescription = \"" + ErrorDescription( error ) + "\"", "Failure to update the warrant", 0x00000000 | 0x00000010 | 0x00040000 ); return(-1); } int deinit() { // Remove all objects created by script ObjectDelete( "Open_Price_Line" ); ObjectDelete( "Stop_Loss_Line" ); ObjectDelete( "Take_Profit_Line" ); ObjectDelete( "Expiration_Line" ); return(0); }