Hey Everyone.
I am having a few problems with my code. There are many problems, and as I hope you can appreciate, I am new to coding and am doing it in my spare time - I am slowly working through them.
The problem I am currently trying to overcome is that occasionally the EA is only placing an order one way. For example, only a sell, or only a buy. This is only sometimes. It should place a buy and sell at the same time.
Before anyone says "this is not a sound trading strategy", I am not worried, the point here is for me to learn how to code, rather than the strategy. I know my code may not be the most efficient, but I am just trying to get it to work before I refine it.
So could anyone tell me why this may be happening? As I said, I am new to coding, so rather than one or two word answers, if you could briefly explain it, it may help me learn what I am doing wrong.
Thanks.
Some general comments:
- the code in your start() function is minimal so will execute very quickly, you do not need RefreshRates() at all, and certainly not four calls. In between your RefreshRates() calls you don't even use Ask or Bid . . .
- if your OrderSend() fails you need to know why, you capture the return values from your OrderSend() calls . . . why aren't you using them ? Read this: What are Function return values ? How do I use them ?
- why are you using pending orders to place trades at the current price ?
- you are using Bid to calculate your SL & TP for both your Buy and Sell orders . . . this does not allow for spread at all. Do your orders comply with this : Requirements and Limitations in Making Trades
The first thing you need to do if find out why one of your Orders fails . . . you need to use the return value and report an error when it occurs . . . read the thread I have linked to above, it will help you do exactly that. When you know why the Order fails you will be in a better position to be able to fix it.
Because OrderSend takes TIME, your bid/ask are out of date after the first. If they are out of date by more than 5 points, (1/2 pip on a 5 digit broker) the second will fail.
- What are Function return values ? How do I use them ? - MQL4 forum You would have found out why.
- Adjusting for 4/5 digit brokers, adjusting for ECN brokers
- RefreshRates() AFTER orderSend. Not the three that don't ever refer to Bid/Ask
I have Adjusted the Pending Orders, to just normal market orders. I have moved the refresh rates so they are between the market orders. I have also modified the SL & TP for the orders, so they use Ask & Bid, rather than just Bid. Hopefuly these will help fix the problem.
I understand the reporting of the error, however I am confused as to where I put it in the code, how does it differentiate between the four OrderSend's? Do I put it after each one? Or at the end of the two tests?
Help with where to place the OrderSend function return would be helpful.
Thanks so far guys.
I understand the reporting of the error, however I am confused as to where I put it in the code, how does it differentiate between the four OrderSend's? Do I put it after each one? Or at the end of the two tests?
Help with where to place the OrderSend function return would be helpful.
Ok, so I have put the function return checks in after each order send. Is this correct?? Can someone let me know that this would report correctly if there was a problem???
// Previous Day High // if(Price > DayHigh && SellLimit == 0 && BuyStop == 0) { RefreshRates(); // Open Sell Limit SellLimit = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,Bid,5,SellLimitSL,SellLimitTP,"SellLimit Order",MagicNumber,0,Green); if( OrderSend(Symbol(),OP_SELLLIMIT,LotSize,Bid,5,SellLimitSL,SellLimitTP,"SellLimit Order",MagicNumber,0,Green) < 0 ) // OrderSend has failed and has returned a value of -1 { Print("Order Send failed, error # ", GetLastError() ); } // Open Buy Stop BuyStop = OrderSend(Symbol(),OP_BUYSTOP,LotSize,Ask,5,BuyStopSL,BuyStopTP,"BuyStop Order",MagicNumber,0,Green); if( OrderSend(Symbol(),OP_BUYSTOP,LotSize,Ask,5,BuyStopSL,BuyStopTP,"BuyStop Order",MagicNumber,0,Green) < 0 ) // OrderSend has failed and has returned a value of -1 { Print("Order Send failed, error # ", GetLastError() ); } }
I have now tried this approach. Will this report the error correctly???
// Previous Day High // if(Price > DayHigh && SellLimit == 0 && BuyStop == 0) { RefreshRates(); // Open Sell Limit SellLimit = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,Price,5,SellLimitSL,SellLimitTP,"SellLimit Order",MagicNumber,0,Green); if(SellLimit < 0 ) // OrderSend has failed and has returned a value of -1 { Print("Order Send failed, error # ", GetLastError() ); } // Open Buy Stop BuyStop = OrderSend(Symbol(),OP_BUYSTOP,LotSize,Price1,5,BuyStopSL,BuyStopTP,"BuyStop Order",MagicNumber,0,Green); if(BuyStop < 0 ) // OrderSend has failed and has returned a value of -1 { Print("Order Send failed, error # ", GetLastError() ); } }
Ok, so I have put the function return checks in after each order send. Is this correct?? Can someone let me know that this would report correctly if there was a problem???
I have now tried this approach. Will this report the error correctly???
That looks OK. You might want to edit the printed message so yo can tell what Order has failed . . . if you use
"Order Send failed, error # ", GetLastError()
for all the OrderSend messages you won't know which is which. Then you may also want to ininclude more information so that when an OrderSend has failed you are able to determine why . . . for example you might want to add . . .
Print("BuyStop failed, error # ", GetLastError(), " Price: ", Price1, " Bid: ", Bid, " Ask: ", Ask );
Then you will find that you only get 4 digits output and you want all 5 . . . so . . .
Print("BuyStop failed, error # ", GetLastError(), " Price: ", DoubleToStr(Price1, 5), " Bid: ", DoubleToStr(Bid, 5), " Ask: ", DoubleToStr(Ask, 5) );
Thanks for that. Ill have a go tomorrow night to refine it further.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hey Everyone.
I am having a few problems with my code. There are many problems, and as I hope you can appreciate, I am new to coding and am doing it in my spare time - I am slowly working through them.
The problem I am currently trying to overcome is that occasionally the EA is only placing an order one way. For example, only a sell, or only a buy. This is only sometimes. It should place a buy and sell at the same time.
Before anyone says "this is not a sound trading strategy", I am not worried, the point here is for me to learn how to code, rather than the strategy. I know my code may not be the most efficient, but I am just trying to get it to work before I refine it.
So could anyone tell me why this may be happening? As I said, I am new to coding, so rather than one or two word answers, if you could briefly explain it, it may help me learn what I am doing wrong.
Thanks.