Color Candle EA





//+----------------------------------------------------------+
//| 4/8/2007 preBar.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+----------------------------------------------------------+
int MAGICMA=20070705; 

extern double Lots = 0.1;
extern double MaximumRisk = 0.03;
extern double stopFactor = 1;
extern double TakeProfit1 = 150;
extern double Stoploss1 = 140;
extern bool takeProfit = true;
extern bool sartrail = false;
extern bool MoneyManagement = false;
//+----------------------------------------------------------+
//| Calculate open positions |
//+----------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{

int buys=0,sells=0;
//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; 

if(OrderSymbol()==symbol && OrderMagicNumber()==MAGICMA)
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//---- return orders volume
if(buys>0) return(buys);
else return(-sells);
} 

double LotsOptimized()
{
//---- select lot size
int lot;
lot = NormalizeDouble( (AccountFreeMargin()*MaximumRisk)/( 
iATR(NULL,0,20,0)*(1/Point) )*0.1,1); 

//---- return lot size
if(lot<0.1) lot=0.1;
return(lot);
}
//+----------------------------------------------------------+
//| Check for open order conditions |
//+----------------------------------------------------------+
void CheckForOpen()
{

if(!NewBar())
return;
int res;
if(MoneyManagement)
Lots = LotsOptimized(); 

double close1 = Close[1];
double open1 = Open[1];

if ( close1< open1 )
{
openSell();
return;
}

//---- buy conditions
if ( close1> open1 )
{
openBuy();

return;
}

//----
}

void openBuy(){
Stoploss1 = stopFactor * iATR(NULL,0,20,0)+ MarketInfo (Symbol(), 
MODE_SPREAD) * Point;
int res;
if(takeProfit)

res=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-Stoploss1,Ask+TakeProfit1*Point 
,"preBar",MAGICMA,0,Blue);
else

res=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-Stoploss1,0,"preBar",MAGICMA,0, 
Blue);
if(res>0)
{
if(OrderSelect(res,SELECT_BY_TICKET,MODE_TRADES))
Print("SELL order opened : ",OrderOpenPrice());
Comment("preBar opened buy order " + Ask);
}
else Print("Error opening SELL order : ",GetLastError());

}

void openSell(){
Stoploss1 = stopFactor * iATR(NULL,0,20,0)+ MarketInfo (Symbol(), 
MODE_SPREAD) * Point;
int res;
if(takeProfit)

res=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+Stoploss1,Bid-TakeProfit1*Point,"preBar",MAGICMA,0,Red);
else

res=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+Stoploss1,0,"preBar",MAGICMA,0 ,Red);
if(res>0)
{
if(OrderSelect(res,SELECT_BY_TICKET,MODE_TRADES))
Print("SELL order opened : ",OrderOpenPrice());
Comment("preBar opened sell order " + Bid);
}
else Print("Error opening SELL order : ",GetLastError());

}

//+----------------------------------------------------------+
//| Check for close order conditions |
//+----------------------------------------------------------+
void CheckForClose()
{ 

//----
for(int i=0;i<OrdersTotal();i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
if(!takeProfit && NewBar() && Open[1]> Close[1])
{ OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
openSell();
}
// should it be closed?
// Function version check Sell condition to exit Buy

// check for trailing stop
if ( sartrail && (iSAR(NULL,0,0.02,0.2,1) > OrderStopLoss()) && 
(Bid > iSAR(NULL,0,0.02,0.2,1)) && (OrderOpenPrice() < 
iSAR(NULL,0,0.02,0.2,1)) && (iSAR(NULL,0,0.02,0.2,1) > 
iSAR(NULL,0,0.02,0.2,2)))
{

OrderModify(OrderTicket(),OrderOpenPrice(),iSAR(NULL,0,0.02,0.2,1),OrderTakeProfit(),0,Blue);
Print("Order # ",OrderTicket()," updated at ",Hour(),":",Minute(),":",Seconds());
return(0);
}

}
if(OrderType()==OP_SELL)
{
if(!takeProfit && NewBar() && Open[1]< Close[1])
{ OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
openBuy();
}

// should it be closed?
// Function version check Buy condition to exit Sell

if (sartrail && (iSAR(NULL,0,0.02,0.2,1) < OrderStopLoss()) && (Ask < 
iSAR(NULL,0,0.02,0.2,1)) && (OrderOpenPrice() > iSAR(NULL,0,0.02,0.2,1)) && 
(iSAR(NULL,0,0.02,0.2,1) < iSAR(NULL,0,0.02,0.2,2)))
{

OrderModify(OrderTicket(),OrderOpenPrice(),iSAR(NULL,0,0.02,0.2,1),OrderTakeProfit(),0,Blue);
Print("Order # ",OrderTicket()," updated at ",Hour(),":",Minute(),":",Seconds());
return(0);
}

}
}
}
return(0);
}
// the end.
//----
//+----------------------------------------------------------+
//| Start function |
//+----------------------------------------------------------+
void start()
{
//---- check for history and trading
if(Bars<100 || IsTradeAllowed()==false) return; 

//---- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
//----
}
//+----------------------------------------------------------
bool NewBar()
{
static datetime lastbar;
datetime curbar = Time[0];
if(lastbar!=curbar){
lastbar=curbar;
return (true);
} else
{ return(false); }
} 





Sample





Analysis



Market Information Used:

Series array that contains close prices for each bar
Series array that contains open prices of each bar
Series array that contains open time of each bar


Indicator Curves created:


Indicators Used:

Indicator of the average true range
Parabolic Stop and Reverse system


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
It can change open orders parameters, due to possible stepping strategy

Other Features:

BackTest : EURUSD on H1

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

BackTest : EURUSD on H1

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

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.74 Total Net Profit:-1368.10

BackTest : USDCAD on H1

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

BackTest : USDCHF on H1

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

BackTest : USDJPY on H1

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

Request Backtest for Color Candle EA


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

Pair: Period: