Damn Error 130 to Hell - page 2

 
cloudbreaker wrote >>

Well I can categorically state that Seawolf and Ruptor are talking out of their collective rear end.

For an OP_BUY order, you are absolutely correct to use the Ask price to generate your entry price and stops.

Actually, Seawolf and Ruptor are correct.

You enter a long order at the ASK price and you exit at the BID, reverse that for a sell order. Since a stop-loss or take-profit is an exit you need to use the BID price to calculate them on a long order.

This is a fairly common point of confusion that is often overlooked, especially when dealing with small spreads since there's not much difference between the ASK and BID price. Sometimes the code will work just fine if your stops and slippage are not to tight, but if you're scalping or doing anything else that requires precision then you need to use these guidelines for proper price on entry and exit.

-

For a long order, i.e. OP_BUY, OP_BUYSTOP, or OP_BUYLIMIT:

Entry Price = ASK

Exit Price (stop-loss, take-profit, or OrderClose(...) ) = BID

-

For a short order, i.e. OP_SELL, OP_SELLSTOP, or OP_SELLLIMIT:

Entry Price = BID

Exit Price (stop-loss, take-profit, or OrderClose(...) ) = ASK

-

Tovan

 
tovan:

Actually, Seawolf and Ruptor are correct.

You enter a long order at the ASK price and you exit at the BID, reverse that for a sell order. Since a stop-loss or take-profit is an exit you need to use the BID price to calculate them on a long order.

This is a fairly common point of confusion that is often overlooked, especially when dealing with small spreads since there's not much difference between the ASK and BID price. Sometimes the code will work just fine if your stops and slippage are not to tight, but if you're scalping or doing anything else that requires precision then you need to use these guidelines for proper price on entry and exit.

-

For a long order, i.e. OP_BUY, OP_BUYSTOP, or OP_BUYLIMIT:

Entry Price = ASK

Exit Price (stop-loss, take-profit, or OrderClose(...) ) = BID

-

For a short order, i.e. OP_SELL, OP_SELLSTOP, or OP_SELLLIMIT:

Entry Price = BID

Exit Price (stop-loss, take-profit, or OrderClose(...) ) = ASK

-

Tovan

Understood, I see exactly what you are saying - and humblest apologies to Seawolf and Ruptor.

Where I close orders normally or operate my own stealth stoploss (which just invokes my normal close orders function), I'm fully aware to use Bid for OP_BUYs and Ask for OP_SELLs.

However, when opening my orders, I've always just used the entry prices as my baseline to calculate the stoploss and never have had any problem. I can see how the combination of a loose spread and tight stop could cause an invalid stops error.

I'd add that there are a hell of a lot of samples which do it the same way I do.

If we don't have tight stops, I guess the only difference between the two techniques that most of us will tend to experience is in the actual money made or lost, although since both types of order would get stopped 'the spread amount sooner', they would tend to cancel each other out if the spread were fairly stable.

Is this logic sound?

 
cloudbreaker wrote >>

Understood, I see exactly what you are saying - and humblest apologies to Seawolf and Ruptor.

Where I close orders normally or operate my own stealth stoploss (which just invokes my normal close orders function), I'm fully aware to use Bid for OP_BUYs and Ask for OP_SELLs.

However, when opening my orders, I've always just used the entry prices as my baseline to calculate the stoploss and never have had any problem. I can see how the combination of a loose spread and tight stop could cause an invalid stops error.

I'd add that there are a hell of a lot of samples which do it the same way I do.

If we don't have tight stops, I guess the only difference between the two techniques that most of us will tend to experience is in the actual money made or lost, although since both types of order would get stopped 'the spread amount sooner', they would tend to cancel each other out if the spread were fairly stable.

Is this logic sound?

Good points. I agree that there are many other EA's out there that do it the way you suggested.

This illustrates some differing views that we take on stop-loss based on how our EA's work. I, for instance, tend to set my stop-loss fairly tight. So I'm generally more concerned with where the order is going to fail than with how much I would loose if it does. On the other hand, though, if you were more interested in what you would loose (or gain) when the order closes then it makes sense to do all of the calculations on the opening price as opposed to the close price. That only becomes a problem if your stop-loss or take profit is tight (within a few pips, depending on the pair). In that case the EA would need to do some extra checking with MODE_STOPLEVEL (as you suggested above) to make sure you can actually complete the transaction.

I was talking mostly from a purist position, but I can see some valid arguments for running it both ways.

- Tovan

 
tovan:

Good points. I agree that there are many other EA's out there that do it the way you suggested.

