Hello, I've started automated trading for 10 days now i've been since then programming my first system based on ema crossings. I've compiled it and when I backtest it, it draws perfectly the two EMA I need but the EA doesn't open positions and I don't understand why, if you could help me out. Thanks
I give you the program :
This is a function :
void x(int type)
- But you are not calling this function anywhere, so this code isn't executed.
- In your function x, you set a MQLTradeRequest but you don't send this request to the trading server, you have to use OrderSend().
- OnTrade() is a function called when an event Trade is triggered, you don't have to procedd trading here.
- Also please use to Styler (Ctrl+,) before posting code, it's more easy to read.
Thank for helping me, i've removed the x and ontrade functions, added order send, and modified a few things hoping it'd work but it still doesn't.
I give you the newest one (i don't manage to use the styler function)
//+------------------------------------------------------------------+ //| ProjectName | //| Copyright 2012, CompanyName | //| http://www.companyname.net | //+------------------------------------------------------------------+ #include "Experts4.mq5" input int MAPeriod = 10; input ENUM_MA_METHOD MAMethod = MODE_EMA; input ENUM_APPLIED_PRICE MAPrice = PRICE_CLOSE; input int MAShift = 0; input int MaPeriod = 3; input ENUM_MA_METHOD MaMethod = MODE_EMA; input ENUM_APPLIED_PRICE MaPrice = PRICE_CLOSE; input int MaShift = 0; input double Lots = 0.1; input int StopLoss = 100; input int TakeProfit = 100; input int Magic = 666; double EMAFast[2],EMASlow[2]; int HEMAFast,HEMASlow; int B,S; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int OnInit() { HEMASlow=iMA(NULL,PERIOD_CURRENT,MAPeriod,MAShift,MAMethod,MAPrice); if(HEMASlow==INVALID_HANDLE) { Alert("Indicator launch has failed"); return(-1); } HEMAFast=iMA(NULL,PERIOD_CURRENT,MaPeriod,MaShift,MaMethod,MaPrice); if(HEMAFast==INVALID_HANDLE) { Alert("Indicator launch has failed"); return(-1); } return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { if(HEMASlow!=INVALID_HANDLE)IndicatorRelease(HEMASlow); if(HEMAFast!=INVALID_HANDLE)IndicatorRelease(HEMAFast); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnTick() { ArraySetAsSeries(EMASlow,true); ArraySetAsSeries(EMAFast,true); int A=CopyBuffer(HEMASlow,0,1,2,EMASlow); int Z=CopyBuffer(HEMAFast,0,1,2,EMAFast); if(A<=0 || B<=0)return; if(!PositionSelect(Symbol())) if(EMAFast[1]>EMASlow[1] && EMAFast[0]<=EMASlow[0]) {S=1;}else{S=0;} if(!PositionSelect(Symbol())) if(EMAFast[1]<EMASlow[1] && EMAFast[0]>=EMASlow[0]) {B=1;}else{B=0;} if(PositionSelect(Symbol()) && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) if(EMAFast[1]>EMASlow[1] && EMAFast[0]<=EMASlow[0]) {S=2;}else{S=0;} if(PositionSelect(Symbol()) && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL) if(EMAFast[1]<EMASlow[1] && EMAFast[0]>=EMASlow[0]) {B=2;}else{B=0;} if(S>=1) { MqlTradeRequest req = {0}; MqlTradeResult res = {0}; req.action = TRADE_ACTION_DEAL; req.magic = Magic; req.symbol = Symbol(); req.volume = S*Lots; req.sl = StopLoss; req.tp = TakeProfit; req.deviation = 0; req.type_filling = ORDER_FILLING_IOC; req.type = ORDER_TYPE_SELL; req.price = SymbolInfoDouble(Symbol(),SYMBOL_BID); OrderSend(req,res); return; } if(B>=1) { MqlTradeRequest req = {0}; MqlTradeResult res = {0}; req.action = TRADE_ACTION_DEAL; req.magic = Magic; req.symbol = Symbol(); req.volume = B*Lots; req.sl = StopLoss; req.tp = TakeProfit; req.deviation = 0; req.type_filling = ORDER_FILLING_IOC; req.type = ORDER_TYPE_BUY; req.price = SymbolInfoDouble(Symbol(),SYMBOL_ASK); OrderSend(req,res); return; } } //+------------------------------------------------------------------+
Edited : I replace your code with styler applied.
Thank for helping me, i've removed the x and ontrade functions, added order send, and modified a few things hoping it'd work but it still doesn't.
I give you the newest one (i don't manage to use the styler function)
Your EA never go beyond :
if(A<=0 || B<=0)return;Can you see why ? ;-)
Yes that's because I should have put Z, B is the variable used to buy and it's worth 0 at the beginning. Now that I have fixed this the algorithm do generate orders but first on a wrong side (when it should buy it sells ...)
Second, noticing that stoploss doesn't work too I've modified this :
req.sl = StopLoss; req.tp = TakeProfit;
into this (when selling) : (but then it doesn't work anymore and i again don't understand why ...)
req.sl = SymbolInfoDouble(Symbol(),SYMBOL_BID)+StopLoss*Point(); req.tp = SymbolInfoDouble(Symbol(),SYMBOL_BID)-TakeProfit*Point();
*For the 2nd question, i've found the answer, that's because stop profit was equal to 0.
You used the same input parameters for both MA.
input int MAPeriod= 10; input ENUM_MA_METHOD MAMethod= MODE_EMA; input ENUM_APPLIED_PRICE MAPrice = PRICE_CLOSE; input int MAShift = 0; input int MaPeriod = 3; input ENUM_MA_METHOD MaMethod = MODE_EMA; input ENUM_APPLIED_PRICE MaPrice = PRICE_CLOSE; input int MaShift = 0;
Yes I do use the same inputs for both MA their periods are not the same (in order to make them cross each other) so I do not understand why this is a problem ...
I want to be sure : does the piece of code below mean
if there is no positions opened
if the next to last quote of the fast Ema is higher than the next to last of the slow ema, and if the last quote of the fast ema is now lower than or equals to the last quote of the fast ema :
S=1 otherwise S=0 ???? Am I right ?
if(!PositionSelect(Symbol())) if(EMAFast[1]>EMASlow[1] && EMAFast[0]<=EMASlow[0]) {S=1;}else{S=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
Hello, I've started automated trading for 10 days now i've been since then programming my first system based on ema crossings. I've compiled it and when I backtest it, it draws perfectly the two EMA I need but the EA doesn't open positions and I don't understand why, if you could help me out. Thanks
I give you the program :