Skip to content

General purpose snippets

Victor Tereschenko edited this page Sep 23, 2020 · 11 revisions

General purpose snippets

OnInit

Insert OnInit function

Translates into

string IndicatorObjPrefix;

bool NamesCollision(const string name)
{
   for (int k = ObjectsTotal(0); k >= 0; k--)
   {
      if (StringFind(ObjectName(0, k), name) == 0)
      {
         return true;
      }
   }
   return false;
}

string GenerateIndicatorPrefix(const string target)
{
   for (int i = 0; i < 1000; ++i)
   {
      string prefix = target + "_" + IntegerToString(i);
      if (!NamesCollision(prefix))
      {
         return prefix;
      }
   }
   return target;
}

int OnInit(void)
{
   IndicatorObjPrefix = GenerateIndicatorPrefix("123Patterns");
   IndicatorSetString(INDICATOR_SHORTNAME, "123Patterns");
   IndicatorSetInteger(INDICATOR_DIGITS, Digits());
   return INIT_SUCCEEDED;//INIT_FAILED
}

OnCalculate

Insert OnCalculate function

Translates into

int OnCalculate(const int rates_total,       // size of input time series
                const int prev_calculated,   // number of handled bars at the previous call
                const datetime& time{},      // Time array
                const double& open[],        // Open array
                const double& high[],        // High array
                const double& low[],         // Low array
                const double& close[],       // Close array
                const long& tick_volume[],   // Tick Volume array
                const long& volume[],        // Real Volume array
                const int& spread[]          // Spread array
)
{
   if (prev_calculated <= 0 || prev_calculated > rates_total)
   {
      ArrayInitialize(out, EMPTY_VALUE);
   }
   int first = 0;
   for (int pos = MathMax(first, prev_calculated - 1); pos < rates_total; ++pos)
   {
      int oldPos = rates_total - pos - 1;
   }
   return rates_total;
}

OnDeinit

Insert OnDeinit function

Translates into

void OnDeinit(const int reason)
{
   ObjectsDeleteAll(0, IndicatorObjPrefix);
}

createStreamMT5

Creates output stream

Translates into

SetIndexBuffer(id, stream, INDICATOR_DATA);
PlotIndexSetInteger(id, PLOT_DRAW_TYPE, |DRAW_NONE,DRAW_LINE,DRAW_SECTION,DRAW_HISTOGRAM,DRAW_HISTOGRAM2,DRAW_ARROW,DRAW_ZIGZAG,DRAW_FILLING,DRAW_BARS,DRAW_CANDLES,DRAW_COLOR_LINE,DRAW_COLOR_SECTION,DRAW_COLOR_HISTOGRAM,DRAW_COLOR_HISTOGRAM2,DRAW_COLOR_ARROW,DRAW_COLOR_ZIGZAG,DRAW_COLOR_BARS,DRAW_COLOR_CANDLES|);
PlotIndexSetInteger(id, PLOT_LINE_COLOR, clr);
PlotIndexSetString(id, PLOT_LABEL, name)

PlotIndexSetInteger

PlotIndexSetInteger function.

Translates into

PlotIndexSetInteger(id, |PLOT_ARROW,PLOT_ARROW_SHIFT,PLOT_DRAW_BEGIN,PLOT_DRAW_TYPE,PLOT_SHOW_DATA,PLOT_SHIFT,PLOT_LINE_STYLE,PLOT_LINE_WIDTH,PLOT_COLOR_INDEXES,PLOT_LINE_COLOR|, value);

pipSizeMT5

Get pip size

Translates into

double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
int digit = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS);
int mult = digit == 3 || digit == 5 ? 10 : 1;
double pipSize = point * mult;

switchPriceMT5

Translates into

switch (priceType)
{
    case PRICE_CLOSE:
        break;
    case PRICE_OPEN:
        break;
    case PRICE_HIGH:
        break;
    case PRICE_LOW:
        break;
    case PRICE_MEDIAN:
        break;
    case PRICE_TYPICAL:
        break;
    case PRICE_WEIGHTED:
        break;
}

stdevMT5

StDev function

Translates into

double StDev(double& data[], int period, int pos)
{
   return MathSqrt(Variance(data, period, pos));
}
double Variance(double& data[], int period, int pos)
{
   double sum = 0;
   double ssum = 0;
   for (int i = 0; i < period; i++)
   {
      sum += data[pos - i];
      ssum += MathPow(data[pos - i], 2);
   }
   return (ssum * period - sum * sum) / (period * (period - 1));
}

OnTickMT5

OnTick function

Translates into

void OnTick()
{
}