//+------------------------------------------------------------------+ //| MACD Sample.mq4 | //| Copyright 2005-2014, MetaQuotes Software Corp. | //| http://www.mql4.com | //+------------------------------------------------------------------+ #property copyright "2005-2014, MetaQuotes Software Corp." #property link "http://www.mql4.com" input double TakeProfit =50000; input double Lots = 0.3; input double TrailingStop =30000; input double MACDOpenLevel =3; input double MACDCloseLevel=2; input int MATrendPeriod =26; extern int MagicNumber ; datetime wBar=0; double var_compteur_GG ; double var_print_error ; double var_print_not_enough_money ; double var_bars_less_than_100; double var_TakeProfit_less_than_1; double var_OrderClose_error; double var_OrderModify_error; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double Highest_EQUITY = 0; double Lowest_EQUITY = 0; datetime GetNextSundayTime() { datetime timeNow = TimeCurrent(); MqlDateTime mqlDateTimeObj; TimeToStruct(timeNow,mqlDateTimeObj); int secondsTillSunday = (7 - mqlDateTimeObj.day_of_week) * 86400; mqlDateTimeObj.hour = 0; mqlDateTimeObj.min = 0; mqlDateTimeObj.sec = 0; timeNow = StructToTime(mqlDateTimeObj); return timeNow + secondsTillSunday; } void HandleHighestLowestEquityDisplay() { static datetime timeToReset = 0; if(!timeToReset) { timeToReset = GetNextSundayTime(); return; } if(TimeCurrent() >= timeToReset) { Highest_EQUITY = 0; Lowest_EQUITY = 0; timeToReset = GetNextSundayTime(); } double equity = AccountInfoDouble(ACCOUNT_EQUITY); if(!Highest_EQUITY) Highest_EQUITY = equity; else if(equity > Highest_EQUITY) Highest_EQUITY = AccountInfoDouble(ACCOUNT_EQUITY); if(!Lowest_EQUITY) Lowest_EQUITY = equity; else if(equity < Lowest_EQUITY) Lowest_EQUITY = AccountInfoDouble(ACCOUNT_EQUITY); } void OnTick(void) { HandleHighestLowestEquityDisplay(); if (var_compteur_GG != 1) { GlobalVariableSet(Symbol()+ "just_1_trade_buy",0); GlobalVariableSet(Symbol() + "just_1_trade_sell",0); var_compteur_GG = 1; } int spread=MarketInfo(Symbol(),MODE_SPREAD); double Pipsvalue = MarketInfo(Symbol(),MODE_TICKVALUE); string identity = WindowExpertName(); // nom de l'EA !! int extract_number = StrToInteger(StringSubstr(identity,3,2)); int ramdom_number = 16384 * extract_number ; double Lots_bis = 0.1* extract_number; double realvalue = spread * Pipsvalue ; string extract_sens = StringSubstr(identity,10,1); string extract_name_of_lot = StringSubstr(identity,0,6); int extract_lot = StrToInteger(StringSubstr(identity,12,2)); int zebalance = AccountBalance(); double diviseur = 10 ; double num_lot_div = extract_lot /diviseur ; string sens_trade; double sens_trade_number =0; if(extract_sens=="u" || extract_sens=="U") { int sens_up=1; int sens_down=0; MagicNumber = 99 + extract_lot; string num_magic = StringConcatenate(9000, extract_lot); } if(extract_sens=="d" || extract_sens=="D") { sens_up=0; sens_down=1; MagicNumber = 66 + extract_lot; num_magic = StringConcatenate(6000, extract_lot); } string num_magic_buy = StringConcatenate(9000, extract_lot); string num_magic_sell = StringConcatenate(6000, extract_lot) ; //************************* //************************* if(Bars<100) { if (var_bars_less_than_100 == 0 || var_bars_less_than_100 =="" ) { Print("bars less than 100"); var_bars_less_than_100 = 1 ; } return; } //********TEST AJOUT MARCH25 if(Bars>=100) { var_bars_less_than_100 = 0 ; } ///****** FIN TEST AJOUT MARCH25 if(TakeProfit<10) { if ( var_TakeProfit_less_than_1 ==0 || var_TakeProfit_less_than_1 == "") { Print("TakeProfit less than 10"); var_TakeProfit_less_than_1 =1; } return; } //******* AJOUT MARCH25 if(TakeProfit>=10) { var_TakeProfit_less_than_1 =0; } //******** FIN AJOUT MARCH25 int ytime= Year() ; int monthtime= Month(); int dtime= Day(); int htime= Hour(); int mtime= Minute(); int stime= Seconds(); int weekday= DayOfWeek(); int zeweekday= DayOfWeek(); string day_for_trade ="yes" ; string stop_for_holiday = "" ; string time_zone_trade ; string nobuy =""; string nosell = ""; //******************* string current_symbol = Symbol(); double PipValue = MarketInfo(Symbol(),MODE_TICKVALUE); string var_alert = 0 ; string var_condition_1 ; double ggbuy = ( GlobalVariableGet(Symbol() + "just_1_trade_buy")); double ggsell = ( GlobalVariableGet(Symbol() + "just_1_trade_sell")); //***************INCLUDE*******INCLUDE******************** // #include //int cnt,ticket,total; string cond_for_buy = ""; string cond_for_sell = ""; string cond_for_buy_bis = ""; string cond_for_sell_bis = ""; string cond_for_take_profit_buy =""; string cond_for_take_profit_sell =""; string high_trade = ""; string low_trade = ""; int view_take_profit_buy = 0; int view_take_profit_sell = 0; double var_octet = 0 ; double var_position = 0 ; double var_ea_position =0 ; string var_ea_position_bis = "null" ; string ok_for_trade_buy = ""; string ok_for_trade_sell = ""; double var_ma_pink = iMA(NULL, 5, 7, 0, MODE_EMA, PRICE_CLOSE, 0); // Pink double high_m01= iHigh(NULL, 1,0); double high_m01p1= iHigh(NULL, 1,1); double high_m05= iHigh(NULL, 5,0); double high_m05p1= iHigh(NULL, 5,1); double high_m15= iHigh(NULL, 15,0); double high_m15p1= iHigh(NULL, 15,1); double high_m15p2= iHigh(NULL, 15,2); double high_m30= iHigh(NULL, 30,0); double high_m30p1= iHigh(NULL, 30,1); double high_m30p2= iHigh(NULL, 30,2); double high_m60= iHigh(NULL, 60,0); double high_m60p1= iHigh(NULL, 60,1); double high_m60p2= iHigh(NULL, 60,2); double high_m60p3= iHigh(NULL, 60,3); double high_m60p4= iHigh(NULL, 60,4); double high_m60p5= iHigh(NULL, 60,5); double high_m60p6= iHigh(NULL, 60,6); double high_m240= iHigh(NULL, 240,0); double high_m240p1= iHigh(NULL, 240,1); double high_m240p2= iHigh(NULL, 240,2); double high_m240p3= iHigh(NULL, 240,3); double high_m240p4= iHigh(NULL, 240,4); double high_m240p5= iHigh(NULL, 240,5); double high_m240p6= iHigh(NULL, 240,6); double high_m240p7= iHigh(NULL, 240,7); double high_m1440= iHigh(NULL, 1440,0); double high_m1440p1= iHigh(NULL, 1440,1); double high_m1440p2= iHigh(NULL, 1440,2); double high_m1440p3= iHigh(NULL, 1440,3); double high_m1440p4= iHigh(NULL, 1440,4); double low_m01 = iLow(NULL,1,0); double low_m01p1 = iLow(NULL,1,1); double low_m05 = iLow(NULL,5,0); double low_m05p1 = iLow(NULL,5,1); double low_m15 = iLow(NULL,15,0); double low_m15p1 = iLow(NULL,15,1); double low_m15p2 = iLow(NULL,15,2); double low_m30 = iLow(NULL,30,0); double low_m30p1 = iLow(NULL,30,1); double low_m30p2 = iLow(NULL,30,2); double low_m60 = iLow(NULL,60,0); double low_m60p1 = iLow(NULL,60,1); double low_m60p2 = iLow(NULL,60,2); double low_m60p3 = iLow(NULL,60,3); double low_m60p4 = iLow(NULL,60,4); double low_m60p5 = iLow(NULL,60,5); double low_m60p6 = iLow(NULL,60,6); double low_m240 = iLow(NULL,240,0); double low_m240p1 = iLow(NULL,240,1); double low_m240p2 = iLow(NULL,240,2); double low_m240p3 = iLow(NULL,240,3); double low_m240p4 = iLow(NULL,240,4); double low_m240p5 = iLow(NULL,240,5); double low_m240p6 = iLow(NULL,240,6); double low_m240p7 = iLow(NULL,240,7); double low_m1440 = iLow(NULL,1440,0); double low_m1440p1 = iLow(NULL,1440,1); double low_m1440p2 = iLow(NULL,1440,2); double low_m1440p3 = iLow(NULL,1440,3); double low_m1440p4 = iLow(NULL,1440,4); //*********** special --------- //*********** special --------- double open_m5 = iOpen(NULL,5,0); double close_m5 = iClose(NULL,5,0); double open_m5p1 = iOpen(NULL,5,1); double close_m5p1 = iClose(NULL,5,1); double open_m5p2 = iOpen(NULL,5,2); double close_m5p2 = iClose(NULL,5,2); double open_m15 = iOpen(NULL,15,0); double close_m15 = iClose(NULL,15,0); double open_m15p1 = iOpen(NULL,15,1); double close_m15p1 = iClose(NULL,15,1); double open_m15p2 = iOpen(NULL,15,2); double close_m15p2 = iClose(NULL,15,2); double open_m30 = iOpen(NULL,30,0); double close_m30 = iClose(NULL,30,0); double open_m30p1 = iOpen(NULL,30,1); double close_m30p1 = iClose(NULL,30,1); double open_m30p2 = iOpen(NULL,30,2); double close_m30p2 = iClose(NULL,30,2); double open_m60 = iOpen(NULL,60,0); double close_m60 = iClose(NULL,60,0); double open_m60p1 = iOpen(NULL,60,1); double close_m60p1 = iClose(NULL,60,1); double open_m60p2 = iOpen(NULL,60,2); double close_m60p2 = iClose(NULL,60,2); double open_m60p3 = iOpen(NULL,60,3); double close_m60p3 = iClose(NULL,60,3); double open_m60p4 = iOpen(NULL,60,4); double close_m60p4 = iClose(NULL,60,4); double open_m60p5 = iOpen(NULL,60,5); double close_m60p5 = iClose(NULL,60,5); double open_m240 = iOpen(NULL,240,0); double close_m240 = iClose(NULL,240,0); double open_m240p1 = iOpen(NULL,240,1); double close_m240p1 = iClose(NULL,240,1); double open_m240p2 = iOpen(NULL,240,2); double close_m240p2 = iClose(NULL,240,2); double open_m240p3 = iOpen(NULL,240,3); double close_m240p3 = iClose(NULL,240,3); double open_m240p4 = iOpen(NULL,240,4); double close_m240p4 = iClose(NULL,240,4); double open_m240p5 = iOpen(NULL,240,5); double close_m240p5 = iClose(NULL,240,5); double open_m240p6 = iOpen(NULL,240,6); double close_m240p6 = iClose(NULL,240,6); double open_m240p7 = iOpen(NULL,240,7); double close_m240p7 = iClose(NULL,240,7); double open_m1440 = iOpen(NULL,1440,0); double close_m1440 = iClose(NULL,1440,0); double open_m1440p1 = iOpen(NULL,1440,1); double close_m1440p1 = iClose(NULL,1440,1); double open_m1440p2 = iOpen(NULL,1440,2); double close_m1440p2 = iClose(NULL,1440,2); double open_m1440p3 = iOpen(NULL,1440,3); double close_m1440p3 = iClose(NULL,1440,3); double open_m1440p4 = iOpen(NULL,1440,4); double close_m1440p4 = iClose(NULL,1440,4); double ma1p1_m5 = iMA(NULL,5, 1, 0, MODE_EMA, PRICE_CLOSE, 1); double ma1p1_m60 = iMA(NULL, 60, 1, 0, MODE_EMA, PRICE_CLOSE, 1); //************************ double sar_m1 = iSAR(NULL, 1, 0.02, 0.2, 0); double sar_m1p1 = iSAR(NULL, 1, 0.02, 0.2, 1); double sar_m5 = iSAR(NULL, 5, 0.02, 0.2, 0); double sar_m5p1 = iSAR(NULL, 5 , 0.02, 0.2, 1); double sar_m15 = iSAR(NULL, 15, 0.02, 0.2, 0); double sar_m15p1 = iSAR(NULL, 15 , 0.02, 0.2, 1); double sar_m30 = iSAR(NULL, 30, 0.02, 0.2, 0); double sar_m30p1 = iSAR(NULL,30 , 0.02, 0.2, 1); double sar_m60 = iSAR(NULL, 60, 0.02, 0.2, 0); double sar_m60p1 = iSAR(NULL, 60 , 0.02, 0.2, 1); double sar_m240 = iSAR(NULL, 240, 0.02, 0.2, 0); double sar_m240p1 = iSAR(NULL, 240 , 0.02, 0.2, 1); double sar_xx1 = iSAR(NULL, 5, 0.09, 0.2, 0); double sar_xx2 = iSAR(NULL, 60, 0.05, 0.2, 0); //**************************// //**************************// if( var_ma_pink > sar_xx1 ) { string P__AA = "buy"; } if( var_ma_pink < sar_xx1 ) { P__AA = "sell"; } ///////////END VAR CONDITION ////////////////// //******** //************* if(P__AA == "buy" ){ string zp_zv12 = "buy" ; ok_for_trade_buy ="yes" ; ok_for_trade_sell ="no" ; // GlobalVariableSet(identity+"var_principale",1111); } if(P__AA == "sell" ){ zp_zv12 = "sell"; ok_for_trade_sell ="yes" ; ok_for_trade_buy ="no" ; } string stop_ze_trade_and_take_profit_buy ; string stop_ze_trade_and_take_profit_sell ; //**// double G_get_buy = MathAbs(GlobalVariableGet(identity+num_magic_buy)); double G_get_sell = MathAbs(GlobalVariableGet(identity+num_magic_sell)); string function_for_profit = "no"; string take_profit_ontrade_buy =""; string take_profit_ontrade_sell =""; string var_take_profit_buy =""; string var_take_profit_sell =""; if( P__AA == "sell" ) { var_take_profit_buy ="ok" ; } if(P__AA == "buy" ) { var_take_profit_sell ="ok"; } //****************************** if( P__AA == "buy" ) { var_condition_1 = "buy" ; } if( P__AA == "sell" ) {var_condition_1 = "sell" ; } //********************************* if( var_condition_1 == "buy" ) { cond_for_buy = "buy"; } if( var_condition_1 == "sell" ) { cond_for_sell = "sell"; } //******************************** string zecomment4 = (" var_ma_pink " + var_ma_pink ); //******** condition to take profit********* int total=OrdersTotal(); double ea_profit = OrderProfit() ; // if(total<1) if(wBar!=Time[0] && !FindOpenOrder(ramdom_number)) { //--- no opened orders identified if(AccountFreeMargin()<(1000*Lots_bis)) { // Alert("We have no money. Free Margin = ",AccountFreeMargin()); // return; } //--- check for long position (BUY) possibility if( //***************** // #include cond_for_buy == "buy" //**************** ) { int ticket=OrderSend(Symbol(),OP_BUY,Lots_bis,Ask,3,0,Ask+TakeProfit*Point,identity,ramdom_number,0,Green); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) { Print("BUY order opened : ",OrderOpenPrice()); var_print_error =0; //POUR EVITER DE SATURER DE MESSAGE LE SERVER DANS LES FICHIERS LOG !! //************ AJOUT MARCH25***** var_OrderClose_error =0; var_OrderModify_error =0; //************ FIN AJOUT MARCH25***** wBar=Time[0]; } } else if ( var_print_error ==0) { Print("Error opening BUY order : ",GetLastError()); var_print_error =1; } return; } //--- check for short position (SELL) possibility if( //**************** // #include cond_for_sell == "sell" //********************* ) { ticket=OrderSend(Symbol(),OP_SELL,Lots_bis,Bid,3,0,Bid-TakeProfit*Point,identity,ramdom_number,0,Red); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) { Print("SELL order opened : ",OrderOpenPrice()); var_print_error =0; //************ AJOUT MARCH25***** var_OrderClose_error =0; var_OrderModify_error =0; //************ FIN AJOUT MARCH25***** wBar=Time[0]; } } else if ( var_print_error ==0) { Print("Error opening SELL order : ",GetLastError()); var_print_error =1 ; } } //--- exit from the "no opened orders" block // return; } for(int cnt=0;cnt= 1 && weekday ==5 && htime > 20 && close_m60 < ( close_m60p1 + open_m60p1)/2 ) { cond_for_take_profit_buy = "buy" ; } //********************* if( cond_for_buy =="buy" ) { cond_for_take_profit_sell = "sell" ; } if( ea_profit >= 1 && weekday ==5 && htime > 20 && close_m60 < ( close_m60p1 + open_m60p1)/2 ) { cond_for_take_profit_sell = "sell" ; } //******************* //--- long position is opened if(OrderType()==OP_BUY) { sens_trade = "buy"; //////// //--- should it be closed? if( //************************************ //#include cond_for_take_profit_buy == "buy" //************************************* ) { //--- close order and exit if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet)) if ( var_OrderClose_error == 0 || var_OrderClose_error =="") { Print("OrderClose error ",GetLastError()); var_OrderClose_error =1; } //return; continue; } //--- check for trailing stop // if(TrailingStop>0) if(TrailingStop>100) { if(Bid-OrderOpenPrice()>Point*TrailingStop) { if(OrderStopLoss() cond_for_take_profit_sell == "sell" //************************* ) { //--- close order and exit if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet)) if ( var_OrderClose_error == 0 || var_OrderClose_error =="") { Print("OrderClose error ",GetLastError()); var_OrderClose_error = 1; } GlobalVariableSet(Symbol() + "continue_next_T_ " , 1111); //return; continue; } //--- check for trailing stop // if(TrailingStop>0) if(TrailingStop>100) { if((OrderOpenPrice()-Ask)>(Point*TrailingStop)) { if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0)) { //--- modify order and exit if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red)) if ( var_OrderModify_error ==0 || var_OrderModify_error =="") { Print("OrderModify error ",GetLastError()); var_OrderModify_error =1; } //return; continue; } } } } } } //--- Comment( "\n", "Pipvalue : " , PipValue , " || Spread : " , spread , " || " , spread*PipValue, " || Sens : " , sens_trade, " || Profit : " , ea_profit , "\n ", "\n ", zecomment4 ,"\n ", "\n", "H E FOR THE WEEK: ",Highest_EQUITY,"\n", "L E FOR THE WEEK: ",Lowest_EQUITY,"\n" ); // ********* ALERT ****************************** if (var_alert != 0) { Alert(var_alert) ; } ; } bool FindOpenOrder(int Magic) //Check market orders. Return true - if found, false - if not { int i; bool fFind; fFind=false; for(i=0;i