Download MetaTrader 5

This EA doesn't sell any suggestion

To add comments, please log in or register
fx0011
72
fx0011  
//+------------------------------------------------------------------+
//|                                                  My_First_EA.mq5 |
//|                        Copyright 2010, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2010, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
//--- input parameters
input int      StopLoss=30;      // Stop Loss
input int      TakeProfit=100;   // Take Profit
input int      ADX_Period=8;     // ADX Period
input int      MA_Period=8;      // Moving Average Period
input int      EA_Magic=12345;   // EA Magic Number
input double   Adx_Min=22.0;     // Minimum ADX Value
input double   Lot=0.1;          // Lots to Trade
input int      open_timeframe=30; //timeframe of open 
input int      open_back_periods=9;//Periods of high or loow
input double   trailing_stop= 0.0035;// 
//--- Other parameters
int adxHandle; // handle for our ADX indicator
int maHandle;  // handle for our Moving Average indicator
double plsDI[],minDI[],adxVal[]; // Dynamic arrays to hold the values of +DI, -DI and ADX values for each bars
double maVal[]; // Dynamic array to hold the values of Moving Average for each bars
double p_close; // Variable to store the close value of a bar
int STP, TKP;   // To be used for Stop Loss & Take Profit values
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Get handle for ADX indicator
   adxHandle=iADX(NULL,0,ADX_Period);
//--- Get the handle for Moving Average indicator
   maHandle=iMA(_Symbol,_Period,MA_Period,0,MODE_EMA,PRICE_CLOSE);
//--- What if handle returns Invalid Handle
   if(adxHandle<0 || maHandle<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;
   if(_Digits==5 || _Digits==3)
     {
      STP = STP*10;
      TKP = TKP*10;
     }
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- Release our indicator handles
   IndicatorRelease(adxHandle);
   IndicatorRelease(maHandle);
  }

//---------------------------------------------Trailing Stop---------------------------------------------------------------------//
void TrailingSL() {
   double StopLossTemp;
   double TakeProfitTemp;



//-----Get the opening time----------------


datetime Open_Trade_Time=PositionGetInteger(POSITION_TIME);

//Print("Open_Trade_Time:",Open_Trade_Time);

double ht[], lt[];
MqlDateTime dt;
TimeCurrent(dt);
datetime TradeTimeCurrent=TimeCurrent();



 CopyHigh(_Symbol,PERIOD_M30,Open_Trade_Time,TradeTimeCurrent,ht);
 CopyLow(_Symbol,PERIOD_M30,Open_Trade_Time,TradeTimeCurrent,lt);
 
int highTradeIndex = ArrayMaximum(ht,0,WHOLE_ARRAY);

//Print("highIndex:",highIndex);

double highestTradeValue=ht[highTradeIndex];
//Print("highestTradeValue:",highestTradeValue);

int lowTradeIndex = ArrayMinimum(lt,0,WHOLE_ARRAY);

//Print("lowIndex:",lowIndex);

double lowestTradeValue=lt[lowTradeIndex];
//Print("lowestTradeValue:",lowestTradeValue);
 
 
//Print("SL:",PositionGetDouble(POSITION_SL));
//Print("TP:",PositionGetDouble(POSITION_TP)); 


//-----Get the opening time----------------


   
int i;   
   
   for(i = 0; i < PositionsTotal(); i++)
   {      
      if(Symbol()==PositionGetSymbol(i))
      {
         if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
         {
            if(PositionGetDouble(POSITION_PRICE_CURRENT) > PositionGetDouble(POSITION_PRICE_OPEN)) {
               
               StopLossTemp = highestTradeValue - trailing_stop;
               TakeProfitTemp = PositionGetDouble(POSITION_PRICE_OPEN)+ TakeProfit*_Point;
               ChangePosition(StopLossTemp,TakeProfitTemp);
            }
         }
   
         if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
         {
            if(PositionGetDouble(POSITION_PRICE_CURRENT) < PositionGetDouble(POSITION_PRICE_OPEN)) {
               StopLossTemp = lowestTradeValue + trailing_stop;
               TakeProfitTemp = PositionGetDouble(POSITION_PRICE_OPEN)- TakeProfit*_Point;
               ChangePosition(StopLossTemp,TakeProfitTemp);
            }
         }
      }
   }
}

void ChangePosition(double StopLossTemp, double TakeProfitTemp) {
   
   
   
   double bidPrice = SymbolInfoDouble(Symbol(),SYMBOL_BID);
   double askPrice = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
   double stopLevel = SymbolInfoInteger(Symbol(), SYMBOL_TRADE_STOPS_LEVEL) *_Point;

   
   //Print("bidPrice:",bidPrice);
   //Print("askPrice:",askPrice);
   //Print("StopLossTemp:",StopLossTemp);

   MqlTradeRequest request;  // To be used for sending our trade requests
   MqlTradeResult result;    // To be used to get our trade results
   MqlTradeCheckResult checkresult;
   
   
   ZeroMemory(request);
   request.action = TRADE_ACTION_SLTP;
   request.symbol = Symbol();
   request.deviation = 0;
   request.sl = NormalizeDouble(StopLossTemp,_Digits);
   request.tp = NormalizeDouble(TakeProfitTemp,_Digits);
   
   
   
   OrderSend(request,result);
   
      
   if(result.retcode==10009 || result.retcode==10008) //Request is completed or order placed
           {
            Alert("An order has been successfully placed with Ticket#:",result.order,"!!");
           }
         else
           {
            Alert("The order request could not be completed -error:",GetLastError()," with trade return code ",result.retcode);
            ResetLastError();
            return;
           }
   
}

//---------------------------------------------Trailing Stop---------------------------------------------------------------------//

void SetSL_TP(double SL,double TP) {
   
   
   
   double bidPrice = SymbolInfoDouble(Symbol(),SYMBOL_BID);
   double askPrice = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
   double stopLevel = SymbolInfoInteger(Symbol(), SYMBOL_TRADE_STOPS_LEVEL) *_Point;
  double STOPLOSS;
  double TAKEPROFIT;

int i;   
   
   for(i = 0; i < PositionsTotal(); i++)
   {      
      if(Symbol()==PositionGetSymbol(i))
      {
         if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
         {
            {
               
               STOPLOSS = PositionGetDouble(POSITION_PRICE_OPEN)-SL*_Point;
               TAKEPROFIT=PositionGetDouble(POSITION_PRICE_OPEN)+TP*_Point;
               
            }
         }
   
         if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
         {
             {
               STOPLOSS = PositionGetDouble(POSITION_PRICE_OPEN)+SL*_Point;
               TAKEPROFIT=PositionGetDouble(POSITION_PRICE_OPEN)-TP*_Point;
               
            }
         }
       }
     }

//Print("SL:",SL*_Point);
//Print("TP:",TP*_Point);
//Print("OpenPrice:",PositionGetDouble(POSITION_PRICE_OPEN));
//Print("STOPLOSS:",STOPLOSS);
//Print("TAKEPROFIT:",TAKEPROFIT);
   MqlTradeRequest request;  // To be used for sending our trade requests
   MqlTradeResult result;    // To be used to get our trade results
   MqlTradeCheckResult checkresult;
   
   
   ZeroMemory(request);
   request.action = TRADE_ACTION_SLTP;
   request.symbol = Symbol();
   request.deviation = 0;
   request.sl = NormalizeDouble(STOPLOSS,_Digits); // Stop Loss
   request.tp = NormalizeDouble(TAKEPROFIT,_Digits); // Take Profit
   
   
   
   OrderSend(request,result);
   
      
   if(result.retcode==10009 || result.retcode==10008) //Request is completed or order placed
           {
            Alert("An order has been successfully placed with Ticket#:",result.order,"!!");
           }
         else
           {
            Alert("The order request could not be completed -error:",GetLastError()," with trade return code ",result.retcode);
            ResetLastError();
            return;
           }
   
}

//---------------------------------------------Trailing Stop---------------------------------------------------------------------//


//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
if (PositionGetDouble(POSITION_SL)==0)
{SetSL_TP(StopLoss,TakeProfit);}      
TrailingSL() ;

  
  
//--- 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
   ZeroMemory(mrequest);      // Initialization of mrequest structure


