10 Fungsi Penting Dalam Menciptakan Expert Advisor (Ea)

Dalam coding Forex EA terdapat fungsi-fungsi (functions) yang biasa digunakan. Berikut ini 10 fungsi penting untuk menciptakan EA :

 Fungsi ini dipakai untuk menyesuaikan digit mata uang yang ada di broker 10 Fungsi Penting Dalam menciptakan Expert Advisor (EA)

1. Fungsi Calculate Point

Fungsi ini dipakai untuk menyesuaikan digit mata uang yang ada di broker
double pipValue = CalculatePipValue(Symbol);
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. Fungsi Calculate Slippage

Slippage juga perlu diadaptasi terhadap digit broker
int Slippage = 10;
Slippage = CalculateSlippage(Symbol, Slippage);
int CalculateSlippage(string _parity, int _slippage)
{
     int digit = MarketInfo(_parity, MODE_DIGITS);
     if (digit == 2 || digit == 3) return _slippage;
     else if (digit == 4 || digit == 5) return _slippage*10;
}

3. Fungsi Enter Position

Ini untuk mengeksekusi perintah OP buy atau sell
void EnterNewTrade(int _orderType)
{

     //Secara default kita set sebagai buy order
     double orderEnterPrice = Ask;
     string orderName = "Buy Order";
     double orderStopLoss = orderEnterPrice - stopLoss * pipValue;
     double orderTakeProfit = orderEnterPrice + takeProfit * pipValue;

     //Jika order SELL, maka kita set sell order
     if (_orderType == OP_SELL)
     {
          orderEnterPrice = Bid;
          orderName = "Sell Order";
          orderStopLoss = orderEnterPrice + stopLoss * pipValue;
         orderTakeProfit = orderEnterPrice - takeProfit * pipValue;
     }

     //Perintah OP
     int ticketNumber = OrderSend(Symbol(), _orderType, lotSize, orderEnterPrice, slippage, orderStopLoss, orderTakeProfit, comment, magic);

}

4. Fungsi Close Position

Untuk close order yang terbuka
bool CloseOrder(int _orderNumber)
{
     //Select order yang mau di close
     if (OrderSelect(_orderNumber, SELECT_BY_TICKET, MODE_TRADES)
     {
          //Secara default nilainya buy
          double orderClosingPrice = Bid;
          string orderName = "Closing buy trade";

          //Jika tipe ordernya sell, ubah nilainya
          if (OrderType() == OP_SELL)
          {
               orderClosingPrice = Ask;
               orderName = "Closing sell trade";
          }

          //Perintah close
          int ticketNumber = OrderClose(_orderNumber, OrderLots(), orderClosingPrice, slippage, clrNONE);
       
          //Jika tidak mau close
          if (ticketNumber == -1)
          {
               return false;
          }
          else
          {
               return true;
          }

     }

}

5. Fungsi Revise Position

Ini untuk merevisi atau mengubah nilai sebagian dari suatu 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);
          if (ticketNumber == -1)
          {
               return false;
          }
          else
          {
               return true;
          }
     }
}

6. Fungsi Count Open Position

Berguna untuk menghitung jumlah order yang terbuka
void CountOpenOrders()
{
     sellCount = 0;
     buyCount = 0;

     for (int i = 0; i < OrdersTotal(); i++)
     {
          if (OrderSelect(i, SELECT_BY_POST, MODE_TRADES))
          {
               if (OrderType() == OP_BUY)
               {
                    buyCount++;
               }
               else if (OrderType() == OP_SELL)
               {
                    sellCount++;
               }
          }
     }
}

7. Fungsi Count Closed Position

Sama menyerupai sebelumnya, hanya saja ini untuk menghitung order yang tertutup
void CountClosedOrders()
{
     sellCount = 0;
     buyCount = 0;

     for (int i = 0; i < OrdersTotal(); i++)
     {
          if (OrderSelect(i, SELECT_BY_POST, MODE_TRADES))
          {
               if (OrderType() == OP_BUY)
               {
                    buyCount++;
               }
               else if (OrderType() == OP_SELL)
               {
                    sellCount++;
               }
          }
     }
}

8. Fungsi Break Even

Fungsi ini untuk menggeser StopLoss ke posisi Break Even
//Perhitungan BEP
void BreakEven()
{
     for (int i = 0; i < OrdersTotal(); i++)
     {
          if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
          {
               if (OrderType() == OP_BUY)
               {
                    if (Ask >= (OrderOpenPrice() + priceBEP * pipValue && OrderStopLoss() < OrderOpenPrice())
                    {
                         ReviseStopLoss(OrderTicket(), OrderOpenPrice());
                    }
               }
               else if (OrderType() == OP_SELL)
               {
                    if (Ask >= (OrderOpenPrice() - priceBEP * pipValue && OrderStopLoss() > OrderOpenPrice())
                    {
                         ReviseStopLoss(OrderTicket(), OrderOpenPrice());
                    }
               }
          }
     }
}

//Fungsi untuk mengubah stoploss
void ReviseStopLoss(int _orderNumber, double _newStopLoss)
{
     if (OrderSelect(_orderNumber, SELECT_BY_TICKET, MODE_TRADES)
     {
          int controlNumber = OrderModify(OrderTicket(), OrderOpenPrice(), _newStopLoss, OrderTakeProfit(), 0, clrNONE);
     }
}

9. Fungsi Trailing

Untuk menggeser StopLoss secara bertahap
//Perhitungan Trailing
void Trail()
{
     for (int i = 0; i < OrdersTotal(); i++)
     {
          if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
          {
               if (OrderType() == OP_BUY)
               {
                    if (Ask >= (OrderOpenPrice() + trail * pipValue && OrderStopLoss() >= OrderOpenPrice() + trailStep * pipValue)
                    {
                         ReviseStopLoss(OrderTicket(), Ask - trail * pipValue);
                    }
               }
               else if (OrderType() == OP_SELL)
               {
                    if (Ask >= (OrderOpenPrice() - trail * pipValue && OrderStopLoss() <= OrderOpenPrice() - trailStep * pipValue)
                    {
                         ReviseStopLoss(OrderTicket(), Bid + trail * pipValue);
                    }
               }
          }
     }
}

//Fungsi untuk mengubah stoploss
void ReviseStopLoss(int _orderNumber, double _newStopLoss)
{
     if (OrderSelect(_orderNumber, SELECT_BY_TICKET, MODE_TRADES)
     {
          int controlNumber = OrderModify(OrderTicket(), OrderOpenPrice(), _newStopLoss, OrderTakeProfit(), 0, clrNONE);
     }
}
     

10. Fungsi Close All Positions

Untuk menutup semua order yang terbuka
void CloseAllOpenOrders()
{
     for (int i = 0; i < OrderTotal(); i++)
     {
          if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
          {
               OrderClose(OrderTicket(), OrderLots(), OrderOpenPrice(), slippage, clrNONE);
          }
     }
}

Demikianlah 10 fungsi penting untuk menciptakan EA, kita biasa menggunakannya guna mempermudah dalam pembuatan robot forex. Tinggal copas dan modiv sedikit, selesai. Semoga bermanfaat.

Subscribe to receive free email updates:

0 Response to "10 Fungsi Penting Dalam Menciptakan Expert Advisor (Ea)"

Post a Comment