//+------------------------------------------------------------------+ //| simplefx.mq4 | //| Copyright © 2007, GLENNBACON.COM LLC | //| http://www.GetForexSoftware.com | //+------------------------------------------------------------------+ // Shift of moving average #define Shift 1 int MATrend ; int SARTrend; int RSITrend; double rsi0; double rsi1; string rsi; int stochTrend; int bearsBullsTrend; // Trend Detection function //#define MABULL 1 //#define MABEAR -1 //#define NOTREND -2 //parametros Medias moveis int Long_MA_Period = 200; int Long_MA_Method = 0; int Long_MA_Applied_Price = 4; int Short_MA_Period = 50; int Short_MA_Method = 0; int Short_MA_Applied_Price = 4; #define MABULL 1 #define MABEAR -1 #define NOTREND -2 // Input variables extern double Lots = 0.30; extern int Slippage = 3; extern string Order_Comment = "EL FODEURO"; extern color Order_Arrow_Color = Green; extern double PercentMargin = 0.005; extern int NOGOVolatility = 200; extern int SMALLGOVolatility = 100; extern int SSP = 34; // bars - calculating period extern int SSK = 29; // tolerance of second line extern double StopLoss = 0; // Global variables int Total_Open_Orders = 1; // we only want to open one order at a time and only manage the one order int cnt = 0; // counter variable, used in for() loops bool init_variables; // init variable when program starts datetime PreviousBar; // record the candle/bar time int NowTrend = NOTREND; int Magic = 1; string pairs[9] ; int SSPS[9] ; int SSKS[9]; int logHandler; string logFileName; double profits[1]; int profitIdx = 1; int lastSARTrend; //Indicators double ExtMapBuffer1[]; double ExtMapBuffer2[]; double Wal1[]; double Wal2[]; double ExtMapBuffer4[]; int i, i1, i2, shift; double SsMax, SsMin, SsMax05, SsMin05, Rsmin, Rsmax, Tsmin, Tsmax; int val1, val2, AvgRange; bool uptrend, old; double iClose0,iClose1,iIchi,iLine1,iLine2; int init() { init_variables = true; // Allows us to init variables in the start function because // we cannot do this in the init function because the variables // use values from the chart ~ logFileName = StringConcatenate( Symbol() , ".log") ; pairs[0] = "EURUSD"; pairs[1] = "GBPCHF"; pairs[2] = "EURJPY"; pairs[3] = "USDJPY"; pairs[4] = "GBPUSD"; pairs[5] = "EURGBP"; pairs[6] = "CADCHF"; pairs[7] = "GBPJPY"; pairs[8] = "EURCHF"; /* Pair Time SSP SSK CADJPY 4H 48 36 EURCHF 4H 72 50 USDCAD 4H 24 60 USDCHF 4H 34 29 */ SSPS[8] = 72; SSKS[8] =50; SSPS[7] = 36; SSKS[7] =29; SSPS[6] = 62; SSKS[6] =52; SSPS[5] = 44; SSKS[5] =38; SSPS[0] = 34; SSKS[0] =34; SSPS[0] = 34; SSKS[0] =34; SSPS[1] = 34; SSKS[1] =29; SSPS[2] = 36; SSKS[2] =29; SSPS[3] = 34; SSKS[3] =29; SSPS[4] = 44; SSKS[4] =36; int i; for(i=0;i<ArraySize(pairs) ;i++ ) { if ( pairs[i] == Symbol() ) { SSP = SSPS[i]; SSK = SSKS[i]; break; } } /* Pair Time SSP SSK EURGBP 4H 44 38 CADCHF 4H 62 52 CADJPY 4H 48 36 GBPUSD 4H 44 36 GBPCHF 4H 34 29 GBPJPY 4H 36 29 EURUSD 4H 34 34 EURCHF 4H 72 50 EURJPY 4H 72 36 USDCAD 4H 24 60 USDCHF 4H 34 29 USDJPY 4H 34 29 */ Magic = createMagicNumber(); return(0); } int deinit() { // clear chart when EA is removed ObjectsDeleteAll(); return(0); } int start() { logHandler=FileOpen(logFileName, FILE_CSV | FILE_READ | FILE_WRITE , ';'); FileSeek(logHandler, 0, SEEK_END); if(logHandler<1) { Print("file error the last error is ", GetLastError()); return (false); } // make sure trader has set Lots to at least the minimum lot size of the broker and // we will normalize the Lots variable so we can properly open an order if(MarketInfo(Symbol(),MODE_MINLOT) == 0.01) { Lots = NormalizeDouble(Lots,2); if(Lots < 0.01) { Comment("The variable Lots must be 0.01 or greater to open an order. "); return(0); } } if(MarketInfo(Symbol(),MODE_MINLOT) == 0.1) { Lots = NormalizeDouble(Lots,1); if(Lots < 0.1) { Comment("The variable Lots must be 0.1 or greater to open an order. "); return(0); } } if(MarketInfo(Symbol(),MODE_MINLOT) == 1) { Lots = NormalizeDouble(Lots,0); if(Lots < 1) { Comment("The variable Lots must be 1 or greater to open an order. "); return(0); } } // init variables when the expert advisor first starts running if(init_variables == true) { PreviousBar = Time[0]; // record the current canle/bar open time // place code here that you only wnat to run one time MathSrand(TimeLocal()); init_variables = false; // change to false so we only init variable once // Magic = MathRand(); } // perform analysis and open orders on new candle/bar if(NewBar() == true) { NowTrend = TrendDetection(); MATrend = MATrend(); SARTrend = getPSarTrend(); RSITrend = getRSITrend(); stochTrend = stochasticAlert(); bearsBullsTrend = bearsBullsAlert(); rsi0 = iRSI(NULL,0,14,PRICE_CLOSE,0); rsi1 = iRSI(NULL,0,14,PRICE_CLOSE,1); rsi = StringConcatenate(rsi0,";",rsi1); // logInfo(logHandler, StringConcatenate( "stoch trend ", stochTrend, " bearbuls " , bearsBullsTrend) ); // only perform analysis and close order if we only have one order open if(TotalOpenOrders() == Total_Open_Orders && SelectTheOrder() == true) { StoreCurrentProfit(); double currentProfit = OrderProfit(); if ( currentProfit < 0 && OrderType() == OP_SELL ) { //estou a ter prejuizo e é uma venda , vamos olhar para o rsi current /* if ( stochTrend == MABULL && bearsBullsTrend == MABULL ) { logInfo(logHandler,StringConcatenate("SAIDAVENDA",";",Symbol(),";",OrderTicket(),";",OrderProfit(),";",TimeLocal())); logInfo(logHandler,getProfitAsString(OrderTicket())); OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Order_Arrow_Color); FileClose(logHandler); return(0); } */ /* if ( rsi0 < 35 ) { logInfo(logHandler,StringConcatenate("SAIDAVENDA",";",Symbol(),";",OrderTicket(),";",OrderProfit(),";",TimeLocal())); logInfo(logHandler,getProfitAsString(OrderTicket())); OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Order_Arrow_Color); FileClose(logHandler); return(0); } */ } if ( currentProfit < 0 && OrderType() == OP_BUY ) { /* if ( stochTrend == MABEAR && bearsBullsTrend == MABEAR ) { logInfo(logHandler,StringConcatenate("SAIDACOMPRA",";",Symbol(),";",OrderTicket(),";",OrderProfit(),";",TimeLocal())); logInfo(logHandler,getProfitAsString(OrderTicket())); OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Order_Arrow_Color); FileClose(logHandler); return (0); } */ /* if ( rsi0 > 65 ) { logInfo(logHandler,StringConcatenate("SAIDACOMPRA",";",Symbol(),";",OrderTicket(),";",OrderProfit(),";",TimeLocal())); logInfo(logHandler,getProfitAsString(OrderTicket())); OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Order_Arrow_Color); FileClose(logHandler); return (0); } */ } string rep = StringConcatenate(profitIdx , " " , currentProfit , " ", rsi0 , " " , OrderType() ); logInfo(logHandler,rep); if((OrderType()) == OP_BUY && ((NowTrend == MABEAR) || (NowTrend == NOTREND ))) { logInfo(logHandler,StringConcatenate("SAIDACOMPRA",";",Symbol(),";",OrderTicket(),";",OrderProfit(),";",TimeLocal())); logInfo(logHandler,getProfitAsString(OrderTicket())); OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Order_Arrow_Color); } if((OrderType() == OP_SELL) && ((NowTrend == MABULL) || (NowTrend == NOTREND ))) { logInfo(logHandler,StringConcatenate("SAIDAVENDA",";",Symbol(),";",OrderTicket(),";",OrderProfit(),";",TimeLocal())); logInfo(logHandler,getProfitAsString(OrderTicket())); OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Order_Arrow_Color); } } // only perform analysis and open new order if we have not reached our Total_Open_Orders max if(TotalOpenOrders() < Total_Open_Orders) { double buyPower = evaluateTrend(MABULL) ; double sellPower = evaluateTrend(MABEAR); if ( buyPower > 0 ) { Print("Sinal de compra força " , buyPower/10); } if ( sellPower > 0 ) { Print("Sinal de venda força " , sellPower/10); } int tickOrder ; // open buy if ( buyPower > 1 ) // if(NowTrend == MABULL && ( MATrend == MABULL && iRSI(NULL,0,14,PRICE_CLOSE,0)<25 && stochTrend == MABULL && bearsBullsTrend == MABULL ) ) { // open order // if ( MATrend == MABULL && (rsi0 > 45 && rsi0 <= 70 ) && stochTrend == MABULL && bearsBullsTrend == MABULL ) { tickOrder = OrderSend(Symbol(),OP_BUY,buyPower/10,Ask,Slippage,0,0,Order_Comment,Magic,0,Order_Arrow_Color); Print("Comprei " , Symbol(), rsi ); PlaySound("alert2.wav"); logInfo(logHandler,StringConcatenate("Compra",";",Symbol(),";",tickOrder,";",rsi,";",SARTrend,";",TimeLocal())); if(tickOrder <0) { Print("OrderSend failed with error #",GetLastError()); } } // open sell else if ( sellPower > 1 ) //if(NowTrend == MABEAR && ( MATrend== MABEAR && stochTrend == MABEAR && bearsBullsTrend == MABEAR ) ) { // open order // if ( MATrend== MABEAR && stochTrend == MABEAR && bearsBullsTrend == MABEAR ) { tickOrder = OrderSend(Symbol(),OP_SELL,sellPower/10,Bid,Slippage, 0,0,Order_Comment,Magic,0,Order_Arrow_Color); Print("Vendi " , Symbol(),rsi ); PlaySound("alert2.wav"); logInfo(logHandler,StringConcatenate("VENDA",";",Symbol(),";",tickOrder,";",rsi,";",SARTrend,";",TimeLocal())); if(tickOrder <0) { Print("OrderSend failed with error #",GetLastError()); } } } /* Print("Date: ",Month(),"-",Day(),"-",Year()," Server Time: ",Hour(),":",Minute(),":",Seconds(), " volatility:", AvgRange, " iClose0: ",iClose0, " iClose1: ",iClose1, " iIchi: ",iIchi, " iLine1: ",iLine1, " iLine2: ",iLine2, " Trend: ",NowTrend); */ Display_Info(); } lastSARTrend = SARTrend; FileClose(logHandler); return(0); } ///////////////////////////////////////////////////////////////////////// // Common functions // /////////////////////////////////////////////////////////////////////// void StoreCurrentProfit() { profits[profitIdx-1] = OrderProfit(); profitIdx++; ArrayResize(profits,profitIdx); } string getProfitAsString(int orderTicket ) { string result = "PROFITREPORT;"; result = StringConcatenate(result,orderTicket,";"); for(int i=0;i<ArraySize(profits);i++) { result = StringConcatenate(result,profits[i],";"); } return (result); } void logInfo(int handle, string value) { //Print(" handle ", handle); Print( value); FileWrite(handle,value); } // This function returns the total amount of orders the expert advisor has open int TotalOpenOrders() { cnt=OrdersTotal(); int TotalOpenOrders = 0; if(cnt==0) { return(0); } else { for(;cnt>=0;cnt--) { RefreshRates(); OrderSelect(cnt,SELECT_BY_POS); if(OrderMagicNumber()==Magic && OrderSymbol() == Symbol() ) { TotalOpenOrders++; } } } return(TotalOpenOrders); } // This function finds the open order and selects it int SelectTheOrder() { cnt=OrdersTotal(); if(cnt==0) { return(false); } else { for(;cnt>=0;cnt--) { RefreshRates(); OrderSelect(cnt,SELECT_BY_POS); if(OrderMagicNumber()==Magic) { return(true); } } } return(false); } // This function return the value true if the current bar/candle was just formed bool NewBar() { if(PreviousBar<Time[0]) { PreviousBar = Time[0]; return(true); } else { return(false); } return(false); // in case if - else statement is not executed } // is trend up/bullish or is it down/bearish int TrendDetection() { double topLine,bottomLine; if (iClose1 == 0) iClose1 = iClose(NULL,0,10); else iClose1 = iClose0; iClose0 = iClose(NULL,0,0); AIchimoku(); if (iLine1 > iLine2) { topLine = iLine1; bottomLine = iLine2; } else { topLine = iLine2; bottomLine = iLine1; } if ((iClose0 > iIchi) && (iClose1 < iIchi)) return(MABULL); if ((iClose0 < iIchi) && (iClose1 > iIchi)) return(MABULL); if ((iClose0 < topLine ) && (iClose0 > bottomLine)) return(MABEAR); if (AvgRange < NOGOVolatility ) { if ((iClose0 > topLine) && (iClose0 > iClose1)) if (((iClose0-topLine)/iClose0)<PercentMargin) return(MABULL); if ((iClose0 < bottomLine) && (iClose0 < iClose1)) if (((bottomLine-iClose0)/bottomLine)<PercentMargin) return(MABEAR); } // flat no trend return 0 return(0); } int MATrend() { // BULL trend if(iMA(NULL,0,Short_MA_Period,0,Short_MA_Method,Short_MA_Applied_Price,0) > iMA(NULL,0,Long_MA_Period,0,Long_MA_Method,Long_MA_Applied_Price,0) && iMA(NULL,0,Short_MA_Period,0,Short_MA_Method,Short_MA_Applied_Price,1) > iMA(NULL,0,Long_MA_Period,0,Long_MA_Method,Long_MA_Applied_Price,1)) { return(MABULL); } // BEAR trend if(iMA(NULL,0,Short_MA_Period,0,Short_MA_Method,Short_MA_Applied_Price,0) < iMA(NULL,0,Long_MA_Period,0,Long_MA_Method,Long_MA_Applied_Price,0) && iMA(NULL,0,Short_MA_Period,0,Short_MA_Method,Short_MA_Applied_Price,1) < iMA(NULL,0,Long_MA_Period,0,Long_MA_Method,Long_MA_Applied_Price,1)) { return(MABEAR); } // flat no trend return 0 return(0); } void Display_Info() { Comment("Price: ",NormalizeDouble(Bid,4),"\n", "Date: ",Month(),"-",Day(),"-",Year()," Server Time: ",Hour(),":",Minute(),":",Seconds(),"\n", "volatility: ", AvgRange,"\n", "iClose0: ",DoubleToStr(iClose0,4),"\n", "iClose1: ",DoubleToStr(iClose1,4),"\n", "iIchi: ",DoubleToStr(iIchi,4),"\n", "iLine1: ",DoubleToStr(iLine1,4),"\n", "iLine2: ",DoubleToStr(iLine2,4),"\n", "TotalOrders : ",TotalOpenOrders(),"\n"); return(0); } bool CheckIfOrderExists(string symbol, int orderType ) { cnt=OrdersTotal(); if(cnt==0) { return(false); } else { for(;cnt>=0;cnt--) { RefreshRates(); OrderSelect(cnt,SELECT_BY_POS); Print ( OrderSymbol(), " ",OrderMagicNumber(), " ", Magic); if ( OrderSymbol() == symbol && OrderType() == orderType ) return (true); } } return(false); } int getPSarTrend() { if(iSAR(NULL,0,0.02,0.2,1)<Low[1] && iSAR(NULL,0,0.02,0.2,0)>High[0]) { return (MABEAR); } if(iSAR(NULL,0,0.02,0.2,1)>High[1] && iSAR(NULL,0,0.02,0.2,0)<Low[0]) { return (MABULL); } else return(0); } int getRSITrend() { double RSI[8]; // 6 double EMA, EMA1; double RSI1[8]; ArraySetAsSeries(RSI, true); // 1 for (int x=0; x<=7;x++) { RSI[x]=iRSI(NULL,0,8,PRICE_CLOSE,x); } EMA=iMAOnArray(RSI,0,8,0,MODE_EMA,0); // 2 for (x=1; x<=8;x++) { RSI1[x]=iRSI(NULL,0,8,PRICE_CLOSE,x); } EMA1=iMAOnArray(RSI1,0,8,0,MODE_EMA,1); // 2 if (EMA<RSI[0] && EMA1>RSI[0]) return(MABULL); else if (EMA>RSI[0] && EMA1<RSI[0] ) return(MABEAR) ; else return (0); } int createMagicNumber() { string symbol = Symbol(); int t = 0 ; for(int i=0;i<StringLen(symbol);i++) { t += StringGetChar(symbol,i); } return (t); } int bearsBullsAlert() { double pos1pre, pos2cur, hzbul1, hzbul2, hzbear1, hzbear2; hzbul1 = iBullsPower(NULL, 0, 13, PRICE_WEIGHTED, 1); hzbul2 = iBullsPower(NULL, 0, 13, PRICE_CLOSE, 0); hzbear1 = iBearsPower(NULL, 0, 13, PRICE_WEIGHTED, 1); hzbear2 = iBearsPower(NULL, 0, 13, PRICE_CLOSE, 0); pos1pre = ((hzbear1 + hzbul1) / 2); pos2cur = ((hzbear2 + hzbul2) / 2); if(pos1pre > pos2cur && pos2cur > 0) { return (MABEAR); } if(pos2cur < 0) { return (MABULL); } } double evaluateTrend(int trendType) { int ret = 0 ; if( NowTrend == trendType ) ret += .1; else return (ret); if (MATrend == trendType ) ret += .1; if ( stochTrend == trendType ) ret += .1; if ( bearsBullsTrend == trendType ) ret += .1; return (ret); } int stochasticAlert() { double mainLine; double prevMainLine; double signalLine; double prevSignalLine; for(int a=0;a<100;a++) { mainLine=iStochastic(0,0,5,3,3,MODE_SMA,0,MODE_MAIN,a); prevMainLine=iStochastic(0,0,5,3,3,MODE_SMA,0,MODE_MAIN,a+1); signalLine=iStochastic(0,0,5,3,3,MODE_SMA,0,MODE_SIGNAL,a); prevSignalLine=iStochastic(0,0,5,3,3,MODE_SMA,0,MODE_SIGNAL,a+1); if(prevMainLine<prevSignalLine && mainLine>signalLine) return (MABULL); if(prevMainLine>prevSignalLine && mainLine<signalLine) return (MABEAR); } return(0); } void AIchimoku() { // maximum of previous SSP bars period SsMax = High[iHighest(NULL, PERIOD_H1, MODE_HIGH, SSP, 0)]; // minimum of previous SSP bars period SsMin = Low[iLowest(NULL, PERIOD_H1, MODE_LOW, SSP, 0)]; // maximum of SSP bars period for SSK bars from begin SsMax05 = High[iHighest(NULL, PERIOD_H1, MODE_HIGH, SSP, 0 + SSK)]; // maximum of SSP bars period for SSK bars from begin SsMin05 = Low[iLowest(NULL, PERIOD_H1, MODE_LOW, SSP, 0 + SSK)]; iLine1 = (SsMax + SsMin) / 2; iLine2 = (SsMax05+SsMin05) / 2; val1 = ExtMapBuffer1[1] / Point; val2 = ExtMapBuffer2[1] / Point; //---- Rsmax = High[iHighest(NULL, PERIOD_H1, MODE_HIGH, SSP*2, 0)]; Rsmin = Low[iLowest(NULL, PERIOD_H1, MODE_LOW, SSP*2, 0)]; AvgRange = (Rsmax / Point) - (Rsmin / Point); //---- Tsmax = High[iHighest(NULL, PERIOD_H1, MODE_HIGH, SSP*1.62, 0)]; Tsmin = Low[iLowest(NULL, PERIOD_H1, MODE_LOW, SSP*1.62, 0)]; iIchi = (Tsmax + Tsmin) / 2; }
Sample
Analysis
Market Information Used:
Series array that contains open time of each bar
Series array that contains close prices for each bar
Series array that contains the highest prices of each bar
Series array that contains the lowest prices of each bar
Indicator Curves created:
Indicators Used:
Relative strength index
Moving average indicator
Parabolic Stop and Reverse system
Bulls Power indicator
Bears Power indicator
Stochastic oscillator
Custom Indicators Used:
Order Management characteristics:
It Closes Orders by itself
It automatically opens orders when conditions are reached
Checks for the total of open orders
Other Features:
Uses files from the file system
It issuies visual alerts to the screen
It plays sound alerts
It writes information to file