This illustrates some differing views that we take on stop-loss based on how our EA's work. I, for instance, tend to set my stop-loss fairly tight. So I'm generally more concerned with where the order is going to fail than with how much I would loose if it does. On the other hand, though, if you were more interested in what you would loose (or gain) when the order closes then it makes sense to do all of the calculations on the opening price as opposed to the close price. That only becomes a problem if your stop-loss or take profit is tight (within a few pips, depending on the pair). In that case the EA would need to do some extra checking with MODE_STOPLEVEL (as you suggested above) to make sure you can actually complete the transaction.

I was talking mostly from a purist position, but I can see some valid arguments for running it both ways.

- Tovan

Tovan is this than correct because i also get a 130 error

if(OrderType()==OP_SELL && OrderSymbol()==Symbol())
{
if (OrderStopLoss()==0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
}
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if(OrderStopLoss()>(Ask+Point*TrailingStop)+Point)
{
if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red))
Print("Error_Modify - ",GetLastError());
else str=StringConcatenate("\n My ticket number is ", OrderTicket(), " and my stop loss setting is ", DoubleToStr(Ask+Point*TrailingStop,Digits)); // new code
}
}
}
if(OrderType()==OP_BUY && OrderSymbol()==Symbol())
{
if (OrderStopLoss()==0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
}

if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop-Point)
{
if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))
Print("Error_Modify - ",GetLastError());
else str=StringConcatenate("\n My ticket number is ", OrderTicket(), " and my stop loss setting is ", DoubleToStr(Bid-Point*TrailingStop,Digits)); // new code
}

}


is the 130 error to do with the entry and exit - or could it be because of slippage

 

Actually, it seems all of you are a bit confused. Some of you are mixing up two totally different things.

The questions to ask are:

1) At what price do Buy and Sell TP and SL orders get executed. Bid or Ask?

2) What price do we use to calculate TP and SL for Buy and Sell. Bid or Ask?

tovan wrote >>

Actually, Seawolf and Ruptor are correct.

You enter a long order at the ASK price and you exit at the BID, reverse that for a sell order.

//---VANGROSH --- Correct but confusing. Better to say your Long order TP or SL will be executed when your TP Price or SL Price == the BID price. This is what you SEE when you watch your order hit TP or SL, but this has nothing to do with how you calculate those prices.

Since a stop-loss or take-profit is an exit you need to use the BID price to calculate them on a long order.

//---VANGROSH--- Sorry that’s wrong. You’re confusing what you SEE- What price will trigger the exit, with what price you use to calculate the TP and SL Prices.

Just think this through a bit. Take a Buy Order. You all agree we Buy at the ASK price- no issue. Let’s say TP is 15 points on a Buy order, and the spread is 5 points. If you set the TP 15 points above the BID price then your TP will only be 10 points because the spread is BELOW the ASK and ABOVE the BID price. It makes no sense to calculate 15 points of TP from a price 5 points BELOW your entry price (ASK). You must always calculate the TP or SL from the price you entered the order at. ASK for BUY, BID for SELL.

It’s easy if you just think at what price your TP or SL will be hit. On a BUY order, entering with ASK price, at what price will your TP get hit? At the BID price. If your TP was at 1.2450 and you set your TP from the ASK price, then when the ASK line is at 1.2450 now your are just starting to pay the spread. Only when the BID line is at 1.2450 will your TP get hit and your order will close with 15 points of profit.

Same with the SL. If your SL is 30 points that means 30 points from the entry price. On Buy the entry price is ASK. If you use the BID price – 30 points, then you are excluding the spread in your SL so your real SL will actually be 35 points from your entry (ASK) price because the BID price is 5 points lower (the spread) then the ASK price. You will get 35 points of SL because your SL PRICE is now 35 points below your ENTRY PRICE which was what for a Buy Order?

I’ve only been doing this for a few months and what helped me was just observing at what price TP and SL get hit when manually trading and then for my first EA, I just double checked my TP and SL lines on an opened EA order using the crosshair tool and also checked the TP and SL prices on some closed EA orders to be sure the TP and SL points were exact.

The only time you need to do different is for a trailing stop. For a Buy order you use the BID price.

if(Bid-OrderOpenPrice() > Point*_trailingStop) StopLoss = Bid-Point*_trailingStop;

Because you want your TS to be X points below the price that will close the order if hit which is the BID price.

For sell you use ASK.

if(OrderOpenPrice()-Ask > Point*_trailingStop) StopLoss = Ask+Point*_trailingStop;

