You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Dear MQL4 experts,
I am learning MQL4 and building my first expert advisor. I tried to learn as much as I can in the beginning but would need some help with my code
Trade Setup:
--------------
Open Buy Order on Fridays at market open, if the D1 Candle open of the previous day AND todays open are above the EMA (x).
The Order should be closed on Monday at D1 open.
I put in a couple of input variables as I need to adjust them for different instruments in the future.
Issue:
-------
In the journal of the stratefy tester I can see that the order is opened and closed again right away without taking care of the Weekday filter.
The price in the strategy tester window is not moving at all. I see only one order.
The journal says:
- unknown ticket #1 for order close function
- Order Close Error 4108
The Code:
-----------
#property strict int MagicNumber=10000; int MaxTrades=1; // Global external variables //-------------------------- enum dayOfWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, }; input dayOfWeek TradeOpenDay = Friday; //TradeOpenDay of the week input dayOfWeek TradeCloseDay = Monday; //TradeCloseDay of the week input int EMAperiod = 10; //EMA period input double SL_ATR_Multi = 1.00; //SL_ATR_Multiplikator input double TP_ATR_Multi = 3.00; //TP_ATR_Multiplikator input double LotSize = 0.01; //Lotsize Traded input int MaxSlippage = 10; //Maximum Slippage //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //----------------------- // Trade Entry Indicators // ----------------------- double EMA=NormalizeDouble((iMA(NULL,0,EMAperiod,0,MODE_EMA,PRICE_CLOSE,1)),Digits); double BarOpen=iOpen(NULL,PERIOD_D1,0); double BarClosePreviousCandle=iClose(NULL,PERIOD_D1,1); //------------------------ // Stop Loss + Take Profit // ----------------------- double StopLossCalculation=NormalizeDouble((iATR(NULL,0,10,1)*SL_ATR_Multi),Digits); double StopLossDistanceFromOpen=(BarOpen-StopLossCalculation); double TakeProfitCalculation=NormalizeDouble((iATR(NULL,0,10,1)*TP_ATR_Multi),Digits); double TakeProfitDistanceFromOpen=(BarOpen+TakeProfitCalculation); // -------------------- LONG ---------------------- // Trade Control Parameters + Ticket Numbers // ------------------------------------------------ bool LongTrade=false; int BuyTicketNumber=OrderSend(Symbol(),OP_BUY,LotSize,Ask,MaxSlippage,StopLossDistanceFromOpen,TakeProfitDistanceFromOpen,"+++ BUY Trade +++",MagicNumber,0,clrGreen); //--------------------- //Check for Trade Setup //--------------------- if((BarClosePreviousCandle>EMA) && (BarOpen>EMA) && (DayOfWeek()==TradeOpenDay)) { LongTrade=true; } else LongTrade=false; //---------------- // Place Buy Order //---------------- if(LongTrade==true) { BuyTicketNumber=OrderSend(_Symbol,OP_BUY,LotSize,Ask,MaxSlippage,StopLossDistanceFromOpen,TakeProfitDistanceFromOpen,"+++ BUY Trade +++",MagicNumber,0,clrGreen); } //----------------- // Select Buy Order //----------------- bool BuyOrderSelected=false; if((BuyTicketNumber>0) && (DayOfWeek()==TradeCloseDay)) { BuyOrderSelected=true; } else BuyOrderSelected=false; if (BuyOrderSelected==true) { BuyOrderSelected=OrderSelect(BuyTicketNumber,SELECT_BY_TICKET); } //---------------- // Close Buy Order //---------------- bool CloseLongTrade=false; if(BuyOrderSelected==true) { CloseLongTrade=true; } if(CloseLongTrade==true) { CloseLongTrade=OrderClose(BuyTicketNumber,LotSize,Bid,MaxSlippage,clrBlue); } } //+------------------------------------------------------------------+Thank you for your help