//+------------------------------------------------------------------+ //| gold-dust.mq4 | //| Copyright © 2011, Yury V. Reshetov | //| http://gold-dust.info | //+------------------------------------------------------------------+ #property copyright "Copyright © 2011, Yury V. Reshetov" #property link "http://gold-dust.info" //---- input parameters extern int x11 = 100; extern int x21 = 100; extern int x31 = 100; extern int x41 = 100; extern int x12 = 100; extern int x22 = 100; extern int x32 = 100; extern int x42 = 100; extern int p = 20; extern double sl = 500; extern int pass = 1; extern double lots = 1.0; extern int moneymanagement = 0; extern double risk = 0.0; extern int mn = 888; static int prevtime = 0; int init() { prevtime = Time[0]; return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { if (Time[0] == prevtime) { return(0); } prevtime = Time[0]; double spread = 3.0; if(IsTradeAllowed()) { RefreshRates(); spread = (Ask - Bid) / Point; } else { again(); return(0); } int ticket = -1; // check for opened position int total = OrdersTotal(); //---- for(int i = total - 1; i >= 0; i--) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); // check for symbol & magic number if ((OrderSymbol() == Symbol()) && (OrderMagicNumber() == mn)) { if (Supervisor() == 0) { if (OrderType() == OP_BUY) { // check profit if (Bid > (OrderStopLoss() + (sl + spread) * Point)) { // Close position if (! OrderClose(OrderTicket(), OrderLots(), Bid, 2, Blue)) { again(); } } } else { // short position is opened // check profit if (Ask < (OrderStopLoss() - (sl + spread) * Point)) { // Close position if (! OrderClose(OrderTicket(), OrderLots(), Ask, 2, Red)) { again(); } } } } else { int prevticket = OrderTicket(); // long position is opened if (OrderType() == OP_BUY) { // check profit if (Bid > (OrderStopLoss() + (sl + spread) * Point)) { if (Supervisor() > 0) { // reverse ticket = OrderSend(Symbol(), OP_SELL, lots * 2.0, Bid, 3, Ask + sl * Point, 0, WindowExpertName(), mn, 0, Red); Sleep(30000); //---- if (ticket < 0) { again(); } else { OrderCloseBy(ticket, prevticket, Blue); } } else { // trailing stop if (!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - sl * Point, 0, 0, Blue)) { again(); } } } } else { // short position is opened // check profit if (Ask < (OrderStopLoss() - (sl + spread) * Point)) { if(Supervisor() < 0) { // reverse ticket = OrderSend(Symbol(), OP_BUY, lots * 2.0, Ask, 3, Bid - sl * Point, 0, WindowExpertName(), mn, 0, Blue); Sleep(30000); //---- if (ticket < 0) { again(); } else { OrderCloseBy(ticket, prevticket, Blue); } } else { // trailing stop if (!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + sl * Point, 0, 0, Blue)) { again(); } } } } } // exit return(0); } } RefreshRates(); if (Supervisor() == 0) { return(0); } double lt = lots; if (moneymanagement > 0) { lt = getLots(); } if (Supervisor() < 0) { ticket = OrderSend(Symbol(), OP_BUY, lt, Ask, 1, Bid - sl * Point, 0, WindowExpertName(), mn, 0, Blue); if (ticket < 0) { again(); } } else { ticket = OrderSend(Symbol(), OP_SELL, lt, Bid, 1, Ask + sl * Point, 0, WindowExpertName(), mn, 0, Red); if (ticket < 0) { again(); } } //-- Exit -- return(0); } //+--------------------------- Supervisor ----------------------------------+ int Supervisor() { if (pass == 1) { return (perceptron(x11, x21, x31, x41)); } if (pass == 2) { return (perceptron(x12, x22, x32, x42)); } if (pass == 3) { if (perceptron(x11, x21, x31, x41) == perceptron(x12, x22, x32, x42)) { return (perceptron(x11, x21, x31, x41)); } } return (0); } int perceptron(int x1, int x2, int x3, int x4) { double w1 = x1 - 100.0; double w2 = x2 - 100.0; double w3 = x3 - 100.0; double w4 = x4 - 100.0; double a1 = Close[0] - iMA(Symbol(), 0, p, 0, MODE_SMA, PRICE_OPEN, 0); double a2 = Open[p] - iMA(Symbol(), 0, p, 0, MODE_SMA, PRICE_OPEN, p); double a3 = Open[p * 2] - iMA(Symbol(), 0, p, 0, MODE_SMA, PRICE_OPEN, p * 2); double a4 = Open[p * 3] - iMA(Symbol(), 0, p, 0, MODE_SMA, PRICE_OPEN, p * 3); double result = w1 * a1 + w2 * a2 + w3 * a3 + w4 * a4; if (result > 0.0) { return (1); } return (-1); } void again() { prevtime = Time[1]; Sleep(30000); } double getLots() { double minlot = MarketInfo(Symbol(), MODE_MINLOT); int round = MathAbs(MathLog(minlot) / MathLog(10.0)) + 0.5; double lot = minlot; //---- select lot size lot = NormalizeDouble(AccountFreeMargin() * risk / 100000.0, round); if (AccountFreeMargin() < lot * MarketInfo(Symbol(), MODE_MARGINREQUIRED)) { lot = NormalizeDouble(AccountFreeMargin() / MarketInfo(Symbol(), MODE_MARGINREQUIRED), round); } if(lot < minlot) lot = minlot; double maxlot = MarketInfo(Symbol(), MODE_MAXLOT); if(lot > maxlot) lot = maxlot; //---- return lot size return(lot); }