// ------------------------------------------------------------------------------------------ // // S O L A G R A C I A E A // // by fosgate_r © January 2009 // // URL : http://forexfreeea.blogspot.com/ // // Time Information by November 28, 2007 : SBFX = Alpari+1 or IBFX+2 // // // // Best Setting with Alpari July 2004 - November 2007 : // // EURUSD : HourOpen = 15; MinutesOpen = 0; HourClose = 7; MinutesClose = 0; // // GBPUSD : HourOpen = 12; MinutesOpen = 0; HourClose = 9; MinutesClose = 0; // // CHFUSD : HourOpen = ; MinutesOpen = 0; HourClose = ; MinutesClose = 0; // // USDJPY : HourOpen = ; MinutesOpen = 0; HourClose = ; MinutesClose = 0; // // ------------------------------------------------------------------------------------------ // #property copyright "© January 2009 fosgate_r" #property link "http://forexfreeea.blogspot.com/" #include #include // ------------------------------------------------------------------------------------------ // // E X T E R N A L V A R I A B L E S // // ------------------------------------------------------------------------------------------ // extern int HourOpen = 16; // Range : 0 - 23 SBFX !!! extern int MinutesOpen = 0; // Range : 0 - 59 extern int HourClose = 8; // Range : 0 - 23 extern int MinutesClose = 0; // Range : 0 - 59 extern bool UseCloseHour = true; extern int Percent = 10; // Range : 0 - 100 extern int StopPercent = 30; // Range : 0 - 300 extern double MMRisk = 0.1; extern string Space = ""; extern string Expert_Name = "---- SolaGracia EA ----"; extern bool StopAfterNoTrades = False; extern bool IsIBFX_Mini_Account = False; extern int Magic = 081006; extern int slippage = 6; extern string Main_Parameters = "Volume & Trade Method"; extern double Lots = 1; extern int TrailingStop = 0; extern int TakeProfit = 0; extern bool MM = true; extern double LossMax = 3200; // Maximum Loss by 1 Lot extern bool UseDifBUY_SELL = False; // Percent for BUY and SELL are different extern int BuyPercent = 25; // Range : 0 - 100 extern int SellPercent = 50; // Range : 0 - 100 extern int TradePeriod = 6; // Max days in trade extern int BreakEven = 0; // BreakEven Level in pips extern int BreakEvenGap = 0; // Pips when BreakEven will be reached extern string Trade = "Trade Days of Week"; extern int Monday = 1; extern int Tuesday = 1; extern int Wednesday = 1; extern int Thursday = 1; extern int Friday = 1; // ------------------------------------------------------------------------------------------ // // I N T E R N A L V A R I A B L E S // // ------------------------------------------------------------------------------------------ // string EAname = "SolaGracia"; int retry_attempts = 10; int c=0,i,cnt=0,ticket,mode=0,digit=0,wenum=0,OrderOpenDay,DaysInTrade,Kz; int err,lotsize,Slippage,StopLevel,OpenTime,CloseTime,EndTime; double high=0,low=0, close=0, open=0, range=0, spread=0, Profit=0; double buy,sell,smin=0, smax=0, BuyStop=0, SellStop=0, Lotsi=0, rates_h1[][6]; double Spread,WorstSpread,NormalSpread,PipValue,TickSize,SellPrice,BuyPrice; bool BuyInTrade=false, SellInTrade=false; datetime StartTime, prevwe=0, Today, PrevDay; string TStatus; // ------------------------------------------------------------------------------------------ // // I N I T I A L I S A T I O N // // ------------------------------------------------------------------------------------------ // int init() { NormalSpread = (Ask-Bid) / Point; CheckTerminalStatus(); return(0); } // ------------------------------------------------------------------------------------------ // // D E - I N I T I A L I S A T I O N // // ------------------------------------------------------------------------------------------ // int deinit() { return(0); } // ------------------------------------------------------------------------------------------ // // M A I N P R O C E D U R E // // ------------------------------------------------------------------------------------------ // int start() { // Initialize Basic Variables // ========================== if (Bars<1) {Print("Not enough bars for this strategy"); return(0);} if (BreakEven>=0 && BreakEvenWorstSpread) { WorstSpread = Spread; } PipValue = MarketInfo(Symbol(),16); TickSize = MarketInfo(Symbol(),17)/Point; StopLevel = MarketInfo(Symbol(),14); if (Slippage=MinutesClose) { CloseOrdbyTime2(); PendOrdDel(); } // OpenTime 1 Hour Trading Period // ============================== if (CurTime()>=OpenTime && CurTime()<=OpenTime+3600) { if (OrderOpenDate() < StrToTime(TimeToStr(OpenTime,TIME_DATE))) { if (ScanTrades()>0 && !BuyInTrade && !SellInTrade) PendOrdDel(); Today = StrToTime(TimeToStr( OpenTime,TIME_DATE)); if (Today != PrevDay) DaysInTrade = DaysInTrade+1; if (TradePeriod > 0 ) CloseOrdbyTime(); } // Check for a New Trade // ===================== if (ScanTrades()<1 && Today!=PrevDay) { spread = MarketInfo(Symbol(),MODE_SPREAD)*Point; digit = MarketInfo(Symbol(),MODE_DIGITS); Lotsi = MoneyManagement ( MM, Lots, MMRisk, LossMax); ArrayCopyRates(rates_h1, Symbol(), PERIOD_H1); open = rates_h1[0][1]; high = 0; low = 10000000; for (i=24;i>=1;i--) { high = MathMax( high, rates_h1[i][3]); low = MathMin( low , rates_h1[i][2]); } range = high - low; sell = open - range*SellPercent/100.0 - spread; buy = open + range*BuyPercent/100.0 + spread; if (Monday == 1) if(DayOfWeek()==1) Trade(); if (Tuesday == 1) if(DayOfWeek()==2) Trade(); if (Wednesday == 1) if(DayOfWeek()==3) Trade(); if (Thursday == 1) if(DayOfWeek()==4) Trade(); if (Friday == 1) if(DayOfWeek()==5) Trade(); DaysInTrade = 0; } } ExtraOrdDel(); if (BreakEven > 0) BreakEvenStop(); if (TrailingStop > 0) TrailStop(); PrevDay = Today; // Print Comments // ============== if (!IsTesting() || IsVisualMode()) { string CLOSETIME = "\nOrders CloseTime = " + HourOpen + ":" + MinutesOpen; if (UseCloseHour) CLOSETIME = "\nOrders CloseTime = " + HourClose + ":" + MinutesClose; Comment ( "Account Company = ", AccountCompany(), "\nIsIBFX_Mini_Account = ",IsIBFX_Mini_Account, "\nSpread : Normal = ",NormalSpread," - Actual = ",Spread," - Worst = ",WorstSpread, "\n",EAname," EA Magic Number = ",Magic, "\n\n",Symbol()," : 1 Full Lot or Contract Tick Value = $ ",PipValue, "\n",Symbol()," : TickSize = ",TickSize," - StopLevel = ", StopLevel," - Slippage Allowed = ",Slippage, "\n\nOrders StartTime = ",HourOpen,":",MinutesOpen,CLOSETIME, "\n\nPrevius 24H High = ",DoubleToStr(high,digit), "\nPrevius 24H Low = ",DoubleToStr(low,digit), "\nPrevius 24H Range = ",range, "\n\nDaily Range Buy Stop Percent = ",BuyPercent," %", "\nDaily Range Sell Stop Percent = ",SellPercent," %", "\nDaily Range StopLoss Percent = ",StopPercent," %", "\nMMRisk = ",MMRisk," - LossMax = ",LossMax, "\n\nBuy Order = ",DoubleToStr(BuyPrice,digit), "\nSell Order = ",DoubleToStr(SellPrice,digit), "\nNext Order Lot Size = ",Lotsi, "\nTick Value x Order Lot Size = $ ",PipValue*Lotsi, "\nOpen Trades = ",ScanTrades(), "\n\nTerminal Status = ", TStatus ); } return(0); } // ------------------------------------------------------------------------------------------ // // M O N E Y M A N A G E M E N T // // ------------------------------------------------------------------------------------------ // double MoneyManagement ( bool flag, double Lots, double risk, double maxloss) { double MinLots = NormalizeDouble(MarketInfo(Symbol(),23),2); double LotStep = NormalizeDouble(MarketInfo(Symbol(),24),2); double MaxLots = NormalizeDouble(MarketInfo(Symbol(),25),2); int LotDigit; if (LotStep==1) { LotDigit = 0; } if (LotStep==0.1) { LotDigit = 1; } if (LotStep==0.01) { LotDigit = 2; } double Lotsi = Lots; double MAXloss = maxloss; if (IsIBFX_Mini_Account) { MAXloss = maxloss/10; } if (flag) Lotsi = NormalizeDouble(Lots*AccountEquity()*risk/MAXloss,LotDigit); if (LotsiMaxLots) Lotsi = MaxLots; return(Lotsi); } // ------------------------------------------------------------------------------------------ // // T R A D E F U N C T I O N // // ------------------------------------------------------------------------------------------ // void Trade() { if (!StopAfterNoTrades && IsTradeAllowed() && IsConnected() && !IsStopped() && IsExpertEnabled() && !IsTradeContextBusy()) { double ema50 = iMA(NULL,PERIOD_D1,50,0,MODE_EMA,PRICE_CLOSE,1); double dayOpen = iOpen(NULL,PERIOD_D1,0); BuyOrdOpen(); SellOrdOpen(); } } // ------------------------------------------------------------------------------------------ // // D E L E T E P E N D I N G O R D E R S // // ------------------------------------------------------------------------------------------ // void PendOrdDel() { bool result; int total = OrdersTotal(); for (int cnt=total-1;cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS,MODE_TRADES); if ( OrderSymbol()==Symbol() && OrderMagicNumber()==Magic ) { mode = OrderType(); result = false; switch(mode) { case OP_BUYSTOP : { result = OrderDelete( OrderTicket() ); if (!result) { Print("BUYSTOP: OrderDelete failed with error #",GetLastError()); return(0); } break; } case OP_SELLSTOP : { result = OrderDelete( OrderTicket() ); if (!result) { Print("SELLSTOP: OrderDelete failed with error #",GetLastError()); return(0); } break; } } } } } // ------------------------------------------------------------------------------------------ // // C L O S E O R D E R B Y T I M E 1 // // ------------------------------------------------------------------------------------------ // void CloseOrdbyTime() { int total = OrdersTotal(); for (cnt=0;cnt= TradePeriod || OrderProfit()>0) { if (mode==OP_BUY ) OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Yellow); if (mode==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,White); } } } } // ------------------------------------------------------------------------------------------ // // C L O S E O R D E R B Y T I M E 2 // // ------------------------------------------------------------------------------------------ // void CloseOrdbyTime2() { int total = OrdersTotal(); for (cnt=0;cnt0) { if (mode==OP_BUY ) OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Yellow); if (mode==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,White); } } } } // ------------------------------------------------------------------------------------------ // // T R A I L I N G S T O P // // ------------------------------------------------------------------------------------------ // void TrailStop() { bool result; for (cnt=0;cnt NormalizeDouble(OrderStopLoss(),Digits)) { result = OrderModify(OrderTicket(),OrderOpenPrice(),BuyStop, OrderTakeProfit(),0,LightGreen); if (!result) {Print("BUY: OrderModify failed with error #",GetLastError());} return(0); } } } if (mode==OP_SELL) { SellStop = NormalizeDouble(Ask + Point * TrailingStop,Digits); if( OrderOpenPrice() > SellStop) { if( NormalizeDouble(OrderStopLoss(), Digits) > SellStop || OrderStopLoss() == 0 ) { result = OrderModify(OrderTicket(),OrderOpenPrice(),SellStop, OrderTakeProfit(),0,DarkOrange); if (!result) {Print("SELL: OrderModify failed with error #",GetLastError());} return(0); } } } } } } // ------------------------------------------------------------------------------------------ // // O P E N S E L L O R D E R // // ------------------------------------------------------------------------------------------ // void SellOrdOpen() { if (!StopAfterNoTrades) { for (c=0;c 0) SellStop = SellPrice + StopPercent*range/100.0; else SellStop = 0; if (TakeProfit > 0) Profit = SellPrice - TakeProfit*Point; else Profit = 0; if (TickSize>1) { SellPrice = NormalizeDouble(SellPrice,0); SellStop = NormalizeDouble(SellStop,0); } if (SellPrice>Bid-(StopLevel*Point+TickSize*Point)) { SellPrice = Bid-(StopLevel*Point+TickSize*Point); } if (SellStop 0) BuyStop = BuyPrice - StopPercent*range/100.0; else BuyStop = 0; if (TakeProfit > 0) Profit = BuyPrice + TakeProfit*Point; else Profit = 0; if (TickSize>1) { BuyPrice = NormalizeDouble(BuyPrice,0); BuyStop = NormalizeDouble(BuyStop,0); } if (BuyPriceBuyPrice-(StopLevel*Point+TickSize*Point)) { BuyStop = BuyPrice-(StopLevel*Point+TickSize*Point); } ticket = OrderSend(Symbol(),OP_BUYSTOP,Lotsi,NormalizeDouble(BuyPrice ,digit), Slippage,NormalizeDouble(BuyStop,digit),Profit,EAname+Magic,Magic,0,Blue); OrderOpenDay = DayOfWeek(); BuyInTrade = false; if(ticket<=0) { err = GetLastError(); Print("Errors opening BUYSTOP order"); Print(ErrorDescription(err),", error ",err); Sleep(5000);continue; } else{break;} } } } // ------------------------------------------------------------------------------------------ // // D E L E T E E X T R A O R D E R // // ------------------------------------------------------------------------------------------ // void ExtraOrdDel() { int total = OrdersTotal(); for (cnt=0;cnt=OP_BUY && OrderMagicNumber() == Magic) numords++; } return(numords); } // ------------------------------------------------------------------------------------------ // // O R D E R O P E N D A T E // // ------------------------------------------------------------------------------------------ // datetime OrderOpenDate() { datetime date; int total = OrdersTotal(); for(cnt=0; cnt=OP_BUY && OrderMagicNumber() == Magic) date = StrToTime(TimeToStr(OrderOpenTime(),TIME_DATE)); } return(date); } // ------------------------------------------------------------------------------------------ // // B R E A K E V E N S T O P // // ------------------------------------------------------------------------------------------ // void BreakEvenStop() { double StopLoss; for (cnt=0;cnt BreakEven*Point) { Kz = MathFloor((Bid-OrderOpenPrice())/(BreakEven*Point)); if (Bid-OrderOpenPrice() > Kz*BreakEven*Point) { BuyStop = OrderOpenPrice()+((Kz-1)*BreakEven+BreakEvenGap)*Point; if ( OrderStopLoss()==0 ) StopLoss = OrderOpenPrice(); else StopLoss = OrderStopLoss(); if (BuyStop > StopLoss ) { OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(BuyStop, digit), OrderTakeProfit(),0,LightBlue); return(0); } } } } if (mode==OP_SELL) { if (OrderOpenPrice()-Ask > BreakEven*Point) { Kz = MathFloor((OrderOpenPrice()-Ask)/(BreakEven*Point)); if (OrderOpenPrice()-Ask > Kz*BreakEven*Point) { SellStop = OrderOpenPrice()-((Kz-1)*BreakEven+BreakEvenGap)*Point; if ( OrderStopLoss() == 0 ) StopLoss = OrderOpenPrice(); else StopLoss = OrderStopLoss(); if ( SellStop < StopLoss ) { OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(SellStop, digit), OrderTakeProfit(),0,Orange); return(0); } } } } } } } // ------------------------------------------------------------------------------------------ // // C H E C K T E R M I N A L S T A T U S // // ------------------------------------------------------------------------------------------ // void CheckTerminalStatus() { TStatus = ""; if (!IsConnected()) {TStatus = "Terminal is not conneted to Trade Server !!!";} if (IsStopped()) {TStatus = "Automatic Trading is Stopped!!!";} if (!IsTradeAllowed()) {TStatus = "Trading is Disabled at our Trade Server !!!";} if (!IsExpertEnabled()) {TStatus = "Expert Advisors is Disabled !!!";} if (IsTradeContextBusy()){TStatus = "Trade Contest is Busy !!!";} if (StopAfterNoTrades) {TStatus = "StopAfterNoTrades = True !!!";} } // ------------------------------------------------------------------------------------------ // // E N D O F E X P E R T A D V I S O R // // ------------------------------------------------------------------------------------------ //