Do anyone use MT4 expert advisor?
Do anyone can edit this expert which is mainly using the Cowabunga Strategy and improve its performance?
//±-----------------------------------------------------------------+
//| pyCowabunga080212-2321.mq4 |
//| Peng Wu |
//| http://pengwu.language123.com |
//±-----------------------------------------------------------------+
//±-----------------------------------------------------------------+
//| TODO: Add Money Management routine |
//±-----------------------------------------------------------------+
#property copyright “Peng Wu”
#property link “http://pengwu.language123.com”
// Trend Detection function
#define BULL 11 // Trend Up
#define BEAR 22 // Trend Down
int pyitShortTimePeriod=PERIOD_M15;
int pyitLongTimePeriod=PERIOD_H4;
int itShift1=0;
//---- input parameters
extern double TakeProfit = 35;
extern double StopLoss = 10;
extern double Lots = 0.1;
extern double TrailingStop = 10;
extern int ShortEma = 5;
extern int LongEma = 10;
extern int BollingerBandsEma=21;
//±-----------------------------------------------------------------+
//| expert initialization function |
//±-----------------------------------------------------------------+
int init()
{
//----
return(0);
}
//±-----------------------------------------------------------------+
//| expert deinitialization function |
//±-----------------------------------------------------------------+
int deinit()
{
//----
return(0);
}
//±-----------------------------------------------------------------+
//| expert start function |
//±-----------------------------------------------------------------+
int start()
{
int cnt=0;
int ticket=0;
int total=0;
int pyValidChart=0;
int itShortPeriodTrend=0;
int itLongPeriodTrend=0;
int itShift1=(0*Point);
static int isCrossed = 0;
pyValidChart=ValidChart();
if (pyValidChart==1)
{return (0);}
total = OrdersTotal();
isCrossed = Crossed (pyitShortTimePeriod,ShortEma,LongEma);
itShortPeriodTrend=funCalTrend(pyitShortTimePeriod,ShortEma,LongEma,BollingerBandsEma);
itLongPeriodTrend=funCalTrend(pyitLongTimePeriod,ShortEma,LongEma,BollingerBandsEma);
pyModifyOrders(total,cnt);
pyOpenOrders(total,isCrossed,ticket,itShortPeriodTrend,itLongPeriodTrend);
return(0);
}
//±-----------------------------------------------------------------+
int pyOpenOrders(int gttotal,int gtisCrossed,int gtticket,
int gtitShortPeriodTrend,int gtitLongPeriodTrend)
{
if(gttotal < 1)
{
if((gtisCrossed == BULL)&&(gtitShortPeriodTrend==BULL)&&(gtitLongPeriodTrend==BULL))
{
gtticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, Ask - StopLoss*Point,
Ask + TakeProfit*Point, "EMA_CROSS", 12345, 0, Green);
if(gtticket > 0)
{
if(OrderSelect(gtticket, SELECT_BY_TICKET, MODE_TRADES))
Print("BUY order opened : ",OrderOpenPrice());
}
else
Print("Error opening BUY order : ", GetLastError());
return(0);
}
if((gtisCrossed == BEAR)&&(gtitShortPeriodTrend==BEAR)&&(gtitLongPeriodTrend==BEAR))
{
gtticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, Bid + StopLoss*Point,
Bid - TakeProfit*Point, "EMA_CROSS", 12345, 0, Red);
if(gtticket > 0)
{
if(OrderSelect(gtticket, SELECT_BY_TICKET, MODE_TRADES))
Print("SELL order opened : ", OrderOpenPrice());
}
else
Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}
return (0);
}
int pyModifyOrders(int gttotal,int gtcnt)
{
//----
for(gtcnt = 0; gtcnt < gttotal; gtcnt++)
{
OrderSelect(gtcnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType() <= OP_SELL && OrderSymbol() == Symbol())
{
if(OrderType() == OP_BUY) // long position is opened
{
// check for trailing stop
if(TrailingStop > 0)
{
if(Bid - OrderOpenPrice() > Point*(TrailingStop+0.5))
{
if(OrderStopLoss() < Bid - Point*(TrailingStop+0.5))
{
OrderModify(OrderTicket(), OrderOpenPrice(),
Bid - PointTrailingStop,
OrderTakeProfit(), 0, Green);
return(0);
}
}
}
}
else // go to short position
{
// check for trailing stop
if(TrailingStop > 0)
{
if((OrderOpenPrice() - Ask) > (Point(TrailingStop+0.5)))
{
if((OrderStopLoss() > (Ask + Point*(TrailingStop+0.5))) ||
(OrderStopLoss() == 0))
{
OrderModify(OrderTicket(), OrderOpenPrice(),
Ask + Point*TrailingStop,
OrderTakeProfit(), 0, Red);
return(0);
}
}
}
}
}
}
//----
return (0);
}
//±-----------------------------------------------------------------+
//| Crossed function |
//±-----------------------------------------------------------------+
int Crossed (int gtitShortTimePeriod,int gtShortEma , int gtLongEma)
{
double dbSEma=0;
double dbLEma=0;
//----
dbSEma = iMA(NULL,gtitShortTimePeriod,gtShortEma, 0, MODE_EMA, PRICE_CLOSE, 1);
dbLEma = iMA(NULL,gtitShortTimePeriod,gtLongEma, 0, MODE_EMA, PRICE_CLOSE, 1);
//----
static int last_direction = 0;
static int current_direction = 0;
//Don’t work in the first load, wait for the first cross!
static bool first_time = true;
if(first_time == true)
{
first_time = false;
return (0);
}
//----
if(dbSEma >dbLEma)
current_direction = BULL; //up
if(dbSEma< dbLEma)
current_direction = BEAR; //down
//----
if(current_direction != last_direction) //changed
{
last_direction = current_direction;
return(last_direction);
}
else
{
return (0); //not changed
}
}
// begin of int ValidChart()
int ValidChart()
{
if(Bars < 100)
{
Print(“bars less than 100”);
return(1);
}
//----
if(TakeProfit < 10)
{
Print(“TakeProfit less than 10”);
return(1); // check TakeProfit
}
return (0);
}
// end of int ValidChart()
//************************************************************************************************
//begin of funCalTrend
int funCalTrend (int gtPeriod,int gtFtEmaPeriod , int gtSwEmaPeriod,int gtBbEmaPeriod)
{
double dbftema0=0;
double dbswema0=0;
double dbbbema0=0;
double dbftema1=0;
double dbswema1=0;
double dbbbema1=0;
double dbftema2=0;
double dbswema2=0;
double dbrsi0=0;
double dbrsi1=0;
double dbrsi2=0;
double dbstochastics0=0;
double dbstochastics1=0;
double dbstochastics2=0;
double dbmacd0=0;
double dbmacd1=0;
double dbmacd2=0;
double dbprebartrend=0;
dbftema0=iMA(NULL,gtPeriod,gtFtEmaPeriod,0,MODE_EMA,PRICE_CLOSE,0);
dbswema0=iMA(NULL,gtPeriod,gtSwEmaPeriod,0,MODE_EMA,PRICE_CLOSE,0);
dbbbema0=iMA(NULL,gtPeriod,gtBbEmaPeriod,0,MODE_EMA,PRICE_CLOSE,0);
dbftema1=iMA(NULL,gtPeriod,gtFtEmaPeriod,0,MODE_EMA,PRICE_CLOSE,1);
dbswema1=iMA(NULL,gtPeriod,gtSwEmaPeriod,0,MODE_EMA,PRICE_CLOSE,1);
dbbbema1=iMA(NULL,gtPeriod,gtBbEmaPeriod,0,MODE_EMA,PRICE_CLOSE,1);
dbftema2=iMA(NULL,gtPeriod,gtFtEmaPeriod,0,MODE_EMA,PRICE_CLOSE,2);
dbswema2=iMA(NULL,gtPeriod,gtSwEmaPeriod,0,MODE_EMA,PRICE_CLOSE,2);
dbrsi0=iRSI(NULL,gtPeriod,9,PRICE_CLOSE,0);
dbrsi1=iRSI(NULL,gtPeriod,9,PRICE_CLOSE,1);
dbrsi2=iRSI(NULL,gtPeriod,9,PRICE_CLOSE,2);
dbstochastics0=iStochastic(NULL,gtPeriod,10,3,3,MODE_EMA,0,MODE_MAIN,0);
dbstochastics1=iStochastic(NULL,gtPeriod,10,3,3,MODE_EMA,0,MODE_MAIN,1);
dbstochastics2=iStochastic(NULL,gtPeriod,10,3,3,MODE_EMA,0,MODE_MAIN,2);
dbmacd0=iMACD(NULL,gtPeriod,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
dbmacd1=iMACD(NULL,gtPeriod,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
dbmacd2=iMACD(NULL,gtPeriod,12,26,9,PRICE_CLOSE,MODE_MAIN,2);
dbprebartrend=(iClose(NULL,gtPeriod,1)-iOpen(NULL,gtPeriod,1));
if (gtPeriod==pyitShortTimePeriod)
{
if (
(dbftema0>dbswema0+itShift1)
//&&(dbswema0>dbbbema0+itShift1)
&&(dbftema0>dbftema1)
&&(dbrsi0>50)
&&(dbstochastics0>dbstochastics1)
&&(dbstochastics0<80)
&&(dbmacd0>dbmacd1)
&&(dbmacd1<0)
)
{
return (BULL);
} //end of bull—(gtPeriod==pyitShortTimePeriod)
if (
(dbftema0<dbswema0-itShift1)
//&&(dbswema0<dbbbema0-itShift1)
&&(dbftema0<dbftema1)
&&(dbrsi0<50)
&&(dbstochastics0<dbstochastics1)
&&(dbstochastics0>20)
&&(dbmacd0<dbmacd1)
&&(dbmacd1>0)
)
{
return (BEAR);
} //end of bear—(gtPeriod==pyitShortTimePeriod)
}//END OF if (gtPeriod==pyitShortTimePeriod)
if (gtPeriod==pyitLongTimePeriod)
{
if (
(dbftema0>dbswema0+itShift1)
//&&(dbftema0>dbbbema0+itShift1)
&&(dbftema0>dbftema1)
&&(dbrsi0>50)
&&(dbstochastics0>dbstochastics1)
&&(dbstochastics0<80)
)
{
return (BULL);
} //end of bull—(gtPeriod==pyitLongTimePeriod)
if (
(dbftema0<dbswema0-itShift1)
//&&(dbftema0<dbbbema0-itShift1)
&&(dbftema0<dbftema1)
&&(dbrsi0<50)
&&(dbstochastics0<dbstochastics1)
&&(dbstochastics0>20)
)
{
return (BEAR);
} //end of bear—(gtPeriod==pyitLongTimePeriod)
} //end of if (gtPeriod==pyitLongTimePeriod)
return (0);
} //end of funCalTrend