Code snippets - page 12

 

Very simple version of support/resistance barry indicator (metatrader 5 version)

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
#property indicator_color1  clrRed
#property indicator_type1   DRAW_ARROW
#property indicator_width1  1
#property indicator_color2  clrBlue
#property indicator_type2   DRAW_ARROW
#property indicator_width2  1

double res[],sup[];
void OnInit()
{
   SetIndexBuffer(0,res,INDICATOR_DATA); PlotIndexSetInteger(0, PLOT_ARROW, 119);
   SetIndexBuffer(1,sup,INDICATOR_DATA); PlotIndexSetInteger(1, PLOT_ARROW, 119);
}
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   for (int i=(int)MathMax(prev_calculated-3,2); i<rates_total  && !_StopFlag; i++)
   {
      double fractalUp = (i<rates_total-2) ? (high[i]>high[i+1] && high[i]>high[i+2] && high[i]>=high[i-1] && high[i]>=high[i-2]) ? high[i] : EMPTY_VALUE : EMPTY_VALUE;
      double fractalDn = (i<rates_total-2) ? (low[i] <low[i+1]  && low[i] <low[i+2]  && low[i] <=low[i-1]  && low[i]<=low[i-2])   ? low[i]  : EMPTY_VALUE : EMPTY_VALUE;
             res[i]       = (fractalUp != EMPTY_VALUE) ? high[i] : res[i-1];
             sup[i]       = (fractalDn != EMPTY_VALUE) ? low[i]  : sup[i-1];
   }  
        return(rates_total);
}
That is all it takes
 

I tested another functions to close orders but fails.. after some days I found this that worked fine for me and finally works!

I use this function to close opened orders at specific time (for example, my EA works at 9am to 17pm, after this I close all orders with

this function.. tested ok ;-)


MT5 - close all orders

void CloseAll()

{
   for (int i=PositionsTotal()-1;i>=0; i--)
   {
      {                
         if(!Trade.PositionClose(PositionGetSymbol(i)))
         {
      //--- failure message
      Print(PositionGetSymbol(i), "PositionClose() method failed. Return code=",Trade.ResultRetcode(),
            ". Code description: ",Trade.ResultRetcodeDescription());
         }
         else
         {
      Print(PositionGetSymbol(i), "PositionClose() method executed successfully. Return code=",Trade.ResultRetcode(),
            " (",Trade.ResultRetcodeDescription(),")");
         }
      }
   }
}
 

More a fooling around than anything else

This indicator does just one thing : creates a button and the, depending on the state of the current chart, turn the display of prices and dates on or of. Like this  - normal :


Hidden prices and dates :


PS: this indicator works the same on metatrader 4 and metatrader 5. just save it as mt4 file if you wish to use it with metatrader 4 and it will work without any change at all

Files:
 
mladen:

More a fooling around than anything else

This indicator does just one thing : creates a button and the, depending on the state of the current chart, turn the display of prices and dates on or of. Like this  - normal :


Hidden prices and dates :


PS: this indicator works the same on metatrader 4 and metatrader 5. just save it as mt4 file if you wish to use it with metatrader 4 and it will work without any change at all

Dearest MLADEN

Really funny toy,extending space :)  .... when price and dates no more needed.especially when only indicators result may required for demonstration purpose.

and more amazing experience is this that some things can be made the way,used for all two terminals (MT5-MT4) just with a very very simple way :):)

can a bit more possible extension to remove candle/bar too,like a complete naked chart :)

regards

 

Hi,

here is the final function to work using Trailing Step (more advanced than the old trailing)

it might be useful ;-) 

//input interface
input int                              Trailing                =  80;       /*Trailing distance*/               // Trailing level, if value is 0 - then trailing off.
input int                              TrailingStep=4;                      /*Trailing step*/          // Trailing step(level/step).Example: Trailing=10, Step=5, shift stoploss = 2


//the modified trailing version to use step

