Because there is no event for modifying an order I wanted to use the CHARTEVENT_CLICK event [...] I doubt this is the best solution and so I just wanted to ask if there is a better way to do this.
I wouldn't try to do this by responding to events on the chart, unless you specifically only want to handle s/l changes which are made via that route.
In essence, I would create your own "modify order" event: build an initial list of orders on startup, and then continually watch for changes. The basic rule seems to be "if one s/l has changed since the last check, change all the other s/l values to the same distance". But, for safety, you should also look out for events such as more than one s/l changing; the appearance of a completely new order; disappearance of orders etc.
With an event I wanted to avoid looping through all orders with each tick because that would be not very efficient. So I thought about an event that must happen in the case of moving the stoploss line by dragging it.
?
[...] because that would be not very efficient.
Unless you have a ludicrous number of orders, it should be fine. I've just tried out a realistic, unoptimised example where a comparison of 10 orders against their previous state takes about 20 microseconds (0.00002 seconds).
You can control the load further by doing the check in OnTimer() rather than OnTick(), e.g. no more often than once per 500 milliseconds, rather than on each tick which could at times be more frequent.
Although in theory, you could do that; should the OP accidentally move the stop-loss from another chart on the same symbol, the EA will not catch it. jjc's suggestion is much more robust, and it is also they way I do it in my own EA's.
In this way, even if you change the stop-loss from a Mobile device on via a Web interface, the EA will still respond correctly. Relying only on the chart events can be dangerous.
Unless you have a ludicrous number of orders, it should be fine. I've just tried out a realistic, unoptimised example where a comparison of 10 orders against their previous state takes about 20 microseconds (0.00002 seconds).
You can control the load further by doing the check in OnTimer() rather than OnTick(), e.g. no more often than once per 500 milliseconds, rather than on each tick which could at times be more frequent.
Yes you are correct, but you can further reduce the load per tick, by processing only one order (or a predetermined count) per tick and/or timer event.
In my own EA's, instead of looping over all open orders on every tick event, I use a state machine and only process a set number of orders per tick or per timer event, using the timer event as a safeguard in case of low volume (slow tick rate).
For example, if I have 80 orders open, and I have set the limit to 10 orders per tick, my function will process up to 10 orders per tick (or timer event) and no more, and then wait for the next tick/timer event before processing another 10, thus requiring a total of 8 tick/timer events to process them all.
This method also speeds up things for the Strategy Tester and otimisation runs.
PS! The timer event is only used in live trading and obviously not during the back-testing for which I set the parameter to only process one order per tick, which greatly speeds up the testing.
EDIT: To improve it even more, you can make it so that it only processes the orders if the quote price has moved at least a set amount to justify the processing. For example, you could set it to only process the orders if the current price has moved more than 5 pips since the last level was reached. This too greatly improves back-testing time.
- 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 a couple of running trades on one instrument. Now I move the stoploss of one order closer to the current price by dragging the stoploss line in the chart. An EA should notice that and change all other stoplosses to the same stoploss price.
Because there is no event for modifying an order I wanted to use the CHARTEVENT_CLICK event. If this event occurs, I check if there are running trades on this pair. If yes, I look for the stoploss which is different from the others and then I modify the orders with the "old" stoploss.
I doubt this is the best solution and so I just wanted to ask if there is a better way to do this.
Thanks!