//+------------------------------------------------------------------+ //| Let's_Trade.mq5 | //| Antoine Blanc | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Antoine Blanc" #property link "http://www.mql5.com" #property version "1.00" //--- #include //--- #include //--- Input parameters input int StopLoss=30; input int TakeProfit=100; input int TrailingStop=0; input int EMA1_Period=5; input int EMA2_Period=8; input int SMA1_Period=200; input int SMA2_Period=120; input int PeriodFast=9; input int PeriodSlow=19; input int PeriodSignal=6; input int PeriodK=14; input int PeriodD=3; input int PeriodS=3; input int EA_Magic=12345; // EA Magic Number input double Lot=0.1; // Lots to Trade //--- Global variables int EMA1Handle; int EMA2Handle; int SMA1Handle; int SMA2Handle; int MACDHandle; int SignalHandle; int StochasticHandle; int SHandle; double EMA1Val[]; double EMA2Val[]; double SMA1Val[]; double SMA2Val[]; double MACDVal[]; double SignalVal[]; double FastVal[],SlowVal[]; double StochasticVal[]; double KVal[],DVal[]; double SVal[]; double p_close; // Variable to store the close value of a bar int STP,TKP; int TSTP; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { EMA1Handle=iMA(_Symbol,_Period,EMA1_Period,0,MODE_EMA,PRICE_CLOSE); EMA2Handle=iMA(_Symbol,_Period,EMA2_Period,0,MODE_EMA,PRICE_CLOSE); SMA1Handle=iMA(_Symbol,_Period,SMA1_Period,0,MODE_SMA,PRICE_CLOSE); SMA2Handle=iMA(_Symbol,_Period,SMA2_Period,0,MODE_SMA,PRICE_CLOSE); MACDHandle=iMACD(_Symbol,_Period,PeriodFast,PeriodSlow,PeriodSignal,PRICE_CLOSE); StochasticHandle=iStochastic(_Symbol,_Period,PeriodK,PeriodD,PeriodS,MODE_SMA,STO_CLOSECLOSE); //--- What if handle returns Invalid Handle if(EMA1Handle<0 || EMA2Handle<0 || SMA1Handle<0 || SMA2Handle<0 || MACDHandle<0 || StochasticHandle<0) { Alert("Error Creating Handles for indicators - error: ",GetLastError(),"!!"); return(-1); } //--- Let us handle currency pairs with 5 or 3 digit prices instead of 4 STP = StopLoss; TKP = TakeProfit; TSTP = TrailingStop; if(_Digits==5 || _Digits==3) { STP = STP*10; TKP = TKP*10; TSTP = TSTP*10; } return(0); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- Release our indicator handles IndicatorRelease(EMA1Handle); IndicatorRelease(EMA2Handle); IndicatorRelease(SMA1Handle); IndicatorRelease(SMA2Handle); IndicatorRelease(MACDHandle); IndicatorRelease(StochasticHandle); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- Do we have enough bars to work with if(Bars(_Symbol,_Period)<60) // if total bars is less than 60 bars { Alert("We have less than 60 bars, EA will now exit!!"); return; } // We will use the static Old_Time variable to serve the bar time. // At each OnTick execution we will check the current bar time with the saved one. // If the bar time isn't equal to the saved time, it indicates that we have a new tick. static datetime Old_Time; datetime New_Time[1]; bool IsNewBar=false; // copying the last bar time to the element New_Time[0] int copied=CopyTime(_Symbol,_Period,0,1,New_Time); if(copied>0) // ok, the data has been copied successfully { if(Old_Time!=New_Time[0]) // if old time isn't equal to new bar time { IsNewBar=true; // if it isn't a first call, the new bar has appeared if(MQL5InfoInteger(MQL5_DEBUGGING)) Print("We have new bar here ",New_Time[0]," old time was ",Old_Time); Old_Time=New_Time[0]; // saving bar time } } else { Alert("Error in copying historical times data, error =",GetLastError()); ResetLastError(); return; } //--- EA should only check for new trade if we have a new bar if(IsNewBar==false) { return; } //--- Do we have enough bars to work with int Mybars=Bars(_Symbol,_Period); if(Mybars<60) // if total bars is less than 60 bars { Alert("We have less than 60 bars, EA will now exit!!"); return; } //--- Define some MQL5 Structures we will use for our trade MqlTick latest_price; // To be used for getting recent/latest price quotes MqlTradeRequest mrequest; // To be used for sending our trade requests MqlTradeResult mresult; // To be used to get our trade results MqlRates mrate[]; // To be used to store the prices, volumes and spread of each bar /* Let's make sure our arrays values for the Rates, ADX Values and MA values is store serially similar to the timeseries array */ // the rates arrays ArraySetAsSeries(mrate,true); // the ADX DI+values array // ArraySetAsSeries(plsDI,true); // the ADX DI-values array // ArraySetAsSeries(minDI,true); // the EMA1 values arrays ArraySetAsSeries(EMA1Val,true); // the EMA2 values arrays ArraySetAsSeries(EMA2Val,true); // the SMA1 values arrays ArraySetAsSeries(SMA1Val,true); // the SMA2 values arrays ArraySetAsSeries(SMA2Val,true); // the MACD values arrays ArraySetAsSeries(FastVal,true); // the MACD values arrays ArraySetAsSeries(SlowVal,true); // the MACD values arrays ArraySetAsSeries(SignalVal,true); // the Stochastic values arrays ArraySetAsSeries(KVal,true); // the Stochastic values arrays ArraySetAsSeries(DVal,true); // the Stochastic values arrays ArraySetAsSeries(SVal,true); //--- Get the last price quote using the MQL5 MqlTick Structure if(!SymbolInfoTick(_Symbol,latest_price)) { Alert("Error getting the latest price quote - error:",GetLastError(),"!!"); return; } //--- Get the details of the latest 3 bars if(CopyRates(_Symbol,_Period,0,3,mrate)<0) { Alert("Error copying rates/history data - error:",GetLastError(),"!!"); ResetLastError(); return; } //--- Copy the new values of our indicators to buffers (arrays) using the handle if(CopyBuffer(EMA1Handle,0,0,3,EMA1Val)<0) { Alert("Error copying Exponantial Moving Average indicator buffer - error:",GetLastError()); ResetLastError(); return; } if(CopyBuffer(EMA2Handle,0,0,3,EMA2Val)<0) { Alert("Error copying Exponantial Moving Average indicator buffer - error:",GetLastError()); ResetLastError(); return; } if(CopyBuffer(SMA1Handle,0,0,3,SMA1Val)<0) { Alert("Error copying Simple Moving Average indicator buffer - error:",GetLastError()); ResetLastError(); return; } if(CopyBuffer(SMA2Handle,0,0,3,SMA2Val)<0) { Alert("Error copying Simple Moving Average indicator buffer - error:",GetLastError()); ResetLastError(); return; } if(CopyBuffer(MACDHandle,0,0,6,FastVal)<0 || CopyBuffer(MACDHandle,0,0,6,SlowVal)<0 || CopyBuffer(MACDHandle,0,0,6,SignalVal)<0) //--- if(CopyBuffer(MACDHandle,0,0,3,MACDVal)<0) { Alert("Error copying MACD indicator buffer - error:",GetLastError()); ResetLastError(); return; } if(CopyBuffer(StochasticHandle,0,0,3,KVal)<0 || CopyBuffer(StochasticHandle,0,0,3,DVal)<0 || CopyBuffer(StochasticHandle,0,0,3,SVal)<0) //--- if(CopyBuffer(StochasticHandle,0,0,3,StochasticVal)<0) { Alert("Error copying Stochastic indicator buffer - error:",GetLastError()); ResetLastError(); return; } //--- we have no errors, so continue //--- Do we have positions opened already? bool Buy_opened=false; // variable to hold the result of Buy opened position bool Sell_opened=false; // variables to hold the result of Sell opened position if(PositionSelect(_Symbol)==true) // we have an opened position { if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) { Buy_opened=true; //It is a Buy } else if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL) { Sell_opened=true; // It is a Sell } } // Copy the bar close price for the previous bar prior to the current bar, that is Bar 1 p_close=mrate[1].close; // bar 1 close price /* 1. Check for a long/Buy Setup : MA-8 increasing upwards, previous price close above it, ADX > 22, +DI > -DI */ //--- Declare bool type variables to hold our Buy Conditions bool Buy_Condition_1 = (SMA2Val[0]>SMA2Val[1]) && (SMA2Val[1]>SMA2Val[2]); // SMA 120Min Increasing upwards bool Buy_Condition_2 = (p_close > EMA1Val[1] && p_close > EMA2Val[1]); // previous price closed above EMA-5 & EMA-8 bool Buy_Condition_3 = (KVal[1]>DVal[1] && DVal[0]>KVal[0] && DVal[0]<80); // Croisement du Stochastic avec sa valeur inférieur à 80 bool Buy_Condition_4 = (FastVal[2]-SlowVal[2]>FastVal[1]-SlowVal[1] && FastVal[0]-SlowVal[0]>FastVal[1]-SlowVal[1]); // Réecartement du MACD //--- Putting all together if(Buy_Condition_1) { if(Buy_Condition_2 && Buy_Condition_3) { // any opened Buy position? if(Buy_opened) { Alert("We already have a Buy Position!!!"); return; // Don't open a new Buy Position } mrequest.action = TRADE_ACTION_DEAL; // immediate order execution mrequest.price = NormalizeDouble(latest_price.ask,_Digits); // latest ask price mrequest.sl = NormalizeDouble(latest_price.ask - STP*_Point,_Digits); // Stop Loss mrequest.tp = NormalizeDouble(latest_price.ask + TKP*_Point,_Digits); // Take Profit mrequest.symbol = _Symbol; // currency pair mrequest.volume = Lot; // number of lots to trade mrequest.magic = EA_Magic; // Order Magic Number mrequest.type = ORDER_TYPE_BUY; // Buy Order mrequest.type_filling = ORDER_FILLING_AON; // Order execution type mrequest.deviation=100; // Deviation from current price //--- send order OrderSend(mrequest,mresult); // get the result code if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed { Alert("A Buy order has been successfully placed with Ticket#:",mresult.order,"!!"); } else { Alert("The Buy order request could not be completed -error:",GetLastError()); ResetLastError(); return; } } if(Buy_Condition_3 && Buy_Condition_4) { // any opened Buy position? if(Buy_opened) { Alert("We already have a Buy Position!!!"); return; // Don't open a new Buy Position } mrequest.action = TRADE_ACTION_DEAL; // immediate order execution mrequest.price = NormalizeDouble(latest_price.ask,_Digits); // latest ask price mrequest.sl = NormalizeDouble(latest_price.ask - STP*_Point,_Digits); // Stop Loss mrequest.tp = NormalizeDouble(latest_price.ask + TKP*_Point,_Digits); // Take Profit mrequest.symbol = _Symbol; // currency pair mrequest.volume = Lot; // number of lots to trade mrequest.magic = EA_Magic; // Order Magic Number mrequest.type = ORDER_TYPE_BUY; // Buy Order mrequest.type_filling = ORDER_FILLING_AON; // Order execution type mrequest.deviation=100; // Deviation from current price //--- send order OrderSend(mrequest,mresult); // get the result code if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed { Alert("A Buy order has been successfully placed with Ticket#:",mresult.order,"!!"); } else { Alert("The Buy order request could not be completed -error:",GetLastError()); ResetLastError(); return; } } } /* 2. Check for a Short/Sell Setup : MA-8 decreasing downwards, previous price close below it, ADX > 22, -DI > +DI */ //--- Declare bool type variables to hold our Sell Conditions bool Sell_Condition_1 = (SMA2Val[0]20); // Current ADX value greater than minimum (22) bool Sell_Condition_4 = (SlowVal[2]-FastVal[2]>SlowVal[1]-FastVal[1] && SlowVal[0]-FastVal[0]>SlowVal[1]-FastVal[1]); // -DI greater than +DI //--- Putting all together if(Sell_Condition_1) { if(Sell_Condition_2 && Sell_Condition_3) { // any opened Sell position? if(Sell_opened) { Alert("We already have a Sell position!!!"); return; // Don't open a new Sell Position } mrequest.action=TRADE_ACTION_DEAL; // immediate order execution mrequest.price = NormalizeDouble(latest_price.bid,_Digits); // latest Bid price mrequest.sl = NormalizeDouble(latest_price.bid + STP*_Point,_Digits); // Stop Loss mrequest.tp = NormalizeDouble(latest_price.bid - TKP*_Point,_Digits); // Take Profit mrequest.symbol = _Symbol; // currency pair mrequest.volume = Lot; // number of lots to trade mrequest.magic = EA_Magic; // Order Magic Number mrequest.type= ORDER_TYPE_SELL; // Sell Order mrequest.type_filling = ORDER_FILLING_AON; // Order execution type mrequest.deviation=100; // Deviation from current price //--- send order OrderSend(mrequest,mresult); // get the result code if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed { Alert("A Sell order has been successfully placed with Ticket#:",mresult.order,"!!"); } else { Alert("The Sell order request could not be completed -error:",GetLastError()); ResetLastError(); return; } } if(Sell_Condition_3 && Sell_Condition_4) { // any opened Sell position? if(Sell_opened) { Alert("We already have a Sell position!!!"); return; // Don't open a new Sell Position } mrequest.action=TRADE_ACTION_DEAL; // immediate order execution mrequest.price = NormalizeDouble(latest_price.bid,_Digits); // latest Bid price mrequest.sl = NormalizeDouble(latest_price.bid + STP*_Point,_Digits); // Stop Loss mrequest.tp = NormalizeDouble(latest_price.bid - TKP*_Point,_Digits); // Take Profit mrequest.symbol = _Symbol; // currency pair mrequest.volume = Lot; // number of lots to trade mrequest.magic = EA_Magic; // Order Magic Number mrequest.type= ORDER_TYPE_SELL; // Sell Order mrequest.type_filling = ORDER_FILLING_AON; // Order execution type mrequest.deviation=100; // Deviation from current price //--- send order OrderSend(mrequest,mresult); // get the result code if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed { Alert("A Sell order has been successfully placed with Ticket#:",mresult.order,"!!"); } else { Alert("The Sell order request could not be completed -error:",GetLastError()); ResetLastError(); return; } } } return; }