Because you want your TS to be X points above the price that will close the order if hit which is the ASK price.

Remember it is the BID price that will close your BUY TP and SL orders,

And it is the ASK price that will close your SELL TP and SL orders.

 
vangrosh:

Actually, it seems all of you are a bit confused. Some of you are mixing up two totally different things.

The questions to ask are:

1) At what price do Buy and Sell TP and SL orders get executed. Bid or Ask?

2) What price do we use to calculate TP and SL for Buy and Sell. Bid or Ask?

Just think this through a bit. Take a Buy Order. You all agree we Buy at the ASK price- no issue. Let’s say TP is 15 points on a Buy order, and the spread is 5 points. If you set the TP 15 points above the BID price then your TP will only be 10 points because the spread is BELOW the ASK and ABOVE the BID price. It makes no sense to calculate 15 points of TP from a price 5 points BELOW your entry price (ASK). You must always calculate the TP or SL from the price you entered the order at. ASK for BUY, BID for SELL.

It’s easy if you just think at what price your TP or SL will be hit. On a BUY order, entering with ASK price, at what price will your TP get hit? At the BID price. If your TP was at 1.2450 and you set your TP from the ASK price, then when the ASK line is at 1.2450 now your are just starting to pay the spread. Only when the BID line is at 1.2450 will your TP get hit and your order will close with 15 points of profit.

Same with the SL. If your SL is 30 points that means 30 points from the entry price. On Buy the entry price is ASK. If you use the BID price – 30 points, then you are excluding the spread in your SL so your real SL will actually be 35 points from your entry (ASK) price because the BID price is 5 points lower (the spread) then the ASK price. You will get 35 points of SL because your SL PRICE is now 35 points below your ENTRY PRICE which was what for a Buy Order?

I’ve only been doing this for a few months and what helped me was just observing at what price TP and SL get hit when manually trading and then for my first EA, I just double checked my TP and SL lines on an opened EA order using the crosshair tool and also checked the TP and SL prices on some closed EA orders to be sure the TP and SL points were exact.

The only time you need to do different is for a trailing stop. For a Buy order you use the BID price.

if(Bid-OrderOpenPrice() > Point*_trailingStop) StopLoss = Bid-Point*_trailingStop;

Because you want your TS to be X points below the price that will close the order if hit which is the BID price.

For sell you use ASK.

if(OrderOpenPrice()-Ask > Point*_trailingStop) StopLoss = Ask+Point*_trailingStop;

Because you want your TS to be X points above the price that will close the order if hit which is the ASK price.

Remember it is the BID price that will close your BUY TP and SL orders,

And it is the ASK price that will close your SELL TP and SL orders.

Vangrosh

thanks for the input

can you help me to under stand

is this correct

i do an EA with auto trail


if(OrderType()==OP_SELL && OrderSymbol()==Symbol())

{
if (OrderStopLoss()==0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red); // place a TP and SL
}
if((OrderOpenPrice()-Ask)>(Point*TrailingStop)) // place TP
{
if(OrderStopLoss()>(Ask+Point*TrailingStop)+Point) // check true
{
if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red)) // if true modify order
Print("Error_Modify - ",GetLastError());
else str=StringConcatenate("\n My ticket number is ", OrderTicket(), " and my stop loss setting is ", DoubleToStr(Ask+Point*TrailingStop,Digits)); // new code
}
}
}
 
vangrosh:

Actually, it seems all of you are a bit confused. Some of you are mixing up two totally different things.

The questions to ask are:

1) At what price do Buy and Sell TP and SL orders get executed. Bid or Ask?

2) What price do we use to calculate TP and SL for Buy and Sell. Bid or Ask?

Just think this through a bit. Take a Buy Order. You all agree we Buy at the ASK price- no issue. Let’s say TP is 15 points on a Buy order, and the spread is 5 points. If you set the TP 15 points above the BID price then your TP will only be 10 points because the spread is BELOW the ASK and ABOVE the BID price. It makes no sense to calculate 15 points of TP from a price 5 points BELOW your entry price (ASK). You must always calculate the TP or SL from the price you entered the order at. ASK for BUY, BID for SELL.

It’s easy if you just think at what price your TP or SL will be hit. On a BUY order, entering with ASK price, at what price will your TP get hit? At the BID price. If your TP was at 1.2450 and you set your TP from the ASK price, then when the ASK line is at 1.2450 now your are just starting to pay the spread. Only when the BID line is at 1.2450 will your TP get hit and your order will close with 15 points of profit.

