#define EXPERT_MAGIC 123456
#define OFFSET 50 // separación desde el precio actual para establecer la orden, en puntos
#define DIRECTION ORDER_TYPE_BUY_LIMIT // tipo de orden
#define VOLUME 1.0 // volumen
#define DEVIATION 2 // desviación permitida respecto al precio
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//-- declaración e inicialización de la consulta, el resultado y las variables
MqlTradeRequest request={};
MqlTradeResult result ={};
double order_price=0;
//--- parámetros de colocación de una orden pendiente
request.action = TRADE_ACTION_PENDING; // tipo de operación comercial
request.symbol = _Symbol; // símbolo
request.volume = VOLUME; // volumen
request.deviation = DEVIATION; // desviación permitida respecto al precio
request.magic = EXPERT_MAGIC; // MagicNumber de la orden
//--- comprobación del tipo de operación
switch(DIRECTION)
{
case ORDER_TYPE_BUY_LIMIT :
request.type = ORDER_TYPE_BUY_LIMIT; // tipo de orden
order_price = SymbolInfoDouble(_Symbol, SYMBOL_ASK)-OFFSET*_Point;// precio de apertura
request.price= NormalizeDouble(order_price, _Digits); // precio normalizado de apertura
break;
case ORDER_TYPE_SELL_LIMIT :
request.type = ORDER_TYPE_SELL_LIMIT; // tipo de orden
order_price = SymbolInfoDouble(_Symbol, SYMBOL_BID)+OFFSET*_Point;// precio para la apertura
request.price= NormalizeDouble(order_price,_Digits); // precio normalizado de apertura
break;
case ORDER_TYPE_BUY_STOP :
request.type = ORDER_TYPE_BUY_STOP; // tipo de orden
order_price = SymbolInfoDouble(_Symbol, SYMBOL_ASK)+OFFSET*_Point;// precio para la apertura
request.price= NormalizeDouble(order_price,_Digits); // precio normalizado de apertura
break;
case ORDER_TYPE_SELL_STOP :
request.type = ORDER_TYPE_SELL_STOP; // tipo de orden
order_price = SymbolInfoDouble(_Symbol, SYMBOL_BID)-OFFSET*_Point;// precio para la apertura
request.price= NormalizeDouble(order_price,_Digits); // precio normalizado de apertura
break;
default: // si no se ha elegido una orden pendiente o StopLimit
Alert("This example is only for placing pending orders BuyLimit, SellLimit, BuyStop and SellStop");
break;
}
//--- enviar la solicitud, si la solicitud no se ha podido enviar, mostramos el código de error y finalizamos el funcionamiento
if(!OrderSend(request, result))
{
Print("OrderSend error ", GetLastError());
return;
}
//--- mostramos la información sobre la operación
PrintFormat("Trade request result: retcode=%u, order=%I64u", result.retcode, result.order);
//--- obtenemos el ticket de la orden a partir del resultado de la operación comercial y seleccionamos la orden según el ticket
ulong ticket=result.order;
ResetLastError();
if(!OrderSelect(ticket))
{
PrintFormat("OrderSelect(%I64u) failed. Error %d", ticket, GetLastError());
return;
}
//--- mostramos en el registro los datos de la orden seleccionada según el ticket
ENUM_ORDER_TYPE type = (ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE);
long time = OrderGetInteger(ORDER_TIME_SETUP_MSC);
double price = OrderGetDouble(ORDER_PRICE_OPEN);
double volume= OrderGetDouble(ORDER_VOLUME_CURRENT);
string symbol= OrderGetString(ORDER_SYMBOL);
int digits= (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
PrintFormat("Current selected order: %s %.2f %s #%I64u at %.*f, %s",
symbol, volume, OrderTypeDescription(type), ticket, digits, price, TimeMscToString(time));
/*
resultado:
Trade request result: retcode=10009, order=2811006719
Current selected order: EURUSD 1.00 Buy Limit #2811006719 at 1.10550, 2024.09.04 10:38:28.563
*/
}
//+------------------------------------------------------------------+
//| Retorna la hora en milisegundos |
//+------------------------------------------------------------------+
string TimeMscToString(const long time_msc, int flags=TIME_DATE|TIME_MINUTES|TIME_SECONDS)
{
return(TimeToString(time_msc/1000, flags) + "." + IntegerToString(time_msc %1000, 3, '0'));
}
//+------------------------------------------------------------------+
//| Retorna la descripción del tipo de orden |
//+------------------------------------------------------------------+
string OrderTypeDescription(const ENUM_ORDER_TYPE type)
{
switch(type)
{
case ORDER_TYPE_BUY : return("Buy");
case ORDER_TYPE_SELL : return("Sell");
case ORDER_TYPE_BUY_LIMIT : return("Buy Limit");
case ORDER_TYPE_SELL_LIMIT : return("Sell Limit");
case ORDER_TYPE_BUY_STOP : return("Buy Stop");
case ORDER_TYPE_SELL_STOP : return("Sell Stop");
case ORDER_TYPE_BUY_STOP_LIMIT : return("Buy Stop Limit");
case ORDER_TYPE_SELL_STOP_LIMIT : return("Sell Stop Limit");
default : return("Unknown order type");
}
}
|