BTC USD 81,198.4 Gold USD 4,372.82
Time now: Jun 1, 12:00 AM

EA pertamaku - HantamKeromoEA

Izham87

Active Member
Messages
1,198
Joined
Jul 14, 2016
Messages
1,198
Reaction score
528
Points
81
Baru nak belaja buat EA. Detail algorithm boleh tengok dalam comment code. MM boleh adjust. TP/SL boleh adjust. kalau nama pair ada suffix macam akaun micro, boleh setting.

EA ni verified boleh guna kat 12 pair jek iaitu "EURCHF","USDCAD","USDCHF","AUDUSD","GBPUSD","GBPJPY","NZDUSD","EURJPY","AUDJPY","EURGBP","USDJPY","EURUSD".

Pair lain pandai2 korang laa..

Code banyak copy paste dari EA orang. Code seperti dibawah:

PHP:
//+------------------------------------------------------------------+
//|                                               HantamKeromoEA.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Izham87 Inc."
#property link      "http://www.google.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

#include <stderror.mqh>
#include <stdlib.mqh>

extern double percentatrisk = 0.05;
extern string pairsuffix = "micro";
extern double SL = 500;
extern double TP = 600;
extern string Pair = "USDJPY";
extern int MagicNumber = 7;
extern int FirstDirect = 0; // 0=buy,1=sell
extern int MaxLossInRow = 1;

