I have a strategy for a trailing stop that I need help with programming. What I am looking for is coding that will use different time frames for the trailing stop placement.
Example: If I start a buy trade on a 5min chart, after price moves above the order open price, I want the EA to use the low (plus a small pip spread) of the last 5min bar as the trailing stop placement. If the low of the last bar on the next higher time frame (15 min) rises above the order open price, I want the EA to now use the lows from that next higher time frame for stop loss placement. And continue on as so when (and if) the low of the last bar on each higher time frame rises above the order open price.
I have written a code that will do this by creating a static integer at the beginning of the program and setting it equal to the time frame of the current chart when an order is opened. When the low of the last higher time frame bar exceeds the order open price, the static integer is given a new value to reflect that higher time frame (this is done in the trailing stop function), thereby remembering to use the higher time frame on the next modification of the trailing stop.
It seems to work the way it should but, only if I am opening just one single trade at a time. If I try to open another trade before the first one is closed, because of the way I set it up, the EA will revert back to using the lows of the last bar on the 5 min chart for both open trades instead of just for the new one and continue using the higher time frames for the older one.
So, that is what I am looking for. A way for the EA to remember which time frame it should be using for each of the different trades that might be open at any one time.
Any help will be greatly appreciated.
You have come up with an interesting strategy that shows some originality. I'm not sure how well it will work, but it seems worth pursuing.
The best way to solve your problem is with the use of arrays. Declare an array of say 99 integers to hold the ticket numbers, then declare another array to hold the corresponding time frames. Then when you find an order you look up its index in the ticket number array and using the index find the time frame.
Thanks for the suggestion. I'm not too good on working with arrays yet but, I think I know what you are getting at. I'll get to work on trying to set it up.
Thanks for your help.
Give It A Try
Get some familarity with arrays; they are not that difficult. I would suggest writing a simple script rather than in an EA. If you get stuck, I can help.
After I did some more thinking, there is a better way to implement my idea with arrays. The one I suggested before will work, but this way is better. Declare a 2 dimensional integer array called orderInfo and in the first dimension put the ticket numbers and in the second one put the time frame.
Then when you find an order you look up its ticket number and the other dimension contains the time frame.
orderInfo = tickNo;
orderInfo = TF;