help me, problem err 130 with pending's configuration or err 138 with ordersend mkt configuration

 

function for management

void hedge
{
for(int i = 0; i<OrdersTotal(); i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if( OrderSymbol()==Symbol()&& OrderMagicNumber()== M1 )
{ // simbolo
if((OrderType()==OP_SELL) )
{
// [ variables taken from order SELL ]
stop = OrderOpenPrice();
price = NormalizeDouble(OrderOpenPrice()+Distanza*p,Digits);
take = OrderStopLoss();

// fine variables


if(Ask > OrderOpenPrice())
{
//qui segnala l'errore 130 o 138 dipende da come lo configuro [here report error 130 or 138 depends on config.

myOrderhedge(OP_BUY, Lots, price1, slip, sl,tp, WindowExpertName(), M2, 0, Blue); //or myOrderhedge(OP_BUYSTOP, Lots, price1, slip, sl,tp, WindowExpertName(), M2, 0, Blue);

}
}
}
}

}

//***************************************

pending config., problem err 130 invalid stop???

void myOrderhedge(int cmd, double Lots, double price1, double slip, double sl, double tp, string aComment, int M2, datetime Expiration, color Color)
{

ticket1 = -1;
int err = 0;
int try = 0;
int Maxtry = 10;
Lots = 1.0;
string openType;

if(!IsTesting())RefreshRates();
switch ( cmd )
{
case OP_BUYSTOP:
// qui riprendo le variabili dichiarate globalmente della funzione HEDGE
price1 = price;
sl = stop;
tp = take;
Color = Green;
break;

case OP_SELLSTOP:

price1 = price;
sl = stop; // stoplose dell ordine sell
tp = take;
Color = Red;
break;
default:break;
}

while( (ticket1 == -1) && (try < Maxtry) )
{

ticket1 = OrderSend(Symbol(), cmd, Lots, price1, slip, 0, 0, aComment, MH, Expiration, Color) ;
try++;
}

if (ticket1 > -1) // se tutto corretto fai la modifica
{
OrderSelect(ticket1, SELECT_BY_TICKET);

bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), sl, tp,0, Color);


}
if(ret == false)// se non inviato
{
err = GetLastError();

if (err == 4 || err == 137 || err == 146 || err == 136 || err== 130)
{
Print("OrderModify() error - ", ErrorDescription(GetLastError()));
Sleep(5000);
RefreshRates();
}
}

}// fine funzione 

//*****************************************

normal ordersend, problem err 138 requote??

void myOrderhedge(int cmd, double Lots, double price1, double slip, double sl, double tp, string aComment, int M2, datetime Expiration, color Color)

{

ticket1 = -1;
int err = 0;
int try = 0;
int Maxtry = 10;
Lots = 1.0;
string openType;

//if(!IsTesting())RefreshRates();
switch ( cmd )
{
case OP_BUY:
price1 = price; // variazione//NormalizeDouble(OrderOpenPrice()+Distanza*p,Digits);
sl = stop;
tp = take;
Color = Green;
break;

case OP_SELL:

price1 = price;
sl = stop; // stoplose dell ordine sell
tp = take;
Color = Red;

break;
default:break;

}

while( (ticket1 == -1) && (try < Maxtry) )
{

ticket1 = OrderSend(Symbol(), cmd, Lots, price1, slip, 0, 0, aComment, MH, Expiration, Color) ;
try++;
}
if (ticket1 > -1)
{
OrderSelect(ticket1, SELECT_BY_TICKET);
OrderPrint();

//if(sl < MarketInfo(Symbol(),MODE_STOPLEVEL))
bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), sl, tp,0, Color);


}
if(ret == false)
{
//err = GetLastError();

if (err == 4 || err == 137 || err == 146 || err == 136 || err== 130 || err == 138)
{
Print("OrderModify() error - ", ErrorDescription(GetLastError()));
Sleep(5000);
RefreshRates();
}
}

}// fine funzione 



my broker is ECN broker, but i have this problem and i don't understand


thanks





 

 

Error Handling according to the docs.

For 130: Does your ECN allow you to send Sl and Tp with pending? Other things you can check, does the Sl and Tp exceed the absolute value of math maximum of the Market Info: StopLevel and FreezeLevel.

For 138: Usually its because the Ask and Bid are flipped around. But with this being an ECN I'll say go ahead and check the general format of the price itself for stuff like extra .decimals etc.

 

thank

for send order i must do

1) ordersend, without SL e TP

after

2) insert SL and TP

I do this procedure for all orders

i'm newbie in this language code

my broker is ATC


can you show me how would you do with example??

 

Why do you pass variables to the function . . . .

void myOrderhedge(int cmd, double Lots, double price1, double slip, double sl, double tp, string aComment, int M2, datetime Expiration, color Color)

and then proceed to change them ?

price1 = price;
sl = stop;
tp = take;
 

Well my codes would have allot of other stuff which clouds the example. Basically you're doing most of what needs to be done already within your example code. I believe what you are seeking now comes with experience of coding and testing. If I was coding this for a live real money EA. I would need the following.

- Market_Info()=Give me all the market info and place them within Global variables (Not the GlobalVariableSet).

- Check for TradeContexBusy. If the Trading Thread is Busy then Sleep() for a random time between 2-4 seconds.

- Refresh() after Sleep() or re-run the Market_Info()

- OrderSend(Sl=0, Tp=0); this returns a Ticket#> -1 if it works.

- Therefore if Ticket==(-1) then you want to GetLastError(). Save this Value because it goes away after the first time you check GetLastError().

- Given your case you should run into error 138, but this is more of an experience error and something you could have avoided and found during testing. Like I said above you can fix this making sure your price variable is not looking for Ask where it should be looking for Bid and vice-versa.

- Once you fix that and your order goes through. Your Ticket Variable will have the value of the Ticket# which you have to Immediately try to search for and correct the Sl and Tp.

- For initial order sl and tp. I'll do something like asking if sl=0 {after the order search of course}. Then you have to make sure the MathAbs(OrderOpenPrice() - New_StopLoss_Price ) is Greater> StopLevel*Point and Greater than FreezeLevel*Point.

Example from my code:

double Oop=OrderOpenPrice(); double Ocp=OrderClosePrice();
if(MathAbs(Oop-Ocp)>Bkr_Freeze*Point2Pips){ Close(.......); }

- The StopLevel and Freezelevels are Global variables you would have saved when you ran you Market_Info function.

- In the case of Pending Orders, you also have to make sure that the Stoploss and TakeProfit values are not anywhere near the current Price-Action. So again you'll also have to check if(mathabs(ask-PendingOpenPerice)>Bkr_StopLv*Points)<---for pending orders above current price obviously. And check reverse for pending orders below.

Well... I don't code any EA's anymore with pending orders and stop-losses anymore. I just use Market_Orders to get In and Out. If you really want to look through codes then try looking at WHRoeders Codes for Pending Orders Stop considerations. You can look here for help on constructing a Reliable OrderSend Routine.

Lastly, don't think you can just get-by on copy and pasting codes. You'll lack the insights the experience brings so I recommend reading the docs, articles, codes in the code base, etc ... which relates to what ever you're stuck on.