Things Coders' Guru Never Taught Me - page 2

 

Good topic

Hi!

Just want to drop you, BigBe, a good word. Valuable thread. Found it when I was searching for DebuggersCoders. Seems I will have to rework the EA. .

Have fun,

Simon

 
 

Using the Current Bar

The current bar (bar 0) has the advantage that it is a developing situation and you can program an EA to take advantage of it, instead of waiting until a large move may already have occurred - that is, looking at the last closed bar.

However it is full of 'land mines' if you do not know exactly what you are doing.

(And even if you do, depending on your EA, it may or may not improve your profit or percentages. Back test, forward test, find out!)

BASICS

(Read over until you understand!)

The opening tick of a bar is the next tick after the close of the previous bar.

Thus the opening tick of a bar is usually near in price to the close of the previous bar.

Exceptions: Gaps due to:

1.The market was closed and a new session has begun.

2. A fast moving market.

In Metatrader:

The current price (tick) IS the current Close value.

Close[0] gives you the current price.

On the first tick of a bar, Open = Close = High = Low.

The second tick is a new High or Low, and is the new Close value.

The third and all subsequent ticks may or may not be a new High or Low, and is the new Close value.

As the number of ticks is used as a substitute for volume in Metatrader, the first tick is referenced by Volume[0] = 1. (Second tick Volume[0] = 2, etc.)

TECHNICAL SECTION

NOT for beginners.

Some indicators will not work right on the current bar. For example, if an indicator uses the proportion of the move within a bar, then for example on the second tick the High to Low price distance has moved 100% of the bar between Open (first tick) and Close (second tick). One of the two ticks is the bar High and one is the bar Low – so far. If you need over 75% to have a valid signal, well you've got it, but price may only have moved 1 pip!

If you want to check or compare price moves across one bar to the next, you can't always just compare (or subtract) Close[0] to Close[1]. Remember, on the first tick, Close[0] is ONE tick after Close[1] (the previous bar's close). If you are looking for a minimum move up or down, of an indicator or just price, you can check it every tick, and when and if it meets the requirement, which may be well into the bar, then you use the result.

Another choice is to compare Open[1] to Open[0]. This will be across one full bar at the open of the current bar. You could also use Close[2] to Open[0]. This would be one bar plus one tick. However if you are wanting to continue comparing values each tick, you would have to then use Close[0], as Open[0] does not change!

These principals apply to indicators as well.

Another potential problem with checking values every tick is that some indicators and some EA's are not written efficiently and recalculate everything from scratch every time they are called, which may be every tick. This can make things take 100 times longer or worse. When IndicatorCounted won't work, or cripples the indicator, sometimes indicator speed can be improved by not allowing 'limit' to exceed for example 300 bars. More in Post 10.

Code can be divided into section(s) that run every tick, and section(s) that are calculated only on the first tick of each bar. One way to set off a section for once per bar is:

if (Volume[0] = = 1)

{DO....

}

However in real trading, if the EA is busy it will ignore a tick. If this occurs, you will be out of the game for the full bar period. So it works better to keep trying, such as:

int start(){

static datetime dtBarTime = 0; // Flag

...

"the EA can use the following code to speed things up.

if (dtBarTime == Time[0]) return(0); // If flag was set last time through (next line), function ends

dtBarTime = Time[0]; // This sets the flag for next run through (next tick)

This code allows the EA to only check the indicator once per candle."

...}

Code and quoted section from MrPip.

// Comments are mine.

This can be further adapted for individual sections.

Any data calculated from a closed bar (bar >=1) should be separated and done only once per bar.

This is a big part of why my EA is fast.

Big Be

 

Mastering IndicatorCounted()

Lesson 1

Mastering IndicatorCounted makes a major difference in the behavior and results of indicators. This knowledge may help you to turn a slow indicator into a fast indicator. This affects EA's as well, at least indirectly when indicators are called.

Note: “SetIndexDrawBegin” only affects the display, not the calculations.

The first lesson is that the MetaEditor's help file, etc., has a mistake that has been endlessly copied. Your ordinary indicator needs to recalculate two bars back. In theory one bar, but in practice two is safe. The maximum value of IndicatorCounted is Bars – 1.

