- This topic has 5 replies, 3 voices, and was last updated 9 months, 2 weeks ago by
Rob Dowel.
-
AuthorPosts
-
September 24, 2018 at 4:53 pm #2100
Kiaria Yoshida
KeymasterIn this thread I would like to quickly analyze a simple Expert Adviser.
We will have a quick look at it’s functionality, but most importantly, we will familiarize ourselves with integral blocks, which can be found in any EA for MT4. So, sit back and enjoy our little journey.September 24, 2018 at 4:54 pm #2101Kiaria Yoshida
KeymasterCode
//+------------------------------------------------------------------+ //| My_First_EA.mq4 | //| Kirill | //| [email protected] | //+------------------------------------------------------------------+ #property copyright "Kirill" #property link "[email protected]" //---- input parameters extern double TakeProfit=350.0; extern double Lots=0.1; extern double TrailingStop=35.0; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } int Crossed (double line1 , double line2) { static int last_direction = 0; static int current_dirction = 0; if(line1>line2)current_dirction = 1; //up if(line1<line2)current_dirction = 2; //down if(current_dirction != last_direction) //changed { last_direction = current_dirction; return (last_direction); } else { return (0); } } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- int cnt, ticket, total; double shortEma, longEma; if(Bars<100) { Print("bars less than 100"); return(0); } if(TakeProfit<10) { Print("TakeProfit less than 10"); return(0); // check TakeProfit } shortEma = iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,0); longEma = iMA(NULL,0,13,0,MODE_EMA,PRICE_CLOSE,0); int isCrossed = Crossed (shortEma,longEma); total = OrdersTotal(); if(total < 1) { if(isCrossed == 1) { ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point, "My EA",12345,0,Green); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice()); } else Print("Error opening BUY order : ",GetLastError()); return(0); } if(isCrossed == 2) { ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0, Bid-TakeProfit*Point,"My EA",12345,0,Red); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice()); } else Print("Error opening SELL order : ",GetLastError()); return(0); } return(0); } for(cnt=0;cnt<total;cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()) { if(OrderType()==OP_BUY) // long position is opened { // should it be closed? if(isCrossed == 2) { OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position return(0); // exit } // check for trailing stop if(TrailingStop>0) { if(Bid-OrderOpenPrice()>Point*TrailingStop) { if(OrderStopLoss()<Bid-Point*TrailingStop) { OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green); return(0); } } } } else // go to short position { // should it be closed? if(isCrossed == 1) { OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position return(0); // exit } // check for trailing stop if(TrailingStop>0) { if((OrderOpenPrice()-Ask)>(Point*TrailingStop)) { if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0)) { OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red); return(0); } } } } } } return(0); } //+------------------------------------------------------------------+
September 24, 2018 at 4:55 pm #2102Kiaria Yoshida
KeymasterThe idea behind our expert advisor.
Before digging into cracking our code we have to explain the idea behind our expert advisor. Any expert advisor has to decide when to enter the market and when to exit.
And the idea behind any expert advisor is what the entering and exiting conditions are.
Our expert advisor is a simple one and the idea behind it is a simple too, let’s look at it.We use two EMA indicators, the first one is the EMA of 8 days (sort EMA) and the second one is the EMA of 13 days (long EMA).
Note: using exactly these EMAs or any other thought in this lesson is not a recommendation of mine, they are for educational purpose only.
Entering (Open):
Our expert advisor will enter the market when the short EMA line crosses the long EMA line, the relative locations of the lines will determine the order type:
If the short EMA is above the long EMA we will BUY (long).
If the short EMA is below the long EMA we will SELL (short).We will open only one order at the same time.
Exiting (Close):
Our expert advisor will close the buy order when the short EMA crosses the long EMA and the short EMA ends up below the long EMA.
And will close the sell order when the short EMA crosses the long EMA and the short EMA ends up above the long EMA.Our order (buy or sell) will also be automatically closed when the Take profit or the Stop loss points of profit/loss are reached.
September 24, 2018 at 4:56 pm #2103Kiaria Yoshida
KeymasterModifying:
Beside entering (opening) and exiting (closing) the market (positions) our expert advisor has the ability to modify existing positions based on the idea of Trailing stop point.
We will know how we implement the idea of Trialing stop later in this lesson.
Now let’s resume our code cracking.
//---- input parameters extern double TakeProfit=350.0; extern double Lots=0.1; extern double TrailingStop=35.0;
In the above lines we have asked the wizard to declare three external variables (which the user can set from the properties window of our expert advisor).
The three variables are all double data type. We have initialized them to default values (the user can change these values from the properties window, but it recommended to leave the defaults).
I have to pause again to tell you a little story about these variables.
Stop loss:
It’s a limit point you set to your order. When reached the order will be closed, this is useful to minimize your loss when the market goes against you.
Stop loss points are always set below the current asking price on a buy or above the current bid price on a sell.Trailing Stop
It’s a kind of stop loss order that is set at a percentage level below (for a long position) or above (for a short position) the market price. The price is adjusted as the price fluctuates.
We will talk about this very important concept later in this lesson.
June 20, 2020 at 1:28 pm #18004Robert
ParticipantIn the forex market, a lot of traders lost their money because of fake or bad brokers. Brokers play an important role in trading. Making profit or having loss is mainly depends on the broker. while choosing a broker, a trader should research properly about the broker. I always trade with Tpglobalfx. Because they give me proper guideline at the time of trading. They also help me to do comfortable trade. So, for me they are perfect broker for me.
July 5, 2020 at 12:45 am #20242Rob Dowel
ParticipantEvery trader choose that kind of broker which has low spreads. Spreads is a very important tool in forex trading. Sometimes high spreads put a trader into danger. he may face loss because of high spreads. I am currently trading with Tpglobalfx. They give low spreads starts from 0.01 pips. It is very lower than most other brokers in the forex market. I love them because they don’t change their rates. They always be helpful for me. I love them because of their low spreads and easy trading techniques.
-
AuthorPosts
- You must be logged in to reply to this topic.