Same with the SL. If your SL is 30 points that means 30 points from the entry price. On Buy the entry price is ASK. If you use the BID price – 30 points, then you are excluding the spread in your SL so your real SL will actually be 35 points from your entry (ASK) price because the BID price is 5 points lower (the spread) then the ASK price. You will get 35 points of SL because your SL PRICE is now 35 points below your ENTRY PRICE which was what for a Buy Order?

I’ve only been doing this for a few months and what helped me was just observing at what price TP and SL get hit when manually trading and then for my first EA, I just double checked my TP and SL lines on an opened EA order using the crosshair tool and also checked the TP and SL prices on some closed EA orders to be sure the TP and SL points were exact.

The only time you need to do different is for a trailing stop. For a Buy order you use the BID price.

if(Bid-OrderOpenPrice() > Point*_trailingStop) StopLoss = Bid-Point*_trailingStop;

Because you want your TS to be X points below the price that will close the order if hit which is the BID price.

For sell you use ASK.

if(OrderOpenPrice()-Ask > Point*_trailingStop) StopLoss = Ask+Point*_trailingStop;

Because you want your TS to be X points above the price that will close the order if hit which is the ASK price.

Remember it is the BID price that will close your BUY TP and SL orders,

And it is the ASK price that will close your SELL TP and SL orders.

Vangrosh, as I alluded to in my response to Tovan, as long as you know exactly what you're doing, you can do it either way. We're far from confused.

That is, given that you know the entry price, the broker's stop limit and the spread you can achieve the 'absolute' stoploss you want (one that won't produce 130 errors either!) by making it 'relative' either to ask or bid and adjusting it as necessary.

I prefer to do it the way you describe by the way.

 
cloudbreaker wrote >>

Vangrosh, as I alluded to in my response to Tovan, as long as you know exactly what you're doing, you can do it either way. We're far from confused.

That is, given that you know the entry price, the broker's stop limit and the spread you can achieve the 'absolute' stoploss you want (one that won't produce 130 errors either!) by making it 'relative' either to ask or bid and adjusting it as necessary.

I prefer to do it the way you describe by the way.

Well, Seawolf said, "Rule of thumb... if you enter on the ask, you exit with bid, if you enter on Bid you exit with ask".

That's not confused? I don't mean he is confused, I mean to me the context to which he is speaking is not clear. His Rule of thumb is not correct IF he is talking about calculating exits. When I say confused I'm not saying you guys don't know what you are doing or that you can't do it another way- what I really meant to say is that the way they were giving instructions was confusing. For example the above quote from Seawolf is correct IF he is talking about at what price point orders exit in MT. I think for someone just learning this you have to be very clear with what context you are referring to, because there are two basic contexts when teaching about TP and SL.

1) The context or point of view that is focusing on at what price (Bid or Ask) that an open order's TP and SL will be hit- which is one of the first things I needed to learn just for manual trading...

2) The context or point of view that is focusing NOT #1 above but is focusing on what price (Bid or Ask) do we use to calculate our TP and SL, IF we want our TP and SL prices to be exact and include the spread.

Look, I'm a newbe at this too and it's easy for me and other newbe's to get confused if things are presented to us in a way where the context is not very clear.

And I for one did not know in the beginning that there was more then one context in which to view TP and SL and many other things we need to understand.

cloudbreaker wrote>>Vangrosh, as I alluded to in my response to Tovan, as long as you know exactly what you're doing, you can do it either way.

That is why I was trying to be as clear and in context as I could in my post. People just learning like me Don't know exactly what we are doing which is why we need to be taught about things in their proper context.

In my experiance it seems that many times knowledge is assumed to us that we do not yet have. We need to know the basic rules first and then get some experience. Only after that will we know what 'rules' we can bend or depart from.

By the way I was not referring to you in my post. I thought your answers were by far the most clear and accurate. And at the risk of sounding like 'kissing butt', when it comes to getting the clearest and accurate answers on this forum, your posts are some of the ones I look for first. There are only about 4 or 5 people on this forum that I've found so far that have proven to be more consistently accurate and that you can tell they actually have more then just a little experience behind them.

 
vangrosh:

Well, Seawolf said, "Rule of thumb... if you enter on the ask, you exit with bid, if you enter on Bid you exit with ask".

That's not confused? I don't mean he is confused, I mean to me the context to which he is speaking is not clear. His Rule of thumb is not correct IF he is talking about calculating exits. When I say confused I'm not saying you guys don't know what you are doing or that you can't do it another way- what I really meant to say is that the way they were giving instructions was confusing. For example the above quote from Seawolf is correct IF he is talking about at what price point orders exit in MT. I think for someone just learning this you have to be very clear with what context you are referring to, because there are two basic contexts when teaching about TP and SL.