So the help file gives the useful lines:

int counted_bars = IndicatorCounted();

counted_bars--;

But this line from the help file:

limit = Bars - counted_bars – 1;

results, when on the current bar, in a limit of '1', NOT '2' which is what we want. Why have two lines that just take you back where you started? This calculates as:

(Bars - counted_bars) – 1

If Bars = 1000, then when on the current bar, counted_bars = 998, so you have:

(1000 – 998) – 1 = 1

So you have two choices:

counted_bars--;

limit = Bars - counted_bars;

OR just:

limit = Bars - counted_bars + 1;

You can check how all this is behaving with Comment statements, such as:

Comment(“ Bars “,Bars," counted_bars ",counted_bars, " limit ",limit);

When running, 'limit' should equal '2'.

Big Be

 

Mastering IndicatorCounted()

Lesson 2

Correctly setting up “limit” can have a huge effect on the left end of a chart. With some indicators this can affect hundreds of bars from the left. This is important if you are trying to understand an indicator over time. If I knew computer graphics I would draw you pretty pictures, but for now I will use simple word pictures. I suggest you draw it and label it. It will only take a minute.

Draw a horizontal line.

The left end is the number 'Bars', the right end is '0'. You can write 'counted_bars' below and to the left. 'counted_bars' starts at 'Bars' as '0', and ends at '1', one left of '0', with a value of 'Bars-1'.

Everything you subtract from 'Bars' reduces the 'limit' length from the left.

You don't want to start plotting before the longest Length used in the indicator, as it won't be accurate. What I have found to work is to use longest Length plus two plus how many bars back are looked back to and used. For example if you have 'i + 3' as your biggest shift back data point in your code, you end up with Length + 2 + 3.

Then you can use:

counted_bars--;

limit = Bars – MathMax(counted_bars, Length + 2 + 3);

It will start plotting at Length + 2 + 3 from the left, and follow counted_bars after that. If the limit loop uses ++, it will count up to the same point, from the right.

This will often clean up or fix a troublesome indicator. This is not a universal fix; it gives you the principles that you can adapt to the code in front of you. Take the time to work with it and understand it and you will be rewarded with more reliable and better results, sometimes much better.

I will admit that though this usually works, occasionally the exact number to add or subtract has been a matter of trial and error in a complicated indicator.

Notes:

If two lines are being calculated independently in one indicator, “limit” can be calculated differently for each.

If you are careless in coding you can end up at times with a negative value for limit!

Big Be

 

Hello people,

Id really like some help with an e.a i am trying to code. Id like my program to execute a trade after a certain number of points in which ever direction. How can i acheive this? A counter that keeps track of the movement in points from the first trade that is put in so that after a certain number of points, it executes again and the code on how it is supposed to execute. thanks

 

Post 3 EDIT, more about BarTime

I sometimes have two or more different sections that only need to run when a new bar starts. If both parts always calculate each time the EA runs, put the "BarTime = Time[0];" line in the second part to run. If they have different calls, and don't always both run, create a second BarTime, like BarTime_MA1 or whatever your code is.

Big Be

 

Post 10 EDIT

Item 5 can be revised: the data in Posts 3, 15, and 16 will handle it. You can ignore the article mentioned.

Big Be

 
Big Be:

I sometimes have two or more different sections that only need to run when a new bar starts. If both parts always calculate each time the EA runs, put the "BarTime = Time[0];" line in the second part to run. If they have different calls, and don't always both run, create a second BarTime, like BarTime_MA1 or whatever your code is.

Big Be

No message sent by you was received by any of the admins, but even if it was received it would not change anything

No user is allowed to edit posts that are older than 3 hours : that is introduced in order to avoid "signal" posts (that were altered after the fact) and similar scamms that were becoming common by simply editing their own past posts, and that rule will not be changed. That rule is common for almost all forums (try changing your old post on FF for example) and there is nothing out of the ordinary in it

all the best

 

I used to gain editing access temporarily. This is a change in policy and now I understand it. It has been a while since I asked for this access, twice, but months or years ago.

Big Be

Reason: