Looking for some ideas/help

To add comments, please log in or register
rwb181
98
rwb181  
Hello All,

I built a trailing stop strategy that utilizes ever increasing time frames to determine the placement of the stop. For example, a buy trade is opened on the 5 min chart and once the low of the last bar is above the order open price, it (and subsequent bars) is/are used as the stop loss (never decreasing though). If the low of the next time frame’s (in this case, the 15Min chart) bar rises above the order open price, trailing stops will now be determined from it’s low and following lows (again, never decreasing) from the 15Min chart. This continues on for each larger time frame as their respective lows exceed the order open price. Once the trailing stop calculations are moved to a larger time frame for that trade, it cannot revert back to a smaller one.

To make it work, I basically set it up as such; At the top of the EA (on a global scale), I created a static integer (so changes to it would be remembered through each run of the start function). At the time that a trade is going to be opened, the static integer is given the value of the current chart’s time frame (using the Period() function). When conditions are met to move the initial stop and start using the trailing stop, the EA uses the value of this static integer to determine from what time frame the calculations are taken. If conditions warrant the calculations to be moved to a higher time frame, the static integer is changed to reflect this move so it will remember to use the higher time frame bars on the next modification of the trailing stop.

At the moment, the EA can only handle one trade at a time. It won’t open a new trade until the order and/or orders (can be one or two) from the last trade are closed.
What I want to do is modify the EA so it will open a new trade each time it gets a signal to do so (in either direction) even if other trades (buys or sells) are still open (with a limit to how many different trades can be opened at any one time).

I think I can accomplish the multiple trades part but the way it is set up now, every time a new trade would be opened, the static integer would be reset to the current time frame of the chart being traded. When already open orders are then processed for trailing stop modifications, the EA will have forgotten that past trades were using larger time frames for trailing stop placements and will revert all calculations back to the current time frame of the chart. Basically, the whole process is restarted for all open trades instead of for only the new one.

And here is where I am looking for ideas. How can I (or is there a way to) get the EA to remember which time frame each particular trade was using for the trailing stop calculations? What I mean is, if trade #1 has progressed to the hourly bars and trade #2 is using the 15Min bars, when trade #3 opens, I want it to start that trade looking at the 5Min bars but, the other two trades to continue using their respective time frames.

Any Ideas anyone????

Ron
Jay
79
Jay  

hey,

the static integer will NOT change unless you (the code) change it. What happens on every timeframe change is that your init() function gets called. And if you dont change your static integer inside it then it will preserve its value and you can continue working with it...

