It may be a critcal error, whenever this happens to me, failure to preform global initialisation or a "zero divide" is the cause. Is this a multi-currency EA? It may help to know what your journal is saying when the crash occurs.
Please Edit your post and put your code in a single SRC box. It is very difficult to read
I don't understand why you have created the array newTime with just 1 element, there is no point at all.
As well as that your logic is faulty,
static datetime oldTime; // serves as the bar time datetime newTime[1]; // current bar time at each OnTick execution bool IsNewBar = false; // check to see that there is a new tick // copy saved bar time to newTime[0] int copied = CopyTime(_Symbol,_Period,0,1,newTime); // check if data copied successfully if(copied > 0)//------------------------------Just because copied is >0 it doesn't mean that this is a new bar { IsNewBar = true; oldTime = newTime[0]; }
Use something like this - much simpler
static datetime oldTime =0; // serves as the bar time datetime newTime=Time[0]; // current bar time at each OnTick execution bool IsNewBar = false; if(newTime>oldTime) { IsNewBar = true; oldTime = newTime; }
You declare the arrays BBUp[] and BBLow[]
But I don't see anywhere that you ReSize the arrays or assign a value to them.
When you try to get the values for BBUp[1] and BBLow[1], you will get Critical error - "Array out of range"
Thank you for your help. I fixed up my code with some of your suggestions and made it easier to read.
I removed the BBUp[] and BBLow[] from the program, they were from an older version I tried before I did some fixes.
The EA runs the whole way through my backtesting, but orders are not being executed correctly. I'm aiming for the EA to go long after a bar opens below the Lower Bound and closes above, and then closes the order when a bar opens above the Upper Bound and closes below, and vice-versa for short orders. On a 15-min chart, positions are being held for days at a time, when they be being held for only a few hours. Not sure if this is a problem with my order execution or indicators.
if(newTime > oldTime) { IsNewBar = true; oldTime = newTime; } else { Alert("Error in copying historical times data - error: ", GetLastError()); ResetLastError(); return; }
Remove the highlighted code - it is not necessary
Thank you for your help. I fixed up my code with some of your suggestions and made it easier to read.
I removed the BBUp[] and BBLow[] from the program, they were from an older version I tried before I did some fixes.
The EA runs the whole way through my backtesting, but orders are not being executed correctly. I'm aiming for the EA to go long after a bar opens below the Lower Bound and closes above, and then closes the order when a bar opens above the Upper Bound and closes below, and vice-versa for short orders. On a 15-min chart, positions are being held for days at a time, when they be being held for only a few hours. Not sure if this is a problem with my order execution or indicators.
// get handle of the indicators BolBandsUpper = iBands(NULL,0,bands_period,deviation,bands_shift,PRICE_CLOSE,MODE_UPPER,0); BolBandsLower = iBands(NULL,0,bands_period,deviation,bands_shift,PRICE_CLOSE,MODE_LOWER,0);
Are in the OnInitt() function. Bear in mind that OnInit() only runs when the Expert initializes. You need to get these values with each new candle so you will need to move this code into the OnTick function. Also I just noticed that you are getting the values of candle zero which is still in progress. You may want to get the values from finished candle 1 since your trader triggers are based on mrate[1].
The second thing I wanted to mention was your order closing code.
if(mrate[1].close < BolBandsUpper && mrate[1].open > BolBandsUpper) { OrderClose(OrderTicket(),OrderLots(),Bid,3,clrWhite); }Here you need to use OrderSelect() before you call for the OrderTicket(). I know that above you have a for loop that is going through the trades looking to see how many trades are open ..
int total = 0; for(int i = 0; i < OrdersTotal(); i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false) break; if(OrderSymbol() == Symbol() && OrderMagicNumber() == MAGICNUM) total++; }
If you have two trades open and the first one was of the proper Magicnumber and the second order selected was not. You may show only one order open on this pair however the ticketnumber of the last order selected is the one that will be closed. It's best to do an OrderSelect just before you ask for the ticket number to insure you are closing the proper order. This of course is not an issue in strategy tester but can cause real problems in actual use on a real platform trading multiple pairs. Hope this helps you a little...PipPip....Jimdandy.
p.s . You mentioned that your program stops.. keep in mind that it is only going to run on the first tick of each candle so it will stop.. for 15m and then run for 1 tick. Not sure what you mean by 'it stops' I guess.
static datetime oldTime =0; // serves as the bar time datetime newTime=Time[0]; // current bar time at each OnTick execution bool IsNewBar = false; if(newTime>oldTime) { IsNewBar = true; oldTime = newTime; }
Simplified, more: | static datetime newTime =0; datetime oldTime=newTime; newTime=Time[0]; bool isNewBar = newTime != oldTime;Don't use ">", use "!=" in case server clock is off and gets reset back (There is a post on this happening, EA stopped functioning for days) or end of DST. Remember statics don't reset on TF changes. |
Sounds like you are making some progress and learning to code better. I want to mention that the following lines where you get the bollinger band values.
Are in the OnInitt() function. Bear in mind that OnInit() only runs when the Expert initializes. You need to get these values with each new candle so you will need to move this code into the OnTick function. Also I just noticed that you are getting the values of candle zero which is still in progress. You may want to get the values from finished candle 1 since your trader triggers are based on mrate[1].
The second thing I wanted to mention was your order closing code.
If you have two trades open and the first one was of the proper Magicnumber and the second order selected was not. You may show only one order open on this pair however the ticketnumber of the last order selected is the one that will be closed. It's best to do an OrderSelect just before you ask for the ticket number to insure you are closing the proper order. This of course is not an issue in strategy tester but can cause real problems in actual use on a real platform trading multiple pairs. Hope this helps you a little...PipPip....Jimdandy.
p.s . You mentioned that your program stops.. keep in mind that it is only going to run on the first tick of each candle so it will stop.. for 15m and then run for 1 tick. Not sure what you mean by 'it stops' I guess.
Simplified, more: |
Don't use ">", use "!=" in case server clock is off and gets reset back (There is a post on this happening, EA stopped functioning for days) or end of DST. Remember statics don't reset on TF changes. |
How to Close the Buy with the previous upperband close??
It don't work that way,
BolBandsUpper is an handle, not the value of bands
don't use handle do it this way : if you are on MT4
OnTick()
{
double BolBandsUpper = iBands(NULL,0,bands_period,deviation,bands_shift,PRICE_CLOSE,MODE_UPPER,0);
double BolBandsLower = iBands(NULL,0,bands_period,deviation,bands_shift,PRICE_CLOSE,MODE_LOWER,0);
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I'm fairly new to automated trading and trying to create an EA that will trade a strategy that I use with Bollinger Bands and RSI. So far I have the coded the EA to trade off of bollinger bands. The program works fine the first round through when I tested it. One trade is opened and on the next new bar the program just stops. Could anyone help me out with why my EA isn't running the whole time through my strategy tester? My code is below. Thanks.