Problem with indicator - help needed

Post Reply
glattonmike
Posts: 3
Joined: Fri May 04, 2012 1:12 pm

Problem with indicator - help needed

Post by glattonmike »

Hi Guys, I am fairly new to coding and would like help with a problem please. I designed the attached indicator to draw 3 lines on the current chart for manual backtesting purposes. The blue line represents where I would open a trade, the green is the target and the red is the stop. After moving the lines to my desired trade criteria the labels in the top right corner automatically update with my stop/target pips and risk reward ratio.

My purpose for the indicator is so that when I am back testing visually (advancing through the chart using F12) I can position the lines where I want and see what the outcome of any trade would be. I know this may seem pretty pointless to most people but it is a good exercise in coding for me and saves lots of time in calculating profits (well, mostly losses actually!).

When I attach the indicator to a new chart everything works fine. I have saved a chart with the indicator as the default template. The problem is that when I open a different currency chart (or drag a different currency onto the existing chart) the lines are wrong - the data used is that which was saved to the default template from the original currency chart. However, if I recompile the indicator then the lines appear in the correct place.

I am confused because I thought that when I opened a new chart (or dragged a new currency.....) the indicator would 'refresh' but it appears that the initialization part of the indicator code is being ignored. Surely there must be a way to 'force' the indicator to refresh using code (so I don't have to keep recompiling or reattaching). I have explored the MQL4 website for answers but my search has been fruitless so far. Any help would be appreciated.
You do not have the required permissions to view the files attached to this post.
glattonmike
Posts: 3
Joined: Fri May 04, 2012 1:12 pm

Re: Problem with indicator - help needed

Post by glattonmike »

Hi, my post has been up nearly a week and so far there has been no response. I see that the indicator has been downloaded 9 times and there have been 47 views at the time of writing. Is there no one who can help with this problem? I feel sure that there is a simple solution but my limited knowledge of MQL4 is preventing me from solving this problem. Surely, if there is any forum in the world that could help then this is the place where I could find it? Thanks in advance to anyone who is willing to offer help.
User avatar
mjws00
Trader
Posts: 609
Joined: Fri Jan 04, 2013 10:17 pm
Location: Victoria

Re: Problem with indicator - help needed

Post by mjws00 »

Mike,

There seems to be an interesting timing issue on windowpricemax() windowpricemin(); If you call it directly from init to set your values you read it before Empty4 has a chance to load the data of the new symbol or timeframe. So you do your math on the old pair or timeframe.

I tried waiting a couple ticks which is painful on slow pairs, and just delaying a couple seconds to give it time to refresh. RefreshWindow() etc all had no effect. Just a slight pause to let Empty4 catch up worked best.

There may be a more elegant solution and the necessary delay is variable based on how much data Empty4 has to load to figure out the screen etc. It is an external you can play with. I didn't bother handling your zero divide error when your open line isn't drawn. You can clean that up, or leave it as it isn't the end of the world.

Mike
You do not have the required permissions to view the files attached to this post.
Reading the dark heart.
glattonmike
Posts: 3
Joined: Fri May 04, 2012 1:12 pm

Re: Problem with indicator - help needed

Post by glattonmike »

Mike

Thanks for the feedback and your solution. As you can probably tell I am very much a novice - but I am keen to develop the skills to become a half-reasonable coder. I think I will modify the indicator to draw the lines based on the last bar (openline in the middle, stop say a couple of ranges below and target vice versa).

Kind regards

Mike
Post Reply

Return to “Coders Hangout”