//+------------------------------------------------------------------+ //| FarhadCrab1.mq4 | //| Copyright © 2006, Farhad Farshad | //| http://www.fxperz.com //| http://farhadfarshad.com //| This EA is optimized to work on //| GBP/JPY & GBP/USD & EUR/USD M1 TimeFrame ... if you want the optimized //| EA s for any currency pair please //| mail me at: info@farhadfarshad.com //| This is the first version of this EA. If //| you want the second edition ('FarhadCrab2.mq4') //| with considerably better performance mail me. 'FarhadMagic.mq4' and 'Farhad2.mq4' are also available from this series. //| (They are not free and they don't have trial version!) //| Enjoy a better automatic investment:) with at least 100% a month. //| If you get money from this EA please donate some to poor people of your country. //+-----------------------------------------------------------------+ #property link "http://www.fxperz.com" #include #include extern string nameEA = "FarhadCrab1-ECN_All.mq4";// To "easy read" which EA place an specific order and remember me forever :) extern double maxorders=5; extern double Lots = 0.01;// you can change the lot but be aware of margin. Its better to trade with 1/4 of your capital. extern int magicEA = 113; // Magic EA identifier. Allows for several co-existing EA with different input values extern string l1 = "--------------------------------------------------"; extern string v1 = "--- Inputs for Original Method ---"; extern double lTakeProfit = 10; // recomended no more than 20 extern double sTakeProfit = 10; // recomended no more than 20 extern double takeProfit = 10; // recomended no more than 20 extern double stopLoss = 0; // do not use s/l at all. Take it easy man. I'll guarantee your profit :) extern double lTrailingStop = 8; // trail stop in points extern double sTrailingStop = 8; // trail stop in points extern string l2 = "--------------------------------------------------"; extern string m = "--- Inputs for MAMA ---"; extern string n1 = " 1. Do not use"; extern string n2 = " 2. Use _MAMA"; extern string n3 = " 3. Use MAMA_v1"; extern int WhichMAMA = 2; extern string l3 = "--------------------------------------------------"; extern string v3 = "--- Inputs for version 3 ---"; extern bool UseVersion3 = false; extern string _Parameters_NewTrend = "Parameters Spyker"; extern int JimmyEMA = 233; extern int SignalSMA = 233; extern string _Parameters_M30_MA = "Parameters M30 MA"; extern int m30.period1 = 89; extern int m30.period3 = 55; extern string _Parameters_4H_MA = "Parameters 4H MA"; extern int h4.period1 = 55; extern int h4.period3 = 13; extern string p = "pSar settings"; extern string p1 = "If TRUE will use Step1"; extern string p2 = "Otherwise will use Step2"; extern bool UseFirstSettings = true; extern double Step1 = 0.009; extern double Step2 = 0.002; double Step; extern string l4 = "--------------------------------------------------"; extern color clOpenBuy = Blue; //Different colors for different positions extern color clCloseBuy = Aqua; //Different colors for different positions extern color clOpenSell = Red; //Different colors for different positions extern color clCloseSell = Violet; //Different colors for different positions extern color clModiBuy = Blue; //Different colors for different positions extern color clModiSell = Red; //Different colors for different positions extern int Slippage = 4; //extern double vVolume; //double macdHistCurrent, macdHistPrevious, macdSignalCurrent, macdSignalPrevious; double highCurrent, lowCurrent; //double stochHistCurrent, stochHistPrevious, stochSignalCurrent, stochSignalPrevious; double sarCurrent, sarPrevious; //double momCurrent, momPrevious; double maLongCurrent, maLongPrevious; //double maShortCurrent, maShortPrevious, faRSICurrent; double realTP, realSL; //double faMiddle, faHighest, faLowest; double closeCurrent, closeCurrentD, closePreviousD; int cnt, ticket; bool isBuying = false, isSelling = false, isBuyClosing = false, isSellClosing = false; double myPoint ; int init() { myPoint = SetPoint(); if (UseFirstSettings) { Step = Step1; } else { Step = Step2; } } void deinit() { //---- } int start() { if (OrdersTotal() > 0) HandleOpenPositions(); if (OrdersTotal() >= maxorders) { return(0); } // Check for invalid bars and takeprofit if(Bars < 200) { Print("Not enough bars for this strategy - ", nameEA); return(0); } if(lTakeProfit<10){ Print("TakeProfit less than 10 on this EA with Magic -", magicEA ); return(0); } if(sTakeProfit<10){ Print("TakeProfit less than 10 on this EA with Magic -", magicEA); return(0); } //Check Margin Requirement if(AccountFreeMargin()<(1000*Lots)){ Print("We have no money. Free Margin = ", AccountFreeMargin()); return(0); } // No longer needed // calculateIndicators(); //Introducing new expressions double faClose0 = iLow(NULL,0,0); double faMA1 = iMA(NULL,0,9,0,MODE_EMA,PRICE_TYPICAL,0); double faClose2 = iHigh(NULL,0,0); double faMA3 = iMA(NULL,0,9,0,MODE_SMA,PRICE_OPEN,0); // Already in calculateIndicators // double stochHistCurrent = iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_MAIN,0); // double sarCurrent = iSAR(NULL,0,0.002,0.2,0); // Parabolic Sar Current // double sarPrevious = iSAR(NULL,0,0.002,0.2,1); //Parabolic Sar Previous // Not used // double vVolume = iVolume(NULL,0,0); // Current Volume // double faHighest = Highest(NULL,PERIOD_H4,MODE_HIGH,30,0); // Highest High in an interval of time // double faLowest = Lowest(NULL,PERIOD_H4,MODE_LOW,30,0); //Lowest Low in an interval of time // double faMiddle = (faHighest+faLowest)/2; //... bool mama_buy=false; bool mama_sell=false; if (WhichMAMA > 1 && WhichMAMA < 4) { int s0_m1=ChkMAMADir(PERIOD_M1, 0); int s0_m30=ChkMAMADir(PERIOD_M30, 0); int s0_h1=ChkMAMADir(PERIOD_H1, 0); int s0_h4=ChkMAMADir(PERIOD_H4, 0); int s1_m1=ChkMAMADir(PERIOD_M1, 1); int s1_m30=ChkMAMADir(PERIOD_M30, 1); int s1_h1=ChkMAMADir(PERIOD_H1, 1); int s1_h4=ChkMAMADir(PERIOD_H4, 1); if (s0_m1 + s0_m30+s0_h1+s0_h4+s1_m30+s1_h1+s1_h4==7) { mama_buy=true; } if (s1_m1 + s0_m30+s0_h1+s0_h4+s1_m30+s1_h1+s1_h4==14) { mama_sell=true; } } bool Version3_buy = false; bool Version3_sell = false; if (UseVersion3) { double silverTrendPrev = iCustom(NULL,PERIOD_H1,"Spyker",JimmyEMA,SignalSMA,0,1); double goldTrendPrev = iCustom(NULL,PERIOD_H1,"Spyker",JimmyEMA,SignalSMA,1,1); double silverTrend = iCustom(NULL,PERIOD_H1,"Spyker",JimmyEMA,SignalSMA,0,0); double goldTrend = iCustom(NULL,PERIOD_H1,"Spyker",JimmyEMA,SignalSMA,1,0); double m30.first = iMA(NULL, PERIOD_M30, m30.period1, 0, MODE_EMA, PRICE_HIGH, 0); //Green. double m30.second = iMA(NULL, PERIOD_M30, m30.period1, 0, MODE_EMA, PRICE_LOW, 0); //Green. double m30.third = iMA(NULL, PERIOD_M30, m30.period3, 0, MODE_EMA, PRICE_MEDIAN, 0); //Red //double h4.first = iMA(NULL, PERIOD_H4, h4.period1, 0, MODE_EMA, PRICE_HIGH, 0); //Green. //double h4.second = iMA(NULL, PERIOD_H4, h4.period1, 0, MODE_EMA, PRICE_LOW, 0); //Green. //double h4.third = iMA(NULL, PERIOD_H4, h4.period3, 0, MODE_EMA, PRICE_MEDIAN, 0); //Red if ((m30.third > m30.first) && (m30.third > m30.second) && //(h4.third > h4.first) && (h4.third > h4.second) && (goldTrend > silverTrend) && (goldTrendPrev < silverTrendPrev)) // (goldTrend < silverTrend) && (goldTrendPrev > silverTrendPrev)) Version3_buy = true; if ((m30.third < m30.first) && (m30.third < m30.second) && //(h4.third < h4.first) && (h4.third < h4.second) && (goldTrend < silverTrend) && (goldTrendPrev > silverTrendPrev)) //(goldTrend > silverTrend) && (goldTrendPrev < silverTrendPrev)) Version3_sell = true; } if (!takeBuyPositions()) { if (faClose0>faMA1) { if (WhichMAMA > 1 && !mama_buy) return(0); if (UseVersion3 && !Version3_buy) return(0); OpenBuy(); return(0); } } if (!takeSellPositions()) { if (faClose2 1 && !mama_sell) return(0); if (UseVersion3 && !Version3_sell) return(0); OpenSell(); return(0); } } return (0); } bool takeBuyPositions() { //+------------------------------------------------------------------+ //| For the currently selected trade, calculate for what multiple of | //| 5 minutes it has been open. | //| Look back past this number of trades in the open pool. If there | //| are any trades opened with this symbol and magic, then return | //| TRUE, else FALSE. If we already have an open trade with this | //| symbol and magic return TRUE, else FALSE ... | //+------------------------------------------------------------------+ datetime ct, oot; ct = TimeCurrent(); oot = OrderOpenTime(); int timeDif = ct - oot; int j; sarCurrent = iSAR(NULL,0,Step,0.2,0); // Parabolic Sar Current lowCurrent = iLow(NULL,0,0); //Low Price Current closeCurrent = iClose(NULL,PERIOD_H4,0); //Close Price Current for H4 TimeFrame maLongCurrent = iMA(NULL,PERIOD_D1,55,1,MODE_SMMA,PRICE_TYPICAL,0); //Current Long Term Moving Average if ((OrdersTotal()==0)) {j=0;} // else j= ??? if (maLongCurrent 19) j = 19; } // if ((timeDif>300) && (sarCurrent600) && (sarCurrent900) && (sarCurrent1200) && (sarCurrent1500) && (sarCurrent1800) && (sarCurrent2100) && (sarCurrent2400) && (sarCurrent2700) && (sarCurrent3000) && (sarCurrent3300) && (sarCurrent3600) && (sarCurrent3900) && (sarCurrent4200) && (sarCurrent4500) && (sarCurrent4800) && (sarCurrent5100) && (sarCurrent5400) && (sarCurrent5700) && (sarCurrentcloseCurrent) { if (sarCurrent > highCurrent) { j = MathFloor(timeDif / 300); if (j > 19) j = 19; } // if ((CurTime()-OrderOpenTime()>300) && (sarCurrent>highCurrent)) {j=1;} // if ((CurTime()-OrderOpenTime()>600) && (sarCurrent>highCurrent)) {j=2;} // if ((CurTime()-OrderOpenTime()>900) && (sarCurrent>highCurrent)) {j=3;} // if ((CurTime()-OrderOpenTime()>1200) && (sarCurrent>highCurrent)) {j=4;} // if ((CurTime()-OrderOpenTime()>1500) && (sarCurrent>highCurrent)) {j=5;} // if ((CurTime()-OrderOpenTime()>1800) && (sarCurrent>highCurrent)) {j=6;} // if ((CurTime()-OrderOpenTime()>2100) && (sarCurrent>highCurrent)) {j=7;} // if ((CurTime()-OrderOpenTime()>2400) && (sarCurrent>highCurrent)) {j=8;} // if ((CurTime()-OrderOpenTime()>2700) && (sarCurrent>highCurrent)) {j=9;} // if ((CurTime()-OrderOpenTime()>3000) && (sarCurrent>highCurrent)) {j=10;} // if ((CurTime()-OrderOpenTime()>3300) && (sarCurrent>highCurrent)) {j=11;} // if ((CurTime()-OrderOpenTime()>3600) && (sarCurrent>highCurrent)) {j=12;} // if ((CurTime()-OrderOpenTime()>3900) && (sarCurrent>highCurrent)) {j=13;} // if ((CurTime()-OrderOpenTime()>4200) && (sarCurrent>highCurrent)) {j=14;} // if ((CurTime()-OrderOpenTime()>4500) && (sarCurrent>highCurrent)) {j=15;} // if ((CurTime()-OrderOpenTime()>4800) && (sarCurrent>highCurrent)) {j=16;} // if ((CurTime()-OrderOpenTime()>5100) && (sarCurrent>highCurrent)) {j=17;} // if ((CurTime()-OrderOpenTime()>5400) && (sarCurrent>highCurrent)) {j=18;} // if ((CurTime()-OrderOpenTime()>5700) && (sarCurrent>highCurrent)) {j=19;} // Calculate multiple of 5 min periods this trade has been open ... // int k=0; // if(sarCurrent>highCurrent) { // k=MathMax(19,(CurTime()-OrderOpenTime())/300); // } // Probably have trade open for more than 19 bars for this to occur // if(k!=j) Print("Whoops! JT you dont understand takeSellPositions()!"); // Check for trade open longer then j 5 minute bars for (int i=j; i= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderSymbol()==Symbol() && OrderMagicNumber() == magicEA) { closeCurrentD = iClose(NULL,PERIOD_D1,0); //Close Price Current for D1 TimeFrame closePreviousD = iClose(NULL,PERIOD_D1,1); //Close Price Previous for D1 TimeFrame maLongCurrent = iMA(NULL,PERIOD_D1,55,1,MODE_SMMA,PRICE_TYPICAL,0); //Current Long Term Moving Average maLongPrevious = iMA(NULL,PERIOD_D1,55,1,MODE_SMMA,PRICE_TYPICAL,1); //Previous Long Term Moving Average os = NormalizeDouble(OrderStopLoss(), Digits); op = NormalizeDouble(OrderOpenPrice(), Digits); if (OrderType()==OP_BUY) { //System Stoploss based on LongTerm Moving Average (Fibo 55 day MA) //StopLoss For Buy Positions (Optional) if ((maLongCurrent>closeCurrentD) && (maLongPreviousclosePreviousD)) { CloseSellPositions(); return(0); } tsp = sTrailingStop * myPoint; price = NormalizeDouble((Ask + tsp), Digits); if (op > price) { if (os > price || os==0) { ModifyStopLoss(price); } } } } } } } void ModifyStopLoss(double ldStopLoss) { // TODO: error trapping ... if (!OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE)) Alert("Modify Stoploss failed - ",ErrorDescription(GetLastError())); } void OpenBuy() { double ldLot = GetSizeLot(); double ldStop = 0; double ldTake = GetTakeProfitBuy(); string lsComm = GetCommentForOrder(); if(stopLoss > 0) ldStop = Bid - stopLoss*myPoint; RefreshRates(); int ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,nameEA,magicEA,0,clOpenBuy); if (ticket==-1) { Alert("OpenBuy: OrderSend failed - ",ErrorDescription(GetLastError())); } else { OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES); if (ldStop != 0 || ldTake != 0) { // Normalize stoploss / takeprofit to the proper # of digits. if (Digits > 0) { ldStop = NormalizeDouble( ldStop, Digits); ldTake = NormalizeDouble( ldTake, Digits); } if(!OrderModify(ticket, OrderOpenPrice(), ldStop, ldTake, 0, LightGreen)) { Alert("OpenBuy: OrderModify failed - ",ErrorDescription(GetLastError())); } } } } void OpenSell() { double ldLot = GetSizeLot(); double ldStop = 0; double ldTake = GetTakeProfitSell(); string lsComm = GetCommentForOrder(); if(stopLoss > 0) ldStop = Ask + stopLoss*myPoint; RefreshRates(); ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,nameEA,magicEA,0,clOpenSell); if(ticket==-1) { Alert("OpenSell: OrderSend failed - ",ErrorDescription(GetLastError())); } else { OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES); if (ldStop != 0 || ldTake != 0) { if (Digits > 0) // when would it ever not be? { ldStop = NormalizeDouble( ldStop, Digits); ldTake = NormalizeDouble( ldTake, Digits); } if(!OrderModify(ticket, OrderOpenPrice(), ldStop, ldTake, 0, LightGreen)) { Alert("OpenSell: OrderModify failed - ",ErrorDescription(GetLastError())); } } } } string GetCommentForOrder() { return(nameEA); } double GetSizeLot() { return(Lots); } double GetTakeProfitBuy() { return(Ask+lTakeProfit*myPoint); } double GetTakeProfitSell() { return(Bid-sTakeProfit*myPoint); } // Robert moved all indicator calls where used //void calculateIndicators() { // Calculate indicators' value // macdHistCurrent = iMACD(NULL,0,12,26,9,PRICE_OPEN,MODE_MAIN,0); // macdHistPrevious = iMACD(NULL,0,12,26,9,PRICE_OPEN,MODE_MAIN,1); // macdSignalCurrent = iMACD(NULL,0,12,26,9,PRICE_OPEN,MODE_SIGNAL,0); // macdSignalPrevious = iMACD(NULL,0,12,26,9,PRICE_OPEN,MODE_SIGNAL,1); // stochHistCurrent = iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_MAIN,0); // stochHistPrevious = iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_MAIN,1); // stochSignalCurrent = iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_SIGNAL,0); // stochSignalPrevious = iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_SIGNAL,1); // sarCurrent = iSAR(NULL,0,Step,0.2,0); // Parabolic Sar Current // sarPrevious = iSAR(NULL,0,Step,0.2,1); //Parabolic Sar Previous // momCurrent = iMomentum(NULL,0,14,PRICE_OPEN,0); // Momentum Current // momPrevious = iMomentum(NULL,0,14,PRICE_OPEN,1); // Momentum Previous // highCurrent = iHigh(NULL,0,0); //High price Current // lowCurrent = iLow(NULL,0,0); //Low Price Current // closeCurrent = iClose(NULL,PERIOD_H4,0); //Close Price Current for H4 TimeFrame // closeCurrentD = iClose(NULL,PERIOD_D1,0); //Close Price Current for D1 TimeFrame // closePreviousD = iClose(NULL,PERIOD_D1,1); //Close Price Previous for D1 TimeFrame // maLongCurrent = iMA(NULL,PERIOD_D1,55,1,MODE_SMMA,PRICE_TYPICAL,0); //Current Long Term Moving Average // maLongPrevious = iMA(NULL,PERIOD_D1,55,1,MODE_SMMA,PRICE_TYPICAL,1); //Previous Long Term Moving Average // maShortCurrent = iMA(NULL,0,2,1,MODE_SMMA,PRICE_TYPICAL,0); //Current Short Term Moving Average // maShortPrevious = iMA(NULL,0,2,1,MODE_SMMA,PRICE_TYPICAL,1); //Previous Long Term Moving Average // faRSICurrent = iRSI(NULL,0,14,PRICE_TYPICAL,0); //Current RSI // Check for BUY, SELL, and CLOSE signal // isBuying = false; // isSelling = false; // isBuyClosing = false; // isSellClosing = false; //} void CloseBuyPositions(){ for (int i=OrdersTotal()-1; i>= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderSymbol()==Symbol() && OrderMagicNumber() == magicEA) { if (OrderType()==OP_BUY) OrderClose(OrderTicket(),Lots,Bid,Slippage); } } } } void CloseSellPositions(){ for (int i=OrdersTotal()-1; i>= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderSymbol()==Symbol() && OrderMagicNumber() == magicEA) { if (OrderType()==OP_SELL) OrderClose(OrderTicket(),Lots,Ask,Slippage); } } } } double SetPoint() { double mPoint; if (Digits < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); } int ChkMAMADir(int t_period, int shift) { int res=0; double mf = 0.0; double ms = 0.0; if (WhichMAMA == 3) { mf = iCustom(Symbol(), t_period, "MAMA_v1", 0, 4, 0.5, 0.05, 0, 0, 0, 0, shift); ms = iCustom(Symbol(), t_period, "MAMA_v1", 0, 4, 0.5, 0.05, 0, 0, 0, 1, shift); } else { if (WhichMAMA == 2) { mf = iCustom(Symbol(), t_period, "_MAMA", 0.5, 0.05, 0, shift); ms = iCustom(Symbol(), t_period, "_MAMA", 0.5, 0.05, 1, shift); } } if (mf>ms) { res=1; } // BUY if (mf