// ------------------------------------------------------------------------------------------ // // L I N E X C U T O R S E X P E R T A D V I S O R // // Trades Executor based on 2 Lines Approach // // by fosgate_r © January 2009 // // URL : http://forexfreeea.blogspot.com/ // // ------------------------------------------------------------------------------------------ // #property copyright "© January 2009 fosgate_r" #property link "http://forexfreeea.blogspot.com/" #include <stdlib.mqh> // ------------------------------------------------------------------------------------------ // // E X T E R N A L V A R I A B L E S // // ------------------------------------------------------------------------------------------ // extern bool TradeInside = true; // Trading inside Trendlines : Bouncing Trade extern bool TradeOutside = true; // Trading outside Trendlines : Breaking Trade extern string UpperLineName = "UPPERline"; extern string LowerLineName = "LOWERline"; extern double LotSize = 0.1; extern double TakeProfit = 50; extern double StopLoss = 30; extern double PipLimit = 3; // Distance from Trendline extern double PipWide = 3; // Margin allowed to open trade from PipLimit extern bool ShowComment = true; extern int Magic = 20090206; extern string EAcomment = "LinexcutorsEA"; // ------------------------------------------------------------------------------------------ // // I N T E R N A L V A R I A B L E S // // ------------------------------------------------------------------------------------------ // double I_LineLevel, I_Hlimit, I_Llimit, I_Hlimit1, I_Llimit1; double II_LineLevel, II_Hlimit, II_Llimit, II_Hlimit1, II_Llimit1; string strtmp; // ------------------------------------------------------------------------------------------ // // I N I T I A L I S A T I O N // // ------------------------------------------------------------------------------------------ // int init() { Comment(""); return(0); } // ------------------------------------------------------------------------------------------ // // D E - I N I T I A L I S A T I O N // // ------------------------------------------------------------------------------------------ // int deinit() { Comment(""); return(0); } // ------------------------------------------------------------------------------------------ // // M A I N P R O C E D U R E // // ------------------------------------------------------------------------------------------ // int start() { // Check for Trendline and Determine the Limits // ============================================ if (ObjectFind(UpperLineName)<0) I_LineLevel = -1; else I_LineLevel = ObjectGetValueByShift(UpperLineName,0); I_Hlimit=0; I_Llimit=0; if (I_LineLevel>0) { I_Hlimit = I_LineLevel + (PipLimit*Point); I_Hlimit1 = I_Hlimit + (PipWide *Point); I_Llimit = I_LineLevel - (PipLimit*Point); I_Llimit1 = I_Llimit - (PipWide *Point); } if (ObjectFind(LowerLineName)<0) II_LineLevel = -1; else II_LineLevel = ObjectGetValueByShift(LowerLineName,0); II_Hlimit=0; II_Llimit=0; if (II_LineLevel>0) { II_Hlimit = II_LineLevel + (PipLimit*Point); II_Hlimit1 = II_Hlimit + (PipWide *Point); II_Llimit = II_LineLevel - (PipLimit*Point); II_Llimit1 = II_Llimit - (PipWide *Point); } // Trade Decision when both Trendlines are Ready // ============================================= if (I_LineLevel>0 && II_LineLevel>0) { if (TradeOutside==True) { // Price is breaking/leaving UP Linexcutor1 if (Close[0]>I_Hlimit && Close[0]<I_Hlimit1) { Alert("Price Breaking UP !!!"); if (OpenOrd(Magic)==0) Buy(Magic,LotSize,TakeProfit,StopLoss); else if (OpenLast(Magic)==1) { CloseOrders(Magic); Sleep(3000); Buy(Magic,LotSize,TakeProfit,StopLoss); } } // Price is breaking/leaving DOWN Linexcutor2 if (Close[0]<II_Llimit && Close[0]>II_Llimit1) { Alert("Price Breaking DOWN !!!"); if (OpenOrd(Magic)==0) Sell(Magic,LotSize,TakeProfit,StopLoss); else if (OpenLast(Magic)==0) { CloseOrders(Magic); Sleep(3000); Sell(Magic,LotSize,TakeProfit,StopLoss); } } } if (TradeInside==True) { // Price is bouncing Linexcutor1 if (Close[0]<I_Llimit && Close[0]>I_Llimit1) { Alert("Price Bouncing UPPERline !!!"); if (OpenOrd(Magic)==0) Sell(Magic,LotSize,TakeProfit,StopLoss); else if (OpenLast(Magic)==0) { CloseOrders(Magic); Sleep(3000); Sell(Magic,LotSize,TakeProfit,StopLoss); } } // Price is bouncing Linexcutor2 if (Close[0]>II_Hlimit && Close[0]<II_Hlimit1) { Alert("Price Bouncing LOWERline !!!"); if (OpenOrd(Magic)==0) Buy(Magic,LotSize,TakeProfit,StopLoss); else if (OpenLast(Magic)==1) { CloseOrders(Magic); Sleep(3000); Buy(Magic,LotSize,TakeProfit,StopLoss); } } } } // Comments // ======== strtmp = "\nLINEXCUTORS EA - © January 2009 fosgate_r "; if (I_LineLevel<0) strtmp = strtmp + "\nTrendline " +UpperLineName+ " Not Available !!!"; if (II_LineLevel<0) strtmp = strtmp + "\nTrendline " +LowerLineName+ " Not Available !!!"; if (I_LineLevel>0 && II_LineLevel>0) strtmp = strtmp + "\n" +UpperLineName+ " : " + DoubleToStr(I_LineLevel,Digits) + " [" + DoubleToStr(I_Llimit,Digits) + "-" + DoubleToStr(I_Hlimit,Digits) + "] " + "\n" +LowerLineName+ " : " + DoubleToStr(II_LineLevel,Digits) + " [" + DoubleToStr(II_Llimit,Digits) + "-" + DoubleToStr(II_Hlimit,Digits) + "] " + "\nProfit: $ " + DoubleToStr(CalcProfit(Magic),2) + " - " + CalcPips(Magic) + " pips - " + "Last Order: " + OpenLast(Magic) + " - Profit: $ " + DoubleToStr(CalcProfit(Magic),2) + " - " + CalcPips(Magic) + " pips - " + "Last Order: " + OpenLast(Magic) + "\nCurrent Time : " +TimeToStr(TimeCurrent()); if (ShowComment) Comment(strtmp); return(0); } // ------------------------------------------------------------------------------------------ // // O R D E R S S T A T U S // // ------------------------------------------------------------------------------------------ // int OpenLast(int mgc) { int last=-1; for (int i = 0; i < OrdersTotal(); i++) { OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if (OrderMagicNumber() == mgc && OrderSymbol()==Symbol() && OrderType()<=1) last = OrderType(); } return(last); } int OpenOrd(int mgc) { int ord=0; for (int i = 0; i < OrdersTotal(); i++) { OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if (OrderMagicNumber() == mgc && OrderSymbol()==Symbol() && OrderType()<=1) ord++; } return(ord); } // ------------------------------------------------------------------------------------------ // // C A L C U L A T E P I P S // // ------------------------------------------------------------------------------------------ // int CalcPips(int mgc) { int pips = 0; double p = 0; for (int i=0; i<OrdersTotal(); i++) { OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if (OrderMagicNumber()==mgc && OrderSymbol()==Symbol()) { if (OrderType()==OP_BUY) { p = (Ask - OrderOpenPrice()) / Point; pips = pips + NormalizeDouble(p,0); } if (OrderType()==OP_SELL) { p = (OrderOpenPrice() - Bid) / Point; pips = pips + NormalizeDouble(p,0); } } } return(pips); } // ------------------------------------------------------------------------------------------ // // C A L C U L A T E P R O F I T // // ------------------------------------------------------------------------------------------ // double CalcProfit(int mgc) { double profit = 0; for (int i=0; i<OrdersTotal(); i++) { OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if (OrderMagicNumber()==mgc && OrderSymbol()==Symbol()) profit = profit + OrderProfit() + OrderSwap(); } return(profit); } // ------------------------------------------------------------------------------------------ // // B U Y F U N C T I O N // // ------------------------------------------------------------------------------------------ // int Buy(int mgc, double lot, double Tpft, double Slos) { int Ticket = 0, err = 0; double TP = 0, SL = 0; if (Tpft>0) TP = NormalizeDouble(Ask + (Tpft*Point), Digits); if (Slos>0) SL = NormalizeDouble(Ask - (Slos*Point), Digits); if (IsTradeAllowed() == true) { for(int c=0; c<5; c++) { RefreshRates(); Ticket = OrderSend(Symbol(),OP_BUY,lot,Ask,3,SL,TP,EAcomment, mgc,0,Blue); err = GetLastError(); if(err==0) break; else { Print("Errors opening BUY order"); Print(ErrorDescription(err),", error ",err); if(err==4 || err==137 ||err==146 || err==136) {Sleep(5000);continue;} else break; //normal error // Busy errors } } } return(Ticket); } // ------------------------------------------------------------------------------------------ // // S E L L F U N C T I O N // // ------------------------------------------------------------------------------------------ // int Sell(int mgc, double lot, double Tpft, double Slos) { int Ticket = 0, err = 0; double TP = 0, SL = 0; if (Tpft>0) TP = NormalizeDouble(Bid - (Tpft*Point), Digits); if (Slos>0) SL = NormalizeDouble(Bid + (Slos*Point), Digits); if (IsTradeAllowed() == true) { for(int c=0; c<5; c++) { RefreshRates(); Ticket = OrderSend(Symbol(),OP_SELL,lot,Bid,3,SL,TP,EAcomment,mgc,0,Red); err = GetLastError(); if(err==0) break; else { Print("Errors opening SELL order"); Print(ErrorDescription(err),", error ",err); if(err==4 || err==137 ||err==146 || err==136) {Sleep(5000);continue;} else break; //normal error // Busy errors } } } return(Ticket); } // ------------------------------------------------------------------------------------------ // // C L O S E O R D E R S // // ------------------------------------------------------------------------------------------ // void CloseOrders(int mgc) { int cnt,c,total=0,ticket=0,err=0; if (IsTradeAllowed() == true) { total = OrdersTotal(); for(cnt=total-1;cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderMagicNumber()==mgc && OrderSymbol()==Symbol()) { switch(OrderType()) { case OP_BUY : for(c=0;c<5;c++) { RefreshRates(); OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Yellow); err = GetLastError(); if (err==0) break; else { Print("Errors Closing BUY order"); Print(ErrorDescription(err),", error ",err); if(err==0 || err==4 || err==136 || err==137 || err==138 || err==146) { Sleep(5000); continue; } // Busy errors } } break; case OP_SELL : for(c=0;c<5;c++) { RefreshRates(); OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Yellow); err = GetLastError(); if (err==0) break; else { Print("Errors Closing SELL order"); Print(ErrorDescription(err),", error ",err); if(err==0 || err==4 || err==136 || err==137 || err==138 || err==146) { Sleep(5000); continue; } // Busy errors } } break; } // end of switch } // end of if } // end of for } return(0); } // ------------------------------------------------------------------------------------------ // // E N D O F E X P E R T A D V I S O R // // ------------------------------------------------------------------------------------------ //
Sample
Analysis
Market Information Used:
Series array that contains close prices for each bar
Indicator Curves created:
Indicators Used:
Custom Indicators Used:
Order Management characteristics:
Checks for the total of open orders
It automatically opens orders when conditions are reached
It Closes Orders by itself
Other Features:
It issuies visual alerts to the screen