mandarine





//+------------------------------------------------------------------+
//|                                                    mandarine.mq4 |
//|                                     Copyright © 2005, Nick Bilak |
//|                                        http://www.forex-tsd.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Nick Bilak"
#property link      "http://www.forex-tsd.com/"

#include <stdlib.mqh>

extern double StopLoss = 100;  
extern double TakeProfit = 200;  
extern double TrailingStop = 100;  
extern int    T3Periods = 5;  
extern int    FastPeriod = 5;  
extern int    SlowPeriod = 20;  
extern double Lots = 0.1;
extern double MaximumRisk = 3;
extern bool   FixedLot = false;


extern int    slippage=2;   	//slippage for market order processing
extern int    shift=1;			//shift to current bar, 
extern int    MAGICNUM=187233;


bool   buysig,sellsig; 
int    ttime,total;
string TradeSymbol;

//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders()
  {
   int ord;
//----
   total=OrdersTotal();
   for(int i=0;i<total;i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICNUM)
        {
         ord++;
        }
     }
//---- return orders volume
	return(ord);
  }

double LotsRisk(int StopLoss)  { 
   double lot=Lots;
//---- select lot size
   if (!FixedLot)
      lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk*0.001/StopLoss,1);
   else
      lot=Lots;
//---- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
}

//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForSignals(string symbol) {
      //double buy =iCustom(NULL,0,"signals-mandarine",T3Periods,FastPeriod,SlowPeriod,shift,0,0);
      //double sell=iCustom(NULL,0,"signals-mandarine",T3Periods,FastPeriod,SlowPeriod,shift,1,0);

      double t3=t3ma(T3Periods);
      double emaf11=iMA(symbol,0,FastPeriod,0,MODE_EMA,PRICE_CLOSE,shift);
      double emaf12=iMA(symbol,0,FastPeriod,0,MODE_EMA,PRICE_CLOSE,shift+1);
      double emas21=iMA(symbol,0,SlowPeriod,0,MODE_EMA,PRICE_OPEN,shift);
      double emas22=iMA(symbol,0,SlowPeriod,0,MODE_EMA,PRICE_OPEN,shift+1);
		
      sellsig=false;
      buysig=false;
      //if (t3>0) Print(t3);
      //if (sell>0) {
      if (emaf11<emas21 && emaf12>=emas22 && emaf11<t3) {
         sellsig=true;
      }
      //if (buy>0) {
      if (emaf11>emas21 && emaf12<=emas22 && emaf11>t3) {
         buysig=true;
      }
}

void CheckForOpen(string symbol) {
   int    res;
//---- sell conditions
   if(sellsig && ttime!=Time[0])  {
      res=OrderSend(symbol,OP_SELL,LotsRisk(StopLoss),Bid,slippage,Bid+StopLoss*Point,Bid-TakeProfit*Point,"t3",MAGICNUM,0,Red);
   	if (res<0) Print("Error opening SELL order : ",ErrorDescription(GetLastError()));
      ttime=Time[0];
      return;
   }
//---- buy conditions
   if(buysig && ttime!=Time[0])  {
      res=OrderSend(symbol,OP_BUY,LotsRisk(StopLoss),Ask,slippage,Ask-StopLoss*Point,Ask+TakeProfit*Point,"t3",MAGICNUM,0,Blue);
   	if (res<0) Print("Error opening BUY order : ",ErrorDescription(GetLastError()));
      ttime=Time[0];
      return;
   }
}
  
  
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose(string symbol)  {
	total=OrdersTotal();
   for(int i=0;i<total;i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)  break;
      if(OrderMagicNumber()!=MAGICNUM || OrderSymbol()!=symbol) continue;
      //---- check order type 
      if(OrderType()==OP_BUY)
        {
         if (sellsig) {
            OrderClose(OrderTicket(),OrderLots(),Bid,slippage,White);
         }
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if (buysig) {
            OrderClose(OrderTicket(),OrderLots(),Ask,slippage,White);
         }
         break;
        }
     }
}


