2 MA Cross - MetaTrader 4 Expert | MT4 EA Download - MetaTrader 4 Resources
We will start creating this EA by defining input variables.
//--- input parameters Input integer period_ma_fast = 8 ; //Period fast moving average Input integer period_ma_slow = 20 ; //Period slow MA Enter double profit = 20.0 ; // Take profit (points) Enter double stop loss = 20.0 ; //Stop loss (points) Enter double lot size = 0.10 ; //Batch size Enter double minimum assets = 100.0 ; // minimum. Equity (USD) Enter integer slippage = 3 ; //Slippage Enter the integer magic number = Chapter 889 ; //magic number
Next is to define global variables. Variables with this global scope will be known or accessed by all functions.
//variable global Double My Points = 0.0 ; Integer My Slippage = 0 ; Integer Buy Tickets = 0 ; Integer Sell Tickets = 0 ;
When the EA is executed, the first function executed is OnInit(). So we often use this function to verify and initialize global variables that will be used.
Integer initialization () { //validasi input, sebiknya kita selalu melakukan validasi pada initialisasi data input if (period_ma_fast >= period_ma_slow || takeProfit < 0.0 || stop loss < 0.0 || lot < 0.01 || minimum net asset < 10 ){ alert ( "Warning - Input data initially valid" ); return ( INIT_PARAMETERS_INCORRECT ); } double's minimum volume = symbolinfo double ( symbol (), SYMBOL_VOLUME_MIN ); if (lot size <minimum trading volume) { string pesan = string format ( "volume lebih kecil dari batas yang dibolehkan yaitu %.2f" , minimum volume); alert (bisan); return ( INIT_PARAMETERS_INCORRECT ); } My point = GetPipPoint( symbol ()); mySlippage = getSlippage( symbol (), slippage); Return ( initialization successful ); }
When the market price changes (tick), the OnTick() function will be called and execute all instructions/functions contained in the OnTick() function block.
Various other functions are called internally in the OnTick() function.
Start calling the checkMinEquity() function to control the adequacy of transaction equity. If the equity capital is sufficient (more than the minimum equity capital), a signal variable is declared later and the NewCandle () function is called, which is used to notify when a new candle is formed.
The getSignal() function will read the values of the two moving average indicators and return signal information whether an upward or downward crossover occurs as a buy/sell signal.
Based on this signal information, it will be forwarded to the transaction() function to set an open buy or sell position.
Next, the setTPSL() function is called, which is used to set the take-profit and stop-loss prices.
If the equity does not reach the minimum equity requirement, an alert will be displayed and the EA will be terminated.
Blank check () { if (cekMinEquity()){ integer signal =- 1 ; Boolean isNewCandle = NewCandle( period (), symbol ()); Signal = getSignal(isNewCandle); trade(isNewCandle, signal); setTPSL(); } other { //Stop trading, stock trading stops printing ( "EA indicates that its equity has expired" ); } }
Function to set TPSL()
blank setTPSL(){ integer order = 0 ; string strMN = ”” , pair = ”” ; Double sl = 0.0 , tp = 0.0 ; pair= symbol (); Orders= Total number of orders (); order ( integer i=t order - 1 ; i >= 0 ; i -) { boolean hourSelect = orderSelect (i, SELECT BY POS, MODE_TRADES); strMN = integer to string (OrderMagicNumber()); if ( string search (strMN, integer to string (magic number), 0 ) == 0 && string search (OrderSymbol(), right, 0 ) == 0 ){ if (OrderType() == OP_BUY && (OrderTakeProfit() == 0 ||OrderStop() == 0 ) ){ if (profit > 0 ) { tp = OrderOpenPrice() + (takeProfit * myPoint); } other { tp = order opening price(); } if (stoploss> 0 ){ sl = OrderOpenPrice() - (stopLoss * myPoint); } other { sl = order stop loss(); } if (OrderTakeProfit() != tp || OrderStopLoss() != sl ){ if (OrderModify(OrderTicket(), OrderOpenPrice(), sl, tp, 0 , clear blue )) { Print ( "Order modified successfully" ); } } } if (OrderType() == OP_SELL && (OrderTakeProfit() == 0 ||OrderStop() == 0 ) ){ if (profit > 0 ) { tp = OrderOpenPrice() - (takeProfit * myPoint); } other { tp = order opening price(); } if (stoploss> 0 ){ sl = OrderOpenPrice() + (stopLoss * myPoint); } other { sl = order stop loss(); } if (OrderTakeProfit() != tp || OrderStopLoss() != sl ){ if (OrderModify(OrderTicket(), OrderOpenPrice(), sl, tp, 0 , red blood cells )) { Print ( "Order modified successfully" ); } } } } //If the magic number && pair ends } //Finish }
For Indonesian language education and sharing, please join our Telegram groupt.me/codeMQL
If you are looking for an app to support your trading, download our SignalForex app on the Play Store
https://play.google.com/store/apps/details?id=com.autobotfx.signalforex
Attachment download
📎 macross.mq4 (6.57 KB)
Source: MQL5 #34176
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •