//+------------------------------------------------------------------+ //| NeuroMoneyManagement.mq4 | //| Copyright © 2006, Yury V. Reshetov | //| http://bigforex.biz/publ/4-1-0-3 | //+------------------------------------------------------------------+ #property copyright "Copyright © 2006, Yury V. Reshetov http://bigforex.biz" #property link "http://bigforex.biz/publ/4-1-0-3" //---- input parameters extern int x1 = 61; extern int x2 = 184; extern int x3 = 92; extern int x4 = 7; extern double MaximumRisk = 0.82; // StopLoss level extern double sl = 50; extern int MagicNumber = 888; static int prevtime = 0; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- check new bar if (Time[0] == prevtime) return(0); prevtime = Time[0]; //---- check allowed for trade if (IsTradeAllowed()) { RefreshRates(); } else { prevtime = Time[1]; return(0); } // --- indexed variable int i = 0; // check for opened position int total = OrdersTotal(); for (i = 0; i < total; i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); // check for symbol & magic number if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { // found opened position - exit return(0); } } //--- check for type last position total = OrdersHistoryTotal(); int op = OP_BUY; for (i = 0; i < total; i++) { OrderSelect(i, SELECT_BY_POS, MODE_HISTORY); // check for symbol & magic number if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { // change type if (OrderType() == OP_BUY) { op = OP_SELL; } else { op = OP_BUY; } } } // default lots size is allowed minimum double lt = MarketInfo(Symbol(), MODE_MINLOT); // refresh RefreshRates(); int ticket = -1; // check for long or short position possibility if (op == OP_BUY) { //long if (perceptron() > 0) lt = getLots(); ticket = OrderSend(Symbol(), OP_BUY, lt, Ask, 3, Bid - sl * Point, Bid + sl * Point, "NeuroMM", MagicNumber, 0, Blue); if (ticket < 0) { Sleep(30000); prevtime = Time[1]; } } else { // short if (perceptron() < 0) lt = getLots(); ticket = OrderSend(Symbol(), OP_SELL, lt, Bid, 3, Ask + sl * Point, Ask - sl * Point, "NeuroMM", MagicNumber, 0, Red); if (ticket < 0) { Sleep(30000); prevtime = Time[1]; } } //--- exit return(0); } //+--- The PERCEPRRON ---+ // a perceiving and recognizing function double perceptron() { double w1 = x1 - 100.0; double w2 = x2 - 100.0; double w3 = x3 - 100.0; double w4 = x4 - 100.0; double a1 = iAC(Symbol(), 0, 0); double a2 = iAC(Symbol(), 0, 7); double a3 = iAC(Symbol(), 0, 14); double a4 = iAC(Symbol(), 0, 21); return (w1 * a1 + w2 * a2 + w3 * a3 + w4 * a4); } //+--------------Calculate optimal lot size -------------------------+ 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() * MaximumRisk / 1000.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); }