#property copyright "Copyright ? 2015 Shafina"
#import "stdlib.ex4"
string ErrorDescription(int a0);
#import
extern int Filter = 140;
extern int Filter_EU = 130; //org 140
extern int Filter_GU = 170;
extern int Filter_UJ = 190;
extern int MagicNumber = 150115;
extern string TradeComment = "Shafina";
extern int MaxTrades = 10;
extern double StopLoss = 5; //this is for our BUY/SELL STOP SL 2.3 pips modify, can not put 200 pips, will lose !!!
extern double NoTrade_Profit_Yet_StopLoss = 30; //30 pips
extern double InTrade_Profit_StopLoss = 60; //2.3 pips
extern double Distance = 30; // not used !!!
extern double NoTrade_Profit_Yet_Distance = 20;
extern double InTrade_Profit_Distance = 30;
extern double Limit = 23.0; // 20 will cause order130 error
extern double NoTrade_Profit_Yet_Limit = 22; // can not play with this , 20 should be ok!!!
extern double InTrade_Profit_Limit = 25;
extern bool Use_TrailingStep = TRUE;
extern double Start_Trailing_At = 2.9; // org 2.9 meaning keep bid/ask distance by x PIPS
extern double TrailingStep = 0.3; // org 0.3
extern bool Use_Set_BreakEven = TRUE;
extern double LockPips = 0.3; // org 0.3
extern double Set_BreakEvenAt = 2.3; // org 2.3
extern double MinLots = 0.01;
extern double MaxLots = 100000000.0;//100000.0;
extern double Risk = 10; // org 60
extern double FixedLots = 0.01;//0.1;
extern bool UseMM = TRUE;
extern double MaxSpreadPlusCommission = 6.3 ;//6.3 ok !
extern double MaxSpreadPlusCommission_EU = 10.3;//0.6 ok !
extern double MaxSpreadPlusCommission_GU = 10.0;//1.0 ok !
extern double MaxSpreadPlusCommission_UJ = 10.0;//0.6 ok !
extern double MaxSpread = 6.3 ;//6.3 ok !
extern double MaxSpread_EU = 0.6;//0.6 ok !
extern double MaxSpread_GU = 1.0;//1.0 ok !
extern double MaxSpread_UJ = 0.6;//0.6 ok !
extern int MAPeriod = 15;
extern int MAMethod = 1;
extern string TimeFilter = "---------- Time Filter ----------";
extern int StartHour = 0;
extern int StartMinute = 0;
extern int EndHour = 23;
extern int EndMinute = 59;
extern string IndicatorSetting = "=== Indicator Settings ===";
extern int IndicatorToUse = 1; // 0 = Moving Average ( iMA ), 1 = Envelope (iEnvelope)
extern int Env_period = 10;
extern double Env_deviation = 0.07; // 0.05
extern int Env_shift = 0;
int Env_low_band_price = PRICE_HIGH;
int Env_high_band_price = PRICE_LOW;
bool Timed_Closing = TRUE;
int Minutes_Buy = 40; //Minute
int Minutes_Sell = 40;
int Timed_Buy_TakeProfit = -5;
int Timed_Sell_TakeProfit = -5;
int Gi_180 = 0;
double G_pips_184 = 0.0;
int G_digits_192 = 0;
double G_point_196 = 0.0;
int Gi_204;
double Gd_208;
double Gd_216;
double Gd_224;
double Gd_232;
double Gd_240;
double Gd_248;
double Gd_256;
int G_slippage_264 = 3;
bool Gi_268;
double Gd_272;
double Gda_280[30];
int Gi_284 = 0;
string Gs_dummy_288;
string Gs_unused_316 = "";
string Gs_unused_324 = "";
double Gd_336;
double Gd_344;
int G_time_352;
int Gi_356;
int G_datetime_360;
int Gi_380;
int Gi_384;
int Gi_388;
int Gi_392 = 40;
double G_timeframe_396 = 240.0;
bool Gi_404 = TRUE;
color G_color_408 = DimGray;
double Gd_420;
color G_color_428 = Red;
color G_color_432 = DarkGray;
color G_color_436 = SpringGreen;
bool Gi_440 = TRUE;
double G_ihigh_444;
double G_ilow_452;
double Gd_460;
int G_datetime_468;
int D_Factor;
string EASymbol;
// E37F0136AA3FFAF149B351F6A4C948E9
int init() {
//int timeframe_8;
ArrayInitialize(Gda_280, 0);
G_digits_192 = Digits;
G_point_196 = Point;
Print("Digits: " + G_digits_192 + " Point: " + DoubleToStr(G_point_196, G_digits_192));
double lotstep_0 = MarketInfo(Symbol(), MODE_LOTSTEP);
Gi_204 = MathLog(lotstep_0) / MathLog(0.1);
Gd_208 = MathMax(MinLots, MarketInfo(Symbol(), MODE_MINLOT));
Gd_216 = MathMin(MaxLots, MarketInfo(Symbol(), MODE_MAXLOT));
EASymbol = StringSubstr (Symbol(),0,6);
if (EASymbol == "EURUSD") {MaxSpread = MaxSpread_EU; Filter = Filter_EU;}
if (EASymbol == "GBPUSD") {MaxSpread = MaxSpread_GU; Filter = Filter_GU;}
if (EASymbol == "USDJPY") {MaxSpread = MaxSpread_UJ; Filter = Filter_UJ;}
Gd_224 = Risk / 100.0;
Gd_232 = NormalizeDouble(MaxSpreadPlusCommission * G_point_196, G_digits_192 + 1);
Gd_240 = NormalizeDouble(Limit * G_point_196, G_digits_192);
Gd_248 = NormalizeDouble(Distance * G_point_196, G_digits_192);
Gd_256 = NormalizeDouble(Filter * G_point_196 , G_digits_192);
Gi_268 = FALSE;
Gd_272 = NormalizeDouble(G_pips_184 * G_point_196, G_digits_192 + 1);
Gd_420 = 0.0001;
D_Factor = 1;
if(Digits == 3|| Digits == 5) D_Factor = 10;
return (0);
}
// 52D46093050F38C27267BCE42543EF60
int deinit() {
return (0);
}
// EA2B2676C28C0DB26D39331A336C6B92
int start() {
int error_8;
string Ls_12;
int ticket_20;
double price_24;
bool bool_32;
double Ld_36;
double Ld_44;
double price_60;
double Ld_112;
int Li_180;
int cmd_188;
double Ld_196;
double Ld_204;
double ihigh_68 = iHigh(NULL, 0, 0);
double ilow_76 = iLow(NULL, 0, 0);
double indicator_low;
double indicator_high;
double indicator_highlow_diff;
// If indicator setting is 1 then we use Envelope
if ( IndicatorToUse == 1 )
{
indicator_low = iEnvelopes ( NULL, 0, Env_period, MODE_SMA, Env_shift, Env_low_band_price, Env_deviation, MODE_LOWER, 0 );
indicator_high = iEnvelopes ( NULL,0, Env_period, MODE_SMA, Env_shift, Env_high_band_price, Env_deviation, MODE_UPPER, 0 );
}
// If indicator setting is 0 or any other number then use the default mMoving Average
else
{
indicator_low = iMA ( NULL, 0, MAPeriod, Gi_180, MAMethod, PRICE_LOW, 0 );
indicator_high = iMA ( NULL, 0, MAPeriod, Gi_180, MAMethod, PRICE_HIGH, 0 );
}
indicator_highlow_diff = indicator_low - indicator_high;
if (!Gi_268) {
for (int pos_108 = OrdersHistoryTotal() - 1; pos_108 >= 0; pos_108--) {
if (OrderSelect(pos_108, SELECT_BY_POS, MODE_HISTORY)) {
if (OrderProfit() != 0.0) {
if (OrderClosePrice() != OrderOpenPrice()) {
if (OrderSymbol() == Symbol()) {
Gi_268 = TRUE;
Ld_112 = MathAbs(OrderProfit() / (OrderClosePrice() - OrderOpenPrice()));
Gd_272 = (-OrderCommission()) / Ld_112;
break;
}
}
}
}
}
}
double Ld_120 = Ask - Bid;
ArrayCopy(Gda_280, Gda_280, 0, 1, 29);
Gda_280[29] = Ld_120;
if (Gi_284 < 30) Gi_284++;
double Ld_128 = 0;
pos_108 = 29;
for (int count_136 = 0; count_136 < Gi_284; count_136++) {
Ld_128 += Gda_280[pos_108];
pos_108--;
}
double Ld_140 = Ld_128 / Gi_284;
double Ld_148 = NormalizeDouble(Ask + Gd_272, G_digits_192); //Ask + comission
double Ld_156 = NormalizeDouble(Bid - Gd_272, G_digits_192); //Bid - comission
double Ld_164 = NormalizeDouble(Ld_140 + Gd_272, G_digits_192 + 1);
double Ld_172 = ihigh_68 - ilow_76;
if (Ld_172 > Gd_256) {
if (Ask < indicator_low/*ima_84*/) Li_180 = 1; // for BUY
else
if (Bid > indicator_high/*ima_92*/) Li_180 = -1; // for SELL
}
int count_184 = 0;
for (pos_108 = 0; pos_108 < OrdersTotal(); pos_108++) {
if (OrderSelect(pos_108, SELECT_BY_POS, MODE_TRADES)) {
if (OrderMagicNumber() == MagicNumber) {
cmd_188 = OrderType();
if (cmd_188 == OP_BUYLIMIT || cmd_188 == OP_SELLLIMIT) continue; //skip this !
double TotalProfit = 0.0;
if (OrderSymbol() == Symbol()) {
count_184++;
switch (cmd_188) {
case OP_BUY:
if (Distance < 0) break;
TotalProfit = (Bid - OrderOpenPrice())/Point/D_Factor;
if (TotalProfit <=1) {
Distance = NoTrade_Profit_Yet_Distance;
Limit = NoTrade_Profit_Yet_Limit;
StopLoss= NoTrade_Profit_Yet_StopLoss; }
Gd_248 = NormalizeDouble(Distance * G_point_196, G_digits_192);
Gd_240 = NormalizeDouble(Limit * G_point_196, G_digits_192);
Ld_44 = NormalizeDouble(OrderStopLoss(), G_digits_192);
price_60 = NormalizeDouble(Bid - Gd_248, G_digits_192); // Bid - Distance
Print("*****TotalProfit=",TotalProfit, ", Distance=", Distance, ", Bid - Distance", price_60, ", Stoploss=",Ld_44 );
if (!((Ld_44 == 0.0 || price_60 > Ld_44))) break; // Bid - Distance > Stoploss
bool_32 = OrderModify(OrderTicket(), OrderOpenPrice(), price_60, OrderTakeProfit(), 0, Blue);
if (!(!bool_32)) break;
error_8 = GetLastError();
Ls_12 = ErrorDescription(error_8);
Print("BUY Modify Error Code: " + error_8 + " Message: " + Ls_12 + " OP: " + DoubleToStr(price_24, G_digits_192) + " SL: " + DoubleToStr(price_60, G_digits_192) + " Bid: " + DoubleToStr(Bid, G_digits_192) + " Ask: " + DoubleToStr(Ask, G_digits_192));
break;
case OP_SELL:
if (Distance < 0) break;
TotalProfit = (OrderOpenPrice()- Ask)/Point/D_Factor;
if (TotalProfit <= 1) {
Distance = NoTrade_Profit_Yet_Distance;
Limit = NoTrade_Profit_Yet_Limit;
StopLoss= NoTrade_Profit_Yet_StopLoss; }
Gd_248 = NormalizeDouble(Distance * G_point_196, G_digits_192);
Gd_240 = NormalizeDouble(Limit * G_point_196, G_digits_192);
Ld_44 = NormalizeDouble(OrderStopLoss(), G_digits_192);
price_60 = NormalizeDouble(Ask + Gd_248, G_digits_192);
if (!((Ld_44 == 0.0 || price_60 < Ld_44))) break;
bool_32 = OrderModify(OrderTicket(), OrderOpenPrice(), price_60, OrderTakeProfit(), 0, Red);
if (!(!bool_32)) break;
error_8 = GetLastError();
Ls_12 = ErrorDescription(error_8);
Print("SELL Modify Error Code: " + error_8 + " Message: " + Ls_12 + " OP: " + DoubleToStr(price_24, G_digits_192) + " SL: " + DoubleToStr(price_60, G_digits_192) + " Bid: " + DoubleToStr(Bid, G_digits_192) + " Ask: " + DoubleToStr(Ask, G_digits_192));
break;
case OP_BUYSTOP:
price_24 = NormalizeDouble(Ask + Gd_240, G_digits_192); // Ask + Limit
Ld_36 = NormalizeDouble(OrderOpenPrice(), G_digits_192);
if (!((price_24 < Ld_36))) break; // If Ask + Limit > Buystop OpenPrice , Go to modify...
price_60 = NormalizeDouble(price_24 - StopLoss * Point, G_digits_192);
bool_32 = OrderModify(OrderTicket(), price_24, price_60, OrderTakeProfit(), 0, Lime);
if (!(!bool_32)) break;
error_8 = GetLastError();
Ls_12 = ErrorDescription(error_8);
Print("BUYSTOP Modify Error Code: " + error_8 + " Message: " + Ls_12 + " OP: " + DoubleToStr(price_24, G_digits_192) + " SL: " + DoubleToStr(price_60, G_digits_192) + " Bid: " + DoubleToStr(Bid, G_digits_192) + " Ask: " + DoubleToStr(Ask, G_digits_192));
break;
case OP_SELLSTOP:
Ld_36 = NormalizeDouble(OrderOpenPrice(), G_digits_192);
price_24 = NormalizeDouble(Bid - Gd_240, G_digits_192);
if (!((price_24 > Ld_36))) break;
price_60 = NormalizeDouble(price_24 + StopLoss * Point, G_digits_192);
bool_32 = OrderModify(OrderTicket(), price_24, price_60, OrderTakeProfit(), 0, Orange);
if (!(!bool_32)) break;
error_8 = GetLastError();
Ls_12 = ErrorDescription(error_8);
Print("SELLSTOP Modify Error Code: " + error_8 + " Message: " + Ls_12 + " OP: " + DoubleToStr(price_24, G_digits_192) + " SL: " + DoubleToStr(price_60, G_digits_192) + " Bid: " + DoubleToStr(Bid, G_digits_192) + " Ask: " + DoubleToStr(Ask, G_digits_192));
break;
}
}
}
}
}
double pp, pd;
if (Digits < 4) {
pp = 0.01;
pd = 2;
} else {
pp = 0.0001;
pd = 4;
}
Ld_196 = AccountBalance() * AccountLeverage() * Gd_224;
if (!UseMM) Ld_196 = FixedLots;
Ld_204 = NormalizeDouble(Ld_196 / MarketInfo(Symbol(), MODE_LOTSIZE), Gi_204);
Ld_204 = MathMax(Gd_208, Ld_204);
Ld_204 = MathMin(Gd_216, Ld_204);
if (count_184 /*== 0*/ <= MaxTrades -1 && Li_180 != 0 /*&& Ld_164 <= Gd_232*/ && NormalizeDouble(Ask - Bid, Digits) < NormalizeDouble(MaxSpread * pp, pd + 1) && f0_4()) { // New BUY STOP & SELL STOP Orders placed here..
if (Li_180 == -1) {
price_24 = NormalizeDouble(Ask + Gd_240, G_digits_192);
price_60 = NormalizeDouble(price_24 - StopLoss * Point, G_digits_192);
ticket_20 = OrderSend(Symbol(), OP_BUYSTOP, Ld_204, price_24, G_slippage_264, price_60, 0, TradeComment, MagicNumber, 0, Lime);
if (ticket_20 <= 0) {
error_8 = GetLastError();
Ls_12 = ErrorDescription(error_8);
Print("BUYSTOP Send Error Code: " + error_8 + " Message: " + Ls_12 + " LT: " + DoubleToStr(Ld_204, Gi_204) + " OP: " + DoubleToStr(price_24, G_digits_192) + " SL: " + DoubleToStr(price_60, G_digits_192) + " Bid: " + DoubleToStr(Bid, G_digits_192) + " Ask: " + DoubleToStr(Ask, G_digits_192));
}
}
if (Li_180 == 1) {
price_24 = NormalizeDouble(Bid - Gd_240, G_digits_192);
price_60 = NormalizeDouble(price_24 + 200 *StopLoss * Point, G_digits_192);
ticket_20 = OrderSend(Symbol(), OP_SELLSTOP, Ld_204, price_24, G_slippage_264, price_60, 0, TradeComment, MagicNumber, 0, Orange);
if (ticket_20 <= 0) {
error_8 = GetLastError();
Ls_12 = ErrorDescription(error_8);
Print("SELLSTOP Send Error Code: " + error_8 + " Message: " + Ls_12 + " LT: " + DoubleToStr(Ld_204, Gi_204) + " OP: " + DoubleToStr(price_24, G_digits_192) + " SL: " + DoubleToStr(price_60, G_digits_192) + " Bid: " + DoubleToStr(Bid, G_digits_192) + " Ask: " + DoubleToStr(Ask, G_digits_192));
}
}
}
string Ls_212
= " \n\n Next Lot: " + DoubleToStr(Ld_204, G_digits_192)
+ " \n\n Current Spread: " + DoubleToStr(MarketInfo(Symbol(), MODE_SPREAD) / MathPow(10, Digits - pd), Digits - pd)
+ IIFs((MarketInfo(Symbol(), MODE_SPREAD) / MathPow(10, Digits - pd))> MaxSpread, " ***Exceeding MaxSpread at: " + DoubleToStr(MaxSpread, Digits -pd )," , MaxSpread: "+DoubleToStr(MaxSpread, Digits - pd))
+ " \n AvgSpread: " + DoubleToStr(Ld_140, G_digits_192)
+ " \n Commission rate:" + DoubleToStr(Gd_272, G_digits_192 + 1)
+ " \n Real avg.spread:" + DoubleToStr(Ld_164, G_digits_192 + 1);
if (Ld_164 > Gd_232) {
Ls_212 = Ls_212
+ "\n\n"
+ " **The EA can not run with this spread ( " + DoubleToStr(Ld_164, G_digits_192 + 1) + " > " + DoubleToStr(Gd_232, G_digits_192 + 1) + " )";
}
Comment(Ls_212);
TimedClosing();
RapidTrailingStop();
return (0);
}
void TimedClosing() {
if (!Timed_Closing) return;
int DC_Digits;
bool TicketClose;
int ExpirationBuyTime,ExpirationSellTime,MinBuyProfit,MinSellProfit;
if (Digits == 4 || Digits == 2) DC_Digits = 1;
else
if (Digits == 5 || Digits == 3) DC_Digits = 10;
ExpirationBuyTime=Minutes_Buy;
ExpirationSellTime=Minutes_Sell;
MinBuyProfit=Timed_Buy_TakeProfit*DC_Digits;
MinSellProfit=Timed_Sell_TakeProfit*DC_Digits;
int total = OrdersTotal();
for (int i = total - 1; i >= 0; i--) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == TRUE) {
if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber ) {
if (TimeCurrent() - OrderOpenTime() >= 60 * ExpirationBuyTime && Bid >= OrderOpenPrice() + MinBuyProfit * Point) {
RefreshRates();
TicketClose = OrderClose(OrderTicket(), OrderLots(), Bid, G_slippage_264 , Blue);
if (!TicketClose) {
Print("There was an error closing the trade. Error is:", GetLastError());
return;
}
}
}
}
if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
if (TimeCurrent() - OrderOpenTime() >= 60 * ExpirationSellTime && Ask <= OrderOpenPrice() - MinSellProfit * Point) {
RefreshRates();
TicketClose = OrderClose(OrderTicket(), OrderLots(), Ask, G_slippage_264, Red);
//---
if (!TicketClose) {
Print("There was an error closing the trade. Error is:", GetLastError());
return;
}
}
}
}
}
// 3B8B9927CE5F3E077818404E64D1C252
int f0_4() {
if ((Hour() > StartHour && Hour() < EndHour) || (Hour() == StartHour && Minute() >= StartMinute) || (Hour() == EndHour && Minute() < EndMinute)) return (1);
return (0);
}
string IIFs(bool condition, string ifTrue, string ifFalse) {
if (condition) return(ifTrue); else return(ifFalse);
}
void RapidTrailingStop() {
double l_point_36;
double ld_44;
double ld_52;
double ld_60;
double ld_68;
bool result;
for (int l_pos_100 = 0; l_pos_100 < OrdersTotal(); l_pos_100++) {
if(OrderSelect(l_pos_100, SELECT_BY_POS, MODE_TRADES)== false) break;
if (OrderSymbol() == Symbol() ) {
if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.00001) l_point_36 = 0.0001;
else {
if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.001) l_point_36 = 0.01;
else l_point_36 = MarketInfo(OrderSymbol(), MODE_POINT);
}
ld_52 = Start_Trailing_At * l_point_36; /// 20 PIPS
ld_44 = TrailingStep * l_point_36; /// 5 PIPS
ld_68 = Set_BreakEvenAt * l_point_36;
ld_60 = LockPips * l_point_36;
if (OrderType() == OP_BUY && (OrderMagicNumber() == MagicNumber)) {
if (Use_TrailingStep && Bid - OrderOpenPrice() >= ld_52) {
if (Bid - OrderStopLoss() >= ld_52 + ld_44) {
result = OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss() + ld_44, OrderTakeProfit(), 0, DodgerBlue);
if ( GetLastError()!=0) Print(Symbol()+ ": Trail BUY OrderModify PROBLEM ! " );
}
}
if (Use_Set_BreakEven && Bid - OrderOpenPrice() >= ld_68) {
if (OrderStopLoss() < OrderOpenPrice()) {
result= OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + ld_60, OrderTakeProfit(),0, DodgerBlue);
if ( GetLastError()!=0) Print(Symbol()+ ": BreakEven BUY OrderModify PROBLEM ! " );
}
}
} else {
if (OrderType() == OP_SELL && (OrderMagicNumber() == MagicNumber)) {
if (Use_TrailingStep && OrderOpenPrice() - Ask >= ld_52) {
if (OrderStopLoss() - Ask >= ld_52 + ld_44) {
result= OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss() - ld_44, OrderTakeProfit(),0, Yellow);
if (GetLastError()!=0) Print(Symbol()+ ": Trail SELL OrderModify PROBLEM ! " );
}
}
if (Use_Set_BreakEven && OrderOpenPrice() - Ask >= ld_68) {
if (OrderStopLoss() > OrderOpenPrice()) {
result= OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() - ld_60, OrderTakeProfit(),0, Yellow);
if ( GetLastError()!=0) Print(Symbol()+ ": BreakEven SELL OrderModify PROBLEM ! " );
}
}
}
}
}
}
}