//+------------------------------------------------------------------+ //| Candle.mq4 | //| Copyright © 2011, Serg Deev | //| http://www.work2it.ru | //+------------------------------------------------------------------+ #property copyright "Copyright © 2011, Serg Deev" #property link "http://www.work2it.ru" #define MAGIC 20050610 double MaxRisk = 0.0; // процент риска для вычисления размера лота (если 0 - не работает) extern double Lots = 0.1; // фиксированный лот (при MaxRisk=0) extern int StepProfit = 300; // шаг профита extern int StopLost = 500; // начальный стоплост extern int TrailingStop = 600; // трал extern int NumOrders = 3; extern int ma_fast = 8; extern int ma_slow = 80; extern int shift_min = 20; // крутизна характеристики в пипсах extern int macd_fast = 4; extern int macd_slow = 76; double MinLot; double MaxLot; double LotStep; int Spread; int StopLevel; double StartLots; double NextProfit; extern int slippage = 5; extern bool UseAllTicks = false; extern bool UseFractalTrail = false; string sx = ""; //+------------------------------------------------------------------+ int init() { StopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL); Spread = MarketInfo(Symbol(),MODE_SPREAD); MinLot = MarketInfo(Symbol(),MODE_MINLOT); MaxLot = MarketInfo(Symbol(),MODE_MAXLOT); LotStep = MarketInfo(Symbol(),MODE_LOTSTEP); return(0); } //+------------------------------------------------------------------+ int deinit() { return(0); } double LotNormalize(double x) { x=MathFloor(x*100)/100; if (x > MaxLot) x = MaxLot; if (x < MinLot) x = MinLot; return(x); } //+------------------------------------------------------------------+ double Get_Lots() { double lot=Lots; if (MaxRisk > 0) { double RiskSumm = AccountFreeMargin()*MaxRisk; lot=RiskSumm/StopLost/100; } return(LotNormalize(lot)); } //+------------------------------------------------------------------+ bool Signal_Stop_Buy() { return(false); } //+------------------------------------------------------------------+ bool Signal_Stop_Sell() { return(false); } //+------------------------------------------------------------------+ bool Signal_Buy() { int x1,x2; if (iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,1) > iMA(NULL,0,ma_slow,0,MODE_EMA,PRICE_CLOSE,1)) if (iMA(NULL,0,ma_slow,0,MODE_EMA,PRICE_CLOSE,1) > iMA(NULL,0,ma_slow,0,MODE_EMA,PRICE_CLOSE,3)) if (iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,1) > iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,2)) if (iMACD(NULL,0,macd_fast,macd_slow,1,PRICE_CLOSE,MODE_MAIN,1) > 0.0) if (iMACD(NULL,0,macd_fast,macd_slow,1,PRICE_CLOSE,MODE_MAIN,3) < 0.0) { x1 = (iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,1) - iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,2))/Point; x2 = (iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,2) - iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,3))/Point; if (x1 > shift_min) { if (x1 >= x2) return(true); if (x2 <= 0) return(true); } } return(false); } //+------------------------------------------------------------------+ bool Signal_Sell() { int x1,x2; if (iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,1) < iMA(NULL,0,ma_slow,0,MODE_EMA,PRICE_CLOSE,1)) if (iMA(NULL,0,ma_slow,0,MODE_EMA,PRICE_CLOSE,1) < iMA(NULL,0,ma_slow,0,MODE_EMA,PRICE_CLOSE,3)) if (iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,1) < iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,2)) if (iMACD(NULL,0,macd_fast,macd_slow,1,PRICE_CLOSE,MODE_MAIN,1) < 0.0) if (iMACD(NULL,0,macd_fast,macd_slow,1,PRICE_CLOSE,MODE_MAIN,3) > 0.0) { x1 = (iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,2) - iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,1))/Point; x2 = (iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,3) - iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,2))/Point; if (x1 > shift_min) { if (x1 >= x2) return(true); if (x2 <= 0) return(true); } } return(false); } //+------------------------------------------------------------------+ void CheckForOpen(int mode) { int res,i; if (Volume[0]>1) return; if ((mode == OP_SELL) && Signal_Sell()) { StartLots = Get_Lots(); for (i=0; iNumOrders)) return; for(int i=0;i= MAGIC) && (OrderMagicNumber() < (MAGIC+NumOrders))) || OrderSymbol()!=Symbol()) continue; //---- check order type if(OrderType()==OP_BUY) { if (Signal_Stop_Buy()) OrderClose(OrderTicket(),OrderLots(),Bid,slippage,White); else if (OrderStopLoss() == 0.0) { SL = NormalizeDouble((Bid-StopLost*Point),Digits); TP = NormalizeDouble(Ask + (OrderMagicNumber()-MAGIC+1)*StepProfit*Point,Digits); if ((OrderMagicNumber()-MAGIC) == (NumOrders-1)) TP = 0.0; OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Blue); } else if ((TrailingStop > 0) || (UseFractalTrail)) { profit = (Bid-OrderOpenPrice())/Point; if (profit > 0.95*StepProfit) { if (UseFractalTrail) { fx = iFractals(NULL,0,MODE_LOWER,3); if ((fx > 0) && (fx > OrderStopLoss())) { OrderModify(OrderTicket(),OrderOpenPrice(),fx,OrderTakeProfit(),0,Blue); } } else if (TrailingStop > 0) { SL = NormalizeDouble((Bid-TrailingStop*Point),Digits); if (SL > OrderStopLoss()) OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0,Blue); } } } } if(OrderType()==OP_SELL) { if (Signal_Stop_Sell()) OrderClose(OrderTicket(),OrderLots(),Ask,slippage,White); else if (OrderStopLoss() == 0.0) { SL = NormalizeDouble((Ask+StopLost*Point),Digits); TP = NormalizeDouble(Bid - (OrderMagicNumber()-MAGIC+1)*StepProfit*Point,Digits); if ((OrderMagicNumber()-MAGIC) == (NumOrders-1)) TP = 0.0; OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Red); } else if ((TrailingStop > 0) || (UseFractalTrail)) { profit = (OrderOpenPrice()-Ask)/Point; if (profit > 0.95*StepProfit) { if (UseFractalTrail) { fx = iFractals(NULL,0,MODE_UPPER,3); if ((fx > 0) && (fx < OrderStopLoss())) { OrderModify(OrderTicket(),OrderOpenPrice(),fx,OrderTakeProfit(),0,Red); } } else if (TrailingStop > 0) { SL = NormalizeDouble((Ask+TrailingStop*Point),Digits); if (SL < OrderStopLoss()) OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0,Red); } } } } } } //+------------------------------------------------------------------+ int CalculateCurrentOrders(int mode) { int num=0; for(int i=0;i= MAGIC) && (OrderMagicNumber() < (MAGIC+NumOrders))) { if ((OrderType()==OP_BUY) && (mode==OP_BUY)) num++; if ((OrderType()==OP_SELL) && (mode==OP_SELL)) num++; } } return(num); } //+------------------------------------------------------------------+ void start() { if(Bars<100 || IsTradeAllowed()==false) return; if (CalculateCurrentOrders(OP_BUY)==0) CheckForOpen(OP_BUY); if (CalculateCurrentOrders(OP_SELL)==0) CheckForOpen(OP_SELL); if ((CalculateCurrentOrders(OP_BUY) > 0) || (CalculateCurrentOrders(OP_SELL) > 0))CheckForClose(); } //+------------------------------------------------------------------+