//+----------------------------------------------------------------------+ //|Newdigital | //|This EA was created according | //|to the system posted | //|on this thread | //|www.forex-tsd.com/manual-trading-systems/669-fibonacci_ema-2.html | //| | //|I modifies EA created by me and Igorad (Scalp_net_v1.2), | //|inserted the system and fixed the error. | //| | //|Please note that if you use TakeProfitMode and/or | //|TrailingStopMode or/and StopLossMode, | //|so EA will close the order according to the signal EMA/close EMA | //|crossing AND according to stop loss, take profit and trailing stop | //|(if it was setted as "True"). | //|Trailing stop option is working even if stop loss and | //|take profit options were setted to "False". | //+----------------------------------------------------------------------+ #property copyright "newdigital" #property link "http://www.forex-tsd.com" extern int MagicNumber = 112233; extern string PARAMETERS_TRADE = "PARAMETERS TRADE"; extern double Lots = 0.1; extern int Slippage = 3; extern bool StopLossMode = False; extern int StopLoss = 100; extern bool TakeProfitMode = False; extern int TakeProfit = 100; extern bool TrailingStopMode = False; extern int TrailingStop = 30; extern string PARAMETERS_MM = "MONEY MANAGEMENT"; extern double MaximumRisk =0.15; extern double DecreaseFactor =3; extern string PARAMETERS_FILTER_ONE = "TIME FILTER"; extern bool UseHourTrade = False; extern int FromHourTrade = 8; extern int ToHourTrade = 17; extern string PARAMETERS_INDICATOR_ONE = "Moving Average signal"; extern int SignalEMA_Period = 8; extern string PARAMETERS_INDICATOR_TWO = "Moving Average main"; extern int MainEMA_Period = 55; extern int Delta1=0; extern string PARAMETERS_INDICATOR_THREE = "Moving Average for close"; extern int CloseEMA_Period = 21; extern int Delta2=0; bool EachTickMode = False; #define SIGNAL_NONE 0 #define SIGNAL_BUY 1 #define SIGNAL_SELL 2 #define SIGNAL_CLOSEBUY 3 #define SIGNAL_CLOSESELL 4 int BarCount; int Current; bool TickCheck = False; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { BarCount = Bars; if (EachTickMode) Current = 0; else Current = 1; return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { if (UseHourTrade){ if (!(Hour()>=FromHourTrade && Hour()<=ToHourTrade)) { Comment("Outside preferred trading hours"); return(0); } } int Order = SIGNAL_NONE; int Total, Ticket; double StopLossLevel, TakeProfitLevel; int digit = MarketInfo(Symbol(),MODE_DIGITS); if (EachTickMode && Bars != BarCount) EachTickMode = False; Total = OrdersTotal(); Order = SIGNAL_NONE; //+------------------------------------------------------------------+ //| Signal Begin | //+------------------------------------------------------------------+ double Buy1_1 = iMA(NULL, 0, SignalEMA_Period, 0, MODE_EMA, PRICE_CLOSE, Current + 1); double Buy1_2 = iMA(NULL, 0, CloseEMA_Period, 0, MODE_EMA, PRICE_CLOSE, Current + 1); double Buy1_3 = iMA(NULL, 0, MainEMA_Period, 0, MODE_EMA, PRICE_CLOSE, Current + 1); double Buy2_1 = iMA(NULL, 0, SignalEMA_Period, 0, MODE_EMA, PRICE_CLOSE, Current + 0); double Buy2_2 = iMA(NULL, 0, CloseEMA_Period, 0, MODE_EMA, PRICE_CLOSE, Current + 0); double Buy2_3 = iMA(NULL, 0, MainEMA_Period, 0, MODE_EMA, PRICE_CLOSE, Current + 0); double Sell1_1 = iMA(NULL, 0, SignalEMA_Period, 0, MODE_EMA, PRICE_CLOSE, Current + 1);//was + 1 on each of these three double Sell1_2 = iMA(NULL, 0, CloseEMA_Period, 0, MODE_EMA, PRICE_CLOSE, Current + 1); double Sell1_3 = iMA(NULL, 0, MainEMA_Period, 0, MODE_EMA, PRICE_CLOSE, Current + 1); double Sell2_1 = iMA(NULL, 0, SignalEMA_Period, 0, MODE_EMA, PRICE_CLOSE, Current + 0); double Sell2_2 = iMA(NULL, 0, CloseEMA_Period, 0, MODE_EMA, PRICE_CLOSE, Current + 0); double Sell2_3 = iMA(NULL, 0, MainEMA_Period, 0, MODE_EMA, PRICE_CLOSE, Current + 0); //Print(" Sell1_2=",Sell1_2," Buy1_2=",Buy1_2); if ( (Buy1_1 < Buy1_3+(Delta1*Point) && Buy2_1 >= Buy2_3+(Delta1*Point)) || (Buy2_1 > Buy2_3 /*&& Buy1_1 <= Buy1_2*/ && Buy2_1 >= Buy2_2+Delta2*Point) ) Order = SIGNAL_BUY; if ( (Sell1_1 > Sell1_3-(Delta1*Point) && Sell2_1 <= Sell2_3-(Delta1*Point)) || (Sell2_1 < Sell2_3 /*&& Sell1_1 >= Sell1_2*/ && Sell2_1 <= Sell2_2-Delta2*Point) ) Order = SIGNAL_SELL; if (Buy1_1 >= Buy1_2 && Buy2_1 < Buy2_2) Order = SIGNAL_CLOSEBUY; if (Sell1_1 <= Sell1_2 && Sell2_1 > Sell2_2) Order = SIGNAL_CLOSESELL; //+------------------------------------------------------------------+ //| Signal End | //+------------------------------------------------------------------+ //Print(" Sell1_2=",Sell1_2," Buy1_2=",Buy1_2); below is the original code: // if ( // (Buy1_1 < Buy1_3 && Buy2_1 >= Buy2_3+Delta*Point) // || // (Buy2_1 > Buy2_3 /*&& Buy1_1 <= Buy1_2*/ && Buy2_1 >= Buy2_2+Delta*Point) // ) Order = SIGNAL_BUY; // // if ( // (Sell1_1 > Sell1_3 && Sell2_1 <= Sell2_3) // || // (Sell2_1 < Sell2_3 /*&& Sell1_1 >= Sell1_2*/ && Sell2_1 <= Sell2_2-Delta*Point) // ) Order = SIGNAL_SELL; // // if (Buy1_1 >= Buy1_2 && Buy2_1 < Buy2_2) Order = SIGNAL_CLOSEBUY; // if (Sell1_1 <= Sell1_2 && Sell2_1 > Sell2_2) Order = SIGNAL_CLOSESELL; //+------------------------------------------------------------------+ //| Signal End | //+------------------------------------------------------------------+ //Buy if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) { if(!ExistPositions()) { //Check free margin if (AccountFreeMargin() < (1000 * Lots)) { Print("We have no money. Free Margin = ", AccountFreeMargin()); return(0); } if (StopLossMode) StopLossLevel = Ask - StopLoss * Point; if (TakeProfitMode) TakeProfitLevel = Ask + TakeProfit * Point; Ticket = OrderSend(Symbol(), OP_BUY, LotsOptimized(), NormalizeDouble(Ask,digit), Slippage, NormalizeDouble(StopLossLevel,digit), NormalizeDouble(TakeProfitLevel,digit), "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue); if(Ticket > 0) { if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) Print("BUY order opened : ", OrderOpenPrice()); else Print("Error opening BUY order : ", GetLastError()); } if (EachTickMode) TickCheck = True; if (!EachTickMode) BarCount = Bars; return(0); } } //Sell if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) { if(!ExistPositions()) { //Check free margin if (AccountFreeMargin() < (1000 * Lots)) { Print("We have no money. Free Margin = ", AccountFreeMargin()); return(0); } if (StopLossMode) StopLossLevel = Bid + StopLoss * Point; if (TakeProfitMode) TakeProfitLevel = Bid - TakeProfit * Point; Ticket = OrderSend(Symbol(), OP_SELL, LotsOptimized(), NormalizeDouble(Bid,digit), Slippage, NormalizeDouble(StopLossLevel,digit), NormalizeDouble(TakeProfitLevel,digit), "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink); if(Ticket > 0) { if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) Print("SELL order opened : ", OrderOpenPrice()); else Print("Error opening SELL order : ", GetLastError()); } if (EachTickMode) TickCheck = True; if (!EachTickMode) BarCount = Bars; return(0); } } //Check position for (int i = 0; i < Total; i ++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if(OrderType() <= OP_SELL && OrderSymbol() == Symbol()) { if(OrderType() == OP_BUY && OrderMagicNumber()==MagicNumber ) { //Close if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) { OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen); if (EachTickMode) TickCheck = True; if (!EachTickMode) BarCount = Bars; return(0); } //Trailing stop if(TrailingStopMode && TrailingStop > 0) { if(Bid - OrderOpenPrice() > Point * TrailingStop) { if(OrderStopLoss() < Bid - Point * TrailingStop) { OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen); if (!EachTickMode) BarCount = Bars; return(0); } } } } else { if(OrderType()==OP_SELL && OrderMagicNumber()==MagicNumber) { //Close if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) { OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange); if (EachTickMode) TickCheck = True; if (!EachTickMode) BarCount = Bars; return(0); } //Trailing stop if (TrailingStopMode && TrailingStop > 0) { if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) { if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) { OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange); if (!EachTickMode) BarCount = Bars; return(0); } } } }} } } if (!EachTickMode) BarCount = Bars; return(0); } bool ExistPositions() { for (int i=0; i0)lot=NormalizeDouble(Lots*AccountFreeMargin()*MaximumRisk/1000.0,1); //---- calcuulate number of losses orders without a break if(DecreaseFactor>0) { for(int i=orders-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; } if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue; //---- if(OrderProfit()>0) break; if(OrderProfit()<0) losses++; } if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1); } //---- return lot size if(lot<0.1) lot=0.1; return(lot); } //+------------------------------------------------------------------+