//+------------------------------------------------------------------+ //| SundayTrader_TypeE.mq4 | //| Copyright © 2011, MQL-Programming.com | //| http://www.mql-programming.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2011, MQL-Programming.com" #property link "http://www.mql-programming.com" #import "kernel32.dll" void GetLocalTime(int& TimeArray[]); void GetSystemTime(int& TimeArray[]); int GetTimeZoneInformation(int& TZInfoArray[]); #import extern string p01="--- Pairs Used ---"; extern string Pair_1="EURGBP", Pair_2="EURUSD", Pair_3="EURCHF", Pair_4="USDCHF", Pair_5="", Pair_6="", Pair_7=""; extern string p12="--- Position(All,Long,Short) ---"; extern string Pair_1Position="Short", Pair_2Position="All", Pair_3Position="All", Pair_4Position="All", Pair_5Position="", Pair_6Position="", Pair_7Position=""; extern string p13="--- MA Used ---"; extern bool Pair_1MAUsed=false, Pair_2MAUsed=true, Pair_3MAUsed=true, Pair_4MAUsed=true, Pair_5MAUsed=false, Pair_6MAUsed=false, Pair_7MAUsed=false, Test_MAUsed=false; extern string p02="--- Risk Settings ---"; extern bool MoneyManagement=true; extern double Risk=2.5, FixedLots=0.1, MaxSpread=40, Slippage=3; extern string p03="--- Setup of trade ---"; extern bool Fixed_TP=false; extern int TP_Pips=25, TP_Percent_of_Gap=80, TP_1_Percent_of_Lot=50; extern bool Use_TP_2=false; extern int TP_2_Pips=75, TP_2_Percent_of_Gap=150; extern bool Use_SL_Percent=true; extern int SL_Percent_of_Gap=100, SL_Pips=50; extern bool Add_Spread_to_SL=true; extern int BreakEven=25, BreakEven_Buffer=5; extern string p04="--- Gap Settings ---"; extern int Min_Gap_Size=7, Max_Gap_Size=80; extern bool Trade_Direction_of_Gap=false, Trade_All_Gaps=false; extern string p05="--- Pyramid Trading Settings ---"; extern bool Pyramiding=false; extern double Pyramid_Risk=1; extern int Max_Levels=2; extern bool Use_Pyramid_Gap_Percent=true; extern int Percent_per_Level=20, Next_Step_Pips=15; extern string p06="** Exit Strategy **"; extern string p07="--- Manual Trail Settings ---"; extern bool Trailing_by_Pips=true; extern int Pips_to_Trail=15; extern string p08="--- Parabolic S/R Settings ---"; extern bool Parabolic_SR_Trailing=false; extern int Parabolic_SR_Buffer=10; extern string p09="--- Decaying SL Settings ---"; extern bool Decaying_Trailing_Stop=false; extern int Trail_Pips=25, Percent_per_Bar=75; extern bool Exponential=true; extern int Minutes_Before_Decay=480; extern string p10="--- Misc Settings ---"; extern int Lot_Digits_After_Decimal=2; extern int Magic=32963; extern string Commentary="Sunday Trader"; extern color Display_Color=Gold; extern string p11="--- MA Settings (PERIOD_D1)---"; extern int MA_Period1=25; extern int MA_Period2=100; int Count, // Number of open orders Numb, // Number of Symbol Difference, tkt; static int Positive_Gap[8]; // Positive Gap if true - SELL static double Poin[8], // Koefficient of Point for this broker PipValue[8]; // this variable is here to support 5-digit static double //Poin[8], // Koefficient of Point for this broker //PipValue[8], // this variable is here to support 5-digit brokers H, // Hours in local time Server_GMT_Difference, // GMT difference Stop[8], Pyr_Stop[8], Level_for_TP[8], Pyr_Level_for_TP[8][8], Level_for_TP_2[8], Pyr_Level_for_TP_2[8][8], Pyr_Level_for_Open[8][8], Pyr_First_Order_TP[8]; string Symb; string Position; // "All" , "Long" , "Short" bool MAUsed; // MA Filter Used Settings; static bool Accept=false, // If no errors than Accept=true ModBE[8], TP_1_Complete[8], Pyr_TP_1_Complete[8][8], Pyr_Open[8][8]; static datetime New_Time[8]; static datetime Time_for_Decaying[8], Time_for_Parabolic[8], Time_for_Begin_Decay[8], Time_for_Cycle[8]; static int Number_of_Decaying[8], Ticket_of_first[8]; //------------------------------------------------------------------------------------------------------------ //+------------------------------------------------------------------+ //| Functions for check LocalTime and shift with GMT | //+------------------------------------------------------------------+ string TimeToStringOrg( datetime when ) { return (TimeToStr( when, TIME_MINUTES )); } string FormatDateTime(int nYear,int nMonth,int nDay,int nHour,int nMin,int nSec) { string sMonth,sDay,sHour,sMin,sSec; //---- sMonth=100+nMonth; sMonth=StringSubstr(sMonth,1); sDay=100+nDay; sDay=StringSubstr(sDay,1); sHour=100+nHour; sHour=StringSubstr(sHour,1); sMin=100+nMin; sMin=StringSubstr(sMin,1); sSec=100+nSec; sSec=StringSubstr(sSec,1); //---- return(StringConcatenate(nYear,".",sMonth,".",sDay," ",sHour,":",sMin,":",sSec)); } string GMT() { // Define GMT time int TimeArray[4]; int TZInfoArray[43]; int nYear,nMonth,nDay,nHour,nMin,nSec,nMilliSec; string sMilliSec; GetLocalTime(TimeArray); //---- parse date and time from array nYear=TimeArray[0]&0x0000FFFF; nMonth=TimeArray[0]>>16; nDay=TimeArray[1]>>16; nHour=TimeArray[2]&0x0000FFFF; nMin=TimeArray[2]>>16; nSec=TimeArray[3]&0x0000FFFF; nMilliSec=TimeArray[3]>>16; string LocalTimeS = FormatDateTime(nYear,nMonth,nDay,nHour,nMin,nSec); datetime localTime = StrToTime( LocalTimeS ); int gmt_shift=0; int dst=GetTimeZoneInformation(TZInfoArray); if(dst!=0) gmt_shift=TZInfoArray[0]; if(dst==2) gmt_shift+=TZInfoArray[42]; //datetime brokerTime = CurTime(); datetime GMT = localTime + gmt_shift * 60; Server_GMT_Difference=(CurTime()-GMT); return(TimeToStringOrg(GMT)); } string CurToGMT() { return( TimeToStringOrg(CurTime()-Server_GMT_Difference) ); } //------------------------------------------------------------------------------------------------------------ //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { // How much hour by GMT GMT(); // Check input values if (Error()==false) Accept=true; // Set begin values for (int i=1; i<=7; i++) { Positive_Gap[i]=-1; ModBE[i]=false; TP_1_Complete[i]=false; PipValue[i]=1.0; Poin[i]=0.0001; Time_for_Cycle[Numb]=0; } // Define Koefficients if (MarketInfo(Pair_1, MODE_DIGITS)==3 || MarketInfo(Pair_1, MODE_DIGITS)==5) PipValue[1]=10.0; else PipValue[1]=1.0; Poin[1]=MarketInfo(Pair_1, MODE_POINT); if (MarketInfo(Pair_2, MODE_DIGITS)==3 || MarketInfo(Pair_2, MODE_DIGITS)==5) PipValue[2]=10.0; else PipValue[2]=1.0; Poin[2]=MarketInfo(Pair_2, MODE_POINT); if (MarketInfo(Pair_3, MODE_DIGITS)==3 || MarketInfo(Pair_3, MODE_DIGITS)==5) PipValue[3]=10.0; else PipValue[3]=1.0; Poin[3]=MarketInfo(Pair_3, MODE_POINT); if (MarketInfo(Pair_4, MODE_DIGITS)==3 || MarketInfo(Pair_4, MODE_DIGITS)==5) PipValue[4]=10.0; else PipValue[4]=1.0; Poin[4]=MarketInfo(Pair_4, MODE_POINT); if (MarketInfo(Pair_5, MODE_DIGITS)==3 || MarketInfo(Pair_5, MODE_DIGITS)==5) PipValue[5]=10.0; else PipValue[5]=1.0; Poin[5]=MarketInfo(Pair_5, MODE_POINT); if (MarketInfo(Pair_6, MODE_DIGITS)==3 || MarketInfo(Pair_6, MODE_DIGITS)==5) PipValue[6]=10.0; else PipValue[6]=1.0; Poin[6]=MarketInfo(Pair_6, MODE_POINT); if (MarketInfo(Pair_7, MODE_DIGITS)==3 || MarketInfo(Pair_7, MODE_DIGITS)==5) PipValue[7]=10.0; else PipValue[7]=1.0; Poin[7]=MarketInfo(Pair_7, MODE_POINT); if (MarketInfo(Symbol(), MODE_DIGITS)==3 || MarketInfo(Symbol(), MODE_DIGITS)==5) PipValue[0]=10.0; else PipValue[0]=1.0; return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { Comment(""); ObjectsDeleteAll(); return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { if (Accept==false) return; //Comment(DayOfWeek()); PrintToChart(); Count=0; if (IsTesting()) { Symb=Symbol(); Numb=1; Position="All"; MAUsed = Test_MAUsed; if (MarketInfo(Symbol(), MODE_DIGITS)==3 || MarketInfo(Pair_1, MODE_DIGITS)==5) PipValue[1]=10.0; else PipValue[1]=1.0; Poin[1]=MarketInfo(Symbol(), MODE_POINT); if (MaxSpread>MarketInfo(Symb, MODE_SPREAD)/PipValue[Numb]) { if (Pyramiding) Pyr_Trail(); else Trail(); if ( !Trade_All_Gaps && (DayOfWeek()!=1 && DayOfWeek()!=0) ) return; if (New_Candle() && !Pyramiding) if (Gap_Size()>=Min_Gap_Size && Gap_Size()<=Max_Gap_Size) OpenOrd(); if (Pyramiding) { if (Pyr_There_Are_Open(-1)) Pyr_Refilling(); else Def_Levels(); if (Gap_Size()>=Min_Gap_Size && Gap_Size()<=Max_Gap_Size) Pyr_Open(); } } } // For Pair 1 if ( MarketInfo(Pair_1, MODE_TRADEALLOWED)!=0 && !IsTesting() ) { Symb=Pair_1; Numb=1; Position=Pair_1Position; MAUsed=Pair_1MAUsed; if (MaxSpread>MarketInfo(Symb, MODE_SPREAD)/PipValue[Numb]) { if (Pyramiding) Pyr_Trail(); else Trail(); if ( !Trade_All_Gaps && (DayOfWeek()!=1 && DayOfWeek()!=0) ) return; if (New_Candle() && !Pyramiding) if (Gap_Size()>=Min_Gap_Size && Gap_Size()<=Max_Gap_Size) OpenOrd(); if (Pyramiding) { if (Pyr_There_Are_Open(-1)) Pyr_Refilling(); else Def_Levels(); if (Gap_Size()>=Min_Gap_Size && Gap_Size()<=Max_Gap_Size) Pyr_Open(); } } } // For Pair 2 if ( MarketInfo(Pair_2, MODE_TRADEALLOWED)!=0 && !IsTesting() ) { Symb=Pair_2; Numb=2; Position=Pair_2Position; MAUsed=Pair_2MAUsed; if (MaxSpread>MarketInfo(Symb, MODE_SPREAD)/PipValue[Numb]) { if (Pyramiding) Pyr_Trail(); else Trail(); if ( !Trade_All_Gaps && (DayOfWeek()!=1 && DayOfWeek()!=0) ) return; if (New_Candle() && !Pyramiding) if (Gap_Size()>=Min_Gap_Size && Gap_Size()<=Max_Gap_Size) OpenOrd(); if (Pyramiding) { if (Pyr_There_Are_Open(-1)) Pyr_Refilling(); else Def_Levels(); if (Gap_Size()>=Min_Gap_Size && Gap_Size()<=Max_Gap_Size) Pyr_Open(); } } } // For Pair 3 if ( MarketInfo(Pair_3, MODE_TRADEALLOWED)!=0 && !IsTesting() ) { Symb=Pair_3; Numb=3; Position=Pair_3Position; MAUsed=Pair_3MAUsed; if (MaxSpread>MarketInfo(Symb, MODE_SPREAD)/PipValue[Numb]) { if (Pyramiding) Pyr_Trail(); else Trail(); if ( !Trade_All_Gaps && (DayOfWeek()!=1 && DayOfWeek()!=0) ) return; if (New_Candle() && !Pyramiding) if (Gap_Size()>=Min_Gap_Size && Gap_Size()<=Max_Gap_Size) OpenOrd(); if (Pyramiding) { if (Pyr_There_Are_Open(-1)) Pyr_Refilling(); else Def_Levels(); if (Gap_Size()>=Min_Gap_Size && Gap_Size()<=Max_Gap_Size) Pyr_Open(); } } } // For Pair 4 if ( MarketInfo(Pair_4, MODE_TRADEALLOWED)!=0 && !IsTesting() ) { Symb=Pair_4; Numb=4; Position=Pair_4Position; MAUsed=Pair_4MAUsed; if (MaxSpread>MarketInfo(Symb, MODE_SPREAD)/PipValue[Numb]) { if (Pyramiding) Pyr_Trail(); else Trail(); if ( !Trade_All_Gaps && (DayOfWeek()!=1 && DayOfWeek()!=0) ) return; if (New_Candle() && !Pyramiding) if (Gap_Size()>=Min_Gap_Size && Gap_Size()<=Max_Gap_Size) OpenOrd(); if (Pyramiding) { if (Pyr_There_Are_Open(-1)) Pyr_Refilling(); else Def_Levels(); if (Gap_Size()>=Min_Gap_Size && Gap_Size()<=Max_Gap_Size) Pyr_Open(); } } } // For Pair 5 if ( MarketInfo(Pair_5, MODE_TRADEALLOWED)!=0 ) { Symb=Pair_5; Numb=5; Position=Pair_5Position; MAUsed=Pair_5MAUsed; if (MaxSpread>MarketInfo(Symb, MODE_SPREAD)/PipValue[Numb]) { if (Pyramiding) Pyr_Trail(); else Trail(); if ( !Trade_All_Gaps && (DayOfWeek()!=1 && DayOfWeek()!=0) ) return; if (New_Candle() && !Pyramiding) if (Gap_Size()>=Min_Gap_Size && Gap_Size()<=Max_Gap_Size) OpenOrd(); if (Pyramiding) { if (Pyr_There_Are_Open(-1)) Pyr_Refilling(); else Def_Levels(); if (Gap_Size()>=Min_Gap_Size && Gap_Size()<=Max_Gap_Size) Pyr_Open(); } } } // For Pair 6 if ( MarketInfo(Pair_6, MODE_TRADEALLOWED)!=0 ) { Symb=Pair_6; Numb=6; Position=Pair_6Position; MAUsed=Pair_6MAUsed; if (MaxSpread>MarketInfo(Symb, MODE_SPREAD)/PipValue[Numb]) { if (Pyramiding) Pyr_Trail(); else Trail(); if ( !Trade_All_Gaps && (DayOfWeek()!=1 && DayOfWeek()!=0) ) return; if (New_Candle() && !Pyramiding) if (Gap_Size()>=Min_Gap_Size && Gap_Size()<=Max_Gap_Size) OpenOrd(); if (Pyramiding) { if (Pyr_There_Are_Open(-1)) Pyr_Refilling(); else Def_Levels(); if (Gap_Size()>=Min_Gap_Size && Gap_Size()<=Max_Gap_Size) Pyr_Open(); } } } // For Pair 7 if ( MarketInfo(Pair_7, MODE_TRADEALLOWED)!=0 ) { Symb=Pair_7; Numb=7; Position=Pair_7Position; MAUsed=Pair_7MAUsed; if (MaxSpread>MarketInfo(Symb, MODE_SPREAD)/PipValue[Numb]) { if (Pyramiding) Pyr_Trail(); else Trail(); if ( !Trade_All_Gaps && (DayOfWeek()!=1 && DayOfWeek()!=0) ) return; if (New_Candle() && !Pyramiding) if (Gap_Size()>=Min_Gap_Size && Gap_Size()<=Max_Gap_Size) OpenOrd(); if (Pyramiding) { if (Pyr_There_Are_Open(-1)) Pyr_Refilling(); else Def_Levels(); if (Gap_Size()>=Min_Gap_Size && Gap_Size()<=Max_Gap_Size) Pyr_Open(); } } } return(0); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Check input values | //+------------------------------------------------------------------+ bool Error() { // Risk if (Risk<=0 || Risk>100) { Comment("Enter correct value for Risk (0..100)"); return(true); } // Exit if ( (Trailing_by_Pips && Parabolic_SR_Trailing) || (Trailing_by_Pips && Decaying_Trailing_Stop) || (Parabolic_SR_Trailing && Decaying_Trailing_Stop) ) { Comment("Please, select only one criteria for Exit"); return(true); } if (TP_Percent_of_Gap<1 || TP_Percent_of_Gap>1000) { Comment("Enter correct value for TP_Percent_of_Gap (1..1000)"); return(true); } if (TP_2_Percent_of_Gap<1 || TP_2_Percent_of_Gap>1000) { Comment("Enter correct value for TP_2_Percent_of_Gap (1..1000)"); return(true); } if (SL_Percent_of_Gap<1 || SL_Percent_of_Gap>1000) { Comment("Enter correct value for SL_Percent_of_Gap (1..1000)"); return(true); } if (Percent_per_Level<1 || Percent_per_Level>1000) { Comment("Enter correct value for Percent_per_Level (1..1000)"); return(true); } if (!IsTesting()) { if (MarketInfo(Pair_1, MODE_TRADEALLOWED)==0 && Pair_1!="") { Comment(Pair_1, " is not VALID for this broker"); return(true); } if (MarketInfo(Pair_2, MODE_TRADEALLOWED)==0 && Pair_2!="") { Comment(Pair_2, " is not VALID for this broker"); return(true); } if (MarketInfo(Pair_3, MODE_TRADEALLOWED)==0 && Pair_3!="") { Comment(Pair_3, " is not VALID for this broker"); return(true); } if (MarketInfo(Pair_4, MODE_TRADEALLOWED)==0 && Pair_4!="") { Comment(Pair_4, " is not VALID for this broker"); return(true); } if (MarketInfo(Pair_5, MODE_TRADEALLOWED)==0 && Pair_5!="") { Comment(Pair_5, " is not VALID for this broker"); return(true); } if (MarketInfo(Pair_6, MODE_TRADEALLOWED)==0 && Pair_6!="") { Comment(Pair_6, " is not VALID for this broker"); return(true); } if (MarketInfo(Pair_7, MODE_TRADEALLOWED)==0 && Pair_7!="") { Comment(Pair_7, " is not VALID for this broker"); return(true); } } return(false); } //+------------------------------------------------------------------+ //| Print to chart | //+------------------------------------------------------------------+ void PrintToChart() { // For define number of open orders Profit_Loss(); // Create display string text[24]; text[1]= "[Sunday Trader]"; text[2]= "-----------------------------------------"; text[3]= "Broker Time: " + TimeToStringOrg(CurTime()); text[4]= "GMT offset: " + DoubleToStr(Server_GMT_Difference/3600, 0); text[5]= "GMT: " + CurToGMT(); text[6]= "Min Lot: " + DoubleToStr(MarketInfo(Symbol(), MODE_MINLOT), 2); text[7]= "Current Lot: " + DoubleToStr(Current_Lot(), 2); //text[8]= "Spread: " + DoubleToStr(MarketInfo(Symbol(), MODE_SPREAD)/PipValue[0], 2); text[8]= "-----------------------------------------"; text[9]= "Profit: " + DoubleToStr(Profit(), 2) + " " + AccountCurrency(); text[10]= "Loss: " + DoubleToStr(Loss(), 2) + " " + AccountCurrency(); if (Loss()==0 || Profit()==0) text[11]= "Profit Factor: 0"; else text[11]= "Profit Factor: " + DoubleToStr(MathAbs(Profit()/Loss()) , 2); text[12]= "-----------------------------------------"; text[13]= "Current trades: " + Count ; text[14]= "Floating P-L: " + DoubleToStr(Profit_Loss(), 2) + " " + AccountCurrency(); text[15]= "Pips Locked in: " + DoubleToStr(Locked(), 0); text[16]= "-----------------------------------------"; if (!IsTesting()) { if (MarketInfo(Pair_1, MODE_TRADEALLOWED)!=0) text[17]= Pair_1 + " Spread:" + DoubleToStr(MarketInfo(Pair_1, MODE_SPREAD)/PipValue[1], 2); if (MarketInfo(Pair_2, MODE_TRADEALLOWED)!=0) text[18]= Pair_2 + " Spread:" + DoubleToStr(MarketInfo(Pair_2, MODE_SPREAD)/PipValue[2], 2); if (MarketInfo(Pair_3, MODE_TRADEALLOWED)!=0) text[19]= Pair_3 + " Spread:" + DoubleToStr(MarketInfo(Pair_3, MODE_SPREAD)/PipValue[3], 2); if (MarketInfo(Pair_4, MODE_TRADEALLOWED)!=0) text[20]= Pair_4 + " Spread:" + DoubleToStr(MarketInfo(Pair_4, MODE_SPREAD)/PipValue[4], 2); if (MarketInfo(Pair_5, MODE_TRADEALLOWED)!=0) text[21]= Pair_5 + " Spread:" + DoubleToStr(MarketInfo(Pair_5, MODE_SPREAD)/PipValue[5], 2); if (MarketInfo(Pair_6, MODE_TRADEALLOWED)!=0) text[22]= Pair_6 + " Spread:" + DoubleToStr(MarketInfo(Pair_6, MODE_SPREAD)/PipValue[6], 2); if (MarketInfo(Pair_7, MODE_TRADEALLOWED)!=0) text[23]= Pair_7 + " Spread:" + DoubleToStr(MarketInfo(Pair_7, MODE_SPREAD)/PipValue[7], 2); } /* text[17]= Pair_1 + " Spread:" + DoubleToStr(MarketInfo(Pair_1, MODE_SPREAD)/PipValue[1], 2); text[18]= Pair_2 + " Spread:" + DoubleToStr(MarketInfo(Pair_2, MODE_SPREAD)/PipValue[2], 2); text[19]= Pair_3 + " Spread:" + DoubleToStr(MarketInfo(Pair_3, MODE_SPREAD)/PipValue[3], 2); text[20]= Pair_4 + " Spread:" + DoubleToStr(MarketInfo(Pair_4, MODE_SPREAD)/PipValue[4], 2); text[21]= Pair_5 + " Spread:" + DoubleToStr(MarketInfo(Pair_5, MODE_SPREAD)/PipValue[5], 2); text[22]= Pair_6 + " Spread:" + DoubleToStr(MarketInfo(Pair_6, MODE_SPREAD)/PipValue[6], 2); text[23]= Pair_7 + " Spread:" + DoubleToStr(MarketInfo(Pair_7, MODE_SPREAD)/PipValue[7], 2); */ /* //text[17]= PipValue[1]; //text[18]= PipValue[2]; //text[19]= PipValue[3]; //text[20]= PipValue[4]; //text[21]= PipValue[5]; //text[22]= PipValue[6]; //text[23]= PipValue[7]; */ int i=1; int k=25; while (i<=23) { string ChartInfo = DoubleToStr(i, 0); ObjectCreate(ChartInfo, OBJ_LABEL, 0, 0, 0); ObjectSetText(ChartInfo, text[i], 9, "Arial", Display_Color); ObjectSet(ChartInfo, OBJPROP_CORNER, 0); ObjectSet(ChartInfo, OBJPROP_XDISTANCE, 8); ObjectSet(ChartInfo, OBJPROP_YDISTANCE, k); i++; k=k+13; } return; } //+------------------------------------------------------------------+ //| Measure value for StopLoss | //+------------------------------------------------------------------+ double SL(int cmd) { double GSize=Gap_Size()-NormalizeDouble(MarketInfo(Symb, MODE_SPREAD)/PipValue[Numb], 1); if (GSize<0.0) GSize=Gap_Size(); // if Use PERCENT if (Use_SL_Percent) double SL=GSize*SL_Percent_of_Gap*0.01; else SL=SL_Pips; // Number of pips of SL Stop[Numb]=SL; // If add spread = true if (Add_Spread_to_SL) { if (cmd==0) SL=MarketInfo(Symb, MODE_BID)-SL*PipValue[Numb]*Poin[Numb]; if (cmd==1) SL=MarketInfo(Symb, MODE_ASK)+SL*PipValue[Numb]*Poin[Numb]; } // if add spread = false if (!Add_Spread_to_SL) { if (cmd==0) SL=MarketInfo(Symb, MODE_ASK)-SL*PipValue[Numb]*Poin[Numb]; if (cmd==1) SL=MarketInfo(Symb, MODE_BID)+SL*PipValue[Numb]*Poin[Numb]; } return(NormalizeDouble(SL, MarketInfo(Symb, MODE_DIGITS))); } //+------------------------------------------------------------------+ //| Measure value for Take Profit | //+------------------------------------------------------------------+ void TP(int cmd) { // Measure 1st TP double TP, TP_2; double GSize=Gap_Size()-NormalizeDouble(MarketInfo(Symb, MODE_SPREAD)/PipValue[Numb], 1); if (GSize<0.0) GSize=Gap_Size(); if (Fixed_TP) TP=TP_Pips; else TP=GSize*TP_Percent_of_Gap*0.01; // Measure Level for 1st TP if (cmd==0) Level_for_TP[Numb]=MarketInfo(Symb, MODE_ASK)+TP*PipValue[Numb]*Poin[Numb]; if (cmd==1) Level_for_TP[Numb]=MarketInfo(Symb, MODE_BID)-TP*PipValue[Numb]*Poin[Numb]; // Measure 2nd TP if (Fixed_TP) TP_2=TP_2_Pips; else TP_2=GSize*TP_2_Percent_of_Gap*0.01; // Measure Level for 2nd TP if (cmd==0) Level_for_TP_2[Numb]=MarketInfo(Symb, MODE_ASK)+TP_2*PipValue[Numb]*Poin[Numb]; if (cmd==1) Level_for_TP_2[Numb]=MarketInfo(Symb, MODE_BID)-TP_2*PipValue[Numb]*Poin[Numb]; return; } //+------------------------------------------------------------------+ //| Measure lot size | //+------------------------------------------------------------------+ double Lots_Optimized() { double Lot; SL(0); double spr=NormalizeDouble(MarketInfo(Symb, MODE_SPREAD)/PipValue[Numb], 2); if (MoneyManagement) { Lot = NormalizeDouble( AccountBalance() * (Risk / 100.0) / (MathAbs((Stop[Numb]+spr)*PipValue[Numb]*Poin[Numb]) / MarketInfo(Symb, MODE_TICKSIZE) * MarketInfo(Symb, MODE_TICKVALUE)), 2); if (Lot0.0) Positive_Gap[Numb]=1; else if (Gap<0.0) Positive_Gap[Numb]=0; else Positive_Gap[Numb]=-1; return(MathAbs(NormalizeDouble(Gap, 1))); } //+------------------------------------------------------------------+ //| Move StopLoss | //+------------------------------------------------------------------+ void Trail() { //-------------------- // Close orders by TP for (int i=0; i=Level_for_TP[Numb] && !TP_1_Complete[Numb]) { double Lot_for_CLose=NormalizeDouble(OrderLots()*0.01*TP_1_Percent_of_Lot, Lot_Digits_After_Decimal); if (Lot_for_CLose=Level_for_TP_2[Numb] && TP_1_Complete[Numb] && Use_TP_2) { Lot_for_CLose=OrderLots(); // 10 attempts to close order for (j=0; j<10; j++) { RefreshRates(); Closed=OrderClose(OrderTicket(), Lot_for_CLose, MarketInfo(Symb, MODE_BID), Slippage*PipValue[Numb]*Poin[Numb]); if (Closed==false) { Sleep(10000); continue; } break; } } */ } // for SELL if (OrderType()==OP_SELL && Use_TP_2) { RefreshRates(); // 1ST TP if (MarketInfo(Symb, MODE_ASK)<=Level_for_TP[Numb] && !TP_1_Complete[Numb]) { Lot_for_CLose=NormalizeDouble(OrderLots()*0.01*TP_1_Percent_of_Lot, Lot_Digits_After_Decimal); if (Lot_for_CLose=OrderOpenPrice()+BreakEven*PipValue[Numb]*Poin[Numb]) for (j=0; j<10; j++) { while(!IsTradeAllowed())Sleep(5000); RefreshRates(); OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(OrderOpenPrice()+BreakEven_Buffer*PipValue[Numb]*Poin[Numb], MarketInfo(Symb, MODE_DIGITS)), OrderTakeProfit(), 0, Green); if (GetLastError()==0) { ModBE[Numb]=true; break; } } // for SELL if (OrderType()==OP_SELL && ModBE[Numb]==false) if (MarketInfo(Symb, MODE_ASK)<=OrderOpenPrice()-BreakEven*PipValue[Numb]*Poin[Numb]) for (j=0; j<10; j++) { while(!IsTradeAllowed())Sleep(5000); RefreshRates(); OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(OrderOpenPrice()-BreakEven_Buffer*PipValue[Numb]*Poin[Numb], MarketInfo(Symb, MODE_DIGITS)), OrderTakeProfit(), 0, Red); if (GetLastError()==0) { ModBE[Numb]=true; break; } } } // For classic trailing if ( Trailing_by_Pips || (CurTime()OrderOpenPrice()) && (MarketInfo(Symb, MODE_BID)-Pips_to_Trail*PipValue[Numb]*Poin[Numb]>OrderStopLoss())) for (j=0; j<10; j++) { while(!IsTradeAllowed())Sleep(5000); RefreshRates(); OrderModify(OrderTicket(), OrderOpenPrice(), MarketInfo(Symb, MODE_BID)-Pips_to_Trail*PipValue[Numb]*Poin[Numb], OrderTakeProfit(), 0, Green); if (GetLastError()==0) break; Sleep(1000); RefreshRates(); } // for SELL if (OrderType()==OP_SELL) if ((MarketInfo(Symb, MODE_ASK)+Pips_to_Trail*PipValue[Numb]*Poin[Numb]OrderOpenPrice()) && (MarketInfo(Symb, MODE_BID)-Trail_Pips*PipValue[Numb]*Poin[Numb]>OrderStopLoss())) for (j=0; j<10; j++) { while(!IsTradeAllowed())Sleep(5000); RefreshRates(); OrderModify(OrderTicket(), OrderOpenPrice(), MarketInfo(Symb, MODE_BID)-Trail_Pips*PipValue[Numb]*Poin[Numb], OrderTakeProfit(), 0, Green); if (GetLastError()==0) break; Sleep(1000); RefreshRates(); } // for SELL if (OrderType()==OP_SELL) if ((MarketInfo(Symb, MODE_ASK)+Trail_Pips*PipValue[Numb]*Poin[Numb] ma1 && MarketInfo(Symb, MODE_ASK) > ma2) { isLong = true; } //isShort if(ma1 > MarketInfo(Symb, MODE_BID) && ma2 > MarketInfo(Symb, MODE_BID)) { isShort = true; } } else { isLong = true; isShort = true; } // Define Lots //double L=Lots_Optimized(); /* if (Use_TP_2) double Lot_for_Open=NormalizeDouble(L/2.0, Lot_Digits_After_Decimal); else Lot_for_Open=L; //if (Lot_for_Open0.0) Lock=Lock+(OrderStopLoss()-OrderOpenPrice()); if (OrderType()==1) if (OrderOpenPrice()-OrderStopLoss()>0.0) Lock=Lock+(OrderOpenPrice()-OrderStopLoss()); } } Lock=Lock/PipValue[0]; return(Lock); } //+------------------------------------------------------------------+ //| Profit by Magic | //+------------------------------------------------------------------+ double Profit() { double Prof=0; for (int i=0; i0.0) Prof=Prof+OrderProfit(); } return(Prof); } //+------------------------------------------------------------------+ //| Loss by Magic | //+------------------------------------------------------------------+ double Loss() { double Loss=0; for (int i=0; i=Pyr_Level_for_Open[Numb][Ord] && Pyr_Open[Numb][Ord]==false && Time_for_Cycle[Numb]!=iTime(Symb, 0, 0) ) { for (int j=0; j<10; j++) { //TP(0); if (Use_TP_2) double TP=Pyr_Level_for_TP_2[Numb][Ord]; else TP=Pyr_Level_for_TP[Numb][Ord]; while(!IsTradeAllowed())Sleep(5000); RefreshRates(); ticket=OrderSend(Symb, OP_BUY, Lots_Optimized(), MarketInfo(Symb, MODE_ASK), Slippage*PipValue[Numb], Pyr_Stop[Numb], TP, Commentary, Magic, 0, Green); //Alert("BUY_1, Ord=", Ord, " Ask=", MarketInfo(Symb, MODE_ASK), " STOP=", Pyr_Stop[Numb], // " TP=", Pyr_Level_for_TP[Numb][Ord]); if (GetLastError()==0) { //Time_for_Begin_Decay[Numb]=CurTime()+Minutes_Before_Decay*60; Pyr_TP_1_Complete[Numb][Ord]=false; Pyr_Open[Numb][Ord]=true; Ticket_of_first[Numb]=ticket; Time_for_Cycle[Numb]=iTime(Symb, 0, 0); break; } else { Sleep(1000); RefreshRates(); continue;} } } // SELL if ((Positive_Gap[Numb]==0 && !Trade_Direction_of_Gap) || (Positive_Gap[Numb]==1 && Trade_Direction_of_Gap) ) if (MarketInfo(Symb, MODE_BID)<=Pyr_Level_for_Open[Numb][Ord] && Pyr_Open[Numb][Ord]==false && Time_for_Cycle[Numb]!=iTime(Symb, 0, 0) ) { //TP(1); // 1st order for (j=0; j<10; j++) { if (Use_TP_2) TP=Pyr_Level_for_TP_2[Numb][Ord]; else TP=Pyr_Level_for_TP[Numb][Ord]; while(!IsTradeAllowed())Sleep(5000); RefreshRates(); ticket=OrderSend(Symb, OP_SELL, Lots_Optimized(), MarketInfo(Symb, MODE_BID), Slippage*PipValue[Numb], Pyr_Stop[Numb], TP, Commentary, Magic, 0, Red); //Alert("SELL_1, Ord=", Ord, " Bid=", MarketInfo(Symb, MODE_BID), " STOP=", Pyr_Stop[Numb], // " TP=", Pyr_Level_for_TP[Numb][Ord]); if (GetLastError()==0) { //Time_for_Begin_Decay[Numb]=CurTime()+Minutes_Before_Decay*60; Pyr_TP_1_Complete[Numb][Ord]=false; Pyr_Open[Numb][Ord]=true; Ticket_of_first[Numb]=ticket; Time_for_Cycle[Numb]=iTime(Symb, 0, 0); break; } else { Sleep(1000); RefreshRates(); continue;} } } } return; } //+------------------------------------------------------------------+ //| Define levels of pyramid | //+------------------------------------------------------------------+ void Def_Levels() { double TP, TP_2; double GSize=Gap_Size()-NormalizeDouble(MarketInfo(Symb, MODE_SPREAD)/PipValue[Numb], 1); if (GSize<0.0) GSize=Gap_Size(); // Measure TPs if (Fixed_TP) { TP=TP_Pips; TP_2=TP_2_Pips; } else { TP=GSize*TP_Percent_of_Gap*0.01; TP_2=GSize*TP_2_Percent_of_Gap*0.01; } // LEVELS FOR OPEN // PERCENT if (Use_Pyramid_Gap_Percent) { // BUY if ((Positive_Gap[Numb]==1 && !Trade_Direction_of_Gap) || (Positive_Gap[Numb]==0 && Trade_Direction_of_Gap)) for (int i=0; i<=Max_Levels; i++) { Pyr_Level_for_Open[Numb][i]=NormalizeDouble(MarketInfo(Symb, MODE_ASK)+ i*Percent_per_Level*0.01*GSize*PipValue[Numb]*Poin[Numb], MarketInfo(Symb, MODE_DIGITS)); Pyr_Level_for_TP[Numb][i]=NormalizeDouble(Pyr_Level_for_Open[Numb][i]+TP*PipValue[Numb]*Poin[Numb], MarketInfo(Symb, MODE_DIGITS)); Pyr_Level_for_TP_2[Numb][i]=NormalizeDouble(Pyr_Level_for_Open[Numb][i]+TP_2*PipValue[Numb]*Poin[Numb], MarketInfo(Symb, MODE_DIGITS)); Pyr_Open[Numb][i]=false; if (i==0) { if (Use_TP_2) Pyr_First_Order_TP[Numb]=Pyr_Level_for_TP[Numb][i]; else Pyr_First_Order_TP[Numb]=Pyr_Level_for_TP_2[Numb][i]; Pyr_Stop[Numb]=SL(0); } } // SELL if ((Positive_Gap[Numb]==0 && !Trade_Direction_of_Gap) || (Positive_Gap[Numb]==1 && Trade_Direction_of_Gap)) for (i=0; i<=Max_Levels; i++) { Pyr_Level_for_Open[Numb][i]=NormalizeDouble(MarketInfo(Symb, MODE_BID)- i*Percent_per_Level*0.01*GSize*PipValue[Numb]*Poin[Numb], MarketInfo(Symb, MODE_DIGITS)); Pyr_Level_for_TP[Numb][i]=NormalizeDouble(Pyr_Level_for_Open[Numb][i]-TP*PipValue[Numb]*Poin[Numb], MarketInfo(Symb, MODE_DIGITS)); Pyr_Level_for_TP_2[Numb][i]=NormalizeDouble(Pyr_Level_for_Open[Numb][i]-TP_2*PipValue[Numb]*Poin[Numb], MarketInfo(Symb, MODE_DIGITS)); Pyr_Open[Numb][i]=false; if (i==0) { if (Use_TP_2) Pyr_First_Order_TP[Numb]=Pyr_Level_for_TP[Numb][i]; else Pyr_First_Order_TP[Numb]=Pyr_Level_for_TP_2[Numb][i]; Pyr_Stop[Numb]=SL(1); } } } // FIX LEVELS else { // BUY if ((Positive_Gap[Numb]==1 && !Trade_Direction_of_Gap) || (Positive_Gap[Numb]==0 && Trade_Direction_of_Gap)) for (i=0; i<=Max_Levels; i++) { Pyr_Level_for_Open[Numb][i]=NormalizeDouble(MarketInfo(Symb, MODE_ASK)+ i*Next_Step_Pips*PipValue[Numb]*Poin[Numb], MarketInfo(Symb, MODE_DIGITS)); Pyr_Level_for_TP[Numb][i]=NormalizeDouble(Pyr_Level_for_Open[Numb][i]+TP*PipValue[Numb]*Poin[Numb], MarketInfo(Symb, MODE_DIGITS)); Pyr_Level_for_TP_2[Numb][i]=NormalizeDouble(Pyr_Level_for_Open[Numb][i]+TP_2*PipValue[Numb]*Poin[Numb], MarketInfo(Symb, MODE_DIGITS)); Pyr_Open[Numb][i]=false; if (i==0) { if (Use_TP_2) Pyr_First_Order_TP[Numb]=Pyr_Level_for_TP[Numb][i]; else Pyr_First_Order_TP[Numb]=Pyr_Level_for_TP_2[Numb][i]; Pyr_Stop[Numb]=SL(0); } } // SELL if ((Positive_Gap[Numb]==0 && !Trade_Direction_of_Gap) || (Positive_Gap[Numb]==1 && Trade_Direction_of_Gap)) for (i=0; i<=Max_Levels; i++) { Pyr_Level_for_Open[Numb][i]=NormalizeDouble(MarketInfo(Symb, MODE_BID)- i*Next_Step_Pips*PipValue[Numb]*Poin[Numb], MarketInfo(Symb, MODE_DIGITS)); Pyr_Level_for_TP[Numb][i]=NormalizeDouble(Pyr_Level_for_Open[Numb][i]-TP*PipValue[Numb]*Poin[Numb], MarketInfo(Symb, MODE_DIGITS)); Pyr_Level_for_TP_2[Numb][i]=NormalizeDouble(Pyr_Level_for_Open[Numb][i]-TP_2*PipValue[Numb]*Poin[Numb], MarketInfo(Symb, MODE_DIGITS)); Pyr_Open[Numb][i]=false; if (i==0) { if (Use_TP_2) Pyr_First_Order_TP[Numb]=Pyr_Level_for_TP[Numb][i]; else Pyr_First_Order_TP[Numb]=Pyr_Level_for_TP_2[Numb][i]; Pyr_Stop[Numb]=SL(1); } } } return; } //+------------------------------------------------------------------+ //| Move StopLoss for Pyramid | //+------------------------------------------------------------------+ void Pyr_Trail() { bool first=false; // Close orders by TP for (int i=0; i=Pyr_Level_for_TP[Numb][Ord] && !Pyr_TP_1_Complete[Numb][Ord]) { double Lot_for_CLose=NormalizeDouble(OrderLots()*0.01*TP_1_Percent_of_Lot, Lot_Digits_After_Decimal); if (Lot_for_CLose=Pyr_Level_for_TP_2[Numb][Ord] && Pyr_TP_1_Complete[Numb][Ord] && Use_TP_2) { Lot_for_CLose=OrderLots(); // 10 attempts to close order for (j=0; j<10; j++) { RefreshRates(); Closed=OrderClose(OrderTicket(), Lot_for_CLose, MarketInfo(Symb, MODE_BID), Slippage*PipValue[Numb]*Poin[Numb]); if (Closed==false) { Sleep(10000); continue; } break; } } */ } // for SELL if (OrderType()==OP_SELL && Use_TP_2) { RefreshRates(); // 1ST TP if (MarketInfo(Symb, MODE_ASK)<=Pyr_Level_for_TP[Numb][Ord] && !Pyr_TP_1_Complete[Numb][Ord]) { Lot_for_CLose=NormalizeDouble(OrderLots()*0.01*TP_1_Percent_of_Lot, Lot_Digits_After_Decimal); if (Lot_for_CLoseOrderOpenPrice()) && (MarketInfo(Symb, MODE_BID)-Pips_to_Trail*PipValue[Numb]*Poin[Numb]>OrderStopLoss())) for (j=0; j<10; j++) { while(!IsTradeAllowed())Sleep(5000); RefreshRates(); OrderModify(OrderTicket(), OrderOpenPrice(), MarketInfo(Symb, MODE_BID)-Pips_to_Trail*PipValue[Numb]*Poin[Numb], OrderTakeProfit(), 0, Green); if (GetLastError()==0) break; Sleep(1000); RefreshRates(); } // for SELL if (OrderType()==OP_SELL) if ((MarketInfo(Symb, MODE_ASK)+Pips_to_Trail*PipValue[Numb]*Poin[Numb]=Pyr_Level_for_Open[Numb][Ord] && Pyr_Open[Numb][Ord]==false) { for (int j=0; j<10; j++) { //TP(0); if (Use_TP_2) double TP=Pyr_Level_for_TP_2[Numb][Ord]; else TP=Pyr_Level_for_TP[Numb][Ord]; while(!IsTradeAllowed())Sleep(5000); RefreshRates(); OrderSend(Symb, OP_BUY, Pyr_Lots(), MarketInfo(Symb, MODE_ASK), Slippage*PipValue[Numb], Pyr_Stop[Numb], TP, Commentary, Magic, 0, Green); //Alert("BUY_1, Ord=", Ord, " Ask=", MarketInfo(Symb, MODE_ASK), " STOP=", Pyr_Stop[Numb], // " TP=", Pyr_Level_for_TP[Numb][Ord]); if (GetLastError()==0) { //Time_for_Begin_Decay[Numb]=CurTime()+Minutes_Before_Decay*60; Pyr_TP_1_Complete[Numb][Ord]=false; Pyr_Open[Numb][Ord]=true; break; } else { Sleep(1000); RefreshRates(); continue;} } } // SELL if ( Pyr_There_Are_Open(1) ) if (MarketInfo(Symb, MODE_BID)<=Pyr_Level_for_Open[Numb][Ord] && Pyr_Open[Numb][Ord]==false) { //TP(1); // 1st order for (j=0; j<10; j++) { if (Use_TP_2) TP=Pyr_Level_for_TP_2[Numb][Ord]; else TP=Pyr_Level_for_TP[Numb][Ord]; while(!IsTradeAllowed())Sleep(5000); RefreshRates(); OrderSend(Symb, OP_SELL, Pyr_Lots(), MarketInfo(Symb, MODE_BID), Slippage*PipValue[Numb], Pyr_Stop[Numb], TP, Commentary, Magic, 0, Red); //Alert("SELL_1, Ord=", Ord, " Bid=", MarketInfo(Symb, MODE_BID), " STOP=", Pyr_Stop[Numb], // " TP=", Pyr_Level_for_TP[Numb][Ord]); if (GetLastError()==0) { //Time_for_Begin_Decay[Numb]=CurTime()+Minutes_Before_Decay*60; Pyr_TP_1_Complete[Numb][Ord]=false; Pyr_Open[Numb][Ord]=true; break; } else { Sleep(1000); RefreshRates(); continue;} } } } return; }