//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()  {


   //---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;

	TradeSymbol=Symbol();

   //---- check for signals
   CheckForSignals(TradeSymbol);

   //---- calculate open orders by current symbol
   if (CalculateCurrentOrders()==0) 
      CheckForOpen(TradeSymbol);
   else
      CheckForClose(TradeSymbol);
	
	TrailStop(TradeSymbol);
}
//+------------------------------------------------------------------+

void TrailStop(string mySymbol) {
   double StopLoss,trailing;
   if ( TrailingStop>=8 ) {
   	trailing=TrailingStop*Point;
      for (int i = 0; i < OrdersTotal(); i++) {
         if ( OrderSelect (i, SELECT_BY_POS) == false )  continue;
         if ( OrderSymbol() != mySymbol || OrderMagicNumber() != MAGICNUM )  continue;
         if ( OrderType() == OP_BUY ) {
            StopLoss = Bid-trailing;
            if ( StopLoss > OrderStopLoss() ) {
               OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, White);
            }
         }
   
         if ( OrderType() == OP_SELL ) {
            StopLoss = Ask+trailing;
            if ( StopLoss < OrderStopLoss() ) {
               OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, Gold);
            }
         }
      }
   }
   return;
}


double t3ma(int Periods) {
	int i,limit=Periods*5;
	double e1[1],e2[1],e3[1],e4[1],e5[1],e6[1],e7[1];
	ArrayResize(e1,limit*4);
	ArrayResize(e2,limit*4);
	ArrayResize(e3,limit*4);
	ArrayResize(e4,limit*4);
	ArrayResize(e5,limit*4);
	ArrayResize(e6,limit*4);
	ArrayResize(e7,limit*4);
	ArraySetAsSeries(e1,true);
	ArraySetAsSeries(e2,true);
	ArraySetAsSeries(e3,true);
	ArraySetAsSeries(e4,true);
	ArraySetAsSeries(e5,true);
	ArraySetAsSeries(e6,true);
	ArraySetAsSeries(e7,true);
   for(i=limit+Periods*5; i>=0; i--) {
   	e1[i]=iMA(NULL,0,Periods,0,MODE_EMA,PRICE_CLOSE,i);
   }
   for(i=limit+Periods*4; i>=0; i--) {
   	e2[i]=iMAOnArray(e1,0,Periods,0,MODE_EMA,i);
   }
   for(i=limit+Periods*3; i>=0; i--) {
   	e3[i]=iMAOnArray(e2,0,Periods,0,MODE_EMA,i);
   }
   for(i=limit+Periods*2; i>=0; i--) {
   	e4[i]=iMAOnArray(e3,0,Periods,0,MODE_EMA,i);
   }
   for(i=limit+Periods; i>=0; i--) {
   	e5[i]=iMAOnArray(e4,0,Periods,0,MODE_EMA,i);
   }
	double a=0.8;
	double c1=-a*a*a;
	double c2=3*a*a+3*a*a*a;
	double c3=-6*a*a-3*a-3*a*a*a;
	double c4=1+3*a+a*a*a+3*a*a;
   for(i=limit; i>=0; i--) {
   	e6[i]=iMAOnArray(e5,0,Periods,0,MODE_EMA,i);
   	e7[i]=c1*e6[i]+c2*e5[i]+c3*e4[i]+c4*e3[i];
   }
   return(e7[0]);
}



Sample





Analysis



Market Information Used:

Series array that contains open time of each bar


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-11-01 to 2009-11-30 Profit Factor:0.48 Total Net Profit:-204.90

BackTest : USDJPY on H1

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

BackTest : EURUSD on H1

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

BackTest : USDCAD on H1

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

BackTest : EURUSD on H1

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

BackTest : GBPUSD on H1

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

BackTest : USDCAD on H1

From 2009-01-01 to 2010-01-01 Profit Factor:0.85 Total Net Profit:-534.98

BackTest : EURUSD on H1

From 2010-03-01 to 2010-03-27 Profit Factor:0.63 Total Net Profit:-80.10

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

Request Backtest for mandarine


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

Pair: Period: