div





//+-----------------+
//|USDCHF H1        |
//|StopLoss 90      |
//|Takeprofit 120   |
//|DivBars 7        |
//|                 |
//|GBPUSD H1        |
//|StopLoss 70      |
//|Takeprofit 100   |
//|DivBars 15       |
//+-----------------+
#property copyright "© 2006, RickD"
#property link      "www.e2e-fx.net"

#include <stdlib.mqh>
#include <stderror.mqh>

#define major   2
#define minor   0


extern double Lots = 0.1;
extern int StopLoss = 50;
extern int TakeProfit = 100;
extern int Slippage = 3;
extern int Magic = 250506;
extern int DivBars = 10;

extern string _Param_Ind_ = "--- Ïàðàìåòðû èíäèêàòîðîâ ---";
extern int MACDFastEMA = 12;
extern int MACDSlowEMA = 26;
extern int MACDSignal = 9;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void init () {
}

void deinit() {
}

void start() {
  if (!IsTesting()) Comment("Version: ", major, ".", minor);

  //-----

  int BuyCnt = 0;
  int SellCnt = 0;
  
  int cnt = OrdersTotal();
  for (int i=0; i < cnt; i++) {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
    if (OrderSymbol() != Symbol()) continue;
    if (OrderMagicNumber() != Magic) continue;
    
    int type = OrderType();
    if (type == OP_BUY) BuyCnt++;
    if (type == OP_SELL) SellCnt++;
  }
  
  //-----
  
  double MACD1 = iMACD(NULL, 0, MACDFastEMA, MACDSlowEMA, MACDSignal, PRICE_CLOSE, MODE_MAIN, 1);
  double MACD2 = iMACD(NULL, 0, MACDFastEMA, MACDSlowEMA, MACDSignal, PRICE_CLOSE, MODE_MAIN, 2);
  double MACD3 = iMACD(NULL, 0, MACDFastEMA, MACDSlowEMA, MACDSignal, PRICE_CLOSE, MODE_MAIN, 3);
  
  double MACDN1 = iMACD(NULL, 0, MACDFastEMA, MACDSlowEMA, MACDSignal, PRICE_CLOSE, MODE_MAIN, DivBars-1);
  double MACDN2 = iMACD(NULL, 0, MACDFastEMA, MACDSlowEMA, MACDSignal, PRICE_CLOSE, MODE_MAIN, DivBars);
  double MACDN3 = iMACD(NULL, 0, MACDFastEMA, MACDSlowEMA, MACDSignal, PRICE_CLOSE, MODE_MAIN, DivBars+1);
  
  //-----
    
  double price, sl, tp;
     
  if (Close[2] < Close[DivBars] && 
    Close[1] > Close[2] && Close[3] > Close[2] && 
    Close[DivBars-1] > Close[DivBars] && Close[DivBars+1] > Close[DivBars] &&
    MACD2 > MACDN2 &&
    MACD1 > MACD2 && MACD3 > MACD2 && 
    MACDN1 > MACDN2 && MACDN3 > MACDN2)
  {
    if (BuyCnt > 0) return;
    if (SellCnt > 0) CloseOrders(OP_SELL);
    if (OrdersCount(OP_SELL) > 0) return;
          
    price = Ask;
    sl = 0;
    tp = 0;
    if (StopLoss > 0) sl = price - StopLoss*Point;
    if (TakeProfit > 0) tp = price + TakeProfit*Point;
    Buy(Symbol(), Lots, price, Slippage, sl, tp, Magic);  
  }

  if (Close[2] > Close[DivBars] && 
    Close[1] < Close[2] && Close[3] < Close[2] && 
    Close[DivBars-1] < Close[DivBars] && Close[DivBars+1] < Close[DivBars] &&
    MACD2 < MACDN2 && 
    MACD1 < MACD2 && MACD3 < MACD2 && 
    MACDN1 < MACDN2 && MACDN3 < MACDN2)
  {
    if (SellCnt > 0) return;
    if (BuyCnt > 0) CloseOrders(OP_BUY);
    if (OrdersCount(OP_BUY) > 0) return;
  
    price = Bid;
    sl = 0;
    tp = 0;
    if (StopLoss > 0) sl = price + StopLoss*Point;
    if (TakeProfit > 0) tp = price - TakeProfit*Point;
    Sell(Symbol(), Lots, price, Slippage, sl, tp, Magic);
  }
  
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

int OrdersCount(int type)
{
  int orders = 0;
  int cnt = OrdersTotal();
  for (int i=0; i<cnt; i++) {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
    if (OrderSymbol() != Symbol()) continue;
    if (OrderMagicNumber() != Magic) continue;
    
    if (OrderType() == type) orders++;
  }
  
  return (orders);
}

void CloseOrders(int type) 
{
  int cnt = OrdersTotal();
  for (int i=cnt-1; i>=0; i--) {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
    if (OrderSymbol() != Symbol()) continue;
    if (OrderMagicNumber() != Magic) continue;
    
    if (OrderType() != type) continue;
    
    if (type == OP_BUY) CloseOrder(OrderTicket(), OrderLots(), Bid, Slippage);
    if (type == OP_SELL) CloseOrder(OrderTicket(), OrderLots(), Ask, Slippage);
  }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

int SleepOk = 2000;
int SleepErr = 6000;

int Buy(string symbol, double lot, double price, int slip, double sl, double tp, int magic, string comment="") {
  RefreshRates();  
  int dig = MarketInfo(symbol, MODE_DIGITS);

  price = NormalizeDouble(price, dig);
  sl = NormalizeDouble(sl, dig);
  tp = NormalizeDouble(tp, dig);
    
  string _lot = DoubleToStr(lot, 1);
  string _price = DoubleToStr(price, dig);
  string _sl = DoubleToStr(sl, dig);
  string _tp = DoubleToStr(tp, dig);

  Print("Buy \"", symbol, "\", ", _lot, ", ", _price, ", ", slip, ", ", _sl, ", ", _tp, ", ", magic, ", \"", comment, "\"");

  int res = OrderSend(symbol, OP_BUY, lot, price, slip, sl, tp, comment, magic);
  if (res >= 0) {
    Sleep(SleepOk);
    return (res);
  } 	
   	
  int code = GetLastError();
  Print("Error opening BUY order: ", ErrorDescription(code), " (", code, ")");
  Sleep(SleepErr);
	
  return (-1);
}

int Sell(string symbol, double lot, double price, int slip, double sl, double tp, int magic, string comment="") {
  RefreshRates();  
  int dig = MarketInfo(symbol, MODE_DIGITS);

  price = NormalizeDouble(price, dig);
  sl = NormalizeDouble(sl, dig);
  tp = NormalizeDouble(tp, dig);
    
  string _lot = DoubleToStr(lot, 1);
  string _price = DoubleToStr(price, dig);
  string _sl = DoubleToStr(sl, dig);
  string _tp = DoubleToStr(tp, dig);

  Print("Sell \"", symbol, "\", ", _lot, ", ", _price, ", ", slip, ", ", _sl, ", ", _tp, ", ", magic, ", \"", comment, "\"");
  
  int res = OrderSend(symbol, OP_SELL, lot, price, slip, sl, tp, comment, magic);
  if (res >= 0) {
    Sleep(SleepOk);
    return (res);
  } 	
   	
  int code = GetLastError();
  Print("Error opening SELL order: ", ErrorDescription(code), " (", code, ")");
  Sleep(SleepErr);
	
  return (-1);
}

bool CloseOrder(int ticket, double lot, double price, int slip) {

  RefreshRates();  
  int dig = MarketInfo(OrderSymbol(), MODE_DIGITS);

  string _lot = DoubleToStr(lot, 1);
  string _price = DoubleToStr(price, dig);

  Print("CloseOrder ", ticket, ", ", _lot, ", ", _price, ", ", slip);
  
  bool res = OrderClose(ticket, lot, price, slip);
  if (res) {
    Sleep(SleepOk);
    return (res);
  } 	
   	
  int code = GetLastError();
  Print("CloseOrder failed: ", ErrorDescription(code), " (", code, ")");
  Sleep(SleepErr);
	
  return (false);
}



Sample





Analysis



Market Information Used:

Series array that contains close prices for each bar


Indicator Curves created:


Indicators Used:

MACD Histogram


Custom Indicators Used:

Order Management characteristics:
Checks for the total of open orders

It automatically opens orders when conditions are reached
It Closes Orders by itself

Other Features:

BackTest : EURUSD on H1

From 2009-08-01 to 2009-10-01 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2009-12-01 to 2010-01-17 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-04-01 to 2010-04-30 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-05-01 to 2010-05-31 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-06-01 to 2010-06-30 Profit Factor:0.00 Total Net Profit:0.00

BackTest : GBPUSD on H1

From 2010-01-01 to 2010-02-27 Profit Factor:0.00 Total Net Profit:0.00

BackTest : USDCAD on H1

From 2009-12-01 to 2010-01-01 Profit Factor:0.00 Total Net Profit:0.00

BackTest : USDCHF on H1

From 2009-12-01 to 2010-01-01 Profit Factor:0.00 Total Net Profit:0.00

BackTest : USDJPY on H1

From 2009-11-01 to 2009-11-30 Profit Factor:0.00 Total Net Profit:0.00

Request Backtest for div


From : (yyyy/mm/dd) To: (yyyy/mm/dd)

Pair: Period: