/*[[ Name := Starting expert Author := Copyright © 2002, MetaQuotes Software Corp Link := http://www.metaquotes.ru Notes := Use on EURUSD 1hour charts only Lots := 1 Stop Loss := 0 Take Profit := 200 Trailing Stop := 40 ]]*/ defines: Slippage(2),lot(1),mini(0.1),main(1.0),back(15),MAFastPeriod(16),MASlowPeriod(60); var: cnt(0),mode(0); var: FastMa(0),FastMa2(0),FastMa5(0); var: SlowMa(0),SlowMa2(0),SlowMa5(0); If Bars<200 or TakeProfit<10 or TrailingStop<10 then Exit; // setup values FastMa=iMA(MAFastPeriod,MODE_EMA,0); FastMa2=iMA(MAFastPeriod,MODE_EMA,2); FastMa5=iMA(MAFastPeriod,MODE_EMA,5); SlowMa=iMA(MASlowPeriod,MODE_EMA,0); SlowMa2=iMA(MASlowPeriod,MODE_EMA,2); SlowMa5=iMA(MASlowPeriod,MODE_EMA,5); // if there are no open positions and orders If TotalTrades<1 then { lot=ceil(freemargin/300)*mini; if freemargin>5000 then { lot=ceil(freemargin/3000); }; lot=1; If FreeMargin<100 then Exit; // not enough money // there are no open positions - check the BUY option // the opening condition: // if EMA(16) crosses EMA(60) upwards // and the current bar is bullish (Close>Open), we place // waiting order BUY LIMIT 15 pips below the execution // price for more optimal entering into the market // If FastMa>SlowMa and FastMa2Open then If (FastMa-SlowMa)>=Point and (SlowMa2-FastMa2)>=Point and (SlowMa5-FastMa5)>=Point and Close>Open then { // try to place a waiting order at the (Ask-15) points price // with maximum slippage 2 points, // while not setting Stop Loss and setting Take Profit // 200 points above the opening price. // at the chart an upward green arrow appears SetOrder(OP_BUYLIMIT,Lot,Ask-back*Point,Slippage,0,Ask+(TakeProfit-back)*Point,RED); Exit; // now we exit as we are not allowed to operate the account in the nearest 10 sec }; // the opening SELL condition: // if EMA(16) crosses EMA(60) downwards // and the current bar is bearish (Close=Point and (FastMa2-SlowMa2)>=Point and (FastMa5-SlowMa5)>=Point and Close iMA(MASlowPeriod,MODE_EMA,2) and iMA(MAFastPeriod,MODE_EMA,5) > iMA(MASlowPeriod,MODE_EMA,5) then { // try to close the position at current Bid price CloseOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_LOTS),Bid,Slippage,RED); Exit; }; // Here we check the trailing stop at open position. // Trailing stop ( Stop Loss) of the BUY position is being // kept at level 40 points below the market. // If the profit (current Bid-OpenPrice) more than TrailingStop (40) pips If (Bid-OrderValue(cnt,VAL_OPENPRICE))>(TrailingStop*Point) then { // we have won already not less than 'TrailingStop' pips! If OrderValue(cnt,VAL_STOPLOSS)<(Bid-TrailingStop*Point) then { // move the trailing stop (Stop Loss) to the level 'TrailingStop' from the market ModifyOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_OPENPRICE), Bid-Point*TrailingStop,OrderValue(cnt,VAL_TAKEPROFIT),Red); Exit; }; }; }; If mode=OP_SELL then // if the already opened position were SELL { // check if EMA(16) has crossed already EMA(60) upwards? If iMA(MAFastPeriod,MODE_EMA,0) > iMA(MASlowPeriod,MODE_EMA,0) and iMA(MAFastPeriod,MODE_EMA,2) < iMA(MASlowPeriod,MODE_EMA,2) and iMA(MAFastPeriod,MODE_EMA,5) < iMA(MASlowPeriod,MODE_EMA,5) then { // try to close the position at current Ask price CloseOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_LOTS),Ask,Slippage,RED); Exit; }; // Here we check the trailing stop at open position. // Trailing stop ( Stop Loss) of the BUY position is being // kept at level 40 points below the market. // If the profit (current Bid-OpenPrice) more than TrailingStop (40) pips If (OrderValue(cnt,VAL_OPENPRICE)-Ask)>(TrailingStop*Point) then { // we have won already not less than 'TrailingStop' pips! If OrderValue(cnt,VAL_STOPLOSS)>(Ask+TrailingStop*Point) or OrderValue(cnt,VAL_STOPLOSS)=0 then { // move the trailing stop (Stop Loss) to the level 'TrailingStop' from the market ModifyOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_OPENPRICE), Ask+Point*TrailingStop,OrderValue(cnt,VAL_TAKEPROFIT),Red); Exit; }; }; }; // there is one very important point - the control // over the waiting orders. An order cannot be valid more than 0.5 hour. // After which It should be canceled // For that purpose we compare current time // and time the order is placed If mode>OP_SELL then // this is a waiting order! { // check how long it exists in the trading terminal // time is counted in seconds: // 10 minutes = 600 seconds, 30 minutes = 1800, 1 hour = 3600, 1 day = 86400 If (CurTime-OrderValue(cnt,VAL_OPENTIME))>1800 then { DeleteOrder(OrderValue(cnt,VAL_TICKET),RED); Exit; }; }; }; // the end //Adapted from "Trend Follower" from InterbankFX.com //by JF van Niekerk