So I have a system where I open a new trade every 10 pips and close out the last trade.
The last trade is also modified to set the stop loss to 10 pips.
Currently, I have a function that does this:
What I am doing is first closing the trade (by running this function) before opening the next trade by doing OpenTrade(Symbol(), OP_BUY, Next_BUY_Lot_Size); or OpenTrade(Symbol(), OP_SELL, Next_SELL_Lot_Size);
Example
However, the trade does not close EXACTLY at the open of the next trade. It happens before but at a different price which causes me uncessecary and extra losses.
Would OrderCloseBy() do the trick here? Example of what I am thinking: OrderCloseBy(Close1B,Close2B, CLR_NONE);
Can you guys spot the mistake? I've been losing sleep over this for some time now.
It's very unclear what you are trying to accomplish here, and your code is difficult to read since it doesn't follow any indentation convention and it's incomplete. If you don't know how to indent your code you can always have metaeditor do it for you by selecting Tools->Styler.
In the future you should clean up your code and be more specific on what you are trying to accomplish.
So I have a system where I open a new trade every 10 pips and close out the last trade.
It's very unclear what you are trying to accomplish here, and your code is difficult to read since it doesn't follow any indentation convention and it's incomplete. If you don't know how to indent your code you can always have metaeditor do it for you by selecting Tools->Styler.
In the future you should clean up your code and be more specific on what you are trying to accomplish.
However the problem is that when it closes a trade and opens the next one, it does not close and open at the exact same price. There is a small difference. The image attached is a visual example of the problem I am trying to explain.
However the problem is that when it closes a trade and opens the next one, it does not close and open at the exact same price. There is a small difference. The image attached is a visual example of the problem I am trying to explain.
A SELL close at Ask and open at Bid. You can have them at the same price so simply. You need to place a SELL STOP at the close price of your previous order, it the broker allow it. And you could have slippage.
Your idea of a SELL/BUY STOP looks promising. Im a little confused/newbie so could you provide an example? particularily in the example ea attached. Thank you for your time.
I tried to open and close a sell at both the ask price and vice versa, however, it did not work and threw error 138. I dont belive you can sell at the wrong price (Ask).
If you want coding help, please provide your attempt.
Sorry I made a type, I mean "you can't have them...". A market sell trade is always open at Bid price, not possible to change that.
If you want coding help, please provide your attempt.
There is only one way to reverse a position "at the exact time" and that is to place an order for double the volume of the open position in the opposite direction. For example to reverse a 1 lot SELL you'd need to place an order for a 2 lot BUY, and your resulting position would net 1 lot long. With hedging accounts you'll need to use closeby to avoid double fees for managing your position this way. The command function is OrderCloseBy and here is an example of how to use it.
bool multiple_closeby(string symbol, int magic=0) { for(int i=OrdersTotal()-1; i>=1; --i) { if(position_select(i, symbol, magic)) { int ticket = OrderTicket(); int type = OrderType(); for(int j=i-1; j>=0; --j) { if(position_select(j, symbol, magic) && OrderType() != type) { if(OrderCloseBy(OrderTicket(), ticket)) return multiple_closeby(symbol, magic); else return false; } } } } return true; } bool position_select(int index, string symbol, int magic) { return ( OrderSelect(index, SELECT_BY_POS) && OrderSymbol() == symbol && OrderMagicNumber() == magic && OrderType() < 2 ); }
There is only one way to reverse a position "at the exact time" and that is to place an order for double the volume of the open position in the opposite direction. For example to reverse a 1 lot SELL you'd need to place an order for a 2 lot BUY, and your resulting position would net 1 lot long. With hedging accounts you'll need to use closeby to avoid double fees for managing your position this way. The command function is OrderCloseBy and here is an example of how to use it.
I am not reversing a position though. when price moves against me 3 pips from my original sell, i place another sell. When price moves 3 pips against my second sell, I place another sell and so forth.
I know it is possible to close the previous sell and place a new sell exactly at the close of the last sell because I did it manually using limit orders. However, I cant get it to work in mt EA. Any more ideas guys? Feeling desperate.
modified some of your code, to select highest lot first.
bool CTrade::multiple_closeby_symbol(const int magic = 0) { double lot = 0; for(int i=OrdersTotal()-1; i>0; i--){ if(position_select(i,magic) && OrderLots()>lot) { int first_ticket = OrderTicket(); int first_type = OrderType(); for(int j=i; j>=0; j--){ if(selectTradeHedging(j,first_type,magic)) { bool closeorder = false; closeorder = OrderCloseBy(first_ticket,OrderTicket()); if(closeorder) return multiple_closeby_symbol(magic); else return false; } } break; } } return true; } bool selectTradeHedging(int i, int masterType, int magic = 0){ int opposite = 100; if(masterType==OP_BUY) opposite = OP_SELL; else if(masterType==OP_SELL) opposite = OP_BUY; return ( OrderSelect(i,SELECT_BY_POS) && OrderSymbol() == _Symbol && OrderType()==opposite ); } bool position_select(int index, int magic) { return ( OrderSelect(index, SELECT_BY_POS) && OrderSymbol() == _Symbol; && OrderMagicNumber() == magic && OrderType() < 2 ); }
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
The last trade is also modified to set the stop loss to 10 pips.
Currently, I have a function that does this:
What I am doing is first closing the trade (by running this function) before opening the next trade by doing OpenTrade(Symbol(), OP_BUY, Next_BUY_Lot_Size); or OpenTrade(Symbol(), OP_SELL, Next_SELL_Lot_Size);
Example
However, the trade does not close EXACTLY at the open of the next trade. It happens before but at a different price which causes me uncessecary and extra losses.
Would OrderCloseBy() do the trick here? Example of what I am thinking: OrderCloseBy(Close1B,Close2B, CLR_NONE);
Can you guys spot the mistake? I've been losing sleep over this for some time now.