Here's how you do it the easy way which may require you to think differently about orders than you're used to. There is no such thing as a "Stop loss order". A stop-loss is a command send along with the market order instructing your broker to set a stop-order at the same time it executes your market position. You can have the same net result by opening a market position and sending a separate stop-order with equal volume as the market order. So when you say you want to reverse at a stoploss, what you really need is a stop-order that is twice the volume of your market order. When the price hits the stop order the original position will be closed and you will have an opposite position of equal volume (reversed by stop).
Here is a MVE snippet to try it for yourself.
#include <trade/trade.mqh> #include <trade/accountinfo.mqh> void OnStart() { CAccountInfo acc; if (acc.TradeMode() != ACCOUNT_TRADE_MODE_DEMO) return; if (!marketBuyWithReversingStop(0.1, 100)) Alert("FAILED"); } bool marketBuyWithReversingStop(double lots, int sl_points) { CTrade buy; buy.SetDeviationInPoints(100); CSymbolInfo symbol; symbol.Name(_Symbol); buy.Buy(lots); if (buy.ResultRetcode() == TRADE_RETCODE_DONE) { double open_price = buy.ResultPrice(); double sl = symbol.NormalizePrice( open_price - sl_points * symbol.Point() ); CTrade sellstop; sellstop.SellStop(buy.ResultVolume() * 2, sl); return (sellstop.ResultRetcode() == TRADE_RETCODE_DONE); } return false; }
Here's how you do it the easy way which may require you to think differently about orders than you're used to. There is no such thing as a "Stop loss order". A stop-loss is a command send along with the market order instructing your broker to set a stop-order at the same time it executes your market position. You can have the same net result by opening a market position and sending a separate stop-order with equal volume as the market order. So when you say you want to reverse at a stoploss, what you really need is a stop-order that is twice the volume of your market order. When the price hits the stop order the original position will be closed and you will have an opposite position of equal volume (reversed by stop).
Here is a MVE snippet to try it for yourself.
Thank you very much for the quick reply, I highly appreciate it.
Where should the bool marketBuyWithReversingStop be place? In OnTick?
I will put this in my code and let you know how I get on!
Thank you very much for the quick reply, I highly appreciate it.
Where should the bool marketBuyWithReversingStop be place? In OnTick?
I will put this in my code and let you know how I get on!
I'm sorry but this isn't like you're decorating a webpage with a CSS snippet. You can't just plop it into your code and expect it to work. It's your responsibility to understand the logic and how implement it yourself.
Alright, no need to be defensive and snob about it, it was only a question.
Thanks for the help anyway, I do appreciate it.
Thank you so much I managed to adapt it and make it work!!
Thank you so much I managed to adapt it and make it work!!
You got this program to work as an EA. Will it have the same functionality as a Script so I can do it manually?
- 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 struggling adding a feature to my EA.
I want to opposite open market orders on every trade at my SL level that get deleted once TP is hit.
So basically, if I have a long position and the SL is hit, I want to go short at the same level as my SL. If TP is hit, the order gets deleted.
At the moment, the EA opens long position is the H4 MACD is positive and the H1 MACD is negative and the H1 RSI is oversold. Vice versa for short positions. SL and TP are set as well.
Here is my code, please let me know your thoughts!
Thanks a lot!
Martin