Thanks, that´s correct. But still does not work. All passed results are zeros, and it is not possible that the condition had never matched, neither for buy nor for sell. There must be an error in the structure. Here is the code with the proper condition:
int diff = bfast[0]-bslow[0]; if( (diff=0) && (bfast[1]<bslow[1]) && (glBuyPlaced = false) && ((positionType != POSITION_TYPE_BUY) || (openPosition == false))) { request.action = TRADE_ACTION_DEAL; request.symbol = _Symbol; request.volume = 1+currVolume; request.price = ask; request.type_filling = ORDER_FILLING_FOK; request.sl = 0; request.tp = 0; request.deviation = 50; OrderSend(request,result); if (result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE) { request.action = TRADE_ACTION_SLTP; do Sleep(100); while(PositionSelect(_Symbol) == false); double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN); if(Inp_sl > 0) request.sl = positionOpenPrice - (Inp_sl * _Point); if(Inp_tp > 0) request.tp = positionOpenPrice + (Inp_tp * _Point); if(request.sl > 0 && request.tp > 0) OrderSend(request,result); glBuyPlaced = true; glSellPlaced = false; } } else if((diff==0) && (bfast[1]-bslow[1]>0) && (glSellPlaced == false) && (positionType != POSITION_TYPE_SELL)) { request.action = TRADE_ACTION_DEAL; request.type = ORDER_TYPE_SELL; request.symbol = _Symbol; request.volume = 1 + currVolume; request.type_filling = ORDER_FILLING_FOK; request.price = bid; request.sl = 0; request.tp = 0; request.deviation = 50; OrderSend(request,result); if((result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE) && (Inp_sl > 0 || Inp_tp > 0)) { request.action = TRADE_ACTION_SLTP; do Sleep(100); while(PositionSelect(_Symbol) == false); double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN); if(Inp_sl > 0) request.sl = positionOpenPrice + (Inp_sl * _Point); if(Inp_tp > 0) request.tp = positionOpenPrice - (Inp_tp * _Point); if(request.sl > 0 && request.tp > 0) OrderSend(request,result); glBuyPlaced = false; glSellPlaced = true; } }
Also, I received an "Array out of range" error while trying to test, in the rows in which bfast[1] and bslow[1] were present. I thought the size of the buffer was the problem (0) and added these lines in OnIit()
ArrayResize(bfast,2,0); ArrayResize(bslow,2,0);
Does it makes sense to you? How can I even debug without "Comment" or "Print"..?
Thanks, that´s correct. But still does not work. All passed results are zeros, and it is not possible that the condition had never matched, neither for buy nor for sell. There must be an error in the structure. Here is the code with the proper condition:
Also, I received an " Array out of range " error while trying to test, in the rows in which bfast[1] and bslow[1] were present. I thought the size of the buffer was the problem (0) and added these lines in OnIit()
Does it makes sense to you? How can I even debug without "Comment" or "Print"..?
Please correct errors and attach the full file (using the button )
I already moved the definition of handlers in OnInit(). Attached the file
Senseless code - you just took the code and jotted it all together.
You have declared two arrays
int hfast,hslow; double bfast[],bslow[]; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit ()
as I understand it, they should contain the values of the indicators - but you NEVER even try to get data from the indicators. Correct this error and re-attach the full advisor.
I moved the lines with the CopyBuffer function in OnTick(), if I got your hint.
If you meant instead I don´t even retrieve data from indicators, then
hfast = iMA(_Symbol,_Period,Inp_FastMA,0,MODE_SMMA,PRICE_CLOSE); hslow = iMA(_Symbol,_Period,Inp_SlowMA,0,MODE_SMMA,PRICE_CLOSE);
I am not sure what is wrong with that..
Thanks!
I moved the lines with the CopyBuffer function in OnTick(), if I got your hint.
If you meant instead I don´t even retrieve data from indicators, then
I am not sure what is wrong with that..
Thanks!
Array indexing starts at '0'.
We all agree on this, an bfast[1] should contain the value of the previous tick. Right?
We all agree on this, an bfast[1] should contain the value of the previous tick. Right?
For ordinary people, yes, but not in your case.
You FORGOT about ArraySetAsSeries (xxxx, true). You copy ONLY one item from the indicator and then try to access index [1].
OK.. count is the amount to be copied, not some kind of step! Silly msitake. But still:
int OnInit() { hfast = iMA(_Symbol,_Period,Inp_SlowMA,0,MODE_SMMA,PRICE_CLOSE); hslow = iMA(_Symbol,_Period,Inp_SlowMA,0,MODE_SMMA,PRICE_CLOSE); //--- //--- return(INIT_SUCCEEDED); } // [...] void OnTick() { // [...] CopyBuffer(hfast,0,0,3,bfast); CopyBuffer(hslow,0,0,3,bslow); ArraySetAsSeries(bfast,true); ArraySetAsSeries(bslow,true);
all variables are defined globally.
Handler is "loaded", arrays normalized with the first value corresponding to the latest tick, arrays filled with CopyBuffer. At this point b[0], b[1] and b[2] have a value. At the second tick b[0] will have the new value and b[1] would still be the previous value, because of ArraySetAsSeries after CopyBuffer function. Still results al

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello all,
I looked on past posts here and elsewhere, tried, tried and tried but still I am stuck. Can anyone lend a hand? I simply want to write a basic EA that works. This is my code:
The condition have obsiouly no meaning, was an attempt to simplify it and being sure the problem was not there.
This should be the most basic, naive, model for an EA, can not get why it doesn´t work. Trying to backtesting no trade at all takes place, and with MT5 I don´t know any way to debug it via the strategy tester: with optimization (is there a way to disable ti in MT5?) no "Print", "Comment", "PlaySound" are possible.
Any hint would be appreciated... Thanks!