You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
The best way to get the stoploss correct is to think properly about the spread. The spread is the distance between the Ask and the Bid. So when you add a stoploss to a buy order using Bid - SL automatically includes the spread and you will not need to code for it and you are able to effectively totally ignore spread. For a sell trade using Ask + SL has the same effect. However, BEWARE --- at times of low trading volume the spread gets larger and you may well end up with a SL well away from the place you expect it to be --- therefore you may want to add some code to prevent the addition of the SL above a set Spread size.
Now the question of the Spread is out of the way the next main causes of error 130s is that the prices have changed while your EA is still executing. This can be caused by an EA that takes a long time to execute or the server is very busy servicing trades which delay your EA execution. The result is that one tick has caused your EA to start executing but another tick has come before the execution has finished and the prices are now invalid.
In either case you need to refresh the prices using RefreshRates: while (RefrshRates() == 1) Sleep(5); ------ or whatever you want as a wait time.
In either case you need to refresh the prices using RefreshRates: while (RefrshRates() == 1) Sleep(5); ------ or whatever you want as a wait time.
AFAIK RefreshRates() has nothing to do with error 130
Point taken RaptorUK and I agree that using MODE_ASK etc would eliminate the necessity for RefreshRates() but I assumed that as in shinobi's code example ....
I send an order with for example:
int ticket = OrderSend(Symbol(), OP_BUY, position_size, Ask, SLIPPAGE, initial_stop, TAKEPROFIT, NULL, EXPERT_ID, 0, Green);
the predefined variable ASK is being used and therefore the ask/bid as well as the spread value may have changed giving the error 130. In such a case then RefreshRates() could be used immediately prior to the OrderSend
Point taken RaptorUK and I agree that using MODE_ASK etc would eliminate the necessity for RefreshRates() but I assumed that as in shinobi's code example ....
I send an order with for example:
int ticket = OrderSend(Symbol(), OP_BUY, position_size, Ask, SLIPPAGE, initial_stop, TAKEPROFIT, NULL, EXPERT_ID, 0, Green);
the predefined variable ASK is being used and therefore the ask/bid as well as the spread value may have changed giving the error 130. In such a case then RefreshRates() could be used immediately prior to the OrderSend
I am a bit busy at the moment, but once I got the chance, I'll try all your suggestions and then write a summarizing post for anyone who might stumble across this thread with the same problem.
Thanks, and take care!
shinobi
int ticket = OrderSend(Symbol(), OP_BUY, position_size, Ask, SLIPPAGE, initial_stop, TAKEPROFIT, NULL, EXPERT_ID, 0, Green);
doesn't say everything. How are you computing initial_stop - using Bid?
How are you computing SLIPPAGE - adjusting for 4/5 digit brokers?
I had to take a break for a while (moving to a new city, new job).
But now I'd like to take up on this thread, and finally find a solution for this cursed #130 stoploss error.
I am grateful for all of your advice and tried to incorporate all of it:
1: Spread.
2: Changing Market Rates
3: 4-5 Digits-Broker
So I incorporated all suggestions, but the error still persists. As far as I can tell it occurs just as often as before, so there has to be another cause for it.
Here's a recent log:
tickvalue: 12.50000000
pos size: 37.00000000
Ask/Bid 1262.00000000/1261.75000000
stoploss:12.59610000
position_size: 37
Spread 0.25000000
Error could not take long position. Error is: #130 invalid stops
The above OrderSend code line was used with the logged values to try and take a long position.
Do you have any more ideas, what could be the cause?
Thanks!
shinobi
Is this an ECN Broker ?
On ECN brokers you must open and THEN set stops.
to eliminate the possible cause of invalid number of digits, I rounded the stoploss according to WHRoeders code:
Just do
extern double StopLoss = 50;
extern double TakeProfit = 50;
then for buys:
double SL=Bid - StopLoss* Point;
double TP=Bid + TakeProfit*Point;
int Ticket=OrderSend(Symbol(),0,1,Ask,2,SL,TP,"",12345);
if( Ticket<0) print("error="GetLastError());
for sells:
double SL=Ask + StopLoss* Point;
double TP=Ask - TakeProfit*Point;
int Ticket=OrderSend(Symbol(),1,1,Bid,2,SL,TP,"",12345);
if( Ticket<0) print("error="GetLastError());
then post the log file if it doesnt work.
SDC:
I tried your code like this:
The only difference is, that I replaced Point, Ask and Bid with MarketInfo to avoid the RefreshRates problem mentioned by Raptor and BigAI.
The problem persists with this simple example. I still get
#ESZ1,M5: Opening Position
#ESZ1,M5: tickvalue: 12.50000000
#ESZ1,M5: pos size: 1.00000000
#ESZ1,M5: Ask/Bid 1244.50000000/1244.25000000
#ESZ1,M5: Spread 0.25000000
#ESZ1,M5: SL: 1244.00000000
#ESZ1,M5: TP: 1245.00000000
#ESZ1,M5: error=130
Raptor:
I currently use UWC-Trader with a demo account for testing.
Like mentioned earlier I am trading Futures. E.g. ESZ1 and FDXZ1.
WHRoeder:
Sorry I don't round either. Replace "round" with "adjust for 4/5-Digit Brokers. I.E. I just applied your code.
I also posted the OrderSend just as I use it in my previous reply. And the values of all the variables involved. I am not sure what other part of the code would be interesting.
As the mini-example, suggested by SDC, showed, the error can be broken down to this simple code. So it must be more fundamental.
I am not trading Metals, so rounding shouldn't be important.
I will try next:
opening (no stoploss) and then modifying (setting stoploss) later.
thanks again for your thoughts,
shinobi