#include CTrade TradeManager; //--- Input Parameters input int MA_Period = 55; input int ADX_Period = 14; input double RR_SL_Kerroin = 0.40; input double RR_TP_Kerroin = 1.0; input double Adx_Min = 25.0; input double AtrMin = 3.0; input double Lot_Size = 0.1; //--- Global Variables long MagicNumber = 0; double atrValue = 0.0; double maValue = 0.0; double adxValue = 0.0; double stopLoss = 0.0, takeProfit = 0.0; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { Print("Fox-In-The-Code Bot käynnistetty."); if (MagicNumber == 0) { MagicNumber = sub_magicnumber(); Print("MagicNumber asetettu: ", MagicNumber); } return INIT_SUCCEEDED; } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { if (!IsNewBar()) return; if (!UpdateIndicators()) return; if (CheckBuyConditions()) { ExecuteTrade(ORDER_TYPE_BUY); } else if (CheckSellConditions()) { ExecuteTrade(ORDER_TYPE_SELL); } } //+------------------------------------------------------------------+ //| Update Indicators | //+------------------------------------------------------------------+ bool UpdateIndicators() { atrValue = iATR(_Symbol, PERIOD_M5, 14); maValue = iMA(_Symbol, PERIOD_M30, MA_Period, 0, MODE_SMA, PRICE_CLOSE); adxValue = iADX(_Symbol, PERIOD_M30, ADX_Period); if (atrValue < AtrMin || adxValue < Adx_Min) { return false; } return true; } //+------------------------------------------------------------------+ //| Tarkistaa osto-olosuhteet | //+------------------------------------------------------------------+ bool CheckBuyConditions() { if (adxValue > Adx_Min && atrValue > AtrMin && iClose(_Symbol, PERIOD_M30, 1) > maValue) { return true; } return false; } //+------------------------------------------------------------------+ //| Tarkistaa myynti-olosuhteet | //+------------------------------------------------------------------+ bool CheckSellConditions() { if (adxValue > Adx_Min && atrValue > AtrMin && iClose(_Symbol, PERIOD_M30, 1) < maValue) { return true; } return false; } //+------------------------------------------------------------------+ //| Execute Trade | //+------------------------------------------------------------------+ void ExecuteTrade(ENUM_ORDER_TYPE orderType) { MqlTradeRequest request; MqlTradeResult result; ZeroMemory(request); ZeroMemory(result); double price = 0.0; if (!SymbolSelect(_Symbol, true)) { Print("Symbolia ei löydy kaupankäyntiin: ", _Symbol, ", Error: ", GetLastError()); return; } if (orderType == ORDER_TYPE_BUY) { price = SymbolInfoDouble(_Symbol, SYMBOL_ASK); stopLoss = NormalizeDouble(price - (atrValue * RR_SL_Kerroin), _Digits); takeProfit = NormalizeDouble(price + (atrValue * RR_TP_Kerroin), _Digits); } else if (orderType == ORDER_TYPE_SELL) { price = SymbolInfoDouble(_Symbol, SYMBOL_BID); stopLoss = NormalizeDouble(price + (atrValue * RR_SL_Kerroin), _Digits); takeProfit = NormalizeDouble(price - (atrValue * RR_TP_Kerroin), _Digits); } request.action = TRADE_ACTION_DEAL; request.symbol = _Symbol; request.volume = Lot_Size; request.price = NormalizeDouble(price, _Digits); request.sl = stopLoss; request.tp = takeProfit; request.type = orderType; request.magic = MagicNumber; request.type_filling = ORDER_FILLING_IOC; if (!OrderSend(request, result)) { Print("Kauppa epäonnistui: ", GetLastError()); } else { Print("Kauppa onnistui. Tunniste: ", result.order); } } //+------------------------------------------------------------------+ //| Utility Functions | //+------------------------------------------------------------------+ long sub_magicnumber() { int hashValue = 0; for (int i = 0; i < StringLen(_Symbol); i++) { hashValue += (int)StringSubstr(_Symbol, i, 1)[0] * (i + 1); } return 146411 + hashValue; } bool IsNewBar() { static datetime lastBar = 0; datetime currentBar = iTime(_Symbol, PERIOD_M5, 0); if (currentBar != lastBar) { lastBar = currentBar; return true; } return false; }