/*[[ Name := EMAPSar Author := Copyright © 2005, Joel Florian Link := www.sapagreenhouses.com Notes := Lots := 1 Stop Loss := 35 Take Profit := 65 Trailing Stop := 10 ]]*/ defines: Slip(2),cciPeriod(14),CCIMA(5),MAPeriod(100),marginlimit(1000); var: cnt(0),pos(0),sl(0),tp(0),opentrade(0),CCImacurrent(0),ccimapast(0),ccicnt(0); // initial data checks For ccicnt = 1 to ccima Begin CCimacurrent = ccimacurrent + (icci(cciperiod,ccicnt-1)); ccimapast = ccimapast + (icci(cciperiod,ccicnt)); End; ccimacurrent = ccimacurrent/ccima; ccimapast = ccimapast/ccima; // it is important to make sure that the Expert Advisor runs on a normal chart and that // the user has correctly set the external variables (Lots, StopLoss, // TakeProfit, TrailingStop) // in our case we only check the TakeProfit If Bars<200 or TakeProfit<10 then Exit; // less than 200 bars on the chart // now we have to check the status of the trading terminal. // we are going to see whether there are any previously opened positions or orders. If TotalTrades<1 then { // there are no opened orders //First make sure no trades have executed in the last 10 seconds if curtime - LastTradeTime <10 then exit // just to be on the safe side, we make sure we have free funds on our account. // the "1000" value is taken just as an example, usually it is possible to open 1 lot If FreeMargin -100 and ccimapast <= -100 and PriceBid>ima(MAPeriod,mode_ema,0) then { SetOrder(OP_BUY,Lots,Ask,slip,bid-stoploss*point,Ask+TakeProfit*Point,RED); // executing Exit; // exiting, since after the execution of a trade // there is a 10-second trading timeout }; // checking for the possibility of taking a short position (SELL) If ccimacurrent < 100 and ccimapast >= 100 and Priceask= 100 and Priceask0 then // the user has put a trailing stop in his settings { // so, we set out to check it If (Bid-OrderValue(cnt,VAL_OPENPRICE))>(Point*TrailingStop) then { If OrderValue(cnt,VAL_STOPLOSS)<(Bid-Point*TrailingStop) then { ModifyOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_OPENPRICE), Bid-Point*TrailingStop,OrderValue(cnt,VAL_TAKEPROFIT),Red); Exit; }; }; }; } else // otherwise it is a short position { // we check - maybe, it's already time to close it? If ccimacurrent > -100 and ccimapast <= -100 and PriceBid>ima(MAPeriod,mode_ema,0) then { CloseOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_LOTS),Ask,3,Violet); Exit; // exiting }; // we check - maybe, we already may or it's already time to set a trailing stop? If TrailingStop>0 then // the user has put a trailing stop in his settings { // so, we set out to check it If (OrderValue(cnt,VAL_OPENPRICE)-Ask)>(Point*TrailingStop) then { If OrderValue(cnt,VAL_STOPLOSS)=0 or OrderValue(cnt,VAL_STOPLOSS)>(Ask+Point*TrailingStop) then { ModifyOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_OPENPRICE), Ask+Point*TrailingStop,OrderValue(cnt,VAL_TAKEPROFIT),Red); Exit; }; }; }; }; }; }; // the end.