Download MetaTrader 5
To add comments, please log in or register
Do you have a trading signal? Publish it in your blog as well!
Amit Kumar
169
Amit Kumar 2014.05.21 17:14 
//+------------------------------------ ------------------------------+
//|                                   Reverse_Grid_Martingale mod.mq4 |
//|  Copyright 2008, Constin@ForexFactory // mod. 03.2014 fxdaytrader |
//+-------------------------------------------------------------------+
// !!! YOU MUST COMPILE THIS EA WITH THE BUILD 509 COMPILER, download the metaeditor for build 509 at http://www.forexfactory.com/showthread.php?t=470340 !!!
#property copyright "Copyright 2008, Constin@ForexFactory // mod. 03.2014 fxdaytrader"
#property link "http://ForexBaron.net"
extern string sbphi = "set to 0.0 to start immediately";
extern string sbphi1 = "0:bid>=price, 1:bid<=price";
extern int StartPriceMode = 0;
extern double StartBidPrice = 1.28630; //0.00000;
bool StartEA=false;
extern int MaximumNumberOfOrdersToSend=25;
extern bool LongOnly=true;
extern bool ShortOnly=true;
extern bool Reverse=false;
extern double StepSizeInPips=50;
extern int MagicNumber=10126;
extern int MaxSlippage=2;


extern int x=10;



int spread=0; // unused
extern double LotSize1=0.1;
extern double LotSize2=1;
extern double LotSize3=2;
extern double LotSize4=3;
extern double LotSize5=4;
extern double LotSize6=5;
extern double LotSize7=6;
extern double LotSize8=7;
extern double LotSize9=8;
extern double LotSize10=9;
extern double LotSize11=10;
extern double LotSize12=11;
extern double LotSize13=12;
extern double LotSize14=13;
extern double LotSize15=14;
extern double LotSize16=15;
extern double LotSize17=16;
extern double LotSize18=17;
extern double LotSize19=18;
extern double LotSize20=1;
extern double LotSize21=1;
extern double LotSize22=1;
extern double LotSize23=1;
extern double LotSize24=1;
extern double LotSize25=1;
extern double LotSize26=1;
extern double LotSize27=1;
extern double LotSize28=1;
extern double LotSize29=1;
extern double LotSize30=0.5;
double LotSizes[31];
int openLongs,openShorts;

int Multiplier; double pips2dbl;
//+------------------------------------ ------------------------------+
//| expert initialization function |
//+------------------------------------ ------------------------------+
int init()
{
BrokerDigitAdjust(Symbol());
StartBidPrice = MathAbs(StartBidPrice);
StartPriceMode = MathAbs(StartPriceMode);
LotSizes[1]=LotSize1;
LotSizes[2]=LotSize2;
LotSizes[3]=LotSize3;
LotSizes[4]=LotSize4;
LotSizes[5]=LotSize5;
LotSizes[6]=LotSize6;
LotSizes[7]=LotSize7;
LotSizes[8]=LotSize8;
LotSizes[9]=LotSize9;
LotSizes[10]=LotSize10;
LotSizes[11]=LotSize11;
LotSizes[12]=LotSize12;
LotSizes[13]=LotSize13;
LotSizes[14]=LotSize14;
LotSizes[15]=LotSize15;
LotSizes[16]=LotSize16;
LotSizes[17]=LotSize17;
LotSizes[18]=LotSize18;
LotSizes[19]=LotSize19;
LotSizes[20]=LotSize20;
LotSizes[21]=LotSize21;
LotSizes[22]=LotSize22;
LotSizes[23]=LotSize23;
LotSizes[24]=LotSize24;
LotSizes[25]=LotSize25;
LotSizes[26]=LotSize26;
LotSizes[27]=LotSize27;
LotSizes[28]=LotSize28;
LotSizes[29]=LotSize29;
LotSizes[30]=LotSize30;
Print("**** starting up");
//if(Reverse)
//{
   //if(LongOnly) PlaceLongOrders(); if(ShortOnly) PlaceShortOrders();
//}
///else
//{
//if(LongOnly) PlaceLongOrders(); else PlaceShortOrders();
//}
//updatePositionCounts();
return(0);
}
//+------------------------------------ ------------------------------+
//| expert deinitialization function |
//+------------------------------------ ------------------------------+
int deinit()
{
//----
 Comment("");
//----
return(0);
}
//+------------------------------------ ------------------------------+
//| expert start function |
//+------------------------------------ ------------------------------+
int start()
{
string sStartPrice="";
if (StartPriceMode==0) sStartPrice="bid >=";
if (StartPriceMode==1) sStartPrice="bid <=";
if (!StartEA) { Comment("Reverse Grid martingale by Constin@ForexFactory, mod. fxdaytrader *** http://ForexBaron.net --> EA is waiting for "+sStartPrice+DoubleToStr(StartBidPrice,Digits)+"\n"+"Longs: "+openLongs+" shorts: "+openShorts+" reported longs:"+openPositions(OP_BUY)+" reported shorts:"+openPositions(OP_SELL)); CheckForStartEA(); }
if (StartEA) {
 Comment("Reverse Grid martingale by Constin@ForexFactory, mod. fxdaytrader *** http://ForexBaron.net --> EA is running"+"\n"+"Longs: "+openLongs+" shorts: "+openShorts+" reported longs:"+openPositions(OP_BUY)+" reported shorts:"+openPositions(OP_SELL));
 check();
}
return(0);
}
//+------------------------------------ ------------------------------+
void updatePositionCounts(){
openLongs=openPositions(OP_BUY);
openShorts=openPositions(OP_SELL);
}

