Help with code

 

Hi all

I have been trying to solve a problem I have on a code, without the results I want.


The code is (only part of the code):


if (Ordem==OP_SELL)
{
Ordem1 = 0;
Ordem2 = 0;

while (Ordem1<=0)
{
Ordem1=OrderSend(Symbol(),OP_SELLLIMIT,Lots,Valor*Point,0,0,PRSell*Point,MagNum",MagNum,0,Red);
}


while (Ordem2<=0)

{
Ordem2=OrderSend(Symbol(),OP_SELLLIMIT,Lots,Valor*Point,0,0,PRSell1*Point,MagNum",MagNum,0,Red);
}
}
....


The code runs fine, but sometimes I get an error 130 (invalid take profit).

What I need to do is to check if the error 130 occurried, and if so, send the order WITHOUT a TF.


If someone of you can help me it will be great


Regards


Paulo

 

You can avoid Error 130 by checking the Stoplevel....

double Takeprofit;

if(Takeprofit>MarketInfo(NULL,MODE_STOPLEVEL))
   {
   Takeprofit=MarketInfo(NULL,MODE_STOPLEVEL);
   }

Not the direct answer to your question, but give me some time and I might ahve the answer....

EP

 
ErrorProgrammer:

You can avoid Error 130 by checking the Stoplevel....

Not the direct answer to your question, but give me some time and I might ahve the answer....

EP


Dear ErrorProgrammer,


Thank you for the reply.

I will be waiting for your answer.


Regards


Paulo

 

Use RefreshRates() in your order loops or you might end up getting stuck in that loop for ages.

.

As for your question, do something like this:

if(Ordem == OP_SELL)
{
   Ordem1 = 0;
   Ordem2 = 0;

   while (Ordem1 <= 0){
      RefreshRates();
      //Update your Valor variable here though
      Ordem1 = OrderSend(Symbol(),OP_SELLLIMIT,Lots,Valor*Point,0,0,PRSell*Point,MagNum",MagNum,0,Red);
      PRSell = 0;
   }

   while (Ordem2 <= 0){
      RefreshRates();
      //Update your Valor variable here though
      Ordem2 = OrderSend(Symbol(),OP_SELLLIMIT,Lots,Valor*Point,0,0,PRSell1*Point,MagNum",MagNum,0,Red);
      PRSell1 = 0;
   }
} 

Jon
 
Archael:

Use RefreshRates() in your order loops or you might end up getting stuck in that loop for ages.

.

As for your question, do something like this:

Jon


Hi Jon,


Thank you very much.

Unfortunetly did not work. The RefreshRates() idea is an idea I will also implement but did not solved the error I am getting.


How can I get out of the "loop" if there is an error when sending the order?

The idea is, in case of an error (in this case 130) get out of the loop and send the order without stop loss...


Regards


Paulo

 

Try this,


CB.


bool bModifyStops;

if (Ordem==OP_SELL)
{
Ordem1 = 0;
Ordem2 = 0;


bModifyStops = false;

while (Ordem1<=0)
{
RefreshRates();

if (bModifyStops)

{

modify your stops here

}

Ordem1=OrderSend(Symbol(),OP_SELLLIMIT,Lots,Valor*Point,0,0,PRSell*Point,MagNum",MagNum,0,Red);

if (GetLastError() == 130)

bModifyStops = true;
}


bModifyStops = false;
while (Ordem2<=0)

{

RefreshRates();

if (bModifyStops)

{

modify your stops here

}

Ordem2=OrderSend(Symbol(),OP_SELLLIMIT,Lots,Valor*Point,0,0,PRSell1*Point,MagNum",MagNum,0,Red);

if (GetLastError() == 130)

bModifyStops = true;

}
}
....

 
batalhadematos wrote >>

Hi Jon,

Thank you very much.

Unfortunetly did not work. The RefreshRates() idea is an idea I will also implement but did not solved the error I am getting.

How can I get out of the "loop" if there is an error when sending the order?

The idea is, in case of an error (in this case 130) get out of the loop and send the order without stop loss...

Regards

Paulo

My code wasn't about getting rid of the error, it was about recovering after the error. Huge difference, same final result. What you want to happen is exactly what my code is doing right now. If there was an error (although int this case it's *any* error), your stoploss variable becomes 0 and 0 means no stoploss so on the second loop there won't be a stoploss.

What you could and perhaps even should do is to put a counter in that loop and get out after a few failures to report an error message. First loop with stops, second loop without stops, third loop without stops and exit if it failed again.

Jon

 
Archael:

My code wasn't about getting rid of the error, it was about recovering after the error. Huge difference, same final result. What you want to happen is exactly what my code is doing right now. If there was an error (although int this case it's *any* error), your stoploss variable becomes 0 and 0 means no stoploss so on the second loop there won't be a stoploss.

What you could and perhaps even should do is to put a counter in that loop and get out after a few failures to report an error message. First loop with stops, second loop without stops, third loop without stops and exit if it failed again.

Jon

Hey Jon, sorry to intrude.

I realized what you were doing.

Just wanted to show the poster how to do what he subsequently asked.

Hopefully it'll help him broaden his knowledge and he'll end up developing his own (completely different) variation!


CB

 
cloudbreaker wrote >>

Hey Jon, sorry to intrude.

I realized what you were doing.

Just wanted to show the poster how to do what he subsequently asked.

Hopefully it'll help him broaden his knowledge and he'll end up developing his own (completely different) variation!

CB

Hey CB, it's no problem at all. I was mostly replying to the thread starter because he tried it and thought it wasn't working. Was just making sure he realized that the errors would still occur but the recovery would take place on the second loop+.

 

Dear all,


Thank you very much for the answers and ideas. With you I am starting to understand how mql programming works.

Using the Refresh and Print functions, and trying to understand how to implement the logic of the code sent by CB, I have discovered that the error itself it is not on the Stop Loss variable or even on the code itself.

The problem is on the market prices during specific periods.


If you look on a EURUSD chart (1M tf), you will see for instance the peaks on prices on dates 2008.04.14 at 00:00 and 2009.02.15 at 23:00.


The problem is that the variable for the PRICE value get "lost" with this peaks and retuns the error on the PRICE level where the BUY or SELL limit should be open.


Based on sample code I have sent, how can I execute a specific function if an error occurs (in this case 130) when sending the limit order.

To see the results, I want to close all open orders (including pending orders).


Thank you very much


Regards


Paulo

 
int ticket = OrderSend(...);
if(ticket == -1){
   int errno = GetLastError();
   if(errno == 130){
      //do something
   }
}
Jon
Reason: