I have been working on an EA for a few weeks now and I am stuck: I want to close all trades and open orders when a profit target (say 10 pips) is reached.
I have a ClosePosition function which doesn't seem to be working.
Would anyone have any idea how to solve this?
Before we even look at close, your EA won't even open... Check these lines:
morningbid = SymbolInfoDouble(_Symbol,SYMBOL_BID); // Get bid price at 07:55am UK time morningask = SymbolInfoDouble(_Symbol,SYMBOL_ASK); // Get ask price at 07:55am UK time Long1 = morningask + _Point*3; Short1 = morningbid - _Point*3;
You cannot just add 3 to a price... you need to multiply it to _Point first. And if your broker uses 3 or 5 digits (check _Digits), which is highly probably nowadays, you should multiple _Point by 30 instead of 3.
And where do you plan to call ClosePosition function? (it isn't called at all, so of course it doesn't work).
Before we even look at close, your EA won't even open... Check these lines:
You cannot just add 3 to a price... you need to multiply it to _Point first. And if your broker uses 3 or 5 digits (check _Digits), which is highly probably nowadays, you should multiple _Point by 30 instead of 3.
And where do you plan to call ClosePosition function? (it isn't called at all, so of course it doesn't work).
Thanks for the tip on adding 3 pips, I hadn't seen that.
I'm calling it in OnTick, at the same place as CheckforOpen, as CheckForClose().
I removed the if statement (below) from the ClosePosition function and added it in this new function which should close all positions.
void CheckForClose() { if (AccountInfoDouble(ACCOUNT_PROFIT) == AccountProfit) { ClosePosition(arr_ticket_long_rsi); ClosePosition(arr_ticket_short_rsi); } }
Thanks for the tip on adding 3 pips, I hadn't seen that.
I'm calling it in OnTick, at the same place as CheckforOpen, as CheckForClose().
I removed the if statement (below) from the ClosePosition function and added it in this new function which should close all positions.
A number of issues:
- In your CheckForClose(), you cannot compare floating numbers with ==, because they're rarely equal. Should change to >= instead.
- Your arr_ticket_long_rsi and arr_ticket_short_rsi arrays - you iterate through them in ClosePositions(), RefreshArray() and ModifyOrders(), but they were never filled with any data. That explains why your positions are never closed (and not modified too).
- Long1 and Short1 - only set at specific time. Beyond that time, your code still attempt to set buystop and sell stop, so you see many error messages when actual price is below Short1 and you try to set sellstop, and vice versa for buystop.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi guys!
I have been working on an EA for a few weeks now and I am stuck: I want to close all trades and open orders when a profit target (say 10 pips) is reached.
I have a ClosePosition function which doesn't seem to be working.
Would anyone have any idea how to solve this?
Thanks a lot,
Martin