Another way to do it is to use an external file where you can save the values you need on every deinit() and load them back on every init(). Please look at the file functions (https://docs.mql4.com/files) for more info how to work with files...

hope the above helps!

thank you

Automated (automatedfx@gmail.com)

--

http://www.forexmosaic.com/ - free publishing, analyzing and portfolio building service

gordon
2325
gordon  

Using static variables on a global scope (outside of any function) is the same as using a normal global variable. All global variables are initialized once only by default and retain their values on each start() call (even if init() and deinit() are called).


Regrading your problem - there are many ways to solve this, for example u can use an array to store ticket numbers and their respective time frame information.

rwb181
98
rwb181  
Automated:

hey,

the static integer will NOT change unless you (the code) change it. What happens on every timeframe change is that your init() function gets called. And if you dont change your static integer inside it then it will preserve its value and you can continue working with it...

Another way to do it is to use an external file where you can save the values you need on every deinit() and load them back on every init(). Please look at the file functions (https://docs.mql4.com/files) for more info how to work with files...

hope the above helps!

thank you

Automated (automatedfx@gmail.com)

--

http://www.forexmosaic.com/ - free publishing, analyzing and portfolio building service

Hi Automated,

I think you misunderstood me. I am not talking about changing the chart's time frame, but a trailing stop that utilizes ever increasing time frames and remembers which one for each particular trade that is open.

Thanks for your help, though

rwb181
98
rwb181  
gordon:

Using static variables on a global scope (outside of any function) is the same as using a normal global variable. All global variables are initialized once only by default and retain their values on each start() call (even if init() and deinit() are called).


Regrading your problem - there are many ways to solve this, for example u can use an array to store ticket numbers and their respective time frame information.

Hi gordon,

I like your idea of an array that stores the ticket numbers and their respective time frame info. Being not to educated on dealing with arrays ( I am still trying to learn to work with them), could you give me a simple example that would get me started?


Thanks for your help.

gordon
2325
gordon  

For example you can have a 2 columned array (first column for ticket number and 2nd column for time-frame):

int ticket_arr[][2];

And then you can write a bunch utility functions to conveniently extract or insert information from the array.


For example here is a function to add a new ticket to the array:

void AddTicket( int ticket, int timeframe, int& ticket_arr[][] )
   {
   int size = ArrayRange( ticket_arr, 0 );        // get size of the first dimension of the array (number of array 'lines')
   ArrayResize( ticket_arr, size + 1 );           // resize the array to add one more 'line'
   
   ticket_arr[size][0] = ticket;                  // set values of ticket and timeframe in the new created 'line' 
   ticket_arr[size][1] = timeframe;
   }

To add a new ticket we can call this function (assuming we selected the order and we want to add the current chart's time-frame):

AddTicket( OrderTicket(), Period(), ticket_arr );


Similarly you can write other functions to interact with the array depending on your needs. More info about arrays -> https://docs.mql4.com/array. Good luck.

rwb181
98
rwb181  
gordon:

For example you can have a 2 columned array (first column for ticket number and 2nd column for time-frame):

And then you can write a bunch utility functions to conveniently extract or insert information from the array.


For example here is a function to add a new ticket to the array:

To add a new ticket we can call this function (assuming we selected the order and we want to add the current chart's time-frame):


Similarly you can write other functions to interact with the array depending on your needs. More info about arrays -> https://docs.mql4.com/array. Good luck.


I want to thank you for your help, gordon.

After going through your example (about 2 dozen times), I was finally able to see how you are getting the info into the function but, for the life of me, I can't seem to figure out how to get it back out again in the trailing stop function. I can't seem to get it, at times it seems to be right on the edge of my brain and then, poof, it's gone.


From what I understand, the array will have 2 elements (the ticket # for trade 1 in slot [0],[0] and it's respective time frame in [0],[1]) "per line". How do I extract that info when it comes time to adjust the trailing stop? I can't seem to figure out how I can determine the index value for each of the trades.


I've been going over it most of the night and will continue working on it today. Again, thanks

gordon
2325
gordon  
rwb181:

How do I extract that info when it comes time to adjust the trailing stop? I can't seem to figure out how I can determine the index value for each of the trades.

For example we can use another utility function to extract the time-frame value for a specific ticket.

int GetTimeFrame( int ticket, int& ticket_arr[][] )
   {
   int size = ArrayRange( ticket_arr, 0 );        // get size of the first dimension of the array (number of array 'lines')
   
   for ( int i = 0; i < size; i++ )               // loop on all elements in tickets column and search for ticket
      if ( ticket_arr[i][0] == ticket )           
         return( ticket_arr[i][1] );              // if ticket found then return timeframe
   
   return(-1);                                    // if ticket not found then return -1
   }

Assuming we have just selected an order and we want to extract it's associated time-frame data, we'll call:

int timeframe = GetTimeFrame( OrderTicket(), ticket_arr )

Note that the function will return -1 if the ticket is not found in the array. Hope this helps...

William Roeder
22428
William Roeder  

I built a trailing stop strategy that utilizes ever increasing time frames to determine the placement of the stop. For example, a buy trade is opened on the 5 min chart and once the low of the last bar is above the order open price, it (and subsequent bars) is/are used as the stop loss (never decreasing though). If the low of the next time frame’s (in this case, the 15Min chart) bar rises above the order open price, trailing stops will now be determined from it’s low and following lows (again, never decreasing) from the 15Min chart. This continues on for each larger time frame as their respective lows exceed the order open price. Once the trailing stop calculations are moved to a larger time frame for that trade, it cannot revert back to a smaller one.

Here's how I would do it.

static SL.bars = 5

set SL to Low[Lowest(SL.bars)]-1*pip.

Each time the SL is moved up. SL.bars *= 5; (each higher time frame is about 5 times longer than the previous M1:M5 M5:M30 H1:H4 etc.)


Personally, if I implement this idea I would only use SL.bars++ instead of *=5.

What I was about to implement in my EA was something in the order of

buy: SL.bars = RSI.Period * RSI/100;

sell: SL..bars = RSI.Period * (100-RSI)/100


int timeframe = GetTimeFrame( OrderTicket(), ticket_arr )
The problem with this time of implementation is if the terminal is restarted (power failure) then you loose ticket_arr and must have a fall back method.
rwb181
98
rwb181  
WHRoeder:

Here's how I would do it.

static SL.bars = 5

set SL to Low[Lowest(SL.bars)]-1*pip.

Each time the SL is moved up. SL.bars *= 5; (each higher time frame is about 5 times longer than the previous M1:M5 M5:M30 H1:H4 etc.)


Personally, if I implement this idea I would only use SL.bars++ instead of *=5.

What I was about to implement in my EA was something in the order of

buy: SL.bars = RSI.Period * RSI/100;

sell: SL..bars = RSI.Period * (100-RSI)/100


The problem with this time of implementation is if the terminal is restarted (power failure) then you loose ticket_arr and must have a fall back method.

Thanks for the input WHRoeder but, I am not too concerned about that yet. I still need to do some backtesting to see if the idea is viable. When I am ready to live test, I'll take your advise about a back up plan. Thanks for your help.

rwb181
98
rwb181  
gordon:

For example we can use another utility function to extract the time-frame value for a specific ticket.

Assuming we have just selected an order and we want to extract it's associated time-frame data, we'll call:

Note that the function will return -1 if the ticket is not found in the array. Hope this helps...





I want to thank you for all your help. I thought with your two examples, I would be able to create the function (and call) I need to change the time frames in the array when it was needed. After most of the day working at it though, sadly, I wasn't.


You have helped me by showing me how to add the ticket and corresponding time frame into the array, and how to return the existing time frame of a particular order. If you could show me how to change the existing time frame to a new time frame, I will be forever grateful to you and your kindred.


Again, thank you for your help. What you have taught me so far has been very helpful.

12
To add comments, please log in or register