/*void initialOrders(){
//OrderSend(Symbol(),OP_BUY,LotSizes[1],Ask,MaxSlippage,NormalizeDouble(As k-StepSizeInPips*Point,Digits),0,"Lon g 1",MagicNumber,0);
//OrderSend(Symbol(),OP_SELL,LotSizes[1],Bid,MaxSlippage,NormalizeDouble(Bi d+StepSizeInPips*Point,Digits),0,"S hort 1",MagicNumber,0);
}
*/
void PlaceLongOrders()
{
OrderSend2Stage(Symbol(),OP_BUY,LotSizes[1],Ask,MaxSlippage,NormalizeDouble(Ask-StepSizeInPips*pips2dbl,Digits ),0,"Long 1",MagicNumber,0,CLR_NONE);
for(int c=1;c<=(MaximumNumberOfOrdersToSend-1);c++){
OrderSend2Stage(Symbol(),OP_BUYSTOP,LotSizes[c+1],NormalizeDouble(Ask+(StepSizeInPips*c*pips2dbl),Digits),MaxSlippage,
NormalizeDouble((Ask+(StepSizeInPips*c*pips2dbl)-x*pips2dbl),Digits),0,"Long "+DoubleToStr(c+1,0),MagicNumber,0,CLR_NONE);
}
updatePositionCounts();
}
void PlaceShortOrders()
{
OrderSend2Stage(Symbol(),OP_SELL,LotSizes[1],Bid,MaxSlippage,NormalizeDouble(Bid+StepSizeInPips*pips2dbl,Digits),0,"Short 1",MagicNumber,0,CLR_NONE);
for(int c=1;c<=(MaximumNumberOfOrdersToSend-1);c++){
OrderSend2Stage(Symbol(),OP_SELLSTOP,LotSizes[c+1],NormalizeDouble(Bid-(StepSizeInPips*c*pips2dbl),Digits),MaxSlippage,
NormalizeDouble((Bid-(StepSizeInPips*c*pips2dbl)+x*pips2dbl),Digits),0,"Short "+DoubleToStr(c+1,0),MagicNumber,0,CLR_NONE) ;
}
updatePositionCounts();
}

int openPositions(int type){
int Total = OrdersTotal();
int Long=0;int Short=0;
int ret = -1;
for (int i = 0; i < Total; i ++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol() == Symbol() && OrderMagicNumber()==MagicNumber && (OrderType()==OP_BUY || OrderType()==OP_SELL ) ) {
if (OrderType()==OP_BUY) Long++;
if (OrderType()==OP_SELL) Short++;
}
}
if(type==OP_SELL) ret=Short;
if(type==OP_BUY) ret=Long;
return(ret);
}
int outstandingOrders(int type){
int Total = OrdersTotal();
int Long=0;int Short=0;
int ret = -1;
for (int i = 0; i < Total; i ++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol() == Symbol() && OrderMagicNumber()==MagicNumber && (OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP ) ) {
if (OrderType()==OP_BUYSTOP) Long++;
if (OrderType()==OP_SELLSTOP) Short++;
}
}
if(type==OP_SELLSTOP) ret=Short;
if(type==OP_BUYSTOP) ret=Long;
return(ret);
}

