//+------------------------------------------------------------------+
//| MACD++ EA.mq4 |
//| Copyright © 2011, MadWill |
//| http://www.evgeno.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, MadWill"
#property link "http://www.evgeno.ru"
extern int Slippage=5;
extern double Lots=0.01;
extern int TrallingStop=0;
extern int TrallingStep=25;
extern int TrallingProfit=0;
extern int EscapeTralling=0;
extern int StopLoss=0;
extern int TakeProfit=0;
extern int Risk=0;
extern int Reverse=1;
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA1=9;
extern int SignalSMA2=25;
double PurpStep=0;
double Purp=0;
int Pos, Tic, i, Orders, BarTime, Trend;
double SL, TP;
string Stringer;
bool Exec=false;
int Sells,Buys;
double TopSell,TopBuy,BotSell,BotBuy;
int Trade=1;
int Trading=0;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
//----
if(Risk>0){
double Min=MarketInfo(Symbol(),MODE_MINLOT);
double Max=MarketInfo(Symbol(),MODE_MAXLOT);
double Req=MarketInfo(Symbol(),MODE_MARGINREQUIRED);
double Free=AccountFreeMargin()/100*Risk;
Lots=Free/Req;
if(Lots<Min) Lots=Min;
if(Lots>Max) Lots=Max;
RefreshRates();
Lots=NormalizeDouble(Lots/MarketInfo(Symbol(),MODE_LOTSTEP),0);
Lots=Lots*MarketInfo(Symbol(),MODE_LOTSTEP);
PurpStep=NormalizeDouble(AccountEquity()/100*Risk,2);
}
if(BarTime!=Time[0]){
BarTime=Time[0];
once();
}
/*****SPECIAL******************/
Orders=OrdersTotal();
for(i=Orders-1;i>=0;i--){
OrderSelect(i,SELECT_BY_POS);
if(OrderType()==OP_BUY && OrderSymbol()==Symbol()){
//TRALLING STOP--------
if(TrallingStop>0){
RefreshRates();
if(Bid>OrderOpenPrice()+TrallingStop*Point+TrallingStep*Point && OrderStopLoss()==0){
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrallingStop*Point,OrderTakeProfit(),0);
}
else if(Bid>OrderOpenPrice()+TrallingStop*Point+TrallingStep*Point && Bid>OrderStopLoss()+TrallingStop*Point+TrallingStep*Point && OrderStopLoss()!=0){
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrallingStop*Point,OrderTakeProfit(),0);
}
}
/*
//TRALLING PROFIT
if(TrallingProfit==-1){
if(Bid>OrderOpenPrice()+TrallingProfit*Point){
if(TrallingStop>0){
SL=Ask-TrallingStop*Point;
if(TakeProfit>0) TP=Ask+TakeProfit*Point;
OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,SL,TP);
}else OpBuy();
}
}
*/
}
if(OrderType()==OP_SELL && OrderSymbol()==Symbol()){
//TRALLING STOP--------
if(TrallingStop>0){
RefreshRates();
if(Ask<OrderOpenPrice()-TrallingStop*Point-TrallingStep*Point && OrderStopLoss()==0){
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrallingStop*Point,OrderTakeProfit(),0);
}
else if(Ask<OrderOpenPrice()-TrallingStop*Point-TrallingStep*Point && Ask<OrderStopLoss()-TrallingStop*Point-TrallingStep*Point && OrderStopLoss()!=0){
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrallingStop*Point,OrderTakeProfit(),0);
}
}
/*
//TRALLING PROFIT
if(TrallingProfit>0){
if(Ask<OrderOpenPrice()-TrallingProfit*Point){
if(TrallingStop==-1){
SL=Bid+TrallingStop*Point;
if(TakeProfit>0) TP=Bid-TakeProfit*Point;
OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,SL,TP);
}else OpSell();
}
}
*/
}
}
RefreshMarkets();
RefreshRates();
if(Purp>0 && AccountEquity()>=Purp){
CloseSell();
CloseBuy();
if(PurpStep==0) Purp=0;
else Purp+=PurpStep;
//ObjectSetText("P_Purp",DoubleToStr(Purp,DepositDigits),F_Size,F_Family,C_Preset);
}
//TRALLING PROFIT
if(TrallingProfit>0){
if(Bid>TopBuy+TrallingProfit*Point && TopBuy!=0){
OpBuy();
TotalStopLoss(Bid-NormalizeDouble(TrallingProfit/2,0)*Point);
}
if(Ask<BotSell-TrallingProfit*Point && BotSell!=0){
OpSell();
TotalStopLoss(Ask+NormalizeDouble(TrallingProfit/2,0)*Point);
}
}
//TRALLING ESCAPE
if(EscapeTralling>0){
if(Ask>TopSell+EscapeTralling*Point && TopSell!=0) OpSell();
if(Bid<BotBuy-EscapeTralling*Point && BotBuy!=0) OpBuy();
}
//STRATEGY
/*********************/
//----
return(0);
}
//+------------------------------------------------------------------+
int RefreshMarkets(){
Orders=OrdersTotal();
Sells=0;
Buys=0;
TopSell=0;
TopBuy=0;
BotSell=0;
BotBuy=0;
for(i=Orders-1;i>=0;i--){
OrderSelect(i,SELECT_BY_POS);
if(OrderType()==OP_BUY && OrderSymbol()==Symbol()){
Buys++;
if(OrderOpenPrice()>TopBuy || TopBuy==0) TopBuy=OrderOpenPrice();
if(OrderOpenPrice()<BotBuy || BotBuy==0) BotBuy=OrderOpenPrice();
}
if(OrderType()==OP_SELL && OrderSymbol()==Symbol()){
Sells++;
if(OrderOpenPrice()>TopSell || TopSell==0) TopSell=OrderOpenPrice();
if(OrderOpenPrice()<BotSell || BotSell==0) BotSell=OrderOpenPrice();
}
}
return(0);
}
int CloseSell(){
Orders=OrdersTotal();
for(i=Orders-1;i>=0;i--){
OrderSelect(i,SELECT_BY_POS);
if(OrderType()==OP_SELL && OrderSymbol()==Symbol()){
RefreshRates();
if(OrderClose(OrderTicket(),OrderLots(),Ask,Slippage)==true)
PlaySound("ok.wav");
//else Alert(GetLastError());
}
}
return(0);
}
int CloseBuy(){
Orders=OrdersTotal();
for(i=Orders-1;i>=0;i--){
OrderSelect(i,SELECT_BY_POS);
if(OrderType()==OP_BUY && OrderSymbol()==Symbol()){
RefreshRates();
if(OrderClose(OrderTicket(),OrderLots(),Bid,Slippage)==true)
PlaySound("ok.wav");
//else Alert(GetLastError());
}
}
return(0);
}
int OpSell(){
RefreshRates();
if(StopLoss>0) SL=Bid+StopLoss*Point; else SL=0;
if(TakeProfit>0) TP=Bid-TakeProfit*Point; else TP=0;
Tic=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,SL,TP);
if(Tic>0){
PlaySound("ok.wav");
//ObjectCreate("XP_DOWN",OBJ_HLINE,0,0,0);
//ObjectSet("XP_DOWN",OBJPROP_COLOR,Blue);
//ObjectSet("XP_DOWN",OBJPROP_STYLE,3);
//if(XP>0) ObjectSet("XP_DOWN",OBJPROP_PRICE1,Bid-XP);
}
//else Print(Lots);
return(0);
}
int OpBuy(){
RefreshRates();
if(StopLoss>0) SL=Ask-StopLoss*Point; else SL=0;
if(TakeProfit>0) TP=Ask+TakeProfit*Point; else TP=0;
Tic=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,SL,TP);
if(Tic>0){
PlaySound("ok.wav");
//ObjectCreate("XP_UP",OBJ_HLINE,0,0,0);
//ObjectSet("XP_UP",OBJPROP_COLOR,Blue);
//ObjectSet("XP_UP",OBJPROP_STYLE,3);
//if(XP>0) ObjectSet("XP_UP",OBJPROP_PRICE1,Ask+XP);
}
//else Print(Lots);
return(0);
}
int TotalStopLoss(double Price){
RefreshRates();
Orders=OrdersTotal();
for(i=Orders-1;i>=0;i--){
OrderSelect(i,SELECT_BY_POS);
if(OrderType()==OP_BUY && Price<Bid && OrderSymbol()==Symbol()){
OrderModify(OrderTicket(),OrderOpenPrice(),Price,OrderTakeProfit(),0);
}
if(OrderType()==OP_SELL && Price>Ask && OrderSymbol()==Symbol()){
OrderModify(OrderTicket(),OrderOpenPrice(),Price,OrderTakeProfit(),0);
}
}
}
int once(){
Trading=0;
RefreshMarkets();
if(
iCustom(Symbol(),0,"MACD++",FastEMA,SlowEMA,SignalSMA1,SignalSMA2,1,1)>iCustom(Symbol(),0,"MACD++",FastEMA,SlowEMA,SignalSMA1,SignalSMA2,2,1)&&
iCustom(Symbol(),0,"MACD++",FastEMA,SlowEMA,SignalSMA1,SignalSMA2,1,2)<=iCustom(Symbol(),0,"MACD++",FastEMA,SlowEMA,SignalSMA1,SignalSMA2,2,2)
){
if(Reverse==1) CloseSell();
OpBuy();
}
if(
iCustom(Symbol(),0,"MACD++",FastEMA,SlowEMA,SignalSMA1,SignalSMA2,1,1)<iCustom(Symbol(),0,"MACD++",FastEMA,SlowEMA,SignalSMA1,SignalSMA2,2,1)&&
iCustom(Symbol(),0,"MACD++",FastEMA,SlowEMA,SignalSMA1,SignalSMA2,1,2)>=iCustom(Symbol(),0,"MACD++",FastEMA,SlowEMA,SignalSMA1,SignalSMA2,2,2)
){
if(Reverse==1) CloseBuy();
OpSell();
}
}
Comments