1) The context or point of view that is focusing on at what price (Bid or Ask) that an open order's TP and SL will be hit- which is one of the first things I needed to learn just for manual trading...

2) The context or point of view that is focusing NOT #1 above but is focusing on what price (Bid or Ask) do we use to calculate our TP and SL, IF we want our TP and SL prices to be exact and include the spread.

Look, I'm a newbe at this too and it's easy for me and other newbe's to get confused if things are presented to us in a way where the context is not very clear.

And I for one did not know in the beginning that there was more then one context in which to view TP and SL and many other things we need to understand.

cloudbreaker wrote>>Vangrosh, as I alluded to in my response to Tovan, as long as you know exactly what you're doing, you can do it either way.

That is why I was trying to be as clear and in context as I could in my post. People just learning like me Don't know exactly what we are doing which is why we need to be taught about things in their proper context.

In my experiance it seems that many times knowledge is assumed to us that we do not yet have. We need to know the basic rules first and then get some experience. Only after that will we know what 'rules' we can bend or depart from.

By the way I was not referring to you in my post. I thought your answers were by far the most clear and accurate. And at the risk of sounding like 'kissing butt', when it comes to getting the clearest and accurate answers on this forum, your posts are some of the ones I look for first. There are only about 4 or 5 people on this forum that I've found so far that have proven to be more consistently accurate and that you can tell they actually have more then just a little experience behind them.

Thanks for the compliment; much appreciated.

When I said we weren't confused, I was really just speaking for myself and Tovan as we seem to operate on a similar waveband.

Apologies if my post sounded abrupt in any way; it certainly wasn't meant to be and I wasn't feeling that way at the time (although I do have my moments...).

My general approach to this forum (when I'm not seeking help myself) is to try my best to help those who are trying to learn a programming skill. I really have no time for folks who want everything done for them, just arriving on the forum with their first post titled "I need a successful EA". Folks like yourself are why I stay interested on here, and you probably know more about forex trading than I do.

A wee bit of background  - I've been a programmer since the early 1980s but gave up a number of years ago to work as a commercial helicopter pilot. However, given the lack of flying by my company this year, I've been writing EAs for a friend of mine who owns a fledgling research company. We take a chaos theory (rather than market knowledge) approach to trading and are already trading the EAs very profitably on behalf of a well known hedge fund. I really enjoy it!

Good luck.

 
delcor wrote >>

Vangrosh

thanks for the input

can you help me to under stand

is this correct

i do an EA with auto trail

if(OrderType()==OP_SELL && OrderSymbol()==Symbol())

{
if (OrderStopLoss()==0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red); // place a TP and SL
}

You should set your base TP and SL when you send your order or else do the above line only once. If you do the above line

on every tick then you are going to also get OrderModify error 1 which will happen when the exsisting SL value is the same

as the new one- meaning the price has not changed yet- SL is the same as before.
if((OrderOpenPrice()-Ask)>(Point*TrailingStop)) // place TP
{
if(OrderStopLoss()>(Ask+Point*TrailingStop)+Point) // check true

This looks fine. And I think what you are doing by adding a point (+Point) is a good idea. I do the same thing

but in a different place and way, but that is to fix a different issue, a problem you can get when compairing doubles does not work right, so you add

a Point to make sure the price is greater or less- or else your Trailing stop will not be executed sometimes.

The rest seems correct.
{
if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red)) // if true modify order
Print("Error_Modify - ",GetLastError());
else str=StringConcatenate("\n My ticket number is ", OrderTicket(), " and my stop loss setting is ", DoubleToStr(Ask+Point*TrailingStop,Digits)); // new code
}
}
}

Here is the trailing stop example from the MQ EA MACD Sample.mq4

              // check for trailing stop
              if(TrailingStop>0)  
              {                 
                 if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                    if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }

But I found that I would get errors from this code because of the known issue of when compairing doubles like above, your greater then > clause would sometimes evaluate as TRUE

even when the prices are exactly the same- you Print() the prices and they are the same. You can do a search on compairing doubles to get the details. There are workarounds such as the CompareDoubles() function found in stdlib.mq4 (in the libraries folder), or using NormalizeDouble() in places it really should not be needed, but I found that in this case simply adding a Point like you did is good way to do it. But I have not tested the way you do it so I'm not certain if it is correct, but is seems ok. I'll give you the code I use in my EA in the next post.