//+------------------------------------------------------------------+
//| Trailing Step function                                           |
//+------------------------------------------------------------------+
void fTrailingStep()
  {
   if(Trailing<=0)
     {
      return;
     }
   if(!Pos.Select(_Symbol))
     {
      return;
     }
   if(!Sym.RefreshRates())
     {
      return;
     }
   double nsl,nsl_step,tmsl,psl;
   switch(Pos.PositionType())
     {
      case POSITION_TYPE_BUY:
        
         nsl=Sym.NormalizePrice(Sym.Bid()-_Point*Trailing);
         if(nsl>=Sym.NormalizePrice(Pos.PriceOpen()))
           {
//---
            if(TrailingStep<=0)
               nsl_step=nsl;
            else
               nsl_step=Sym.NormalizePrice(Pos.PriceOpen()+fabs(nsl-Pos.PriceOpen())/TrailingStep);
//---
            if(nsl_step>Sym.NormalizePrice(Pos.StopLoss()))
              {
               tmsl=Sym.NormalizePrice(Sym.Bid()-_Point*Sym.StopsLevel());
               if(nsl_step<tmsl)
                 {
                  Trade.PositionModify(_Symbol,nsl_step,Pos.TakeProfit());
                 }
              }
           }
         break;
      case POSITION_TYPE_SELL:
         nsl=Sym.NormalizePrice(Sym.Ask()+_Point*Trailing);
         if(nsl<=Sym.NormalizePrice(Pos.PriceOpen()))
           {
//---
            if(TrailingStep<=0)
               nsl_step=nsl;
            else
               nsl_step=Sym.NormalizePrice(Pos.PriceOpen()-fabs(Pos.PriceOpen()-nsl)/TrailingStep);
//---
            psl=Sym.NormalizePrice(Pos.StopLoss());
            if(nsl_step<psl || psl==0)
              {
               tmsl=Sym.NormalizePrice(Sym.Ask()+_Point*Sym.StopsLevel());
               if(nsl_step>tmsl)
                 {
                  Trade.PositionModify(_Symbol,nsl_step,Pos.TakeProfit());
                 }
              }
           }
         break;
     }
  }
 

How embedding a "indicator" in a Expert - MT5

note: after this you can distribute your EA without need to install the indicators used in EA.

This works with for me in MT5  (maybe is the same for MT4 , I don't know..)

//+------------------------------------------------------------------+
//|                                                       Sample.mq5 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                                       BaraoZemo  |
//+------------------------------------------------------------------+
#property copyright "BaraoZemo"
#property link      "x@x.com"
#property version   "1.0"
//+------------------------------------------------------------------+
//| Include                                                          |
//+------------------------------------------------------------------+


//---- The inclusion of an expert as a resource code indicators

//First compile your hilo.mq5 and put in the "MT5\Indicators" folder
//Second add the line of .ex5 below
#resource "\\Indicators\\hilo.ex5"   

//global
int Handle=INVALID_HANDLE;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

  
      if(Handle==INVALID_HANDLE)
        {
         //Now, how call your "embedded indicator"
         Handle=iCustom(_Symbol,PERIOD_CURRENT,"::Indicators\\hilo.ex5",0,0);
         if(Handle==INVALID_HANDLE
           {
            Print("Failt to create indicator. Error Code:",GetLastError());
            return(INIT_FAILED);
           }
        }


   }
 
baraozemo:

How embedding a "indicator" in a Expert - MT5

note: after this you can distribute your EA without need to install the indicators used in EA.

This works with for me in MT5  (maybe is the same for MT4 , I don't know..)

//+------------------------------------------------------------------+
//|                                                       Sample.mq5 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                                       BaraoZemo  |
//+------------------------------------------------------------------+
#property copyright "BaraoZemo"
#property link      "x@x.com"
#property version   "1.0"
//+------------------------------------------------------------------+
//| Include                                                          |
//+------------------------------------------------------------------+


//---- The inclusion of an expert as a resource code indicators

//First compile your hilo.mq5 and put in the "MT5\Indicators" folder
//Second add the line of .ex5 below
#resource "\\Indicators\\hilo.ex5"   

//global
int Handle=INVALID_HANDLE;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

  
      if(Handle==INVALID_HANDLE)
        {
         //Now, how call your "embedded indicator"
         Handle=iCustom(_Symbol,PERIOD_CURRENT,"::Indicators\\hilo.ex5",0,0);
         if(Handle==INVALID_HANDLE
           {
            Print("Failt to create indicator. Error Code:",GetLastError());
            return(INIT_FAILED);
           }
        }


   }
It works with mt4 too
 
mladen:
It works with mt4 too

Dearest MLADEN

Though it is not related to the thread topic but because it is running discussion,so easy to understand and pick.

Do we needs all those multiple parameters (from property window) of features and function same as when we code icustom call within some EA.

" Handle=iCustom(_Symbol,PERIOD_CURRENT,"::Indicators\\hilo.ex5",0,0,..................................);"

regards

 
mntiwana:

Dearest MLADEN

Though it is not related to the thread topic but because it is running discussion,so easy to understand and pick.

Do we needs all those multiple parameters (from property window) of features and function same as when we code icustom call within some EA.

" Handle=iCustom(_Symbol,PERIOD_CURRENT,"::Indicators\\hilo.ex5",0,0,..................................);"

regards

the number of parameters depends of the needs of the indicators.. some indicators use less parameters other use a lot of parameters.
 
baraozemo:
the number of parameters depends of the needs of the indicators.. some indicators use less parameters other use a lot of parameters.

No,my question was a bit different,may be i have not explained well ...... when you embed your indicator "#resource "\\Indicators\\hilo.ex5""

and then call by a global "HANDLE" function

do you needs all required parameters here "

Handle=iCustom(_Symbol,PERIOD_CURRENT,"::Indicators\\hilo.ex5",0,0,..................................);"

same as we do with icustom call .... i don't means less or more , i means all that a indicator have or that we required to add in EA.

Reason: