Taweesak Pintakam
Taweesak Pintakam
588280 à MT5
pkg install mql5
pkg update
pkg upgrade
Taweesak Pintakam
Taweesak Pintakam
//+------------------------------------------------------------------+
//| Simple_MA_Cross.mq5 |
//| Copyright 2026, Gemini AI User |
//+------------------------------------------------------------------+
#property strict
#include

// Input parameters
input int FastMAPeriod = 10; // Fast Moving Average Period
input int SlowMAPeriod = 20; // Slow Moving Average Period
input double LotSize = 0.1; // Trading Lot Size
input int StopLoss = 100; // Stop Loss in Points
input int TakeProfit = 200; // Take Profit in Points

// Global variables
CTrade trade; // Trading class
int fastMAHandle; // Handle for fast MA
int slowMAHandle; // Handle for slow MA

//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Initialize Moving Average indicators
fastMAHandle = iMA(_Symbol, _Period, FastMAPeriod, 0, MODE_SMA, PRICE_CLOSE);
slowMAHandle = iMA(_Symbol, _Period, SlowMAPeriod, 0, MODE_SMA, PRICE_CLOSE);

if(fastMAHandle == INVALID_HANDLE || slowMAHandle == INVALID_HANDLE)
{
Print("Error creating indicator handles");
return(INIT_FAILED);
}
return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double fastMA[], slowMA[];

// Copy indicator values to arrays
ArraySetAsSeries(fastMA, true);
ArraySetAsSeries(slowMA, true);

if(CopyBuffer(fastMAHandle, 0, 0, 3, fastMA) < 3 ||
CopyBuffer(slowMAHandle, 0, 0, 3, slowMA) < 3) return;

// Check if we already have a position open
bool hasPosition = PositionSelect(_Symbol);

// Buy Logic: Fast MA crosses ABOVE Slow MA
if(!hasPosition && fastMA[0] > slowMA[0] && fastMA[1] <= slowMA[1])
{
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double sl = ask - StopLoss * _Point;
double tp = ask + TakeProfit * _Point;
trade.Buy(LotSize, _Symbol, ask, sl, tp, "MA Cross Buy");
}

// Sell Logic: Fast MA crosses BELOW Slow MA
if(!hasPosition && fastMA[0] < slowMA[0] && fastMA[1] >= slowMA[1])
{
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double sl = bid + StopLoss * _Point;
double tp = bid - TakeProfit * _Point;
trade.Sell(LotSize, _Symbol, bid, sl, tp, "MA Cross Sell");
}
}
Taweesak Pintakam
Enregistré à MQL5.community