int LastTicket=0; 
int LossCounter=0;
 
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  **
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  **
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{

double lotvalue;
string usdexchangepair;
string sigpair, sigpairbase, sigpaircounter;
int sigaction;
int ticket;

  sigpair = StringConcatenate(Pair, pairsuffix);
  sigpairbase = StringSubstr(Pair, 0, 3);
  sigpaircounter = StringSubstr(Pair, 3, 3);

   //---Calculate lot based on MM
  if (sigpaircounter == "USD") {
  lotvalue = NormalizeDouble((AccountFreeMargin() * percentatrisk) / (MarketInfo(sigpair, MODE_LOTSIZE) * MarketInfo(sigpair, MODE_POINT) * SL), 2);
  ** else {
  if (sigpaircounter == "GBP") {
    lotvalue = NormalizeDouble((AccountFreeMargin() * percentatrisk) / (MarketInfo(sigpair, MODE_LOTSIZE) * MarketInfo(sigpair, MODE_POINT) * MarketInfo(StringConcatenate("GBPUSD", pairsuffix), MODE_BID) * SL), 2);
  ** else {
    usdexchangepair = StringConcatenate("USD", sigpaircounter, pairsuffix);
    lotvalue = NormalizeDouble((AccountFreeMargin() * percentatrisk * MarketInfo(usdexchangepair, MODE_BID)) / (MarketInfo(sigpair, MODE_LOTSIZE) * MarketInfo(sigpair, MODE_POINT) * SL), 2);
  **
  **


//Algorithm:
//check if there is no order, make 1 order depend on first direction
//if there is previous order, check status, if open, do nothing
// if closed, check direction, check result (loss or tp)
// if loss, record a loss, proceed with new order in same direction
//if tp or only one loss record, new order in same direction

//
if(LastTicket == 0) // check if there was previous order
{
  sigaction=FirstDirect;
  if (sigaction == 0) {
    ticket = subOpenOrder(OP_BUY, sigpair, lotvalue);
    subCheckError(ticket, "BUY");
  ** else {
    ticket = subOpenOrder(OP_SELL, sigpair, lotvalue);
    subCheckError(ticket, "SELL");
  **
**  
else
{
  if(OrderSelect(LastTicket, SELECT_BY_TICKET)==true) 
    {
      if(OrderCloseTime() != 0) //if the previous order is closed
      {
         if(OrderProfit()>0) //closed in profit, 
         {
          sigaction=OrderType(); // maintained order type & open new order
              if (sigaction == 0) {
                 ticket = subOpenOrder(OP_BUY, sigpair, lotvalue);
                 subCheckError(ticket, "BUY");
              ** else {
                 ticket = subOpenOrder(OP_SELL, sigpair, lotvalue);
                 subCheckError(ticket, "SELL");
              **
         **
         else //closed in loss
         {
          LossCounter++; //increase loss counter aka record loss
           if(LossCounter < MaxLossInRow) //self explanatory
            {
             sigaction=OrderType(); // maintained order type & open new order
                 if (sigaction == 0) {
                  ticket = subOpenOrder(OP_BUY, sigpair, lotvalue);
                  subCheckError(ticket, "BUY");
                  ** else {
                  ticket = subOpenOrder(OP_SELL, sigpair, lotvalue);
                  subCheckError(ticket, "SELL");
                 **
            **
            else  //exceed MaxLossInRow setting
            {
             sigaction=MathAbs(OrderType()-1);  //change direction 
                  if (sigaction == 0) {
                  ticket = subOpenOrder(OP_BUY, sigpair, lotvalue);
                  subCheckError(ticket, "BUY");
                  ** else {
                  ticket = subOpenOrder(OP_SELL, sigpair, lotvalue);
                  subCheckError(ticket, "SELL");
                 **
             MaxLossInRow=0; //reset loss counter    
            **
            
         **
      **
   **
  else
   Print("OrderSelect returned the error of ",GetLastError());
** 

return;              
**
//+------------------------------------------------------------------+

int subOpenOrder(int type, string symbol, double openlot)
{
   int
         ticket      = 0,
         err         = 0,
         c           = 0;
         
   double         
         aStopLoss   = 0,
         aTakeProfit = 0,
         bStopLoss   = 0,
         bTakeProfit = 0;

      aStopLoss   = NormalizeDouble(MarketInfo(symbol,MODE_ASK)-SL*MarketInfo(symbol,MODE_POINT),(int)MarketInfo(symbol,MODE_DIGITS));
      bStopLoss   = NormalizeDouble(MarketInfo(symbol,MODE_BID)+SL*MarketInfo(symbol,MODE_POINT),(int)MarketInfo(symbol,MODE_DIGITS));
      aTakeProfit = NormalizeDouble(MarketInfo(symbol,MODE_ASK)+TP*MarketInfo(symbol,MODE_POINT),(int)MarketInfo(symbol,MODE_DIGITS));
      bTakeProfit = NormalizeDouble(MarketInfo(symbol,MODE_BID)-TP*MarketInfo(symbol,MODE_POINT),(int)MarketInfo(symbol,MODE_DIGITS));
 
  
   
   if(type==OP_BUY)
   {
      for(c=0;c<10;c++)
      {  
         ticket=OrderSend(symbol,OP_BUY,openlot,MarketInfo(symbol,MODE_ASK),3,aStopLoss,aTakeProfit,"",MagicNumber,0);
         err=GetLastError();
         if(err==0)
         { 
            if(ticket>0) break;
         **
         else
         {
            if(err==0 || err==4 || err==136 || err==137 || err==138 || err==146) //Busy errors
            {
               Sleep(5000);
               continue;
            **
            else //normal error
            {
               if(ticket>0) break;
            **  
         **
      **   
   **
   if(type==OP_SELL)
   {   
      for(c=0;c<10;c++)
      {
         ticket=OrderSend(symbol,OP_SELL,openlot,MarketInfo(symbol,MODE_BID),3,bStopLoss,bTakeProfit,"",MagicNumber,0);
         err=GetLastError();
         if(err==0)
         { 
            if(ticket>0) break;
         **
         else
         {
            if(err==0 || err==4 || err==136 || err==137 || err==138 || err==146) //Busy errors
            {
               Sleep(5000);
               continue;
            **
            else //normal error
            {
               if(ticket>0) break;
            **  
         **
      **   
   **  
   return(ticket);
**



void subCheckError(int ticket, string Type)
{
    if(ticket>0) 
    {
      if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
      {
      Print(Type + " order opened : ",OrderOpenPrice());
      LastTicket = ticket;
    **
    else Print("Error opening " + Type + " order : (",GetLastError(),") ", ErrorDescription(GetLastError()));
    **
**
 
Tq sharing
Perfomamce mcm mane tuan?

Performance tak berapa memberansangkan. saya tak test kat banyak pair..tapi yang terbaik setakat ni kat pair UJ, setting TP=600,SL=500,MaxLossInRow=1, test data bulan Jan 2017. dapat 170usd dari modal 100usd. result backtest lain semua ke laut :eek:
 

Live Forex Chart

Currency
Rates
EUR / USD
1.14789
USD / JPY
157.068
GBP / USD
1.33893
USD / CHF
0.82229
USD / CAD
1.39904
EUR / JPY
180.210
AUD / USD
0.71216
Back
Top
Log in Register