/*
     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 ADX values arrays
   ArraySetAsSeries(adxVal,true);
// the MA-8 values arrays
   ArraySetAsSeries(maVal,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(adxHandle,0,0,3,adxVal)<0 || CopyBuffer(adxHandle,1,0,3,plsDI)<0
      || CopyBuffer(adxHandle,2,0,3,minDI)<0)
     {
      Alert("Error copying ADX indicator Buffers - error:",GetLastError(),"!!");
      ResetLastError();
      return;
     }
   if(CopyBuffer(maHandle,0,0,3,maVal)<0)
     {
      Alert("Error copying Moving Average indicator buffer - error:",GetLastError());
      ResetLastError();
      return;
     }
  */  



   double bidPrice = SymbolInfoDouble(Symbol(),SYMBOL_BID);
   double askPrice = SymbolInfoDouble(Symbol(),SYMBOL_ASK);


     
//--- 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




double h[], l[];
MqlDateTime dt;
TimeCurrent(dt);
datetime i=TimeCurrent();



 CopyHigh(_Symbol,PERIOD_M30,i,open_back_periods,h);
 CopyLow(_Symbol,PERIOD_M30,i,open_back_periods,l);


int highIndex = ArrayMaximum(h,0,WHOLE_ARRAY);

//Print("highIndex:",highIndex);

double highestValue=NormalizeDouble(h[highIndex],_Digits);

int lowIndex = ArrayMinimum(l,0,WHOLE_ARRAY);

//Print("lowIndex:",lowIndex);

double lowestValue=NormalizeDouble(l[lowIndex],_Digits);
//Print("highestValue:",highestValue);
//Print("lowestValue:",lowestValue);
//Print("latest_price.bid:",latest_price.bid);
//Print("latest_price.ask:",latest_price.ask);  

/*
    1. Check for a long/Buy Setup : MA-8 increasing upwards, 
    previous price close above it, ADX > 22, +DI > -DI
*/

 if (bidPrice<lowestValue)
 {Print("it is lower");}
 Print("bidPrice:",bidPrice);
 Print("lowestValue:",lowestValue);
 Print("Diferrence:",bidPrice-lowestValue);
 
 //--- Declare bool type variables to hold our Buy Conditions
   bool Buy_Condition_1=(askPrice>highestValue); // MA-8 Increasing upwards
   bool Buy_Condition_2 = (1+1==2);         // previuos price closed above MA-8
   bool Buy_Condition_3 = (1+1==2);          // Current ADX value greater than minimum value (22)
   bool Buy_Condition_4 = (1+1==2);          // +DI greater than -DI

//--- Putting all together   
   if(Buy_Condition_1 && Buy_Condition_2)
     {
      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
           }
         ZeroMemory(mrequest);
         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_FOK;                             // Order execution type
         mrequest.deviation=100;                                                // Deviation from current price
         //--- send order
     OrderSend(mrequest,mresult);
          
         // get the result code
         
 /*        
   Print("SLSETUP:",NormalizeDouble(latest_price.bid + STP*_Point,_Digits));
   
   
   MqlTradeRequest lrequest;  // To be used for sending our trade requests
   MqlTradeResult lresult;    // To be used to get our trade results
   
   
   ZeroMemory(lrequest);
   lrequest.action = TRADE_ACTION_SLTP;
   lrequest.symbol = Symbol();
   lrequest.sl = NormalizeDouble(latest_price.bid + STP*_Point,_Digits); // Stop Loss
   lrequest.tp = NormalizeDouble(latest_price.bid - TKP*_Point,_Digits); // Take Profit
   
   
   
   OrderSend(lrequest,lresult);
         
   */      
        
         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;  
           }
        }
     }
 
 
 
 
 
 //--- Declare bool type variables to hold our Sell Conditions
   bool Sell_Condition_1 = (bidPrice<lowestValue);
   // MA-8 decreasing downwards
   bool Sell_Condition_2 = (1+1==2);                         // Previous price closed below MA-8
   bool Sell_Condition_3 = (1+1==2);                         // Current ADX value greater than minimum (22)
   bool Sell_Condition_4 = (1+1==2);                         // -DI greater than +DI

