//+------------------------------------------------------------------+
//| 01.mq4 |
//| Copyright 2023, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
// Input parameters
input double LotSize = 0.1;
input int StopLoss = 500;
input int TakeProfit = 5000;
int OnInit()
{
//---
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
//---
}
void OnTick()
{
double ma20 = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,0);
double angle = MathArctan((iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,0) - iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,1)) / 1) * (180/MathPi);
if (angle > 30 && Close[0] > ma20 && Close[1] > ma20 && Close[2] > ma20)
{
if (OrdersTotal() == 0)
{
// Open long position
OrderSend(Symbol(), OP_BUY,LotSize,Ask,0,Bid - StopLoss * Point,Bid + TakeProfit * Point);
}
}
else if (angle < -30 && Close[0] < ma20 && Close[1] < ma20 && Close[2] < ma20)
{
if (OrdersTotal() == 0)
{
// Open short position
OrderSend(Symbol(), OP_SELL,LotSize,Bid,0,Ask + StopLoss * Point,Ask - TakeProfit * Point);
}
}
else if (Close[0] < ma20 && Close[1] > ma20)
{
// Close long position
CloseOrder(OP_BUY);
}
else if (Close[0] > ma20 && Close[1] < ma20)
{
// Close short position
CloseOrder(OP_SELL);
}
}
void CloseOrder(int orderType)
{
int total = OrdersTotal();
for (int i = total - 1;i >= 0;i--)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if (OrderType() == orderType)
{
OrderClose(OrderTicket(), OrderLots(), Bid,Slippage);
}
}
}
}
|