//+------------------------------------------------------------------+ //| Sadukey_exp.mq4 | //+------------------------------------------------------------------+ #property copyright "Copyright © 2009, " #property link "" #include <stdlib.mqh> extern int TakeProfit = 300; extern int StopLoss = 200; extern int TrailingStop = 120; extern int TrailingStep = 20; extern bool CloseOppositePosition = false; extern double Lots = 0.1; // ëîò extern int Slippage = 1; extern string s______ = "Ïàðàìåòðû èíäèêàòîðà Stoch"; extern int KPeriod = 30; extern int Slowing = 10; extern int DPeriod = 10; extern string note4 = "0=sma, 1=ema, 2=smma, 3=lwma"; extern int MAMethod = 0; extern string note5 = "0=high/low, 1=close/close"; extern int PriceField = 1; extern string note6 = "overbought level"; extern int overBought = 60; extern string note7 = "oversold level"; extern int overSold = 40; #define Buy 0 #define Sell 1 #define Unknown 2 #define Up 0 #define Dn 1 double spread; int BuyMagic = 5124000; int SellMagic = 51250000; bool CanOpenBuy = true; bool CanOpenSell = true; int prevBars = -1; int init() { spread = Ask - Bid; CanOpenBuy = true; CanOpenSell = true; BuyMagic = 5124000; SellMagic = 5125000; prevBars = -1; RunIndicator(); return(0); } int start() { DoTrail(); int curBars = iBars(Symbol(), Period()); if (prevBars == -1) { prevBars = curBars; } if (curBars == prevBars) { return; } // ïîÿâèëñÿ íîâûé áàð prevBars = curBars; double Stoch = iCustom(Symbol(), 0, "Color Stochastic", "", KPeriod, Slowing, DPeriod, "", MAMethod, "", PriceField, "", overBought, "", overSold, 1, 1); double Filter1 = iCustom(Symbol(), 0, "Figure", 0, 1); double Filter2 = iCustom(Symbol(), 0, "Figure", 2, 1); double Filter3 = iCustom(Symbol(), 0, "Figure", 3, 1); int Direction; if(Filter1 > Filter3 && Filter2 > Filter3) { CanOpenSell = true; if(Filter1 > Filter2 && Filter2 > Filter3) { Direction = Up; } else { Direction = Unknown; } } else if(Filter1 < Filter3 && Filter2 < Filter3) { CanOpenBuy = true; if(Filter1 < Filter2 && Filter2 < Filter3) { Direction = Dn; } else { Direction = Unknown; } } else { Direction = Unknown; } if(CanOpenBuy && Stoch >= overBought && Direction == Up) { if(CloseOppositePosition) { CloseAllSell(); } CanOpenBuy = false; OpenBuy(); } if(CanOpenSell && Stoch <= overSold && Direction == Dn) { if(CloseOppositePosition) { CloseAllBuy(); } CanOpenSell = false; OpenSell(); } return(0); } // ôóíêöèÿ ïðîãîíÿåò èíäèêàòîð íà èñòîðèè äî òåêóùåãî ìîìåíòà äëÿ òîãî, ÷òîá óñòàíîâèòü òåêóùåå íàïðàâëåíèå èíäèêàòîðà è // óñòàíîâèòü ïðàâèëüíûå çíà÷åíèÿ ïåðåìåííûõ, îòâå÷àþùèõ çà òîðãîâëþ void RunIndicator() { int i; for(i = 100; i >= 1; i--) { double Stoch = iCustom(Symbol(), 0, "Color Stochastic", "", KPeriod, Slowing, DPeriod, "", MAMethod, "", PriceField, "", overBought, "", overSold, 1, i); double Filter1 = iCustom(Symbol(), 0, "Figure", 0, i); double Filter2 = iCustom(Symbol(), 0, "Figure", 2, i); double Filter3 = iCustom(Symbol(), 0, "Figure", 3, i); int Direction; if(Filter1 > Filter3 && Filter2 > Filter3) { CanOpenSell = true; if(Filter1 > Filter2 && Filter2 > Filter3) { Direction = Up; } else { Direction = Unknown; } } else if(Filter1 < Filter3 && Filter2 < Filter3) { CanOpenBuy = true; if(Filter1 < Filter2 && Filter2 < Filter3) { Direction = Dn; } else { Direction = Unknown; } } else { Direction = Unknown; } if(CanOpenBuy && Stoch >= overBought && Direction == Up) { CanOpenBuy = false; } if(CanOpenSell && Stoch <= overSold && Direction == Dn) { CanOpenSell = false; } } } void OpenBuy() { int i; int Orders = OrdersTotal(); double BuyOpen; double BuyTP = 0; double BuySL = 0; RefreshRates(); BuyOpen = Ask; if(TakeProfit != 0) BuyTP = BuyOpen + TakeProfit*Point; if(StopLoss != 0) { BuySL = BuyOpen - StopLoss*Point; } //---------------------------------- datetime begin = TimeCurrent(); int ticket = OrderSend(Symbol(), OP_BUY, Lots, BuyOpen, Slippage, BuySL, BuyTP, "", BuyMagic, 0, Blue); int error; if (ticket==-1) { while(ticket == -1 && TimeCurrent() - begin < 20) { error = GetLastError(); Print("Îøèáêà îòêðûòèÿ îðäåðà BUY. " + ErrorDescription(error)); Sleep(100); Print("Ïîâòîð"); RefreshRates(); BuyOpen = Ask; if(TakeProfit != 0) BuyTP = BuyOpen + TakeProfit*Point; if(StopLoss != 0) { BuySL = BuyOpen - StopLoss*Point; } ticket = OrderSend(Symbol(), OP_BUY, Lots, BuyOpen, Slippage, BuySL, BuyTP, "", BuyMagic, 0, Blue); } if(ticket == -1) { error = GetLastError(); Print("Îøèáêà îòêðûòèÿ îðäåðà BUY. " + ErrorDescription(error)); } } if (ticket != -1) { Print("Îòêðûòî BUY íà " + Symbol() + " " + Period() + " îáúåìîì " + Lots + "!"); } } void OpenSell() { int i; int Orders = OrdersTotal(); double SellOpen; double SellTP = 0; double SellSL = 0; RefreshRates(); SellOpen = Bid; if(TakeProfit != 0) SellTP = SellOpen - TakeProfit*Point; if(StopLoss != 0) { SellSL = SellOpen + StopLoss*Point; } datetime begin = TimeCurrent(); int ticket = OrderSend(Symbol(), OP_SELL, Lots, SellOpen, Slippage, SellSL, SellTP, "", SellMagic, 0, Red); int error; if (ticket==-1) { //error=GetLastError(); //Print("SELL error(",error,"): ",ErrorDescription(error)); while(ticket == -1 && TimeCurrent() - begin < 20) { error = GetLastError(); Print("Îøèáêà îòêðûòèÿ îðäåðà SELL. " + ErrorDescription(error)); Sleep(100); Print("Ïîâòîð"); RefreshRates(); SellOpen = Bid; if(TakeProfit != 0) SellTP = SellOpen - TakeProfit*Point; if(StopLoss != 0) { SellSL = SellOpen + StopLoss*Point; } ticket = OrderSend(Symbol(), OP_SELL, Lots, SellOpen, Slippage, SellSL, SellTP, "", SellMagic, 0, Red); } if(ticket == -1) { error = GetLastError(); Print("Îøèáêà îòêðûòèÿ îðäåðà SELL. " + ErrorDescription(error)); } } if (ticket != -1) { Print("Îòêðûòî SELL íà " + Symbol() + " " + Period() + " îáúåìîì " + Lots + "!"); } } void DoTrail() { double TickSize = 0; if(Digits == 5) { TickSize = 0.00005; } else { TickSize = 0; } for (int i=0; i < OrdersTotal(); i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() == Symbol()) { if(OrderType() == OP_BUY) { if(OrderMagicNumber() == BuyMagic) { double newstop = (Bid - MathMod(Bid, TickSize)) - (TrailingStop*Point - MathMod(TrailingStop*Point, TickSize)); if(newstop > OrderOpenPrice() && (OrderOpenPrice() > OrderStopLoss() || OrderStopLoss() == 0)) { if(!OrderModify(OrderTicket(), OrderOpenPrice(), newstop, OrderTakeProfit(), 0, Pink)) { Print("Íå ïîëó÷èëîñü ìîäèôèöèðîâàòü îðäåð ¹" + OrderTicket() + ". Îøèáêà¹" + GetLastError()); } } else if (OrderStopLoss() > OrderOpenPrice() && newstop - OrderStopLoss() >= TrailingStep*Point) { if(!OrderModify(OrderTicket(), OrderOpenPrice(), newstop, OrderTakeProfit(), 0, Pink)) { Print("Íå ïîëó÷èëîñü ìîäèôèöèðîâàòü îðäåð ¹" + OrderTicket() + ". Îøèáêà¹" + GetLastError()); } } } } else if (OrderType() == OP_SELL) { if(OrderMagicNumber() == SellMagic) { newstop = (Ask - MathMod(Ask, TickSize)) + (TrailingStop*Point - MathMod(TrailingStop*Point, TickSize)); if(newstop < OrderOpenPrice() && (OrderStopLoss() > OrderOpenPrice() || OrderStopLoss() == 0)) { if(!OrderModify(OrderTicket(), OrderOpenPrice(), newstop, OrderTakeProfit(), 0 ,Pink)) { Print("Íå ïîëó÷èëîñü ìîäèôèöèðîâàòü îðäåð ¹" + OrderTicket() + ". Îøèáêà¹" + GetLastError()); } } else if(OrderStopLoss() < OrderOpenPrice() && OrderStopLoss() - (newstop) >= TrailingStep*Point) { if(!OrderModify(OrderTicket(), OrderOpenPrice(), newstop, OrderTakeProfit(), 0 ,Pink)) { Print("Íå ïîëó÷èëîñü ìîäèôèöèðîâàòü îðäåð ¹" + OrderTicket() + ". Îøèáêà¹" + GetLastError()); } } } } } } } void CloseAllSell() { int i; int orders = OrdersTotal(); bool result; datetime begin; if(orders > 0) { for(i = orders-1; i >= 0; i--) { if( OrderSelect(i, SELECT_BY_POS, MODE_TRADES) ) { if(OrderSymbol() == Symbol() && OrderType() == OP_SELL && OrderMagicNumber() == SellMagic) { begin = TimeCurrent(); result = OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Yellow); // çàêðûâàåì âñå âñòðå÷íûå îðäåðà while(!result && TimeCurrent() - begin <= 30 && !IsTesting()) { Sleep(100); RefreshRates(); result = OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Yellow); // çàêðûâàåì âñå âñòðå÷íûå îðäåðà } if(!result) { int error = GetLastError(); Print("Îøèáêà çàêðûòèÿ îðäåðà SELL #" + OrderTicket() + " " + ErrorDescription(error)); } } } else { Print("Íå óäàëîñü âûáðàòü îòêðûòûé îðäåð:" + ErrorDescription(error)); } } } } void CloseAllBuy() { int i; int Orders = OrdersTotal(); bool result; datetime begin; if(Orders > 0) { for(i = Orders-1; i >= 0; i--) { if( OrderSelect(i, SELECT_BY_POS, MODE_TRADES) ) { if(OrderSymbol() == Symbol() && OrderType() == OP_BUY && OrderMagicNumber() == BuyMagic) { begin = TimeCurrent(); result = OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Yellow); // çàêðûâàåì âñå âñòðå÷íûå îðäåðà while(!result && TimeCurrent() - begin <= 30 && !IsTesting()) { Sleep(100); RefreshRates(); result = OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Yellow); // çàêðûâàåì âñå âñòðå÷íûå îðäåðà } if(!result) { int error = GetLastError(); Print("Îøèáêà çàêðûòèÿ îðäåðà BUY #" + OrderTicket() + " " + ErrorDescription(error)); } } } else { Print("Íå óäàëîñü âûáðàòü îòêðûòûé îðäåð:" + ErrorDescription(error)); } } } } int deinit() { return(0); }
Sample
Analysis
Market Information Used:
Indicator Curves created:
Indicators Used:
Custom Indicators Used:
Color Stochastic
Figure
Order Management characteristics:
Checks for the total of open orders
It automatically opens orders when conditions are reached
It can change open orders parameters, due to possible stepping strategy
It Closes Orders by itself
Other Features: