MnQInvestmentDevelopment/MnQInvestment/04_Tutorials/KIBOT.mq5

77 lines
2.1 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 15:08:44 +02:00
// Expert Advisor properties
input double LotSize = 0.1;
input int StopLoss = 100;
input int TakeProfit = 200;
input int MagicNumber = 12345;
// Expert Advisor initialization function
int OnInit()
{
return(INIT_SUCCEEDED);
}
// Expert Advisor start function
void OnTick()
{
// Check if there are any open orders
if (OrdersTotal() == 0)
{
// Get current candlestick data
MqlRates rates[];
int copied = CopyRates(_Symbol, PERIOD_CURRENT, 0, 1, rates);
// Check if candle is bullish
if (rates[0].close > rates[0].open)
{
// Get current market price
double price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
// Calculate stop loss and take profit levels
double stopLossPrice = price - StopLoss * _Point;
double takeProfitPrice = price + TakeProfit * _Point;
// Place a buy order
int ticket = OrderSend(_Symbol, OP_BUY, LotSize, price, 0, stopLossPrice, takeProfitPrice, "Buy order", MagicNumber, 0, Green);
// Check if order was successfully placed
if (ticket > 0)
{
Print("Buy order placed successfully");
}
else
{
Print("Failed to place buy order, error code: ", GetLastError());
}
}
// Check if candle is bearish
else if (rates[0].close < rates[0].open)
{
// Get current market price
double price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
// Calculate stop loss and take profit levels
double stopLossPrice = price + StopLoss * _Point;
double takeProfitPrice = price - TakeProfit * _Point;
// Place a sell order
int ticket = OrderSend(_Symbol, OP_SELL, LotSize, price, 0, stopLossPrice, takeProfitPrice, "Sell order", MagicNumber, 0, Red);
// Check if order was successfully placed
if (ticket > 0)
{
Print("Sell order placed successfully");
}
else
{
Print("Failed to place sell order, error code: ", GetLastError());
}
}
}
}
// Expert Advisor deinitialization function
void OnDeinit(const int reason)
{
Print("Expert Advisor deinitialized with reason: ", reason);
}