Cycle to send several order with diferente SL and TP

 

Hi everybody, I need help on this, if someone know how to fix the problem, please tell me. I´m making a script to open several orders with the same SL and diferent TP, the problem it´s that sometimes it works well and opens all the order and sometimes not given a return error 129 (Invalid price).

I attach the code:

//+------------------------------------------------------------------+
//| trade.mq4 |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| https://www.metaquotes.net// |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link "https://www.metaquotes.net//"

#property show_inputs
#include <stdlib.mqh>
#include <WinUser32.mqh>

//----
extern int lotes=3; // El # de contratos a comprar
extern string operacion="BUY";
extern int risk_model=1;

int X;
int Y;
int ticket;
int error;
int Fibs[5];
int SL;
int Take_Profit;
//
//+------------------------------------------------------------------+
//| script "trading for all money" |
//+------------------------------------------------------------------+
int start()
{
//----
// if(MessageBox("Do you really want to BUY 1.00 "+Symbol()+" at ASK price? ",
// "Script",MB_YESNO|MB_ICONQUESTION)!=IDYES) return(1);
//----

if (risk_model == 1)
{
Fibs[0]=550;
Fibs[1]=890;
Fibs[2]=1440;
Fibs[3]=2330;
Fibs[4]=3770;
SL=270;
// MessageBox("El primer fib es "+Fibs[0]+" .");
// MessageBox("El SL es "+SL+" .");
}
if (risk_model == 2)
{
Fibs[0]=890;
Fibs[1]=1440;
Fibs[2]=2330;
Fibs[3]=3770;
Fibs[4]=6100;
SL=440;

// MessageBox("El primer fib es "+Fibs[0]+" .");
// MessageBox("El SL es "+SL+" .");
}

Y=0;
if(operacion=="BUY")
{
for(X=0;X<lotes;X++)
{
Take_Profit=Fibs[X];
ticket=0;
ticket=OrderSend(Symbol(),OP_BUY,0.1,Ask,0,Ask-SL*Point,Ask+Take_Profit*Point,"expert comment",12345,0,CLR_NONE);
// MessageBox("CONTRATO "+(X+1)+" y ticket: "+ticket+" .");
if(ticket<1)
{
error=GetLastError();
MessageBox("El número de error es "+error+".");
if (Y<3)
{X--;
Y++;
}
else
Y=0;
}
}
}
if(operacion=="SELL")
{
for(X=0;X<lotes;X++)
{
Take_Profit=Fibs[X];
ticket=0;
ticket=OrderSend(Symbol(),OP_SELL,0.1,Bid,100,Bid+SL*Point,Bid-Take_Profit*Point,"expert comment",12345,0,CLR_NONE);
// MessageBox("CONTRATO "+(X+1)+" y ticket: "+ticket+" .");
if(ticket<1)
{
error=GetLastError();
MessageBox("El número de error es "+error+".");
if (Y<3)
{X--;
Y++;
}
else
Y=0;
}
}
}

//----
ticket=0;
// MessageBox("FINALMENTE LLegó hasta la "+X+" .");
return(0);
}
//+------------------------------------------------------------------+

Please.......

Reason: