Janweckmueller EA

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 23

//+------------------------------------------------------------------+

//| Janweckmueller EA.mq4 |


//| Francis Ferrer |
//| https://eabuilder21.wordpress.com |
//+------------------------------------------------------------------+
#property copyright "Francis Ferrer"
#property link "https://eabuilder21.wordpress.com"
#property version "1.00"
#property strict

extern string
EA_Settings = "+++ General Settings +++";
extern int
MagicNumber = 12345;
extern double
Lots = 0.10; //Starting Lot
extern double
MartingaleFactor = 2;
extern double
TakeProfit = 20, //TakeProfit (in PIPs)
StopLoss = 20; //StopLoss (in PIPs)
int
MaxOpenTrades = 1;
extern bool
BrokerIsECN = true;
bool
CloseOnReverseSignal = false; //Close Trade When Opposite Arrow Appears

//+------------------------------------------------------------------+
//| expert LOCAL VARIABLES |
//+------------------------------------------------------------------+
datetime TT,STT,BTT;
int MyDigits;
string TicketComment = "Janweckmueller EA";
extern int Slippage= 6;
extern int
FilterSpread = 20; //Max Spread

extern string MASettings = "+++ Moving Average Settings +++"; //Settings


ENUM_TIMEFRAMES MovingAverage_TimeFrame = PERIOD_CURRENT;
extern int MovingAverage_Period = 50;
extern int MovingAverage_Shift = 0;
extern ENUM_MA_METHOD MovingAverage_Method = MODE_EMA;
extern ENUM_APPLIED_PRICE MovingAverage_AppliedPrice = PRICE_CLOSE;
extern int MAShift = 1; //MA Entry Candle Shift (0=current, 1=previous)

extern string EnableTrading_SpecficDayOfWeek = "+++ No Default Session +++";


//Enable Trading Day of Week
enum enum_Time
{
AutoGMT = 1, //AutoGMT(0)
ManualGMT = 2,
BrokersTime = 3 //Broker's Time
};
input enum_Time TimeSettings = BrokersTime;
input double ManualGMTAdjustment = 0; //Manual GMT+/-

enum ENUM_HOUR
{
h00 = 00, // 00:00
h01 = 01, // 01:00
h02 = 02, // 02:00
h03 = 03, // 03:00
h04 = 04, // 04:00
h05 = 05, // 05:00
h06 = 06, // 06:00
h07 = 07, // 07:00
h08 = 08, // 08:00
h09 = 09, // 09:00
h10 = 10, // 10:00
h11 = 11, // 11:00
h12 = 12, // 12:00
h13 = 13, // 13:00
h14 = 14, // 14:00
h15 = 15, // 15:00
h16 = 16, // 16:00
h17 = 17, // 17:00
h18 = 18, // 18:00
h19 = 19, // 19:00
h20 = 20, // 20:00
h21 = 21, // 21:00
h22 = 22, // 22:00
h23 = 23, // 23:00
};
enum ENUM_MIN
{
m00 = 00, // 00:00
m01 = 01, // 00:01
m02 = 02, // 00:02
m03 = 03, // 00:03
m04 = 04, // 00:04
m05 = 05, // 00:05
m06 = 06, // 00:06
m07 = 07, // 00:07
m08 = 08, // 00:08
m09 = 09, // 00:09
m10 = 10, // 00:10
m11 = 11, // 00:11
m12 = 12, // 00:12
m13 = 13, // 00:13
m14 = 14, // 00:14
m15 = 15, // 00:15
m16 = 16, // 00:16
m17 = 17, // 00:17
m18 = 18, // 00:18
m19 = 19, // 00:19
m20 = 20, // 00:20
m21 = 21, // 00:21
m22 = 22, // 00:22
m23 = 23, // 00:23
m24 = 24, // 00:24
m25 = 25, // 00:25
m26 = 26, // 00:26
m27 = 27, // 00:27
m28 = 28, // 00:28
m29 = 29, // 00:29
m30 = 30, // 00:30
m31 = 31, // 00:31
m32 = 32, // 00:32
m33 = 33, // 00:33
m34 = 34, // 00:34
m35 = 35, // 00:35
m36 = 36, // 00:36
m37 = 37, // 00:37
m38 = 38, // 00:38
m39 = 39, // 00:39
m40 = 40, // 00:40
m41 = 41, // 00:41
m42 = 42, // 00:42
m43 = 43, // 00:43
m44 = 44, // 00:44
m45 = 45, // 00:45
m46 = 46, // 00:46
m47 = 47, // 00:47
m48 = 48, // 00:48
m49 = 49, // 00:49
m50 = 50, // 00:50
m51 = 51, // 00:51
m52 = 52, // 00:52
m53 = 53, // 00:53
m54 = 54, // 00:54
m55 = 55, // 00:55
m56 = 56, // 00:56
m57 = 57, // 00:57
m58 = 58, // 00:58
m59 = 59, // 00:59
};
extern bool Monday = true;
extern ENUM_HOUR StartTimeMon = h00;
extern ENUM_MIN StartMinMon = m00;
extern ENUM_HOUR StopTimeMon = h23;
extern ENUM_MIN StopMinMon = m59;

extern bool Tuesday = true;


extern ENUM_HOUR StartTimeTue = h00;
extern ENUM_MIN StartMinTue = m00;
extern ENUM_HOUR StopTimeTue = h23;
extern ENUM_MIN StopMinTue = m59;

extern bool Wednesday = true;


extern ENUM_HOUR StartTimeWed = h00;
extern ENUM_MIN StartMinWed = m00;
extern ENUM_HOUR StopTimeWed = h23;
extern ENUM_MIN StopMinWed = m59;

extern bool Thursday = true;


extern ENUM_HOUR StartTimeThu = h00;
extern ENUM_MIN StartMinThu = m00;
extern ENUM_HOUR StopTimeThu = h23;
extern ENUM_MIN StopMinThu = m59;

extern bool Friday = true;


extern ENUM_HOUR StartTimeFri = h00;
extern ENUM_MIN StartMinFri = m00;
extern ENUM_HOUR StopTimeFri = h23;
extern ENUM_MIN StopMinFri = m59;
extern bool Saturday = true;
extern ENUM_HOUR StartTimeSat = h00;
extern ENUM_MIN StartMinSat = m00;
extern ENUM_HOUR StopTimeSat = h23;
extern ENUM_MIN StopMinSat = m59;

extern bool Sunday = true;


extern ENUM_HOUR StartTimeSun = h00;
extern ENUM_MIN StartMinSun = m00;
extern ENUM_HOUR StopTimeSun = h23;
extern ENUM_MIN StopMinSun = m59;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

if(DaytoTrade() && CheckActiveHours() && CheckActiveMinutes())


{
Alert("Trading is Enabled");
}
else
{
Alert("Trading is not Enabled");
}

TicketComment = WindowExpertName();
if(Digits==5)
MyDigits=4;
else
if(Digits==3)
MyDigits=2;
else
MyDigits = Digits;

return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----

string Signal = subSignal();


subCloseTrades(Signal); // CONTROLS THE CLOSING OF ORDERS BASED ON SIGNAL

if(MarketInfo(NULL,MODE_SPREAD)<FilterSpread && DaytoTrade() &&


CheckActiveHours() && CheckActiveMinutes())
{
subOpenTrades(Signal);// CONTROLS THE OPENING OF ORDERS BASED ON SIGNAL
}

//----
return(0);
}
//+-------------------- end of start function -----------------------+

//-------------------------------------------------------------------+
// DEFAULT FUNCTION DEFINITIONS |
//-------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string subSignal()
{
string Signal = "none";

int MABuySignal=0;
int MASellSignal=0;
{
double MA1Value =
iMA(NULL,MovingAverage_TimeFrame,MovingAverage_Period,MovingAverage_Shift,MovingAve
rage_Method,MovingAverage_AppliedPrice,MAShift);

if(Close[MAShift]<=MA1Value)
{
MABuySignal = 1;
}
if(Close[MAShift]>=MA1Value)
{
MASellSignal = 1;
}
}
if(PrevProfit()<=0)
{
if(PrevType()==1)
Signal = "SELL";
if(PrevType()==2)
Signal = "BUY";
if(PrevProfit()==0)
{
if(MABuySignal==1)
Signal = "BUY";
if(MASellSignal==1)
Signal = "SELL";
}
}
else
{
if(MABuySignal==1)
Signal = "BUY";
if(MASellSignal==1)
Signal = "SELL";
}

return(Signal);
}
//-------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void subOpenTrades(string signal)
{

int ticket = 0;

if(subTotalTrade()<MaxOpenTrades && TT!=iTime(Symbol(),0,0))


{

//BUY
if(signal == "BUY")
{
ticket = subOpenOrder(OP_BUY, StopLoss, TakeProfit, subLots());
if(ticket>0)
TT=iTime(Symbol(),0,0);

}
//SELL
if(signal == "SELL")
{
ticket = subOpenOrder(OP_SELL, StopLoss, TakeProfit, subLots());
if(ticket>0)
TT=iTime(Symbol(),0,0);
}
}

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void subCloseTrades(string signal)
{
if(CloseOnReverseSignal && subTotalTrade()>0)
{
//CLOSE SELL TRADES BASED WHEN THERE IS A BUY SIGNAL
if(signal == "BUY" && subTotalTypeOrders(OP_SELL)>0)
{
subCloseOrder(OP_SELL);
}
//CLOSE BUY TRADES BASED WHEN THERE IS A SELL SIGNAL
if(signal == "SELL" && subTotalTypeOrders(OP_BUY)>0)
{
subCloseOrder(OP_BUY);
}
}
}
//-------------------------------------------------------------------+
// COUNTS THE TOTAL NUMBER OF OPEN TRADES
int subTotalTrade()
{
int
cnt,
total = 0;

for(cnt=0; cnt<OrdersTotal(); cnt++)


{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
{
if(OrderSymbol()==Symbol() &&
OrderMagicNumber()==MagicNumber)
total++;
}
}
return(total);
}
//-------------------------------------------------------------------+
// OPEN ORDERS (UPDATED v2)
int subOpenOrder(int type, double stoploss, double takeprofit,double Lotz)
{
int NumberOfTries = 10;
int
ticket = 0,
err = 0,
c = 0;

double
aStopLoss = 0,
aTakeProfit = 0,
bStopLoss = 0,
bTakeProfit = 0;

int decimalPlaces=0;
if(MarketInfo(Symbol(),MODE_LOTSTEP)<1)
decimalPlaces=1;
if(MarketInfo(Symbol(),MODE_LOTSTEP)<0.1)
decimalPlaces=2;

double Lotzs = NormalizeDouble(Lotz,decimalPlaces);


if(Lotzs<MarketInfo(Symbol(),MODE_MINLOT))
Lotzs=MarketInfo(Symbol(),MODE_MINLOT);
if(Lotzs>MarketInfo(Symbol(),MODE_MAXLOT))
Lotzs=MarketInfo(Symbol(),MODE_MAXLOT);

if(type==OP_BUY)
{
for(c=0; c<NumberOfTries; c++)
{
RefreshRates();
if(stoploss!=0)
{
aStopLoss = NormalizeDouble(Ask-stoploss*GetPipValue(),Digits);
bStopLoss = NormalizeDouble(Bid+stoploss*GetPipValue(),Digits);
}

if(takeprofit!=0)
{
aTakeProfit = NormalizeDouble(Ask+takeprofit*GetPipValue(),Digits);
bTakeProfit = NormalizeDouble(Bid-takeprofit*GetPipValue(),Digits);
}

if(BrokerIsECN==false)

ticket=OrderSend(Symbol(),OP_BUY,Lotzs,NormalizeDouble(Ask,Digits),Slippage,aStopLo
ss,aTakeProfit,TicketComment,MagicNumber,0,Green);
if(BrokerIsECN==True)
{

ticket=OrderSend(Symbol(),OP_BUY,Lotzs,NormalizeDouble(Ask,Digits),Slippage,0,0,Tic
ketComment,MagicNumber,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET)==true)
{
if(stoploss!=0)
{
aStopLoss = NormalizeDouble(OrderOpenPrice()-
stoploss*GetPipValue(),Digits);
bStopLoss = NormalizeDouble(OrderOpenPrice()
+stoploss*GetPipValue(),Digits);
}

if(takeprofit!=0)
{
aTakeProfit = NormalizeDouble(OrderOpenPrice()
+takeprofit*GetPipValue(),Digits);
bTakeProfit = NormalizeDouble(OrderOpenPrice()-
takeprofit*GetPipValue(),Digits);
}
if(aStopLoss>0 || aTakeProfit>0)
{

if(OrderModify(ticket,OrderOpenPrice(),aStopLoss,aTakeProfit,0,Red)==false)
{
bool ModBuy =
OrderModify(ticket,OrderOpenPrice(),aStopLoss,aTakeProfit,0,Red);
}
}
}
}
}
err=GetLastError();
if(err==0)
{
if(ticket>0)
break;
}
else
{
Print("ERROR "+IntegerToString(err)+": "+error(err));
if(err==0 || err==4 || err==136 || err==137 || err==138 ||
err==146) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
if(ticket>0)
break;
}
}
}
}
if(type==OP_SELL)
{
for(c=0; c<NumberOfTries; c++)
{
RefreshRates();
if(stoploss!=0)
{
aStopLoss = NormalizeDouble(Ask-stoploss*GetPipValue(),Digits);
bStopLoss = NormalizeDouble(Bid+stoploss*GetPipValue(),Digits);
}

if(takeprofit!=0)
{
aTakeProfit = NormalizeDouble(Ask+takeprofit*GetPipValue(),Digits);
bTakeProfit = NormalizeDouble(Bid-takeprofit*GetPipValue(),Digits);
}
if(BrokerIsECN==false)

ticket=OrderSend(Symbol(),OP_SELL,Lotzs,NormalizeDouble(Bid,Digits),Slippage,bStopL
oss,bTakeProfit,TicketComment,MagicNumber,0,Red);
if(BrokerIsECN==True)
{

ticket=OrderSend(Symbol(),OP_SELL,Lotzs,NormalizeDouble(Bid,Digits),Slippage,0,0,Ti
cketComment,MagicNumber,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET)==true)
{
if(stoploss!=0)
{
aStopLoss = NormalizeDouble(OrderOpenPrice()-
stoploss*GetPipValue(),Digits);
bStopLoss = NormalizeDouble(OrderOpenPrice()
+stoploss*GetPipValue(),Digits);
}

if(takeprofit!=0)
{
aTakeProfit = NormalizeDouble(OrderOpenPrice()
+takeprofit*GetPipValue(),Digits);
bTakeProfit = NormalizeDouble(OrderOpenPrice()-
takeprofit*GetPipValue(),Digits);
}
if(bStopLoss>0 || bTakeProfit>0)
{

if(OrderModify(ticket,OrderOpenPrice(),bStopLoss,bTakeProfit,0,Red)==false)
{
bool ModSell =
OrderModify(ticket,OrderOpenPrice(),bStopLoss,bTakeProfit,0,Red);
}
}
}
}
}
err=GetLastError();
if(err==0)
{
if(ticket>0)
break;
}
else
{
Print("ERROR "+IntegerToString(err)+": "+error(err));
if(err==0 || err==4 || err==136 || err==137 || err==138 ||
err==146) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
if(ticket>0)
break;
}
}
}
}
return(ticket);
}

//-------------------------------------------------------------------+

//CLOSES ALL OPEN ORDERS - FIFO COMPLIANT


void subCloseOrder(int TYPE)
{
int
cnt,
total = 0,
ticket = 0,
err = 0,
c = 0;

RefreshRates();
for(cnt=0; cnt<OrdersTotal(); cnt++)
{
if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)==true)
{

if(OrderType() == TYPE &&


OrderSymbol() == Symbol() &&
OrderMagicNumber()==MagicNumber)
{
Print("Close Order# "+IntegerToString(OrderTicket()));
switch(OrderType())
{
case OP_BUY :
for(c=0; c<9; c++)
{
RefreshRates();
ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),6,Violet);
err=GetLastError();
if(err==0)
{
if(ticket>0)
{
cnt = cnt-1;
break;
}
}
else
{
if(err==0 || err==4 || err==136 || err==137 || err==138 ||
err==146) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
if(ticket>0)
{
cnt = cnt-1;
break;
}
}
}
}
break;

case OP_SELL :
for(c=0; c<9; c++)
{
RefreshRates();

ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),6,Violet);
err=GetLastError();
if(err==0)
{
if(ticket>0)
{
cnt = cnt-1;
break;
}
}
else
{
if(err==0 || err==4 || err==136 || err==137 || err==138 ||
err==146) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
if(ticket>0)
{
cnt = cnt-1;
break;
}
}
}
}
break;

case OP_BUYLIMIT :
{bool Del = OrderDelete(OrderTicket());}
case OP_BUYSTOP :
{bool Del = OrderDelete(OrderTicket());}
case OP_SELLLIMIT:
{bool Del = OrderDelete(OrderTicket());}
case OP_SELLSTOP :
{bool Del = OrderDelete(OrderTicket());}
}
}
}
}
}

//-------------------------------------------------------------------+

// COUNTS THE TOTAL ORDERS DEPENDING ON TYPE


