2MA






#property copyright "Copyright © 2005-2006, RickD"
#property link      "http://e2e-fx.net"

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

#define major   1
#define minor   0

extern double Lots = 0.1;
extern int Slippage = 3;

extern int FastP = 5;
extern int SlowP = 20;


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

void init () {
}

void deinit() {
}

void start() {
  if (!IsTesting()) Comment("Version: ", major, ".", minor);
 
  double FastMA = iMA(NULL, 0, FastP, 0, MODE_EMA, PRICE_OPEN, 0);
  double SlowMA = iMA(NULL, 0, SlowP, 0, MODE_EMA, PRICE_OPEN, 0);


  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;
    
    int type = OrderType();
    if (type == OP_BUY) BuyCnt++;
    if (type == OP_SELL) SellCnt++;
  }


  double price, sl, tp;
    
  if (FastMA > SlowMA) {
    CloseOrders(OP_SELL);

    if (BuyCnt == 0) {    
      price = Ask;
      sl = 0;
      tp = 0;

      Buy(Symbol(), Lots, price, Slippage, sl, tp, 0);
    }
  }

  if (FastMA < SlowMA) {
    CloseOrders(OP_BUY);

    if (SellCnt == 0) {    
      price = Bid;
      sl = 0;
      tp = 0;

      Sell(Symbol(), Lots, price, Slippage, sl, tp, 0);
    }
  }
}

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;
    
    int type2 = OrderType();
    if (type != type2) 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:



Indicator Curves created:


Indicators Used:

Moving average indicator


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.63 Total Net Profit:-681.56

BackTest : EURUSD on H1

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

BackTest : EURUSD on H1

From 2010-03-01 to 2010-03-27 Profit Factor:1.45 Total Net Profit:216.66

BackTest : EURUSD on H1

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

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.76 Total Net Profit:-420.85

BackTest : GBPUSD on H1

From 2010-01-01 to 2010-04-16 Profit Factor:0.51 Total Net Profit:-2358.14

BackTest : USDCAD on H1

From 2009-01-01 to 2010-01-01 Profit Factor:0.51 Total Net Profit:-6424.55

BackTest : USDCAD on H1

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

BackTest : USDCHF on H1

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

BackTest : USDJPY on H1

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

Request Backtest for 2MA


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

Pair: Period: