Top 10 popular functions in MQL program language - Singapore Forex Trading, Singapore Forex Academy, Singapore Forex Association

Header Ads

Top 10 popular functions in MQL program language

1. Calculate point

double pipValue = CalculatePipValue(Symbol);
//Market Pip value calculation
double CalculatePipValue(string _parity)
{
    int digit = MarketInfo(_parity, MODE_DIGITS);
    if (digit == 2 || digit == 3) return 0.01;
    else if (digit == 4 || digit == 5) return 0.0001;
}

 2. Calculate slippage 

int slippage = 10;
slippage = CalculateSlippage(Symbol, slippage);

//Calculate Slippage Value
int CalculateSlippage (string _parity, int _slippage)
{
    int digit = MarketInfo(_parity, MODE_DIGITS);
    if (digit == 2 || digit == 4) return _slippage;
    else if (digit == 3 || digit == 5) return _slippage * 10;
}

 3. Enter position 


//Enter new trade
void EnterNewTrade(int _orderType)
{
    //First variables are set as buy order
    double orderEnterPrice = Ask;
    string orderName = "Buy Order";
    double orderStopLoss = orderEnterPrice - stopLoss * pipValue;
    double orderTakeProfit = orderEnterPrice + takeProfit * pipValue;

    //If the actual type of order is SELL then variables are set as sell order
    if (_orderType == OP_SELL)
    {
        orderEnterPrice = Bid;
        orderName = "Sell Order";

        orderStopLoss = orderEnterPrice + stopLoss * pipValue;
        orderTakeProfit = orderEnterPrice - takeProfit * pipValue;
    }



    //Enter Position
    int ticketNumber = OrderSend(Symbol(), _orderType, lotSize, orderEnterPrice, slippage, orderStopLoss, orderTakeProfit, "HareketliOrtalamaAlgoritmesi", 0, 0, clrAliceBlue);

    //Error Handling
    if (ticketNumber == -1)
    {
        int errorCode = GetLastError();
        string errorDefinition = ErrorDescription(errorCode);

        string errorAlarm = StringConcatenate("Error Code: ", (string)errorCode, " Error Definition: ", (string)errorDefinition);
        Alert(errorAlarm);

        string hataLogu = StringConcatenate("Error: ", (string)Symbol(), " / ", orderName, " type order / Enter Price:",
        (string)orderEnterPrice, " / ", (string)slippage, " / StopLoss:", (string)stopLoss, " / TakeProfit:", (string)takeProfit);
    }
}

 4. Close position 


 bool CloseOrder(int _orderNumber)
{
    //Select order to close
    if (OrderSelect(_orderNumber, SELECT_BY_TICKET, MODE_TRADES))
    {

        //set variable values as buy order
        double orderClosingPrice = Bid;
        string orderName = "Closing buy trade";

        //If ordertype is sell then revise variable values
        if (OrderType() == OP_SELL)
        {
            orderClosingPrice = Ask;
            orderName = "Closing sell trade";
        }

        //Process done and ticketNumber is taken to check
        int ticketNumber = OrderClose(_orderNumber, OrderLots(), orderClosingPrice, slippage, clrNONE);

                         //Error Handling
                         if (ticketNumber == -1)
                         {
                                                  int errorCode = GetLastError();
                                                  string errorDefinition = ErrorDescription(errorCode);

                                                  string errorAlarm = StringConcatenate("Error Code: ", (string)errorCode, " Error Definition: ", (string)errorDefinition);
                                                  Alert(errorAlarm);

                                                  string hataLogu = StringConcatenate("Error: ", (string)Symbol(), " / ", orderName, " type order / Enter Price:",
        (string)orderEnterPrice, " / ", (string)slippage, " / StopLoss:", (string)stopLoss, " / TakeProfit:", (string)takeProfit);
return false;
                         }
        else
        {
            return true;
        }

    }
}

 5. Revise position 


 //Revise order
bool ReviseOrder(int _ticketNumber, double _newStopLoss, double _newTakeProfit)
{
    if (OrderSelect(_ticketNumber, SELECT_BY_TICKET, MODE_TRADES))
    {
        int ticketNumber = OrderModify(OrderTicket(), OrderOpenPrice(), _ newStopLoss, _ newTakeProfit, 0, clrNONE);

                         //Error Handling
                         if (ticketNumber == -1)
                         {
                                                  int errorCode = GetLastError();
                                                  string errorDefinition = ErrorDescription(errorCode);

                                                  string errorAlarm = StringConcatenate("Error Code: ", (string)errorCode, " Error Definition: ", (string)errorDefinition);
                                                  Alert(errorAlarm);

                                                  string hataLogu = StringConcatenate("Error: ", (string)Symbol(), " / ", orderName, " type order / Enter Price:",
        (string)orderEnterPrice, " / ", (string)slippage, " / StopLoss:", (string)stopLoss, " / TakeProfit:", (string)takeProfit);
return false;
                         }
        else
        {
            return true;
        }
    }
}

 6. Count open positions


//Count Open Orders
void CountOpenOrders()
{
    sellCount = 0;
    buyCount = 0;

    for (int i = 0; i < OrdersTotal(); i++)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (OrderType() == OP_BUY)
            {
                buyCount++;
            }
            else if (OrderType() == OP_SELL)
            {
                sellCount++;
            }
        }
    }
}
 7. Count close positions

//Count Close Orders
void CountCloseOrders()
{
    sellCount = 0;
    buyCount = 0;

    for (int i = 0; i < OrdersTotal(); i++)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
        {
            if (OrderType() == OP_BUY)
            {
                buyCount++;
            }
            else if (OrderType() == OP_SELL)
            {
                sellCount++;
            }
        }
    }
}
 8. Break even

//Break Even Algorithm
void BreakEven()
{
    for (int i = 0; i < OrdersTotal(); i++)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (OrderType() == OP_BUY)
            {
                if (Ask >= (OrderOpenPrice() + breakEvenControl * pipValue) && OrderStopLoss() < OrderOpenPrice())
                {
                    ReviseStopLoss(OrderTicket(), OrderOpenPrice() + breakEvenConstant * pipValue);
                }
            }
            else if (OrderType() == OP_SELL)
            {
                if (Bid <= (OrderOpenPrice() - breakEvenControl * pipValue) && OrderStopLoss() > OrderOpenPrice())
                {
                    ReviseStopLoss(OrderTicket(), OrderOpenPrice() - breakEvenConstant * pipValue);
                }
            }
        }
    }
}


//Stop Loss Revise
void ReviseStopLoss(int _orderNumber, double _newStopLoss)
{
    if (OrderSelect(_orderNumber, SELECT_BY_TICKET, MODE_TRADES))
    {
        int controlNumber = OrderModify(OrderTicket(), OrderOpenPrice(), _newStopLoss, OrderTakeProfit(), 0, clrNONE);
    }
}

 9. Trailing stop


//Trail Algorithm
void Trail()
{
    for (int i = 0; i < OrdersTotal(); i++)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (OrderType() == OP_BUY)
            {
                if (Ask > OrderStopLoss() + trail * pipValue && OrderStopLoss() >= OrderOpenPrice() + breakEven * pipValue)
                {
                    ReviseStopLoss (OrderTicket(), Ask - trail * pipValue);
                }
            }
            else if (OrderType() == OP_SELL)
            {
                if (Bid < OrderStopLoss() - trail * pipValue && OrderStopLoss() <= OrderOpenPrice() - breakEven * pipValue)
                {
                    ReviseStopLoss (OrderTicket(), Bid + takipMesafesi * pipValue);
                }
            }
        }
    }
}

//Stop Loss Revise
void ReviseStopLoss(int _orderNumber, double _newStopLoss)
{
    if (OrderSelect(_orderNumber, SELECT_BY_TICKET, MODE_TRADES))
    {
        int controlNumber = OrderModify(OrderTicket(), OrderOpenPrice(), _newStopLoss, OrderTakeProfit(), 0, clrNONE);
    }
}


 10. Close all positions


 //Close All Open Orders
void CloseAllOpenOrders()
{
    for (int i = 0; i < OrdersTotal(); i++)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)
        {
            OrderClose(OrderTicket(), OrderLots(), OrderOpenPrice(), slippage, clrNONE);
        }
    }
}