int subTotalTypeOrders(int Type)
{
int
cnt,
total = 0;

for(cnt=0; cnt<OrdersTotal(); cnt++)


{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
{
if(OrderType()==Type&&OrderSymbol()==Symbol()&&
OrderMagicNumber()==MagicNumber)
total++;
}
}
return(total);
}

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string error(int ai_0)
{
switch(ai_0)
{
case 0:
return ("No error returned.");
case 1:
return ("No error returned, but the result is unknown. ");
case 2:
return ("Common error. ");
case 3:
return ("Invalid trade parameters. ");
case 4:
return ("Trade server is busy. ");
case 5:
return ("Old version of the client terminal. ");
case 6:
return ("No connection with trade server. ");
case 7:
return ("Not enough rights. ");
case 8:
return ("Too frequent requests.");
case 9:
return ("Malfunctional trade operation. ");
case 64:
return ("Account disabled. ");
case 65:
return ("Invalid account. ");
case 128:
return ("Trade timeout. ");
case 129:
return ("Invalid price. ");
case 130:
return ("Invalid stops. ");
case 131:
return ("Invalid trade volume. ");
case 132:
return ("Market is closed. ");
case 133:
return ("Trade is disabled. ");
case 134:
return ("Not enough money. ");
case 135:
return ("Price changed. ");
case 136:
return ("Off quotes. ");
case 137:
return ("Broker is busy.");
case 138:
return ("Requote. ");
case 139:
return ("Order is locked. ");
case 140:
return ("Long positions only allowed. ");
case 141:
return ("Too many requests. ");
case 145:
return ("Modification denied because order too close to market. ");
case 146:
return ("Trade context is busy. ");
case 147:
return ("Expirations are denied by broker. ");
case 148:
return ("The amount of open and pending orders has reached the limit set
by the broker. ");
case 4000:
return ("No error. ");
case 4001:
return ("Wrong function pointer. ");
case 4002:
return ("Array index is out of range. ");
case 4003:
return ("No memory for function call stack. ");
case 4004:
return ("Recursive stack overflow. ");
case 4005:
return ("Not enough stack for parameter. ");
case 4006:
return ("No memory for parameter string. ");
case 4007:
return ("No memory for temp string. ");
case 4008:
return ("Not initialized string. ");
case 4009:
return ("Not initialized string in array. ");
case 4010:
return ("No memory for array string. ");
case 4011:
return ("Too long string. ");
case 4012:
return ("Remainder from zero divide. ");
case 4013:
return ("Zero divide. ");
case 4014:
return ("Unknown command. ");
case 4015:
return ("Wrong jump (never generated error). ");
case 4016:
return ("Not initialized array. ");
case 4017:
return ("DLL calls are not allowed. ");
case 4018:
return ("Cannot load library. ");
case 4019:
return ("Cannot call function. ");
case 4020:
return ("Expert function calls are not allowed. ");
case 4021:
return ("Not enough memory for temp string returned from function. ");
case 4022:
return ("System is busy (never generated error). ");
case 4050:
return ("Invalid function parameters count. ");
case 4051:
return ("Invalid function parameter value. ");
case 4052:
return ("String function internal error. ");
case 4053:
return ("Some array error. ");
case 4054:
return ("Incorrect series array using. ");
case 4055:
return ("Custom indicator error. ");
case 4056:
return ("Arrays are incompatible. ");
case 4057:
return ("Global variables processing error. ");
case 4058:
return ("Global variable not found. ");
case 4059:
return ("Function is not allowed in testing mode. ");
case 4060:
return ("Function is not confirmed. ");
case 4061:
return ("Send mail error. ");
case 4062:
return ("String parameter expected. ");
case 4063:
return ("Integer parameter expected. ");
case 4064:
return ("Double parameter expected. ");
case 4065:
return ("Array as parameter expected. ");
case 4066:
return ("Requested history data in updating state. ");
case 4067:
return ("Some error in trading function. ");
case 4099:
return ("End of file. ");
case 4100:
return ("Some file error.");
case 4101:
return ("Wrong file name. ");
case 4102:
return ("Too many opened files. ");
case 4103:
return ("Cannot open file. ");
case 4104:
return ("Incompatible access to a file. ");
case 4105:
return ("No order selected. ");
case 4106:
return ("Unknown symbol. ");
case 4107:
return ("Invalid price. ");
case 4108:
return ("Invalid ticket. ");
case 4109:
return ("Trade is not allowed. ");
case 4110:
return ("Longs are not allowed. ");
case 4111:
return ("Shorts are not allowed. ");
case 4200:
return ("Object exists already. ");
case 4201:
return ("Unknown object property. ");
case 4202:
return ("Object does not exist. ");
case 4203:
return ("Unknown object type. ");
case 4204:
return ("No object name. ");
case 4205:
return ("Object coordinates error. ");
case 4206:
return ("No specified subwindow. ");
case 4207:
return ("Some error in object function. ");
}
return ("Unknown error:" + IntegerToString(ai_0));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double GetPipValue()
{

if(_Digits >= 4)
{
double pipvalue = 0.0001;
return pipvalue;
}
else
{
double pipvalue = 0.01;
return pipvalue;
}
}

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double subLots()
{
double profit;
if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
if(OrderProfit()<0)
profit = NormalizeDouble(OrderLots()*MartingaleFactor,GetLotDecimal());
else
profit = Lots;
}
}
else
{
profit = Lots;
}
return(profit);
}

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double PrevProfit()
{
double Profit;
if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
Profit = OrderProfit();
}
}
return(Profit);
}

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int PrevType()
{
int type;
if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
if(OrderType()==OP_BUY)
type = 1; //buy
else
type = 2; //sell
}
}
return(type);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int GetLotDecimal()
{
int LotDecimal;
double marketInfoLot = MarketInfo(NULL,MODE_MINLOT);
if(marketInfoLot==.0001)
LotDecimal=4;
if(marketInfoLot==.001)
LotDecimal=3;
if(marketInfoLot==.01)
LotDecimal=2;
if(marketInfoLot==.1)
LotDecimal=1;
if(marketInfoLot==1)
LotDecimal=0;
return LotDecimal;
}

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CheckActiveHours()
{
bool OperationsAllowed = false;
int Offset;
if(TimeSettings==1) //AutoGMT
Offset = TimeHour(TimeGMT())-TimeHour(TimeCurrent());
if(TimeSettings==2) //ManualGMT
Offset = TimeHour(TimeGMT())-TimeHour(TimeCurrent())+ManualGMTAdjustment;
if(TimeSettings==3) //Brokers
Offset = 0;

if(DayOfWeek()==1)
{
if((StartTimeMon == StopTimeMon) && (Hour()+Offset == StartTimeMon))
OperationsAllowed = true;
if((StartTimeMon < StopTimeMon) && (Hour()+Offset >= StartTimeMon) && (Hour()
+Offset <= StopTimeMon))
OperationsAllowed = true;
if((StartTimeMon > StopTimeMon) && (((Hour()+Offset >= StopTimeMon) &&
(Hour()+Offset <= 23)) || ((Hour()+Offset <= StartTimeMon) && (Hour()+Offset >
0))))
OperationsAllowed = true;
}

if(DayOfWeek()==2)
{
if((StartTimeTue == StopTimeTue) && (Hour()+Offset == StartTimeTue))
OperationsAllowed = true;
if((StartTimeTue < StopTimeTue) && (Hour()+Offset >= StartTimeTue) && (Hour()
+Offset <= StopTimeTue))
OperationsAllowed = true;
if((StartTimeTue > StopTimeTue) && (((Hour()+Offset >= StopTimeTue) &&
(Hour()+Offset <= 23)) || ((Hour()+Offset <= StartTimeTue) && (Hour()+Offset >
0))))
OperationsAllowed = true;
}

if(DayOfWeek()==3)
{
if((StartTimeWed == StopTimeWed) && (Hour()+Offset == StartTimeWed))
OperationsAllowed = true;
if((StartTimeWed < StopTimeWed) && (Hour()+Offset >= StartTimeWed) && (Hour()
+Offset <= StopTimeWed))
OperationsAllowed = true;
if((StartTimeWed > StopTimeWed) && (((Hour()+Offset >= StopTimeWed) &&
(Hour()+Offset <= 23)) || ((Hour()+Offset <= StartTimeWed) && (Hour()+Offset >
0))))
OperationsAllowed = true;
}

if(DayOfWeek()==4)
{
if((StartTimeThu == StopTimeThu) && (Hour()+Offset == StartTimeThu))
OperationsAllowed = true;
if((StartTimeThu < StopTimeThu) && (Hour()+Offset >= StartTimeThu) && (Hour()
+Offset <= StopTimeThu))
OperationsAllowed = true;
if((StartTimeThu > StopTimeThu) && (((Hour()+Offset >= StopTimeThu) &&
(Hour()+Offset <= 23)) || ((Hour()+Offset <= StartTimeThu) && (Hour()+Offset >
0))))
OperationsAllowed = true;
}

if(DayOfWeek()==5)
{
if((StartTimeFri == StopTimeFri) && (Hour()+Offset == StartTimeFri))
OperationsAllowed = true;
if((StartTimeFri < StopTimeFri) && (Hour()+Offset >= StartTimeFri) && (Hour()
+Offset <= StopTimeFri))
OperationsAllowed = true;
if((StartTimeFri > StopTimeFri) && (((Hour()+Offset >= StopTimeFri) &&
(Hour()+Offset <= 23)) || ((Hour()+Offset <= StartTimeFri) && (Hour()+Offset >
0))))
OperationsAllowed = true;
}

if(DayOfWeek()==6)
{
if((StartTimeSat == StopTimeSat) && (Hour()+Offset == StartTimeSat))
OperationsAllowed = true;
if((StartTimeSat < StopTimeSat) && (Hour()+Offset >= StartTimeSat) && (Hour()
+Offset <= StopTimeSat))
OperationsAllowed = true;
if((StartTimeSat > StopTimeSat) && (((Hour()+Offset >= StopTimeSat) &&
(Hour()+Offset <= 23)) || ((Hour()+Offset <= StartTimeSat) && (Hour()+Offset >
0))))
OperationsAllowed = true;
}

if(DayOfWeek()==0)
{
if((StartTimeSun == StopTimeSun) && (Hour()+Offset == StartTimeSun))
OperationsAllowed = true;
if((StartTimeSun < StopTimeSun) && (Hour()+Offset >= StartTimeSun) && (Hour()
+Offset <= StopTimeSun))
OperationsAllowed = true;
if((StartTimeSun > StopTimeSun) && (((Hour()+Offset >= StopTimeSun) &&
(Hour()+Offset <= 23)) || ((Hour()+Offset <= StartTimeSun) && (Hour()+Offset >
0))))
OperationsAllowed = true;
}

return OperationsAllowed;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CheckActiveMinutes()
{
bool OperationsAllowed = false;
int Offset;
if(TimeSettings==1) //AutoGMT
Offset = TimeHour(TimeGMT())-TimeHour(TimeCurrent());
if(TimeSettings==2) //ManualGMT
Offset = TimeHour(TimeGMT())-TimeHour(TimeCurrent())+ManualGMTAdjustment;
if(TimeSettings==3) //Brokers
Offset = 0;

if(Monday && DayOfWeek()==1)


{
if((Hour()+Offset == StartTimeMon) || (Hour()+Offset == StopTimeMon))
{
if(Hour()+Offset == StartTimeMon && Hour()+Offset < StopTimeMon)
{
if(Minute() >= StartMinMon)
OperationsAllowed = true;
}
if(Hour()+Offset == StopTimeMon && Hour()+Offset > StartTimeMon)
{
if(Minute() < StopMinMon)
OperationsAllowed = true;
}
}
if((Hour()+Offset == StartTimeMon) && (Hour()+Offset == StopTimeMon))
{
if((Minute() >= StartMinMon) && (Minute() < StopMinMon))
OperationsAllowed = true;
}
if((Hour()+Offset > StartTimeMon) && (Hour()+Offset < StopTimeMon))
{
OperationsAllowed = true;
}
}

if(Tuesday && DayOfWeek()==2)


{
if((Hour()+Offset == StartTimeTue) || (Hour()+Offset == StopTimeTue))
{
if(Hour()+Offset == StartTimeTue && Hour()+Offset < StopTimeTue)
{
if(Minute() >= StartMinTue)
OperationsAllowed = true;
}
if(Hour()+Offset == StopTimeTue && Hour()+Offset > StartTimeTue)
{
if(Minute() < StopMinTue)
OperationsAllowed = true;
}
}
if((Hour()+Offset == StartTimeTue) && (Hour()+Offset == StopTimeTue))
{
if((Minute() >= StartMinTue) && (Minute() < StopMinTue))
OperationsAllowed = true;
}
if((Hour()+Offset > StartTimeTue) && (Hour()+Offset < StopTimeTue))
{
OperationsAllowed = true;
}
}

if(Wednesday && DayOfWeek()==3)


{
if((Hour()+Offset == StartTimeWed) || (Hour()+Offset == StopTimeWed))
{
if(Hour()+Offset == StartTimeWed && Hour()+Offset < StopTimeWed)
{
if(Minute() >= StartMinWed)
OperationsAllowed = true;
}
if(Hour()+Offset == StopTimeWed && Hour()+Offset > StartTimeWed)
{
if(Minute() < StopMinWed)
OperationsAllowed = true;
}
}
if((Hour()+Offset == StartTimeWed) && (Hour()+Offset == StopTimeWed))
{
if((Minute() >= StartMinWed) && (Minute() < StopMinWed))
OperationsAllowed = true;
}
if((Hour()+Offset > StartTimeWed) && (Hour()+Offset < StopTimeWed))
{
OperationsAllowed = true;
}
}

if(Thursday && DayOfWeek()==4)


{
if((Hour()+Offset == StartTimeThu) || (Hour()+Offset == StopTimeThu))
{
if(Hour()+Offset == StartTimeThu && Hour()+Offset < StopTimeThu)
{
if(Minute() >= StartMinThu)
OperationsAllowed = true;
}
if(Hour()+Offset == StopTimeThu && Hour()+Offset > StartTimeThu)
{
if(Minute() < StopMinThu)
OperationsAllowed = true;
}
}
if((Hour()+Offset == StartTimeThu) && (Hour()+Offset == StopTimeThu))
{
if((Minute() >= StartMinThu) && (Minute() < StopMinThu))
OperationsAllowed = true;
}
if((Hour()+Offset > StartTimeThu) && (Hour()+Offset < StopTimeThu))
{
OperationsAllowed = true;
}
}

if(Friday && DayOfWeek()==5)


{
if((Hour()+Offset == StartTimeFri) || (Hour()+Offset == StopTimeFri))
{
if(Hour()+Offset == StartTimeFri && Hour()+Offset < StopTimeFri)
{
if(Minute() >= StartMinFri)
OperationsAllowed = true;
}
if(Hour()+Offset == StopTimeFri && Hour()+Offset > StartTimeFri)
{
if(Minute() < StopMinFri)
OperationsAllowed = true;
}
}
if((Hour()+Offset == StartTimeFri) && (Hour()+Offset == StopTimeFri))
{
if((Minute() >= StartMinFri) && (Minute() < StopMinFri))
OperationsAllowed = true;
}
if((Hour()+Offset > StartTimeFri) && (Hour()+Offset < StopTimeFri))
{
OperationsAllowed = true;
}
}

if(Saturday && DayOfWeek()==6)


{
if((Hour()+Offset == StartTimeSat) || (Hour()+Offset == StopTimeSat))
{
if(Hour()+Offset == StartTimeSat && Hour()+Offset < StopTimeSat)
{
if(Minute() >= StartMinSat)
OperationsAllowed = true;
}
if(Hour()+Offset == StopTimeSat && Hour()+Offset > StartTimeSat)
{
if(Minute() < StopMinSat)
OperationsAllowed = true;
}
}
if((Hour()+Offset == StartTimeSat) && (Hour()+Offset == StopTimeSat))
{
if((Minute() >= StartMinSat) && (Minute() < StopMinSat))
OperationsAllowed = true;
}
if((Hour()+Offset > StartTimeSat) && (Hour()+Offset < StopTimeSat))
{
OperationsAllowed = true;
}
}

if(Sunday && DayOfWeek()==7)


{
if((Hour()+Offset == StartTimeSun) || (Hour()+Offset == StopTimeSun))
{
if(Hour()+Offset == StartTimeSun && Hour()+Offset < StopTimeSun)
{
if(Minute() >= StartMinSun)
OperationsAllowed = true;
}
if(Hour()+Offset == StopTimeSun && Hour()+Offset > StartTimeSun)
{
if(Minute() < StopMinSun)
OperationsAllowed = true;
}
}
if((Hour()+Offset == StartTimeSun) && (Hour()+Offset == StopTimeSun))
{
if((Minute() >= StartMinSun) && (Minute() < StopMinSun))
OperationsAllowed = true;
}
if((Hour()+Offset > StartTimeSun) && (Hour()+Offset < StopTimeSun))
{
OperationsAllowed = true;
}
}

return OperationsAllowed;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool DaytoTrade()
{
bool daytotrade = false;

if(DayOfWeek() == 0 && Sunday)


daytotrade = true;
if(DayOfWeek() == 1 && Monday)
daytotrade = true;
if(DayOfWeek() == 2 && Tuesday)
daytotrade = true;
if(DayOfWeek() == 3 && Wednesday)
daytotrade = true;
if(DayOfWeek() == 4 && Thursday)
daytotrade = true;
if(DayOfWeek() == 5 && Friday)
daytotrade = true;
if(DayOfWeek() == 6 && Saturday)
daytotrade = true;
return(daytotrade);
}
//+------------------------------------------------------------------+

You might also like