//+------------------------------------------------------------------+ //| Script tick function | //+------------------------------------------------------------------+ void OnTick() { // Calculate moving averages double ma7 = iMA(_Symbol, PERIOD_CURRENT, 7, 0, MODE_SMA, PRICE_CLOSE, 0); double ma5 = iMA(_Symbol, PERIOD_CURRENT, 5, 0, MODE_SMA, PRICE_CLOSE, 0); // Check for buy/sell conditions if (ma7 < ma5 && Close[1] > ma5 && Open[1] < ma5) { // Buy condition met, execute trade if (OrderSelect(0, SELECT_BY_POS) && OrderType() == OP_BUY) { // Close existing buy order OrderClose(OrderTicket(), OrderLots(), Bid, 3, clrNONE); } else { // Open new buy order OrderSend(_Symbol, OP_BUY, 0.1, Ask, 3, Bid-20*Point, Bid+20*Point, "MyOrder", 0, 0, Green); } } else if (ma7 > ma5 && Close[1] < ma5 && Open[1] > ma5) { // Sell condition met, execute trade if (OrderSelect(0, SELECT_BY_POS) && OrderType() == OP_SELL) { // Close existing sell order OrderClose(OrderTicket(), OrderLots(), Ask, 3, clrNONE); } else { // Open new sell order OrderSend(_Symbol, OP_SELL, 0.1, Bid, 3, Ask-20*Point, Ask+20*Point, "MyOrder", 0, 0, Red); } } } //+------------------------------------------------------------------+ //| Risk management function | //+------------------------------------------------------------------+ void OnTrade() { // Calculate fixed risk amount (5% of initial balance) double riskAmount = AccountBalance() * 0.05; // Set stop-loss and take-profit levels double stopLoss = 15 * Point; double takeProfit = 75 * Point; // Set lot size based on risk amount double lotSize = riskAmount / (stopLoss * 100); // Execute trade with calculated lot size OrderSend(_Symbol, OP_BUY, lotSize, SymbolInfoDouble(_Symbol, SYMBOL_ASK), 3, stopLoss, takeProfit, "MyOrder", 0, 0, Green); }