//+---------------------------------------------------------------------------------+
//| Lucky.v.1.2.mq4 |
//| Copyright © 2007 Serg_ASV and ak97052d |
//| http://codebase.mql4.com/ru/1555 |
//| http://www.forex-tsd.com/expert-advisors-metatrader-4/10366-lucky1-1-a.html |
//| |
//| 10.10.2007 v.1 by Serg_ASV |
//| 25.10.2007 v.1.1 by ak97052d (WeekEndStop/TradingTime/MM/MicroLot) |
//| 26.10.2007 v.1.2 by ak97052d (MagicNumber/Slippage/DiferentTradingTimeByChart) |
//| [EURGBP][EURCHF][GBPCHF][USDCAD] |
//+---------------------------------------------------------------------------------+ :p
extern bool MM = true; // razmer lota avtomatitchecki chtitaetca
extern int Risk = 100; // v procentax (%). minimum '1' maximum '100'
extern double ManLot = 0.1; // razmer lota ecli 'MM=false'
extern int Shift = 3;
extern int Limit = 18;
extern int TP = 2;
extern int Slippage = 3;
extern bool UsePersoSetting = false; // if 'false', 'PersoMagic' 'PersoOpenHour' and 'PersoCloseHour' will be used.
/*--------PersoSetting--------*/
extern int PersoMagic = 261007;
int PersoOpenHour = 19;
int PersoCloseHour = 7;
/*--------WeekEndStop--------*/
int WED1 = 5; // Day of begin
int WEH1 = 22; // Hour of begin
int WED2 = 1; // Day of end
int WEH2 = 2; // hour of end
//----------------------------------------------------------------------------
bool first = true;
double MaxLot = 100;
double MinLot = 0.1;
int LotsDigit = 1;
int Magic = 0;
double TV,SP;
//----------------------------------------------------------------------------
int TradingTime,WE,OpenHour,CloseHour; double a, b;
int init()
{
if (UsePersoSetting == false)
{
if (Symbol() == "EURGBPm" || Symbol() == "EURGBP")
{
Magic = 1001; OpenHour = 19; CloseHour = 7;
}
if (Symbol() == "EURCHFm" || Symbol() == "EURCHF")
{
Magic = 1010; OpenHour = 19; CloseHour = 7;
}
if (Symbol() == "GBPCHFm" || Symbol() == "GBPCHF")
{
Magic = 1011; OpenHour = 19; CloseHour = 7;
}
if (Symbol() == "USDCADm" || Symbol() == "USDCAD")
{
Magic = 1100; OpenHour = 1; CloseHour = 13;
}
// add here other chart config
if (Magic == 0)
{
Magic = 1000; OpenHour = 19; CloseHour = 7;
}
}
else
{
Magic = PersoMagic; OpenHour = PersoOpenHour; CloseHour = PersoCloseHour;
}
}
int start()
{
if (WEstop() == 0 && Tradetime() == 1)
{
if (first)
{
a=Ask;
b=Bid;
first=false;
return(0);
}
if (Ask-a>=Shift*Point)
{
OrderSend(Symbol(),OP_SELL,GetLots(),Bid,Slippage,0,0,"Lucky",Magic,0,Red);
}
if (b-Bid>=Shift*Point)
{
OrderSend(Symbol(),OP_BUY,GetLots(),Ask,Slippage,0,0,"Lucky",Magic,0,Blue);
}
}
a=Ask;
b=Bid;
CloseAll();
return(0);
}
//-----------------------------------------------------------------------------
void CloseAll()
{
for (int cnt = OrdersTotal()-1 ; cnt >= 0; cnt--)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if (OrderSymbol() == Symbol() && OrderMagicNumber()==Magic)
{
if (SecurProfit()==1 || Tradetime() == 0)
{
if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Blue);
if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red);
}
else
{
if((OrderType()==OP_BUY) && (((OrderOpenPrice()-Ask)/Point) > Limit))
OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Blue);
if((OrderType()==OP_SELL) && (((Bid-OrderOpenPrice())/Point) > Limit))
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red);
}
}
}
}
//--------------------------------------------------------------------------
int WEstop()
{
WE=0;
if (TimeHour(TimeCurrent()) > WEH1 && DayOfWeek() == WED1) WE=1;
if (TimeHour(TimeCurrent()) < WEH2 && DayOfWeek() == WED2) WE=2;
return(WE);
}
//--------------------------------------------------------------------------
int Tradetime()
{
TradingTime=0;
if (TimeHour(TimeCurrent()) >= OpenHour || TimeHour(TimeCurrent())< CloseHour) TradingTime=1;
return(TradingTime);
}
//--------------------------------------------------------------------------
double GetLots()
{
double lots,MD,RM,FMM,MinLot,MaxLot; int lotsdigit;
MD = NormalizeDouble(MarketInfo(Symbol(), MODE_LOTSTEP), 2); // chag mejdu lotami (0.1 ili 0.01)pomojit nam uznat ecli eto micro accunt ili net
RM = NormalizeDouble(MarketInfo(Symbol(), MODE_MARGINREQUIRED), 4);
FMM = (RM+5)*100;
if (MD==0.01) lotsdigit=2;
else lotsdigit=1;
LotsDigit=lotsdigit;
if (MM==true) lots = NormalizeDouble(AccountFreeMargin()/(FMM/Risk)-0.05,LotsDigit);
else lots=ManLot;
MinLot=NormalizeDouble(MarketInfo(Symbol(),MODE_MINLOT),2); // minimalnaia pozvolena stavka
MaxLot=NormalizeDouble(MarketInfo(Symbol(), MODE_MAXLOT),2); // maximalnaia pozvolena stavka
if (LotsDigit == 2) MinLot = 0.01;
if (lots < MinLot) lots = MinLot;
if (lots > MaxLot) lots = MaxLot;
return (lots);
}
//-------------------------------------------------------------------------
double TickValue() //
{
double tv;
tv = NormalizeDouble(MarketInfo(Symbol(), MODE_TICKVALUE), 4); // pribl za 1 pip c 1 lot
return(tv);
}
int SecurProfit()
{
int sp=0;
if (OrderProfit()>(TickValue()*GetLots()*TP)) sp=1; // ecli pribilnix ticksov viche 'TP'. rezultat 'SecurProfit()' budit '1'
return(sp);
}
Comments