/*void deletePending()
{
bool flag;
for(int cnt=OrdersTotal()-1; cnt>=0; cnt--)
{
flag=false;
if(OrderSelect(cnt,SELECT_BY_POS) && OrderSymbol()==Symbol())
{
if(OrderType()!=OP_SELL && OrderType()!=OP_BUY && OrderMagicNumber()==MagicNumber) { flag=true; OrderDelete(OrderTicket()); }
if(flag)
{
Sleep(500);
RefreshRates();
}
}
}
return(0);
}
void deletePendingOrdersOfType(int type)
{
bool flag;
for(int cnt=OrdersTotal()-1; cnt>=0; cnt--)
{
flag=false;
if(OrderSelect(cnt,SELECT_BY_POS) && OrderSymbol()==Symbol())
{
if(OrderType()==type && OrderMagicNumber()==MagicNumber) { flag=true;Print("Deleting:"+OrderTi cket()); OrderDelete(OrderTicket()); }
/*if(flag)
{
Sleep(500);
RefreshRates();
}
}
}
return(0);
}
*/
void deleteFirstPendingOrdersOfType(int type)
{
bool flag;
bool done=false;
int tot=OrdersTotal();
int i=0;
while(!done)
{
flag=false;
if(OrderSelect(i,SELECT_BY_POS) && OrderSymbol()==Symbol())
{
if(OrderType()==type && OrderMagicNumber()==MagicNumber) { flag=true;Print("Deleting:"+OrderTicket()); OrderDelete(OrderTicket());done=true; }
/*if(flag)
{
Sleep(500);
RefreshRates();
}*/
}
i++;
if (i==tot) {done=true;}
}
return(0);
}

void closeShorts()
{
int Total = OrdersTotal();
for (int i = 0; i < Total; i ++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol() && OrderMagicNumber()==MagicNumber && (OrderType()==OP_SELL ) ) {
OrderClose(OrderTicket(),OrderLots( ),Ask,MaxSlippage);
}
}
}
void closeLongs()
{
int Total = OrdersTotal();
for (int i = 0; i < Total; i ++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol() && OrderMagicNumber()==MagicNumber && (OrderType()==OP_BUY ) ) {
OrderClose(OrderTicket(),OrderLots( ),Bid,MaxSlippage);
}
}
}

void check()
{
if((openPositions(OP_BUY)<openLongs ))
{
}
if((openPositions(OP_SELL)<openShorts))
{
}
updatePositionCounts();
}

bool OrderSend2Stage(string symbol,int type,double lots,double price,int slippage,double sl,double tp,string ocomment,int magic,datetime expiry,color col) {
 while (IsTradeContextBusy()) Sleep(100);
 bool result=true;
 RefreshRates();
 int ticket=OrderSend(symbol,type,lots,price,slippage,0,0,ocomment,magic,expiry,col); 
  if (!OrderSelect(ticket, SELECT_BY_TICKET)) return(false);
   if (sl!=0.00000 && tp!=0.00000) result = OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,OrderExpiration(),CLR_NONE);
   if (sl!=0.00000 && tp==0.00000) result = OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),OrderExpiration(),CLR_NONE);
   if (sl==0.00000 && tp!=0.00000) result = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),tp,OrderExpiration(),CLR_NONE);
 return(result);
}

void BrokerDigitAdjust(string symbol) {
 Multiplier = 1;
 if (MarketInfo(symbol,MODE_DIGITS) == 3 || MarketInfo(symbol,MODE_DIGITS) == 5) Multiplier = 10;
 if (MarketInfo(symbol,MODE_DIGITS) == 6) Multiplier = 100;   
 if (MarketInfo(symbol,MODE_DIGITS) == 7) Multiplier = 1000;
 pips2dbl = Multiplier*MarketInfo(symbol,MODE_POINT);
 MaxSlippage*=Multiplier;
}


void CheckForStartEA() {
RefreshRates();
if (StartPriceMode==0 && Bid>=StartBidPrice)  StartEA=true;
 else if (StartPriceMode==1 && Bid<=StartBidPrice) StartEA=true;
  else if (StartBidPrice==0.0000) StartEA=true;
  if (!StartEA) return(0);
 if(LongOnly) PlaceLongOrders(); if(ShortOnly) PlaceShortOrders();
}
/
To add comments, please log in or register