//+------------------------------------------------------------------+
//|                                  Copyright © 2010, Steve Hopwood |
//|                              http://www.hopwood3.freeserve.co.uk |
//|   #10.2 SELL--MODIFIED BY NANNINGBOB March 2012                  |
//|   #modified for NB 10.2 by fmuir April, 2012                     |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Steve Hopwood"
#property link      "http://www.hopwood3.freeserve.co.uk"
#include <WinUser32.mqh>
#include <stdlib.mqh>

extern double  Lot=0.06;
extern double  TakeProfit=0;
extern double  StopLoss=0;
extern int     MagicNumber=51;
extern string  TradeComment="10.2 5.1 MA BUY";
extern bool    CriminalIsECN=false;
extern bool    UseSafetyFeature=true;

double         Var1;
bool           RobotSuspended;
color          col;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
    //----

    //Accommodate different quote sizes
    double multiplier;
    if(Digits == 2 || Digits == 4) multiplier = 1;
    if(Digits == 3 || Digits == 5) multiplier = 10;
    if(Digits == 6) multiplier = 100;   
    TakeProfit*= multiplier;
    StopLoss*= multiplier;
   
    if(UseSafetyFeature)
      {
        int retval = MessageBox(Symbol() + ": do you wish to continue to use this robot?", "Question", MB_YESNO|MB_ICONQUESTION);
        if(retval == IDNO) RobotSuspended = true;
        else RobotSuspended = false;   
      } //if(UseSafetyFeature)
   
    if(TradeComment == "") TradeComment = " ";
   
    //----
    return(0);
  }

//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
    //----
   
    //----
    return(0);
  }

//+------------------------------------------------------------------+
//| SendSingleTrade function                                         |
//+------------------------------------------------------------------+
bool SendSingleTrade(int type, string comment, double lotsize, double price, double stop, double take)
  {
    int slippage = 10;
    if(Digits == 3 || Digits == 5) slippage = 100;
   
    if(type == OP_BUY || type == OP_BUYSTOP) col = Green;
    if(type == OP_SELL || type == OP_SELLSTOP) col = Red;
   
    int expiry = 0;
    
    if(col==Green)MarkBuy(); else MarkSell();

    if(!CriminalIsECN)
      int ticket = OrderSend(Symbol(),type, lotsize, price, slippage, stop, take, comment, MagicNumber, expiry, col);
    
    //Is a 2 stage criminal
    if(CriminalIsECN)
      {
        ticket = OrderSend(Symbol(),type, lotsize, price, slippage, 0, 0, comment, MagicNumber, expiry, col);
	     if(ticket > -1)
          {
            if(stop >0 && take > 0)
               bool result = OrderModify(OrderTicket(), OrderOpenPrice(), stop, take, OrderExpiration(), CLR_NONE);
            //Stop loss but no take profit
            if(stop >0 && take == 0)
               result = OrderModify(OrderTicket(), OrderOpenPrice(), stop, OrderTakeProfit(), OrderExpiration(), CLR_NONE);
            //Take profit but no stop loss
            if(stop ==0 && take > 0)
               result = OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), take, OrderExpiration(), CLR_NONE);
            if(!result)
              {
                int err=GetLastError();
                Print(Symbol(), " ", type," #10.2 NB 5.1 MA Buy ordermodify failed with error(",err,"): ",ErrorDescription(err));
              } //if(!result)			  
          } //if(ticket > -1)
      } //if(CriminalIsECN)

    // Error trapping for both
    if(ticket < 0)
      {
        string stype;
        if(type == OP_BUY) stype = "OP_BUY";
        if(type == OP_BUYSTOP) stype = "OP_BUYSTOP";
        if(type == OP_SELL) stype = "OP_SELL";
        if(type == OP_SELLSTOP) stype = "OP_SELLSTOP";
        err=GetLastError();
        // Alert(Symbol(), " ", stype," #10.2 NB 5.1 MA Buy ordersend failed with error(",err,"): ",ErrorDescription(err));
        Print(Symbol(), " ", stype," #10.2 NB 5.1 MA Buy ordersend failed with error(",err,"): ",ErrorDescription(err));
        return(false);
      } //if(ticket < 0)  
   
    //Got this far, so trade send succeeded
    return(true);
   
  } //End bool SendSingleTrade(int type, string comment, double lotsize, double price, double stop, double take)

//+------------------------------------------------------------------+
//| MarkBuy and MarkSell functions                                          |
//+------------------------------------------------------------------+
void MarkSell()
{
ObjectCreate("10.2 Sell "+Bars,OBJ_ARROW,0,TimeCurrent(),Bid); 
ObjectSet("10.2 Sell "+Bars,OBJPROP_ARROWCODE,117);
ObjectSet("10.2 Sell "+Bars,OBJPROP_COLOR,Orange);
}
//MarkSell();

void MarkBuy()
{
ObjectCreate("10.2 Buy "+Bars,OBJ_ARROW ,0,TimeCurrent(),Ask); 
ObjectSet("10.2 Buy "+Bars,OBJPROP_ARROWCODE,117);
ObjectSet("10.2 Buy "+Bars,OBJPROP_COLOR,Violet);
}
// MarkBuy

//+------------------------------------------------------------------+
//| DoesTradeExist function                                          |
//+------------------------------------------------------------------+
bool DoesTradeExist()
  {
    if(OrdersTotal() == 0) return(false);
    for(int cc = OrdersTotal() - 1; cc >= 0 ; cc--)
      {
        if(!OrderSelect(cc,SELECT_BY_POS)) continue;
      
        if(OrderMagicNumber()==MagicNumber && OrderSymbol() == Symbol() )      
          {
            return(true);         
          } //if(OrderMagicNumber()==MagicNumber && OrderSymbol() == Symbol() )      
      } //for (int cc = OrdersTotal() - 1; cc >= 0 ; cc--)
    return(false);
  } //End bool DoesTradeExist()

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
    //----
   
    if(RobotSuspended)
      {
        Comment("......................This robot is suspended.................");
        return;
      } //if(RobotSuspended)

    if(DoesTradeExist()) return;
   
    // Moving Average Calculation
    Var1 = iMA(NULL, PERIOD_H4, 5, 1, MODE_LWMA, PRICE_OPEN, 0);
 
    RefreshRates();
    if(Ask > Var1)
      {
        double stop = NormalizeDouble(Ask + (StopLoss * Point),Digits);
        if(StopLoss == 0) stop = 0;
        double take = NormalizeDouble(Ask - (TakeProfit * Point),Digits);
        if(TakeProfit == 0) take = 0;
        bool result = SendSingleTrade(OP_BUY, TradeComment, Lot, Ask, stop, take);
        if(result && UseSafetyFeature) 
          {
            RobotSuspended = true;
            Alert(Symbol() + " #10.2 NB 5.1 MA Buy EA has sent a trade and suspended itself");
          } //if(result && UseSafetyFeature) 
      } //if(Bid > Var1)
    //----
    return(0);
  }
//+------------------------------------------------------------------+

