Colocar orden con cantidad automática.

 

Hola, buenos días. 

Trabajo con sistemas donde no se toma en cuenta la relación riesgo/beneficio, pero el tamaño de la posición es dinámico, es decir cada vez que aumenta o disminuye el balance el tamaño de la posición cambia, donde manejo muchas cuentas al mismo tiempo (con riesgo diversificado) debo en algunas ocasiones hacer movimientos rápidos por lo que no da el tiempo de andar calculando los lotes en forma manual o desde Excel. 

Lo que necesito hacer es crear un ROBOT/sistema que me preconfigure el lotaje para no tener que colocar el tamaño de la posición en forma manual, pero nose como se hace, necesito empezar primeramente por:

1) Calcular el tamaño de lote, está listo aquí esta: 

-Esto calcula el tamaño de la posición las entradas son el tamaño de la orden (en porcentaje) y el valor del pip para asi convertir el % de posición a cantidad en lotes. 

#property copyright ""
#property link      ""
#property version   "1.00"
#property indicator_chart_window
input int riesgo = 20;
input int valorlote = 100000;

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {


double lotaje;



lotaje = (((AccountInfoDouble(ACCOUNT_EQUITY))*riesgo)/valorlote); 


printf(lotaje);


   return(rates_total);
  }

2) Crear un robot/sistema que me permita colocar ordenes de compra/venta a precio mercado, SIN tener que colocar el tamaño de la orden (que el tamaño se calcule solo, como el código de arriba).


Solo me falta el punto dos. ¿Alguien me podría decir como se puede hacer?, pienso que puede ser a través de atajos del teclado y desde allí configurar el sistema para que coloque la cantidad según el riesgo. 

¿Sugerencias? 


Saludos y gracias. 

 

Muy bien, para empezar he tratado de abrir una operación a través de un SCRIPT, pero no he tenido éxito, según mi broker (XM) admite posiciones por experts advisors, estaba usando una DEMO cuenta ZERO y la cambie a una normal, el mismo error en ambas. 

El error es:

2018.11.07 03:13:46.181 Trades '25255221': failed market buy 0.10 EURUSD [Unsupported filling mode]

2018.11.07 03:13:46.181 buy (EURUSD,M1) retcode=10030  deal=0  order=0

2018.11.07 03:13:46.181 buy (EURUSD,M1) OrderSend error 4756


10030

TRADE_RETCODE_INVALID_FILL

Está especificado el tipo de ejecución de orden no válido


Estoy tratando de solucionar el problema, nose mucho de programación, el error que aparece QUIERE DECIR que la orden es RECHAZADA por el Broker? ¿Si me dice "el tipo de ejecución de orden no es valido? ¿Cual es el método valido? 


void OnStart()
  {
   MqlTradeRequest request={0};
   MqlTradeResult  result={0};

   request.action   =TRADE_ACTION_DEAL;                     
   request.symbol   =Symbol();                              
   request.volume   =0.1;                                   
   request.type     =ORDER_TYPE_BUY;                        
   request.price    =SymbolInfoDouble(Symbol(),SYMBOL_ASK); 
   request.deviation=5;                                     
   request.magic    =EXPERT_MAGIC;                          

   if(!OrderSend(request,result))
      PrintFormat("OrderSend error %d",GetLastError());     

   PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
  }
 

I did it, I'm incredible haha.

This SCRIP is for the BUY and it automatically calculates the volume of the order with respect to a chosen percentage, for fixed percentage risk systems, the SCRIPT must be edited for each account according to the chosen risk, the leverage and the value of the lot. It gives faster to open orders when many accounts are handled because you only have to run the SCRIPT from the browser, you can even assign a keyboard shortcut. I corrected a critical error in the calculation of the size of the position (codes above were totally wrong in the calculation, it was corrected).

Forgive my English and greetings, thanks to the one who helped me at the beginning.  Now, I will work on how I can manage multiple accounts from a single one, for example when executing a SCRIPT on an MT5 (1), an SCRIPT is also executed on an MT5 (2), something similar to signals but locally on several MT5 open at the same time in the same computer.

input int riesgo = 20;
input int valorlote = 100000;
input int leverage = 400;

void OnStart()
  {

double lots;

lots = (((((AccountInfoDouble(ACCOUNT_BALANCE))*leverage)/100)*riesgo)/valorlote);

double lots2;

lots2 = (DoubleToString(lots,2));

   MqlTradeRequest request={0};
   MqlTradeResult  result={0};

   request.action   =TRADE_ACTION_DEAL;                     
   request.symbol   =Symbol();                             
   request.volume   =lots2;                               
   request.type     =ORDER_TYPE_BUY;   
   request.type_filling =ORDER_FILLING_IOC;                     
   request.price    =SymbolInfoDouble(Symbol(),SYMBOL_ASK);
   request.deviation=5;                                     

   if(!OrderSend(request,result))
      PrintFormat("OrderSend error %d",GetLastError());     

   PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
  }
  
Documentation on MQL5: MQL5 programs / Runtime Errors
Documentation on MQL5: MQL5 programs / Runtime Errors
  • www.mql5.com
The executing subsystem of the client terminal has an opportunity to save the error code in case it occurs during a MQL5 program run. There is a predefined variable _LastError for each executable MQL5 program. Before starting the OnInit function, the _LastError variable is reset...