I am still wrapping my head around mql5, would anyone be so kind as to let me know what I'm doing wrong?
When I try to compile it says: "OnCalculate function declared with wrong type or/and parameters"
I need to make an EA, can I do the calculation in the EA or should I build the indicator separately?See "CDC ATR Trailing Stop V1" on TradingView for reference.
There are only two ways to define OnCalculate() - refer to https://www.mql5.com/en/docs/event_handlers/oncalculate.
So this is wrong:
int OnCalculate( const int rates_total, const int prev_calculated, const double& open[], const double& high[], const double& low[], const double& close[])
- www.mql5.com
The error message tells you what you did wrong. <Deleted>, compare your code to the documentation. Post your code if you still can't understand.
Do you really expect an answer? We can't see your broken code. There are no mind readers here and our crystal balls are cracked.
There are only two ways to define OnCalculate() - refer to https://www.mql5.com/en/docs/event_handlers/oncalculate.
So this is wrong:
The error message tells you what you did wrong. <Deleted>, compare your code to the documentation. Post your code if you still can't understand.
Do you really expect an answer? We can't see your broken code. There are no mind readers here and our crystal balls are cracked.
Hi William, I've changed the code a little .. I dont get any compiler errors but nothing is plotted to the chart.. could you tell me what Im doing wrong?
He's already used "<Deleted>", so you better read the manuals... for a start, I suggest you read https://www.mql5.com/en/docs/indicators/iatr.
- www.mql5.com
so I found a working ATR trailing stop from someone in the forums and I'm building an EA based on it... I'm testing it now but its not working correctly, stop loss didnt trigger. This code is inside OnTick. Ill upload the whole EA and indicator (chandelier.mq5) files.
I dont want a trailing stop order, I want to close the position when period closes above/below trailing stop line. If I'd use a
trailing stop it will just trigger at the touch.
If you can just check it out and tell me what Im doing wrong so I can get on trying to fix it I'd really appreciate it man.
He's already used "<Deleted>", so you better read the manuals... for a start, I suggest you read https://www.mql5.com/en/docs/indicators/iatr.
if(CopyBuffer(hATR_stop,2,0,3,atr_long_stop)<0 || CopyBuffer(hATR_stop,3,0,3,atr_short_stop)<0) { Alert("Error copying ATR trailing stop indicator buffers - error:",GetLastError(),"!!"); return; } //---- insert copybuffer for moving average //---- //---- bool long_open=false; bool short_open=false; if(PositionSelect(_Symbol) ==true) { if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) { long_open = true; } else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) { short_open = true; } } last_close = rate[1].close; previous_close = rate[2].close; if(last_close >= atr_short_stop[1] && previous_close <= atr_short_stop[2]) { if(short_open) { trade.PositionClose(_Symbol); } // buy long request.action = TRADE_ACTION_DEAL; request.price = NormalizeDouble(latest_price.ask,_Digits); request.symbol = _Symbol; request.volume = Lot; request.magic = EA_magic; request.type = ORDER_TYPE_BUY; request.type_filling = ORDER_FILLING_FOK; request.deviation = 100; // send order if(PositionSelect(_Symbol) ==false) { int ticket = OrderSend(request,result); if(result.retcode==10009 || result.retcode==10008) //Request is completed or order placed { Alert("A Buy order has been successfully placed with Ticket#:",result.order,"!!"); } else { Alert("The Buy order request could not be completed -error:",GetLastError()); ResetLastError(); return; } } } else if(last_close <= atr_long_stop[1] && previous_close >= atr_long_stop[2]) { if(long_open) { trade.PositionClose(_Symbol); } // sell short request.action = TRADE_ACTION_DEAL; request.price = NormalizeDouble(latest_price.bid,_Digits); request.symbol = _Symbol; request.volume = Lot; request.magic = EA_magic; request.type = ORDER_TYPE_SELL; request.type_filling = ORDER_FILLING_FOK; request.deviation = 100; //send order if position is closed if(PositionSelect(_Symbol) ==false) { int ticket = OrderSend(request,result); if(result.retcode==10009 || result.retcode==10008) //Request is completed or order placed { Alert("A Sell order has been successfully placed with Ticket#:",result.order,"!!"); } else { Alert("The Sell order request could not be completed -error:",GetLastError()); ResetLastError(); return; } } }
so I found a working ATR trailing stop from someone in the forums and I'm building an EA based on it... I'm testing it now but its not working correctly, stop loss didnt trigger. This code is inside OnTick. Ill upload the whole EA and indicator (chandelier.mq5) files.
I dont want a trailing stop order, I want to close the position when period closes above/below trailing stop line. If I'd use a trailing stop it will just trigger at the touch.
If you can just check it out and tell me what Im doing wrong so I can get on trying to fix it I'd really appreciate it man.
You forgot this line:
ArraySetAsSeries(atr_long_stop, true); ArraySetAsSeries(atr_short_stop, true); ArraySetAsSeries(rate, true); ArraySetAsSeries(ma, true);
This resulted in wrong values assigned to your previous_close.
Another bigger problem is the main logic - where you check close prices above or below the trailing stop lines. Run chandelier.mq5 on a chart and observe - you'll notice that your checks for open and close orders rarely pin-point the right bar for opening and closing - because when price close above short_stop, most of the time short_stop already becomes EMPTY_VALUE, and long_stop begins to appear. The same is true for the other direction.
So you'll need to rework your 'if' checks for the open/close parts.
Thank you!
I'm still testing it but think I fixed it. I did:
if(last_close >= atr_short_stop[2] && previous_close <= atr_short_stop[2])
and ..
else if(last_close <= atr_long_stop[2] && previous_close >= atr_long_stop[2]
Seng Joo Thio:
You forgot this line:
This resulted in wrong values assigned to your previous_close.
Another bigger problem is the main logic - where you check close prices above or below the trailing stop lines. Run chandelier.mq5 on a chart and observe - you'll notice that your checks for open and close orders rarely pin-point the right bar for opening and closing - because when price close above short_stop, most of the time short_stop already becomes EMPTY_VALUE, and long_stop begins to appear. The same is true for the other direction.
So you'll need to rework your 'if' checks for the open/close parts.
Thank you!
I'm still testing it but think I fixed it. I did:
and ..
Yes, these lines should capture more signals than your original code, but still won't capture all. Example:
The circled bars will give you a close sell and open buy signal without a stop loss... so your 'if' checks must also consider such cases... or even look for additional indicators to complement this...
- 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 trying to implement an ATR trailing stop indicator . The compiler doesn't throw any errors. Nothing is plotted to the chart.
See "CDC ATR Trailing Stop V1" on TradingView for reference.