//--- Putting all together
   if(Sell_Condition_1 && Sell_Condition_2)
     {
      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
           }
          
         ZeroMemory(mrequest);
         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_FOK;                          // Order execution type
         mrequest.deviation=100;                                             // Deviation from current price
         //--- send order
     OrderSend(mrequest,mresult);
                  // get the result code
         
 //  Print("SLSETUP:",NormalizeDouble(latest_price.bid + STP*_Point,_Digits));
   
  /* 
   MqlTradeRequest lrequest;  // To be used for sending our trade requests
   MqlTradeResult lresult;    // To be used to get our trade results
   
   ZeroMemory(lrequest);
   ZeroMemory(lresult);
   lrequest.action = TRADE_ACTION_SLTP;
   lrequest.symbol = Symbol();
   lrequest.sl = NormalizeDouble(latest_price.bid + STP*_Point,_Digits); // Stop Loss
   lrequest.tp = NormalizeDouble(latest_price.bid - TKP*_Point,_Digits); // Take Profit
   
   
   
   OrderSend(lrequest,lresult);
         
         
       */
         
         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;
           }
                      
        }
     }  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

     
/*
    2. Check for a Short/Sell Setup : MA-8 decreasing downwards, 
    previous price close below it, ADX > 22, -DI > +DI
*/



//Print("latest_price.bid:",latest_price.bid);
//Print("lowestValue:",lowestValue);

  
   return;
  }
//+------------------------------------------------------------------+
Alain Verleyen
Moderator
33627
Alain Verleyen  
fx0011:
Big piece of code. What have you tried to found the problem ?
fx0011
72
fx0011  

You are right, it seems that there is a problem with the calculation of the bidPrice and the lowestValue

for Sell_Condition_1
bool Sell_Condition_1 = (bidPrice<lowestValue)

if you test the code for 03.01.2013 till 05.01.2013

on 201301.04 13:34:00 bidPrice:1.30068

                                   lowestValue:1.30022

                                   Diferrence:0.000460000000000127

on 2013.01.04 13:35:00 bidPrice:1.30009

                                   lowestValue:1.30009

                                   Diferrence:0.0

on 2013.01.04 13:36:00 bidPrice:1.29994

                                   lowestValue:1.29973

                                  Diferrence:0.0002099999999998214

the lowestValue is always lower than the bidPrice

the bidPrice is set

   double bidPrice = SymbolInfoDouble(Symbol(),SYMBOL_BID);

the lowestPrice is set

 CopyHigh(_Symbol,PERIOD_M30,i,open_back_periods,h);
 CopyLow(_Symbol,PERIOD_M30,i,open_back_periods,l);


int highIndex = ArrayMaximum(h,0,WHOLE_ARRAY);

//Print("highIndex:",highIndex);

double highestValue=NormalizeDouble(h[highIndex],_Digits);

int lowIndex = ArrayMinimum(l,0,WHOLE_ARRAY);

//Print("lowIndex:",lowIndex);

double lowestValue=NormalizeDouble(l[lowIndex],_Digits);

if I change the Sell_Condition_1

(bidPrice=lowestValue)

the EA places sell and buy orders

Thanks for your help

Alain Verleyen
Moderator
33627
Alain Verleyen  
fx0011:

...

This condition for buy isn't right :

   bool Buy_Condition_1=(askPrice>highestValue); // MA-8 Increasing upwards

Your highestValue is calculated on Bid price and then you compare ask price and bid price. This wrong condition explains why you get buy trade. If you correct it, you don't get any trade.

Obviously, your conditions are

bool Buy_Condition_1 = (bidPrice>highestValue) (if corrected)
and
bool Sell_Condition_1 = (bidPrice<lowestValue)

These condition are always false, as you calculate the lowest low and highest high from current time to 9 periods back. The bid price is at best the lowest or the highest but cant' be lower than the lowest or higher than the highest price.

fx0011
72
fx0011  
So, how can I make a condition to buy when the Ask price is higher than the highest price of the last X periods and to sell when the Bid price is lower than the lowest price of the last Xperiods?
To add comments, please log in or register