1
0
Fork 0
forked from e187038/SP_GVA1
SP_GVA1/SP_Pedro1/Homework1/SP_Pedro_Chapter09_v2.mq5
super.admin 066c2f4791 convert
2025-05-30 16:25:41 +02:00

116 lines
No EOL
11 KiB
MQL5

// Se hace sólo una compra con un criterio chorra y se intenta la venta con criterio chorrilla
//Definir variables para almacenar los parámetros de la orden
//us500 da market closed, pero
#define POSICION 0
double lotSize = 0.01; // Tamaño del lote
double stopLoss = 50; // Nivel de stop loss en puntos
double takeProfit = 100; // Nivel de take profit en puntos
bool comprahecha=false,ventahecha=false;
ulong desviacion=200;
ulong ticketdecompra;
double precioanterior,preciocomprafinal;
ulong serverTime,miliseconds;
int L;
// Precio actual de oferta del símbolo
void OnTick()
{
//---
double precio=0;
MqlTradeRequest request,request2;
MqlTradeResult result,result2;
if (!comprahecha) {
precio = SymbolInfoDouble(_Symbol, SYMBOL_BID);// L=__LINE__;Print (" L: ",L);
Print ("PRECIO PA INTENTO DE BUY:= ",precio);L=__LINE__;Print (" L: ",L);
Print (" volume size step:= ",SymbolInfoDouble (Symbol(), SYMBOL_VOLUME_STEP));
Print (" Mi coeficiente mágico ",(precio-precioanterior)/precioanterior);//L=__LINE__;Print (" L: ",L);
if (((precio-precioanterior)/precioanterior)>0.000003 || precio<0.666*preciocomprafinal) { // si en un tick hubo subida del 0,003%
ZeroMemory(result);
ZeroMemory(request);//L=__LINE__;//Print (" L: ",L);
request.action = TRADE_ACTION_DEAL;//L=__LINE__;Print (" L: ",L); // Tipo de acción: Deal (acuerdo)
request.type = ORDER_TYPE_BUY;//L=__LINE__;Print (" L: ",L); // Tipo de orden: Compra
request.volume = 2*lotSize; //L=__LINE__;Print (" L: ",L); // Tamaño del lote
request.price = precio; //L=__LINE__;Print (" L: ",L); // Precio de mercado actual
// request.deviation=desviacion;
// request.sl = precio - stopLoss * _Point; // Stop Loss
//request.tp = precio + takeProfit * _Point; // Take Profit
request.sl=precio *0.80;
request.tp=precio*1.20;
request.symbol = _Symbol;// L=__LINE__;Print (" L: ",L); // Símbolo
request.type_filling=ORDER_FILLING_IOC;L=__LINE__;Print (" L: ",L);
request.magic = 1001; // Número mágico
request.position=POSICION;//L=__LINE__;Print (" L: ",L);
Print ("action ",request.action," type ",request.type," volume ",request.volume," price ",request.price,
" stop loss ",request.sl," take ",request.tp," symbol ",request.symbol," position: ",request.position);
//Print (SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL) ); //da 0, no hay restricciones de distancia entre precio y sl,tp
comprahecha=OrderSend(request,result);L=__LINE__;Print (" L: ",L);
//imprimeResult (result);
if (comprahecha) {
Print ("compra realizada");//L=__LINE__;Print (" L: ",L);
preciocomprafinal=request.price;//L=__LINE__;Print (" L: ",L);
Print (" la comprica fue en el ticket número:===> ",result.order);
ticketdecompra=result.order;
}
else {
int error_code = GetLastError();//L=__LINE__;Print (" L: ",L);
Print("Error al enviar la orden de compra: ", error_code);
}
}//cierre de la posible compra
} else if (!ventahecha) {
precio = SymbolInfoDouble(_Symbol, SYMBOL_ASK);//L=__LINE__;//Print (" L: ",L);
Print ("PRECIO PARA INTENTAR VENDER ",precio);//L=__LINE__;//Print (" L: ",L);
if (((precio-preciocomprafinal)/preciocomprafinal) >0.025) { //en cuanto suba el 2,5%
ZeroMemory(request2);
ZeroMemory(result2);
request2.action = TRADE_ACTION_DEAL;L=__LINE__;//Print (" L: ",L); // Tipo de acción: Deal (acuerdo)
request2.type = ORDER_TYPE_SELL; L=__LINE__; // Tipo de orden: venta
request2.volume = 2*lotSize;L=__LINE__; //Print (" L: ",L); // Tamaño del lote
request2.price = precio;L=__LINE__; //Print (" L: ",L); // Precio de mercado actual
// request2.deviation=desviacion;L=__LINE__; //Print (" L: ",L);
request2.type_filling=ORDER_FILLING_IOC;
// request2.sl = preciocomprafinal*0.8; //Print (" L: ",L); // Stop Loss
// request2.tp = preciocomprafinal*1.2;// Print (" L: ",L); // Take Profit
request2.symbol = _Symbol;L=__LINE__;// Print (" L: ",L); // Símbolo
// request2.magic = 1001;L=__LINE__; //Print (" L: ",L); // Número mágico
request2.position=ticketdecompra;
request2.position_by=ticketdecompra;// en netting no es necesario, sería para llevar un hedging
Print ("Estoy en la VENTA donde action ",request2.action," type ",request2.type," volume ",request2.volume," price ",request2.price,
" stop loss ",request2.sl," take ",request2.tp," symbol ",request2.symbol," position: ",request2.position);
ventahecha=OrderSend(request2,result2);L=__LINE__; Print (" L: ",L);
if (ventahecha) {
Print ("Venta hecha");
}
else {
int error_code = GetLastError();//L=__LINE__;Print (" L: ",L);
Print("Error al enviar la orden de venta : ", error_code);
}
}
}
precioanterior=precio;
Comment("© Singularity Partners 2024","\n",
" CODE LINE:",__LINE__,"\n");
}
//+------------------------------------------------------------------+
bool imprimeResult (MqlTradeResult &presultado) {
Print (presultado.retcode," Return code ",presultado.deal," Deal ticket (for market orders) ",presultado.order," Order ticket (for pending orders) ",
presultado.volume," Deal volume ",presultado.price," Deal price ",presultado.bid, " Current Bid price ",presultado.ask," Current Ask price ",
presultado.comment," Broker comment to operation ");
return (true);
}