#property copyright "Copyright © 2010, Me" #property link "http://www.me.com/" #include #include #include //+------------------------------------------------------------------+ //| Global Variables / Includes | //+------------------------------------------------------------------+ datetime CurrTime = 0; datetime PrevTime = 0; string Sym = ""; int TimeFrame = 0; int Shift = 1; int SymDigits = 5; double SymPoints = 0.0001; //+------------------------------------------------------------------+ //| Expert User Inputs | //+------------------------------------------------------------------+ extern bool Trade = false; extern string lotsize = "Lot Calculations"; extern double Risk = 10; extern string accounttypes = "0 if Normal Lots, 1 for mini lots, 2 for micro lots"; extern int AccountType = 0; extern double MinLots = 0.1; extern double MaxLots = 50; extern double TP = 30; extern double StopLoss = 60; extern int MagicNumber = 1235; extern int Slippage = 3; extern string th = "TradeHours to True for Time Filter"; extern bool TradeHours = false; extern int StartTime = 7; extern int EndTime = 12; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int init() { Sym = Symbol(); TimeFrame = Period(); SymPoints = MarketInfo( Sym, MODE_POINT ); SymDigits = MarketInfo( Sym, MODE_DIGITS ); //--- if( SymPoints == 0.001 ) { SymPoints = 0.01; SymDigits = 3; } else if( SymPoints == 0.00001 ) { SymPoints = 0.0001; SymDigits = 5; } //---- return(0); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { return(0); } //+------------------------------------------------------------------+ //| Expert start function | //+------------------------------------------------------------------+ int start() { int RealTime = 0; CurrTime = iTime(Sym, TimeFrame, 1 ); if( CurrTime == PrevTime ) { return(0); } //---- Update Vars PrevTime = CurrTime; RealTime = 1; //---- Need to chek for a new Signal? //---- Indicator 1 Values double Indicator1CurrentValue = iCustom(NULL,0,"Gann_HiLo_Activator_v2",10,0,0+RealTime); double Indicator1PreviousValue = iCustom(NULL,0,"Gann_HiLo_Activator_v2",10,0,1+RealTime); double MA1 = iMA(NULL, 0, 13, 0, 1, PRICE_CLOSE, 0); double MA2 = iMA(NULL, 0, 21, 0, 1, PRICE_CLOSE, 0); double MA3 = iMA(NULL, 0, 55, 0, 1, PRICE_CLOSE, 0); //---- Gann Cross System if( Bid > Indicator1CurrentValue && Open[1] <= Indicator1PreviousValue && Bid>MA1 && Bid>MA2 && Bid>MA3 && TimeFilter()==0) { string buy_alerter = StringConcatenate(Symbol()," ",Period()," Gann Value: ",Indicator1CurrentValue); SendMail("Gann: BUY",""+buy_alerter); Alert("BUY ",Symbol(),Period()," Gann Value: ",Indicator1CurrentValue); if(Trade) EnterLong(Sym, CalculateLots(), FibRetracement(1), getMagic()); } else if( Bid < Indicator1CurrentValue && Open[1] >= Indicator1PreviousValue && Bid0) { int total,cnt; total=OrdersTotal(); for(cnt=0;cnt(OrderOpenPrice()-OrderStopLoss()+5*Point)) { OrderModify(cnt,OrderOpenPrice(),Bid-Point*5,OrderTakeProfit(),0,Green); return(0); } } if(OrderType()==OP_SELL) { if((OrderOpenPrice()-Ask)>(OrderStopLoss()-OrderOpenPrice()+5*Point)) { OrderModify(cnt,OrderOpenPrice(),Ask+Point*5,OrderTakeProfit(),0,Red); return(0); } } } } } //---- return(0); } //+------------------------------------------------------------------+ //| Expert Custom Functions | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Calculate Stop Loss (based on Recent Hi/Low) | //+------------------------------------------------------------------+ double FibRetracement(int trend) { //Check current price //Go back max of 15 bars for min/max double sellmin, // Minimal price buymax; // Maximal price double mypoint =MarketInfo(Symbol(),MODE_POINT); int Quant_Bars=15; string numberofbarstotest=StringConcatenate("Number of bars to test ",Quant_Bars); int Ind_max =ArrayMaximum(High,Quant_Bars,1);// Bar index of max. price int Ind_min =ArrayMinimum(Low, Quant_Bars,1);// Bar index of min. price buymax=High[Ind_max]; // Desired max. price sellmin=Low[Ind_min]; // Desired min. price if(trend==1) { //subtract bar price from current price and get pips double bdifference=(buymax - Ask)/mypoint; if(Ask>=buymax) { return (50); } else { return (bdifference); } } if(trend==2) { int sdifference=(Bid - sellmin)/mypoint; if(Bid<=sellmin) { return (50); } else { return (sdifference); } } return (0); } //+------------------------------------------------------------------+ //| Time Filter (for smaller timeframes) | //+------------------------------------------------------------------+ int TimeFilter() { if( TradeHours ) { if((Hour()>=0&&Hour()<(StartTime-1))||(Hour()>=(EndTime)&&Hour()<24)) return(1); } return(0); } //+------------------------------------------------------------------+ //| Calculate Lots Based on Percentage | //+------------------------------------------------------------------+ double CalculateLots() { double MLots, div; div = 1; switch (AccountType) { case 0 : div = 1; // Standard break; case 1 : div = 10; // Mini break; case 2 : div = 100; // Micro break; } MLots=AccountBalance()*Risk/100000/div; if (MLots<0.01) MLots=0.01; if (MLots>50) MLots=50; return(MLots); } //+------------------------------------------------------------------+ //| Iterate Magic Number | //+------------------------------------------------------------------+ double getMagic() { MathSrand(TimeLocal()); double Magic=MathRand(); return(Magic); } //+------------------------------------------------------------------+ //| CountAll() | //+------------------------------------------------------------------+ int CountAll( string Symbole, int Magic ) { //---- int count = 0; for (int i = OrdersTotal() - 1; i >= 0; i--) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if ( OrderMagicNumber() != Magic ) continue; if ( OrderSymbol() != Symbole ) continue; if ( OrderType() == OP_BUY ) { count++; } else if ( OrderType() == OP_SELL ) { count++; } } //---- return(count); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Calculate Stop Long | //+------------------------------------------------------------------+ double StopLong(double price,double stop,double point,double SymDgts ) { if(stop==0) { return(0); } else { return(NormalizeDouble( price-(stop*point),SymDgts)); } } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Calculate Stop Short | //+------------------------------------------------------------------+ double StopShrt(double price,double stop,double point,double SymDgts ) { if(stop==0) { return(0); } else { return(NormalizeDouble( price+(stop*point),SymDgts)); } } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Calculate Profit Target Long | //+------------------------------------------------------------------+ double TakeLong(double price,double take,double point,double SymDgts ) { if(take==0) { return(0);} else { return(NormalizeDouble( price+(take*point),SymDgts));} } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Calculate Profit Target Long | //+------------------------------------------------------------------+ double TakeShrt(double price,double take,double point,double SymDgts ) { if(take==0) { return(0);} else { return(NormalizeDouble( price-(take*point),SymDgts));} } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Place Long Order | //+------------------------------------------------------------------+ int EnterLong( string FinalSymbol, double FinalLots, double SL, double Magic ) { int Ticket = -1; int err = 0; bool OrderLoop = False; int TryCount = 0; while( !OrderLoop ) { while( IsTradeContextBusy() ) { Sleep( 7 ); } RefreshRates(); double SymAsk = NormalizeDouble( MarketInfo( FinalSymbol, MODE_ASK ), SymDigits ); double SymBid = NormalizeDouble( MarketInfo( FinalSymbol, MODE_BID ), SymDigits ); Ticket = OrderSend( FinalSymbol, OP_BUY, FinalLots, SymAsk, Slippage, Ask-SL*Point, Ask+TP*2*Point, DoubleToStr(Magic,0), Magic, 0, Green ); int Err=GetLastError(); switch (Err) { //---- Success case ERR_NO_ERROR: OrderLoop = true; break; //---- Retry Error case ERR_SERVER_BUSY: case ERR_NO_CONNECTION: case ERR_INVALID_PRICE: case ERR_OFF_QUOTES: case ERR_BROKER_BUSY: case ERR_TRADE_CONTEXT_BUSY: TryCount++; break; case ERR_PRICE_CHANGED: case ERR_REQUOTE: continue; //---- Fatal known Error case ERR_INVALID_STOPS: OrderLoop = true; Print( "Invalid Stops" ); break; case ERR_INVALID_TRADE_VOLUME: OrderLoop = true; Print( "Invalid Lots" ); break; case ERR_MARKET_CLOSED: OrderLoop = true; Print( "Market Close" ); break; case ERR_TRADE_DISABLED: OrderLoop = true; Print( "Trades Disabled" ); break; case ERR_NOT_ENOUGH_MONEY: OrderLoop = true; Print( "Not Enough Money" ); break; case ERR_TRADE_TOO_MANY_ORDERS: OrderLoop = true; Print( "Too Many Orders" ); break; //---- Fatal Unknown Error case ERR_NO_RESULT: default: OrderLoop = true; Print( "Unknown Error - " + Err ); break; //---- } // end switch if( TryCount > 10) { OrderLoop = true; } } //---- return(Ticket); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Place Shrt Order | //+------------------------------------------------------------------+ int EnterShrt( string FinalSymbol, double FinalLots, double SL, double Magic ) { int Ticket = -1; int err = 0; bool OrderLoop = False; int TryCount = 0; while( !OrderLoop ) { while( IsTradeContextBusy() ) { Sleep( 7 ); } RefreshRates(); double SymAsk = NormalizeDouble( MarketInfo( FinalSymbol, MODE_ASK ), SymDigits ); double SymBid = NormalizeDouble( MarketInfo( FinalSymbol, MODE_BID ), SymDigits ); Ticket = OrderSend( FinalSymbol, OP_SELL, FinalLots, SymBid, Slippage, Ask+SL*Point, Ask-SL*2*Point, DoubleToStr(Magic,0), Magic, 0, Red ); int Err=GetLastError(); switch (Err) { //---- Success case ERR_NO_ERROR: OrderLoop = true; break; //---- Retry Error case ERR_SERVER_BUSY: case ERR_NO_CONNECTION: case ERR_INVALID_PRICE: case ERR_OFF_QUOTES: case ERR_BROKER_BUSY: case ERR_TRADE_CONTEXT_BUSY: TryCount++; break; case ERR_PRICE_CHANGED: case ERR_REQUOTE: continue; //---- Fatal known Error case ERR_INVALID_STOPS: OrderLoop = true; Print( "Invalid Stops" ); break; case ERR_INVALID_TRADE_VOLUME: OrderLoop = true; Print( "Invalid Lots" ); break; case ERR_MARKET_CLOSED: OrderLoop = true; Print( "Market Close" ); break; case ERR_TRADE_DISABLED: OrderLoop = true; Print( "Trades Disabled" ); break; case ERR_NOT_ENOUGH_MONEY: OrderLoop = true; Print( "Not Enough Money" ); break; case ERR_TRADE_TOO_MANY_ORDERS: OrderLoop = true; Print( "Too Many Orders" ); break; //---- Fatal Unknown Error case ERR_NO_RESULT: default: OrderLoop = true; Print( "Unknown Error - " + Err ); break; //---- } // end switch if( TryCount > 10) { OrderLoop = true; } } //---- return(Ticket); } //+------------------------------------------------------------------+ //| Set Trailing Stops | //+------------------------------------------------------------------+ void TrailOrder(int type, double TrailingStop) { if(TrailingStop>0) { if(OrderMagicNumber() == MagicNumber) { if(type==OP_BUY) { if(Bid-OrderOpenPrice()>Point*TrailingStop) { if(OrderStopLoss()(Point*TrailingStop)) { if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0)) { OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red); } } } } } } //+------------------------------------------------------------------+