//+------------------------------------------------------------------+ //| ExFractals.mq4 | //| Copyright © 2006, Alex Sidd (Executer) | //| mailto:work_st@mail.ru | //+------------------------------------------------------------------+ #property copyright "Copyright © 2006, Alex Sidd (Executer)" #property link "mailto:work_st@mail.ru" //---- #define MAGIC 612457 //---- extern double lStopLoss = 40; extern double sStopLoss = 40; extern double lTakeProfit = 100; extern double sTakeProfit = 100; extern double lTrailingStop = 30; extern double sTrailingStop = 30; extern double Lots = 0.10; extern int a = 3; extern double DecreaseFactor = 100; extern double MaximumRisk = 0.025; extern double MaximumLots = 100; extern int coeff = 0; //---- int IsLastBuy = 0; int what = 0; color clOpenBuy = Blue; color clCloseBuy = Aqua; color clOpenSell = Red; color clCloseSell = Violet; color clModiBuy = Blue; color clModiSell = Red; string Name_Expert = "ExFractals"; int Slippage = 0; bool UseSound = True; string NameFileSound = "alert.wav"; //---- double FractUp1 = 0; double FractUp2 = 0; double FractDn1 = 0; double FractDn2 = 0; datetime FTimeUp1, FTimeUp2, FTimeDn1, FTimeDn2, curTime; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double GetSizeLot() { double Lot = Lots; int cycle; int prof = 0; int orders = HistoryTotal(); // history orders total int losses = 0; // number of losses orders without a break int vinn = 0; int i = orders; //---- select lot size Lot = NormalizeDouble(AccountFreeMargin()*MaximumRisk / 100, 1); if(AccountFreeMargin() <= 10000 && AccountFreeMargin() > 5000) DecreaseFactor = NormalizeDouble(AccountFreeMargin() / 100, 1); if(AccountFreeMargin() <= 5000 && AccountFreeMargin() > 3000) DecreaseFactor = NormalizeDouble(AccountFreeMargin() / 200, 1); if(AccountFreeMargin() <= 14) DecreaseFactor = 14; if(AccountFreeMargin() > 10000) DecreaseFactor = 60; if(DecreaseFactor > 0 && orders > DecreaseFactor) { for(cycle = 1; cycle < DecreaseFactor; cycle++) { i--; if(OrderSelect(i, SELECT_BY_TICKET, MODE_HISTORY) == false) { Print("Error in history!"); break; } if(OrderCloseTime()>0) { prof = prof + OrderProfit(); if(OrderProfit() <= 0 ) losses++; else vinn++; } } if(prof <= 0 ) Lot = 0.1;//Lot - (0.1*losses); //Lot = 0.1;// // if(prof <=0 && vinn>losses) Lot = Lot - (0.1*losses); if(prof > 0 && losses > vinn) { Lot = Lot + (0.1*NormalizeDouble(vinn / 4, 0.1)); } if(prof > 0 && losses <= vinn ) { Lot = Lot + (0.1*NormalizeDouble(vinn / 2, 0.1)); } } if(AccountFreeMargin() < 300 || Lot < 0.1) Lot = 0.1; if(Lot*1275 >= AccountFreeMargin()) Lot = NormalizeDouble(AccountFreeMargin()*MaximumRisk / 100, 1); if(MaximumLots != 0 && Lot > MaximumLots) Lot = MaximumLots; if(DecreaseFactor > orders) Lot = Lots; Lot = Lots; //---- return(Lot); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void deinit() { //---- } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double LevelCalculate(double Price1, double Time1, double Price2, double Time2, double NewTime) { double level; if(Time2 != Time1) // На всякий случай, чтобы не было деления на 0. level = (NewTime - Time1)*(Price2 - Price1) / (Time2-Time1) + Price1; else return(Price2); return(level); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int start() { if(Bars < 100) { Print("bars less than 100"); return(0); } if(lStopLoss < 3) { Print("StopLoss less than 3"); return(0); } if(lTakeProfit < 3) { Print("TakeProfit less than 3"); return(0); } if(sStopLoss < 3) { Print("StopLoss less than 3"); return(0); } if(sTakeProfit < 3) { Print("TakeProfit less than 3"); return(0); } if(AccountFreeMargin() < (1000*Lots)) { Print("We have no money. Free Margin = ", AccountFreeMargin()); return(0); } double Fup, Fdn; Fup = iFractals(NULL, 0, MODE_UPPER, 3); Fdn = iFractals(NULL, 0, MODE_LOWER, 3); double AO_1, AO_2, AO_D1, AO_D2; AO_1 = iAO(NULL, 60, 1); AO_2 = iAO(NULL, 60, 2); AO_D1 = iAO(NULL, 240, 1); AO_D2 = iAO(NULL, 240, 2); if(AO_1 > AO_2 && AO_D1 > AO_D2) what = 1; if(AO_1 < AO_2 && AO_D1 < AO_D2) what = 2; if(AO_1 > AO_2 && AO_D1 < AO_D2) what = 3; if(AO_1 < AO_2 && AO_D1 > AO_D2) what = 4; if(Fup > 0) { if(FractUp1 == 0 && FractUp2 == 0) { FractUp1 = Fup; FTimeUp1 = Time[3]; } if(FractUp1 != 0&&FractUp2 == 0 && FTimeUp1 != Time[3]) { FractUp2 = Fup; FTimeUp2 = Time[3]; } if(FractUp1 != 0 && FractUp2 != 0 && FTimeUp2 != Time[3]) { FractUp1 = FractUp2; FTimeUp1 = FTimeUp2; FractUp2 = Fup; FTimeUp2 = Time[3]; } } if(Fdn > 0) { if(FractDn1 == 0 && FractDn2 == 0) { FractDn1 = Fdn; FTimeDn1 = Time[3]; } if(FractDn1 != 0 && FractDn2 == 0 && FTimeDn1 != Time[3]) { FractDn2 = Fdn; FTimeDn2 = Time[3]; } if(FractDn1 != 0 && FractDn2 != 0 && FTimeDn2 != Time[3]) { FractDn1 = FractDn2; FTimeDn1 = FTimeDn2; FractDn2 = Fdn; FTimeDn2 = Time[3]; } } double lvldn, lvlup; double I1 = iCustom(NULL, 0, "ExVol", 0, 1); if(FractUp1 != 0 && FractUp2 != 0) { lvlup = LevelCalculate(FractUp1, FTimeUp1, FractUp2, FTimeUp2, Time[0]); lvldn = LevelCalculate(FractDn1, FTimeDn1, FractDn2, FTimeDn2, Time[0]); ObjectCreate("FractalLineUp", OBJ_TREND, 0, FTimeUp1, FractUp1, FTimeUp2, FractUp2); ObjectSet("FractalLineUp", OBJPROP_TIME1, FTimeUp1); ObjectSet("FractalLineUp", OBJPROP_PRICE1, FractUp1); ObjectSet("FractalLineUp", OBJPROP_TIME2, FTimeUp2); ObjectSet("FractalLineUp", OBJPROP_PRICE2, FractUp2); if(!ExistPositionsBuy()) { if(Ask > lvlup && Close[1] > lvlup&&I1 < 0) { OpenBuy(lvlup); ObjectCreate("FractUp" + Bars, OBJ_ARROW, 0, Time[0], lvlup); ObjectSet("FractUp" + Bars, OBJPROP_COLOR, Blue); ObjectSet("FractUp" + Bars, OBJPROP_ARROWCODE, SYMBOL_ARROWUP); return(0); } } } if(ExistPositionsBuy()) { if((what == 1 && AO_1 < AO_2 || AO_D1 < AO_D2) || (what == 2 && AO_1 > AO_2 || AO_D1 > AO_D2) || (what == 3 && AO_1 < AO_2 || AO_D1 > AO_D2) || (what == 4 && AO_1 > AO_2 || AO_D1 < AO_D2)) CloseBuy(); } if(FractDn1 != 0 && FractDn2 != 0) { lvldn = LevelCalculate(FractDn1, FTimeDn1, FractDn2, FTimeDn2, Time[0]); lvlup = LevelCalculate(FractUp1, FTimeUp1, FractUp2, FTimeUp2, Time[0]); ObjectCreate("FractalLineDn", OBJ_TREND, 0, FTimeDn1, FractDn1, FTimeDn2, FractDn2); ObjectSet("FractalLineDn", OBJPROP_TIME1, FTimeDn1); ObjectSet("FractalLineDn", OBJPROP_PRICE1, FractDn1); ObjectSet("FractalLineDn", OBJPROP_TIME2, FTimeDn2); ObjectSet("FractalLineDn", OBJPROP_PRICE2, FractDn2); if(!ExistPositionsSell()) { if(lvldn > Bid && Close[1] < lvldn && I1 > 0) { OpenSell(lvldn); ObjectCreate("FractDn" + Bars, OBJ_ARROW, 0, Time[0], lvldn); ObjectSet("FractDn" + Bars, OBJPROP_COLOR, Red); ObjectSet("FractDn" + Bars, OBJPROP_ARROWCODE, SYMBOL_ARROWDOWN); return(0); } } } if(ExistPositionsSell()) { if((what == 1 && AO_1 < AO_2 || AO_D1 < AO_D2) || (what == 2 && AO_1 > AO_2 || AO_D1 > AO_D2) || (what == 3 && AO_1 < AO_2 || AO_D1 > AO_D2) || (what == 0 && AO_1 > AO_2 || AO_D1 < AO_D2)) CloseSell();// ; } TrailingPositionsBuy(lTrailingStop); TrailingPositionsSell(sTrailingStop); //---- return (0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool ExistPositionsSell() { for(int i = 0; i < OrdersTotal(); i++) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if(OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC && OrderType() == OP_SELL) { return(True); } } } //---- return(false); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool ExistPositionsBuy() { for(int i = 0; i < OrdersTotal(); i++) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if(OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC && OrderType() == OP_BUY) { return(True); } } } //---- return(false); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void TrailingPositionsBuy(int trailingStop) { for(int i = 0; i < OrdersTotal(); i++) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if(OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC) { if(OrderType() == OP_BUY) { if(Bid - OrderOpenPrice() > trailingStop*Point) { if(OrderStopLoss() < Bid - trailingStop*Point) ModifyStopLoss(Bid - trailingStop*Point); } } } } } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void TrailingPositionsSell(int trailingStop) { for(int i = 0; i < OrdersTotal(); i++) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if(OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC) { if(OrderType() == OP_SELL) { if(OrderOpenPrice() - Ask > trailingStop*Point) { if(OrderStopLoss() > Ask + trailingStop*Point || OrderStopLoss() == 0) ModifyStopLoss(Ask + trailingStop*Point); } } } } } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void ModifyStopLoss(double ldStopLoss) { bool fm; fm = OrderModify(OrderTicket(), OrderOpenPrice(), ldStopLoss, OrderTakeProfit(), 0, CLR_NONE); if(fm && UseSound) PlaySound(NameFileSound); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CloseBuy() { bool fc; fc = OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, clCloseBuy); if(fc && UseSound) PlaySound(NameFileSound); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CloseSell() { bool fc; fc = OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, clCloseSell); if(fc && UseSound) PlaySound(NameFileSound); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int OpenBuy(double pr_op) { double ldLot, ldStop, ldTake; string lsComm; ldLot = GetSizeLot(); ldStop = GetStopLossBuy(); ldTake = GetTakeProfitBuy(); lsComm = GetCommentForOrder(); int i = OrderSend(Symbol(), OP_BUY, ldLot, NormalizeDouble(Ask, 4), Slippage, ldStop, ldTake, lsComm, MAGIC, 0, clOpenBuy); if(UseSound) PlaySound(NameFileSound); return(i); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int OpenSell(double pr_op) { double ldLot, ldStop, ldTake; string lsComm; ldLot = GetSizeLot(); ldStop = GetStopLossSell(); ldTake = GetTakeProfitSell(); lsComm = GetCommentForOrder(); int i = OrderSend(Symbol(), OP_SELL, ldLot, NormalizeDouble(Bid, 4), Slippage, ldStop, ldTake, lsComm, MAGIC, 0, clOpenSell); if(UseSound) PlaySound(NameFileSound); return(i); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ string GetCommentForOrder() { return(Name_Expert); } //double GetSizeLot() { return(Lots); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double GetStopLossBuy() { return (Bid - lStopLoss*Point); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double GetStopLossSell() { return(Ask + sStopLoss*Point); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double GetTakeProfitBuy() { return(Ask + lTakeProfit*Point); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double GetTakeProfitSell() { return(Bid - sTakeProfit*Point); } //+------------------------------------------------------------------+