//+------------------------------------------------------------------+ //| SimpleMA.mq5 | //| Copyright 2024, Your Name | //| https://www.yourwebsite.com | //+------------------------------------------------------------------+ input int FastMA_Period = 10; // Period for the fast MA input int SlowMA_Period = 30; // Period for the slow MA input double LotSize = 0.1; // Lot size for trades input double StopLoss = 100; // Stop loss in points input double TakeProfit = 100; // Take profit in points //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { // Calculate the moving averages double FastMA = iMA(NULL, 0, FastMA_Period, 0, MODE_SMA, PRICE_CLOSE, 0); double SlowMA = iMA(NULL, 0, SlowMA_Period, 0, MODE_SMA, PRICE_CLOSE, 0); double PreviousFastMA = iMA(NULL, 0, FastMA_Period, 0, MODE_SMA, PRICE_CLOSE, 1); double PreviousSlowMA = iMA(NULL, 0, SlowMA_Period, 0, MODE_SMA, PRICE_CLOSE, 1); // Check for a buy signal if (PreviousFastMA < PreviousSlowMA && FastMA > SlowMA) { if (PositionSelect(Symbol()) == false) // Check if there's no existing position { double price = NormalizeDouble(Ask, _Digits); double sl = price - StopLoss * _Point; double tp = price + TakeProfit * _Point; // Open a buy position if (OrderSend(Symbol(), OP_BUY, LotSize, price, 3, sl, tp, "Buy Order", 0, 0, clrGreen) < 0) { Print("Error opening buy order: ", GetLastError()); } } } // Check for a sell signal if (PreviousFastMA > PreviousSlowMA && FastMA < SlowMA) { if (PositionSelect(Symbol()) == false) // Check if there's no existing position { double price = NormalizeDouble(Bid, _Digits); double sl = price + StopLoss * _Point; double tp = price - TakeProfit * _Point; // Open a sell position if (OrderSend(Symbol(), OP_SELL, LotSize, price, 3, sl, tp, "Sell Order", 0, 0, clrRed) < 0) { Print("Error opening sell order: ", GetLastError()); } } } } //+------------------------------------------------------------------+

Comments

Popular Posts