كود PHP:
//+------------------------------------------------------------------+
//| El-Sakka v1.mq4 |
//| Copyright © 2007 , dr_waleed |
//| www.moneyexpertsclub.com |
//| www.arabtraderacademy.com |
//| dr_waleed@msn.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, dr_waleed"
#property link "http://www.moneyexpertsclub.net/forum/forumdisplay.php?f=48"
//---- Trades Limits
extern double MovingAverage = 50;
extern double TakeProfit = 150;
extern double StopLoss = 100;
extern double TrailingStop = 0;
extern bool SmartClose = true;
//---- Hour Trades
extern bool UseHourTrade = false;
extern int FromHourTrade = 8;
extern int ToHourTrade = 20;
//---- Money Monagement
extern string MM_Parameters = "Money Management";
extern int MaxTrades = 1;
extern double Lots = 1;
extern bool MM = true,
AccountIsMicro = false;
extern int Risk = 10;
//---- Global variables
int ID = 109811;
int ID2 = 109800;
string eaComment = "El-Sakka v1";
//+------------------------------------------------------------------+
//| Initialation function |
//+------------------------------------------------------------------+
int init()
{
return(0);
}
//+------------------------------------------------------------------+
//| deinitialization function |
//+------------------------------------------------------------------+
void deinit()
{
Comment("bye");
}
int orderscnt()
{
int cnt=0;
for(int i =0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && ID==OrderMagicNumber())
{
cnt++;
}
}
}
return(cnt);
}
//----------
bool isNewSymbol(string current_symbol)
{
int total = OrdersTotal();
for(int cnt = 0 ; cnt < total ; cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
string selected_symbol = OrderSymbol();
if (current_symbol == selected_symbol && (OrderMagicNumber()==ID ||OrderMagicNumber()==ID2))
return (False);
}
return (True);
}
//+------------------------------------------------------------------+
//| Start function |
//+------------------------------------------------------------------+
int start()
{
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(MM) Lots = subLotSize();
double sl, tp;
int cnt, total;
if (UseHourTrade)
{
if(!(Hour()>=FromHourTrade&&Hour()<=ToHourTrade))
{
Comment("Non-Trading Hours!");
return(0);
}
}
double MA;
MA = iMA(NULL, 0, MovingAverage, 0, MODE_EMA, PRICE_CLOSE,0);
total=OrdersTotal();
if(total<1 || isNewSymbol(Symbol()))
{
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ",
AccountFreeMargin());
return(0);
}
if (Close[0]>MA) //---- Buy
{
if(orderscnt()<MaxTrades)
{
if(StopLoss==0)
{sl=0;TrailingStop=0;}
else
{sl=Ask-StopLoss*Point;}
if(TakeProfit==0)
{tp=0;}
else
{tp=Ask+TakeProfit*Point;}
OrderSend(Symbol(),OP_BUY,Lots,Ask,3,sl,tp,eaComment,ID,0,Green);
PlaySound("Alert.wav");
}
}
if (Close[0]<MA) //---- Sell
{
if(orderscnt()<MaxTrades)
{
if(StopLoss==0)
{sl=0;TrailingStop=0;}
else
{sl=Bid+StopLoss*Point;}
if(TakeProfit==0)
{tp=0;}
else
{tp=Bid-TakeProfit*Point;}
OrderSend(Symbol(),OP_SELL,Lots,Bid,3,sl,tp,eaComment,ID2,0,Red);
PlaySound("Alert.wav");
}
}
return(0);
}
//---------- Trailing Stop
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()+Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
else
{
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()-Point*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
}
//---------- Smart Close
if (SmartClose)
{
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
if(Close[0]<MA) //---- Cloes Buy
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
return(0);
}
}
else
{
if(Close[0]>MA) //---- Close Sell
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
return(0);
}
}
}
}
}
return(0);
}
//+------------------------------------------------------------------+
//| Money Management |
//+------------------------------------------------------------------+
double subLotSize()
{
double lotMM = MathCeil(AccountFreeMargin() * Risk / 1000) / 100;
if(AccountIsMicro==false)
{
if(lotMM < 0.1) lotMM = Lots;
if((lotMM > 0.5) && (lotMM < 1)) lotMM = 0.5;
if(lotMM > 1.0) lotMM = MathCeil(lotMM);
if(lotMM > 100) lotMM = 100;
}
else
{
if(lotMM < 0.01) lotMM = Lots;
if(lotMM > 1.0) lotMM = MathCeil(lotMM);
if(lotMM > 100) lotMM = 100;
}
return (lotMM);
}
//------------------------------------------------------------------------------------- The End.