Number of bars on a chart

 

my EA depends on a text file to open trades. the text file is created by an Indicator. the EA and the Indicator does not talk to each other, they communicate through the text file.

the text file is in the format of "type @ price".

now what my EA does is that it opens the text file to get two values - "type" and "price" of the last trade signal recorded by my Indicator. then it opens another text file, which was created by the EA itself upon successful opening of a trade, to get another two values and name them "lastType" and "lastPrice". then it compares "type" with "lastType", "price" with "lastPrice". if they are all the same, then no new trades. otherwise new trades will be opened according to the indicator's record.

my problem is, on the off event that my EA got disconnected, a new trade signal was recorded by the Indicator. if this signal happens to be exactly the same type and at the same price as the last trade, then my EA wouldn't open a new trade as it should.

although this hasn't happened yet, it is enough of a trouble to make me worry. the only solution I can think of is to tell my indicator to record also the number of bars before the signal bar to the text file, then EA will compare it with its own record, if Indicator's record is greater, then new trades, otherwise no new trades.

because of the nature of my EA, I can not backtest. so I won't know if this will work until a few trades have been made. besides, I'm not sure I know how to tell an EA to find out the number of bars on a chart.

if you know it works, could you please tell me how to do it? if not, do you have any other suggestions?

looking forward to hearing from you.

iPG

 
 

thank you! FX Sniper! I thought about this and it seems that to give each record a unique serial number the Indicator will have to open the file and read it. it can be done but I very much prefer to keep my Indicator as simple as possible, because I'll be running it on many charts. though what you suggested probably is the best solution, I'll certainly take this approach if all else fails.

somebody else has suggested that I embed the indicator logic into my EA. though it won't work with my trading method, I'm quite intrigued by this suggestion. I thought about this and could see its merits, but I've never done it before, though I remember there was an article by codersguru on this subject. but in his example the indicator he embedded was quite a simple one, I wonder if it applies to all kinds of indicators? do you perhaps have any experience in this?

iPG

 

I just gave embedding a try. by the time I got a working version, I realised that it wouldn't really work. :-(

suppose you have the following trading rules:

1. buy when price breaks the last 20 day's high,

2. sell when price breaks the last 20 day's low,

3. only take the 1st signal of the breaking. that means, for example, if you missed a buy signal, you can not buy no matter how long the price keeps breaking the last 3 day's high.

now if I got my EA to give me signals by checking for the breaking of the last 3 day's high/low, what would happen if it got disconnected and missed the first break to the upside? it wouldn't know, and it would place a new order as soon as price broke again to the upside, thus violating my trading rule.

though this problem could be solved by instructing the EA to look further back in time, when buy conditions were met, until it found a sell signal, then the 1st buy signal after that would be the original signal that should have been taken.

but there is still another problem. this looking back in time has to be triggered by either fulfilling the buy or the sell conditions. waht if right after breaking to the upside, price drops back within the last 3 day's range, and stayed there for 2 days, then unexpectedly, it shoots up, way above the original buying point. EA would be triggered to look back for signals now, but it'll be too late when it returns with the correct findings, price is already out of reach...

though this could be avoided by having the EA always check for signals, instead of checking only when either condition was met. but the rule is to take the 1st signal only, and only once. what if EA starts, sees that there is no order in the pool, looks back in time, finds a signal, and sends the order without knowing that the order had already been taken, and that it was either stopped out or took profit? again this would be violating the rules...

though this could be solved by keeping a trade log, EA starts, reads the log, finds old signal, compare the two. if same, no trade, otherwise, issue new trade. then we come back to my original question - how to make sure that new trades won't be missed by doing this?

the last record could be from two or more trades ago, and it happened to be the same as the new signal. will a serial number solve this problem? I'll have to think about it.

 
 

Addendum to my last post:

Perhaps rather fix your indicators so that they do the checking for an initial 20 bar break at the indicator level and ignore subsequent breaks, that way your EA does not have to bother with such things and can safely assume that all trades in the log file created by the indicator where in fact real trades.

 

This thread has some breakout EA's apparently related to Turtle system:

https://www.mql5.com/en/forum/general

 

I was rewritting my Indicator when I realised that serial numbers wouldn't work.

when I start MT4 the following will happen.

1. Indicator checks for the last valid signal on the chart and finds it, let's call it the newSig, it has 2 properties: type and price;

2. indicator reads the last recorded signal from a log file, let's call it the lastSig, it has three values: type, price and serial,

3. indicator then tries to compare newSig with lastSig. because it's impossible to get newSig's serial number (because it doesn't have one in the 1st place), the comparison won't work. indicator will either ignore the newSig or ignore the lastSig, depends on how it was configured. either won't be desirable.

so I went back to my number of bars before signal solution.

1. indicator checks for the last valid signal on the chart and fins it, records it to a file with three values: type, price, number of bars before.

2. EA reads the 3 values from the file, if there are no entries in its own log, it makes the trade, and record the same 3 values to its own log.

3. if it already had a log, it opens and reads from it the 3 vlaues, and compare them with the indicator's records. if the number of bars from the indicator's record is greater than from the EA's, new trade, otherwise, no new trade.

the only problem with this approach is that before I can use the EA on a chart for the 1st time, I have to manually download all the available historical data from the server (by pressing the 'end' key while the chart window is active), afterwards the EA and the indicator will work just fine. I have given up on the embedding attempt for the time being, it requires too radical a structural change of my EA, maybe at some later time I will have a go at it again. meanwhile, I'm going to check out the link you provided on the turtle system. :-)

have a good day.

iPG