Download MetaTrader 5

Experts: Attempt to create an Expert Advisor based on the Center of Gravity indicator

To add comments, please log in or register
Use searching on MQL5.community. It is easy!
Automated-Trading
Admin
99874
Automated-Trading 2016.07.01 09:21 

Attempt to create an Expert Advisor based on the Center of Gravity indicator:

The idea has existed for a long time - based on the "Conservative intraday scalping" strategy, but I could not find a suitable indicator in MT4... Integration of the custom indicator to the EA.

Author: Vitaliy

Hans Krijgsman
184
Hans Krijgsman 2016.07.08 18:26  

Hello Vitaliy,

I like your idea. See the code. It has to be improved in order to make it profitable.

Hope to hear from you and other people to contribute to this idea.

//+------------------------------------------------------------------+
//|                                                   COG_MA_X_1.mq4 |
//|                                   Copyright 2016, Hans Krijgsman |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Hans Krijgsman"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property description ""

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

//--- input parameters
input double   lots=0.1;
input string   Info1="COG Indicator values";
input int      bars_back=125;
input int      m=2;
input int      i=0;
input double   kstd=2.0;
input int      sName=1102;
input bool     Audible_Alerts=true;

//--- internal parameters
int ticket;
datetime time_order; //used when opening order and/or sending alert
double MyPoint; //initialized in OnInit
static datetime dt=Time[0]; //timestamp of New Bar open
bool BuyOpportunity; // opportunity for possible Buy order
bool SellOpportunity; // opportunity for possible Sell order
int BuyOpportunityCount; // counter for Buy opportunities
int SellOpportunityCount; // counter for Sell opportunities
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   MyPoint=Point;
   if(Digits==3 || Digits==5) MyPoint=Point*10;
//---
   BuyOpportunityCount=0;
   SellOpportunityCount=0;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(NewBar())
     {
      //--- reset
      ticket=0;
      BuyOpportunity  = false;
      SellOpportunity = false;
      //--- previous moving average values
      double maH1 = iMA(NULL,0,1,0,MODE_LWMA,PRICE_HIGH,1);
      double maL1 = iMA(NULL,0,1,0,MODE_LWMA,PRICE_LOW,1);
      //--- previous COG extreme values
      double cgH1 = iCustom(NULL,0,"Center of Gravity",bars_back,m,i,kstd,sName,3,1); //COG high extreme
      double cgL1 = iCustom(NULL,0,"Center of Gravity",bars_back,m,i,kstd,sName,4,1); //COG low extreme
      //--- check opportunities
      if(maL1<cgL1)
        {
         BuyOpportunity=true;
         BuyOpportunityCount++;
         myAlert("indicator",StringFormat("Buy Opportunity [%d]",BuyOpportunityCount));
        }
      if(maH1>cgH1)
        {
         SellOpportunity=true;
         SellOpportunityCount++;
         myAlert("indicator",StringFormat("Sell Opportunity [%d]",SellOpportunityCount));
        }
     }

//--- ONLY OPEN ONE POSITION PER BAR
   if(Time[0]!=time_order)
     {
      //--- actual moving average values
      double maH0 = iMA(NULL,0,1,0,MODE_LWMA,PRICE_HIGH,0);
      double maL0 = iMA(NULL,0,1,0,MODE_LWMA,PRICE_LOW,0);
      //--- actual COG extreme values
      double cgH0 = iCustom(NULL,0,"Center of Gravity",bars_back,m,i,kstd,sName,3,0); //COG high extreme
      double cgL0 = iCustom(NULL,0,"Center of Gravity",bars_back,m,i,kstd,sName,4,0); //COG low extreme

      //--- BUY
      if(BuyOpportunity && maL0<cgL0)
        {
         ticket=OrderSend(Symbol(),OP_BUY,lots,Ask,3,NormalizeDouble(Ask-(10*MyPoint),Digits),NormalizeDouble(Ask+(20*MyPoint),Digits),"COG_MA_X_1",123,0,Lime);
         
         if(ticket>0)
           {
            myAlert("order",StringFormat("Buy position opened [%d]",BuyOpportunityCount));
            time_order=Time[0];
           }
         else
           {
            myAlert("error",StringFormat("error opening Buy position [%d]",BuyOpportunityCount));
           }
        }

      //--- SELL
      if(SellOpportunity && maH0>cgH0)
        {
         ticket=OrderSend(Symbol(),OP_SELL,lots,Bid,2,NormalizeDouble(Bid+(10*MyPoint),Digits),NormalizeDouble(Bid-(20*MyPoint),Digits),"COG_MA_X_1",123,0,Red);
         
         if(ticket>0)
           {
            myAlert("order",StringFormat("Sell position opened [%d]",SellOpportunityCount));
            time_order=Time[0];
           }
         else
           {
            myAlert("error",StringFormat("error opening Sell position [%d]",SellOpportunityCount));
           }
        }
     }

//--- SCREEN INFO  
   string str_opp=StringFormat("\n* Buy Opportunities : %d \n* Sell Opportunities : %d",BuyOpportunityCount,SellOpportunityCount);
   Comment(str_opp);
  }
//+------------------------------------------------------------------+

//--- FUNCTIONS -----------------------------------------------------+
//+------------------------------------------------------------------+
//| Returns true when a New Bar has occured, else false.             |
//+------------------------------------------------------------------+
bool NewBar(){if(dt!=Time[0]){dt=Time[0]; return(true);} return(false);}
//+------------------------------------------------------------------+
//| Alerts, Messages                                                 |
//+------------------------------------------------------------------+
void myAlert(string type,string message)
  {
   if(type=="print")
      Print(message);
   else if(type=="error")
     {
      Print(type+" | COG_MA_X_1 @ "+Symbol()+","+TF2String()+" | "+message);
     }
   else if(type=="order")
     {
      if(Audible_Alerts) Alert(type+" | COG_MA_X_1 @ "+Symbol()+","+TF2String()+" | "+message);
     }
   else if(type=="modify")
     {
     }
   else if(type=="indicator")
     {
      if(Audible_Alerts) Alert(type+" | COG_MA_X_1 @ "+Symbol()+","+TF2String()+" | "+message);
     }
  }
//+------------------------------------------------------------------+
//| Returns a visual representation for the given timeperiod.        |
//+------------------------------------------------------------------+
string TF2String()
  {
   string tfs="N/A";
   switch(_Period)
     {
      case PERIOD_M1:  tfs="M1"; break;
      case PERIOD_M5:  tfs="M5"; break;
      case PERIOD_M15: tfs="M15"; break;
      case PERIOD_M30: tfs="M30"; break;
      case PERIOD_H1:  tfs="H1"; break;
      case PERIOD_H4:  tfs="H4"; break;
      case PERIOD_D1:  tfs="D1"; break;
      case PERIOD_W1:  tfs="W1"; break;
      case PERIOD_MN1: tfs="MN";
     }
   return(tfs);
  }
//+------------------------------------------------------------------+

golad
9
golad 2016.10.13 18:16  
Hans Krijgsman:

Hello Vitaliy,

I like your idea. See the code. It has to be improved in order to make it profitable.

Hope to hear from you and other people to contribute to this idea.

Thank you Hans for your code.

I have add to my MT4 chart on demo account, will see how it works.

Will this EA send ALERT to my push notification email, which I have set

in the TOOLS > OPTIONS > EMAIL.


I would love if you can help me. How / where do I add custom indicator mq4 file for

additional entry point? Like MA crossover arrow indicator. Add to which block?


Thanks in advance.

To add comments, please log in or register