//+------------------------------------------------------------------+ //| Forex EA and Script Generator version 5.0 EA | //| Copyright 2012, Etasoft Inc. | //| http://www.forexgenerator.com | //+------------------------------------------------------------------+ // Keywords: developer, Forex EA builder, create EA, expert advisor, MT5, MT4 #property copyright "Copyright © 2013, Etasoft Inc. Forex EA Generator v5.0" #property link "http://www.forexgenerator.com" #include // exported variables input double Lots27 = 1; input int Stoploss27 = 80; input int Takeprofit27 = 500; input double Lots24 = 1; input int Stoploss24 = 80; input int Takeprofit24 = 500; // local variables double PipValue=1; // this variable is here to support 5-digit brokers bool Terminated = false; string LF = "\n"; // use this in custom or utility blocks where you need line feeds int NDigits = 4; // used mostly for NormalizeDouble in Flex type blocks int ObjCount = 0; // count of all objects created on the chart, allows creation of objects with unique names int current = 0; int handle1 = 0; int handle2 = 0; int handle3 = 0; int handle4 = 0; int handle5 = 0; int handle6 = 0; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- NDigits = Digits(); if (NDigits == 3 || NDigits == 5) PipValue = 10; if (AccountInfoInteger(ACCOUNT_TRADE_EXPERT) == false) { Print("Check terminal options because EA trade option is set to not allowed."); Comment("Check terminal options because EA trade option is set to not allowed."); } ObjectsDeleteAll(0); // clear the chart handle1 = iMA(NULL, NULL,5,0,MODE_SMA,PRICE_LOW); handle2 = iMA(NULL, NULL,5,0,MODE_SMA,PRICE_HIGH); handle3 = iMA(NULL, NULL,1,14,MODE_SMA,PRICE_OPEN); handle4 = iMA(NULL, NULL,5,0,MODE_SMA,PRICE_HIGH); handle5 = iMA(NULL, NULL,1,14,MODE_SMA,PRICE_OPEN); handle6 = iMA(NULL, NULL,5,0,MODE_SMA,PRICE_LOW); Comment(""); // clear the chart //--- return(0); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- if (true) ObjectsDeleteAll(0); IndicatorRelease(handle1); IndicatorRelease(handle2); IndicatorRelease(handle3); IndicatorRelease(handle4); IndicatorRelease(handle5); IndicatorRelease(handle6); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if (Terminated == true) { Comment("EA Terminated."); } OnEveryTick16(); } //+------------------------------------------------------------------+ //| Get Low for specified bar index | //+------------------------------------------------------------------+ double Low(int index) { double arr[]; double low = 0; ArraySetAsSeries(arr, true); int copied = CopyLow(Symbol(), PERIOD_CURRENT, 0, Bars(Symbol(), PERIOD_CURRENT), arr); if (copied>0 && index0 && index0 && index0 && index Executed: [", function, "]"); return (false); } bool IsError(CTrade& trade, string function) { if (trade.ResultRetcode() != 0 && trade.ResultRetcode() != TRADE_RETCODE_DONE && trade.ResultRetcode() != TRADE_RETCODE_PLACED) { Print("Function: ", function, " Error: ", trade.ResultRetcode(), " ", trade.ResultRetcodeDescription()); return (true); } else Print("> Executed: [", function, "]"); return (false); } //+------------------------------------------------------------------+ //| Get indicator value back | //+------------------------------------------------------------------+ double GetIndicator(int handle, int buffer_num, int index) { //--- array for the indicator values double arr[]; //--- obtain the indicator value in the last two bars if (CopyBuffer(handle, buffer_num, 0, index+1, arr) <= 0) { Sleep(200); for(int i=0; i<100; i++) { if (BarsCalculated(handle) > 0) break; Sleep(50); } int copied = CopyBuffer(handle, buffer_num, 0, index+1, arr); if (copied <= 0) { Print("CopyBuffer failed. Maybe history has not download yet? Error = ", GetLastError()); return -1; } else return (arr[index]); } else { return (arr[index]); } return 0; } //+------------------------------------------------------------------+ //| Building blocks | //+------------------------------------------------------------------+ void OnEveryTick16() { if (NDigits == 3 || NDigits == 5) PipValue = 10; TechnicalAnalysis28(); TechnicalAnalysis30(); OncePerBar17(); } void TechnicalAnalysis28() { if (Ask() < GetIndicator(handle1,0,current)) { IfPositionDoesNotExist29(); } } void IfPositionDoesNotExist29() { bool exists = false; // go through all positions for (int i=PositionsTotal()-1;i>=0;i--) { if (PositionSelect(Symbol())) // position with appropriate ORDER_MAGIC, symbol and order type if (PositionGetInteger(POSITION_MAGIC) == 1 && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) exists = true; } if (exists == false) { ClosePosition13(); } } void ClosePosition13() { // go through all positions for (int i=PositionsTotal()-1;i>=0;i--) { if (PositionSelect(Symbol())) // position with appropriate ORDER_MAGIC, symbol and order type if (PositionGetInteger(POSITION_MAGIC) == 1) { CTrade trade; trade.SetExpertMagicNumber(1); trade.PositionClose(Symbol(), 4); IsError(trade, __FUNCTION__); } } } void TechnicalAnalysis30() { if (Ask() > GetIndicator(handle2,0,current)) { IfPositionDoesNotExist31(); } } void IfPositionDoesNotExist31() { bool exists = false; // go through all positions for (int i=PositionsTotal()-1;i>=0;i--) { if (PositionSelect(Symbol())) // position with appropriate ORDER_MAGIC, symbol and order type if (PositionGetInteger(POSITION_MAGIC) == 1 && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) exists = true; } if (exists == false) { ClosePosition21(); } } void ClosePosition21() { // go through all positions for (int i=PositionsTotal()-1;i>=0;i--) { if (PositionSelect(Symbol())) // position with appropriate ORDER_MAGIC, symbol and order type if (PositionGetInteger(POSITION_MAGIC) == 1) { CTrade trade; trade.SetExpertMagicNumber(1); trade.PositionClose(Symbol(), 4); IsError(trade, __FUNCTION__); } } } void OncePerBar17() { static datetime Old_Time; datetime New_Time[1]; // copying the last bar time to the element New_Time[0] int copied = CopyTime(Symbol(), Period(), 0, 1, New_Time); if (copied > 0) // ok, the data has been copied successfully { if (Old_Time != New_Time[0]) // if old time isn't equal to new bar time { Old_Time=New_Time[0]; // saving bar time TechnicalAnalysis33(); TechnicalAnalysis32(); } else { return; // still the same bar } } else { Alert("Error in copying historical times data, error =",GetLastError()); ResetLastError(); return; } } void TechnicalAnalysis33() { if (Ask() < GetIndicator(handle3,0,current)) { TechnicalAnalysis35(); } } void TechnicalAnalysis35() { if (High(1) < High(2)) { TechnicalAnalysis25(); } } void TechnicalAnalysis25() { if (Ask() < GetIndicator(handle4,0,current)) { IfPositionDoesNotExist26(); } } void IfPositionDoesNotExist26() { bool exists = false; // go through all positions for (int i=PositionsTotal()-1;i>=0;i--) { if (PositionSelect(Symbol())) // position with appropriate ORDER_MAGIC, symbol and order type if (PositionGetInteger(POSITION_MAGIC) == 1 && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) exists = true; } if (exists == false) { SellOrder27(); } } void SellOrder27() { //--- prepare a request MqlTradeRequest request; ZeroMemory(request); request.action = TRADE_ACTION_DEAL; // setting a deal order request.magic = 1; // ORDER_MAGIC request.symbol = Symbol(); // symbol request.volume = Lots27; // volume in lots request.price = Bid(); request.sl = Bid() + Stoploss27*PipValue*Point(); // Stop Loss specified request.tp = Bid() - Takeprofit27*PipValue*Point(); // Take Profit specified request.deviation = 4; // deviation in points request.type = ORDER_TYPE_SELL; request.comment = "Order"; MqlTradeResult result; ZeroMemory(result); OrderSend(request,result); // check the result if (!IsError(result, __FUNCTION__)) { } } void TechnicalAnalysis32() { if (Ask() > GetIndicator(handle5,0,current)) { TechnicalAnalysis34(); } } void TechnicalAnalysis34() { if (Low(1) > Low(2)) { TechnicalAnalysis22(); } } void TechnicalAnalysis22() { if (Ask() > GetIndicator(handle6,0,current)) { IfPositionDoesNotExist23(); } } void IfPositionDoesNotExist23() { bool exists = false; // go through all positions for (int i=PositionsTotal()-1;i>=0;i--) { if (PositionSelect(Symbol())) // position with appropriate ORDER_MAGIC, symbol and order type if (PositionGetInteger(POSITION_MAGIC) == 1 && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) exists = true; } if (exists == false) { BuyOrder24(); } } void BuyOrder24() { //--- prepare a request MqlTradeRequest request; ZeroMemory(request); request.action = TRADE_ACTION_DEAL; // setting a deal order request.magic = 1; // ORDER_MAGIC request.symbol = Symbol(); // symbol request.volume= Lots24; // volume in lots request.price = Ask(); request.sl = Ask() - Stoploss24*PipValue*Point(); // Stop Loss specified request.tp = Ask() + Takeprofit24*PipValue*Point(); // Take Profit specified request.deviation= 4; // deviation in points request.type = ORDER_TYPE_BUY; request.comment = "Order"; MqlTradeResult result; ZeroMemory(result); OrderSend(request,result); // check the result if (!IsError(result, __FUNCTION__)) { } }