Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 629

 
Who else can help?)
 
Rustam Bikbulatov:
Who else can help?)

You seem to have been given comprehensive answers and examples, but you still need to read the help to understand how functions are called and why the static modifier is used.

Here's another way, maybe it'll be easier for you to understand:

int N(bool reset=false) // если вызываем N() то возвращает следующее значение n++, при первом вызове n=0, но вернет 1, т.к. n=0, а затем n++, если нужно 0 то 
			// или  static int n=-1; или вызывайте сначала N(true)
  {                     // если вызываем N(true), то сбрасываем n=0 и возвращзаем 0
   static int n=0;
   if(reset) n=0; else n++;
   return n;
  }
 
Igor Makanu:

You seem to have been given comprehensive answers and examples, but you still need to read the help to understand how functions are called and why the static modifier is used.

Here's another variant, maybe this will make it easier for you to understand:

The first example shows one function call when no position is taken and the returned result is compared.

if( ((OpenB-Ask)/ma+TimeB/60) > N() )

That's why it might be a good idea to do a simple return without zeroing and adding. Which I have implemented in 2 variants

 
//+------------------------------------------------------------------+
//|                                                 elliotbutton.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
  ButtonCreate();
  ButtonCreate(0,"ButtonMinor",0,10,60,50,50,CORNER_LEFT_UPPER,"(i)","Arial",12,clrBlack, C'236,233,216',clrNONE,false,false,false,false,0);
  
  
     return(INIT_SUCCEEDED);
  }

void OnTick()
  {
  }

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
         if (ObjectGetInteger(0,"Button",OBJPROP_STATE,0)==true) 
          {
               int      x     =(int)lparam;
               int      y     =(int)dparam;
               datetime dt    =0;
               double   price =0;
               int      window=0;
                        if(ChartXYToTimePrice(0,x,y,window,dt,price))  
                         {
                         PrintFormat("Window=%d X=%d  Y=%d  =>  Time=%s  Price=%G",window,x,y,TimeToString(dt),price);
                                 if(ChartTimePriceToXY(0,window,dt,price,x,y))
                                     PrintFormat("Time=%s  Price=%G  =>  X=%d  Y=%d",TimeToString(dt),price,x,y);
                                 ChartRedraw(0);
                         }
               TextCreate(0,"Text "+string(MathRand()),0,dt,price,"i","Arial",10,clrBlack,0.0,ANCHOR_LEFT_UPPER,false,true,false,0); 
            
               TextCreate(0,"Text "+string(MathRand()),0,dt,price,"ii","Arial",10,clrBlack,0.0,ANCHOR_LEFT_UPPER,false,true,false,0); 
          
               TextCreate(0,"Text "+string(MathRand()),0,dt,price,"iii","Arial",10,clrBlack,0.0,ANCHOR_LEFT_UPPER,false,true,false,0);
           
               TextCreate(0,"Text "+string(MathRand()),0,dt,price,"iv","Arial",10,clrBlack,0.0,ANCHOR_LEFT_UPPER,false,true,false,0); 
             
               TextCreate(0,"Text "+string(MathRand()),0,dt,price,"v","Arial",10,clrBlack,0.0,ANCHOR_LEFT_UPPER,false,true,false,0); 
               
               ObjectSetInteger(0,"Button",OBJPROP_STATE,False);
             }
                        
               
          if (ObjectGetInteger(0,"ButtonMinor",OBJPROP_STATE,0)==true) 
          {
               int      x     =(int)lparam;
               int      y     =(int)dparam;
               datetime dt    =0;
               double   price =0;
               int      window=0;
                        if(ChartXYToTimePrice(0,x,y,window,dt,price))  
                         {
                         PrintFormat("Window=%d X=%d  Y=%d  =>  Time=%s  Price=%G",window,x,y,TimeToString(dt),price);
                                 if(ChartTimePriceToXY(0,window,dt,price,x,y))
                                     PrintFormat("Time=%s  Price=%G  =>  X=%d  Y=%d",TimeToString(dt),price,x,y);
                                 ChartRedraw(0);
                         }
               TextCreate(0,"Text "+string(MathRand()),0,dt,price,"(i)","Arial",10,clrBlack,0.0,ANCHOR_LEFT_UPPER,false,true,false,0); 
            
               TextCreate(0,"Text "+string(MathRand()),0,dt,price,"(ii)","Arial",10,clrBlack,0.0,ANCHOR_LEFT_UPPER,false,true,false,0); 
          
               TextCreate(0,"Text "+string(MathRand()),0,dt,price,"(iii)","Arial",10,clrBlack,0.0,ANCHOR_LEFT_UPPER,false,true,false,0);
           
               TextCreate(0,"Text "+string(MathRand()),0,dt,price,"(iv)","Arial",10,clrBlack,0.0,ANCHOR_LEFT_UPPER,false,true,false,0); 
             
               TextCreate(0,"Text "+string(MathRand()),0,dt,price,"(v)","Arial",10,clrBlack,0.0,ANCHOR_LEFT_UPPER,false,true,false,0); 
               
               ObjectSetInteger(0,"ButtonMinor",OBJPROP_STATE,False);
               
            
           }
 }
            
  
  

void OnDeinit(const int reason)
  {
   ObjectDelete(0,"Button");
   ObjectDelete(0,"ButtonMinor");
  }
  
bool ButtonCreate(const long              chart_ID=0,               // ID графика 
                  const string            name="Button",            // имя кнопки 
                  const int               sub_window=0,             // номер подокна 
                  const int               x=10,                      // координата по оси X 
                  const int               y=10,                      // координата по оси Y 
                  const int               width=50,                 // ширина кнопки 
                  const int               height=50,                // высота кнопки 
                  const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // угол графика для привязки 
                  const string            text="i",                 // текст 
                  const string            font="Arial",             // шрифт 
                  const int               font_size=10,             // размер шрифта 
                  const color             clr=clrBlack,             // цвет текста 
                  const color             back_clr=C'236,233,216',  // цвет фона 
                  const color             border_clr=clrNONE,       // цвет границы 
                  const bool              state=false,              // нажата/отжата 
                  const bool              back=false,               // на заднем плане 
                  const bool              selection=false,          // выделить для перемещений 
                  const bool              hidden=false,              // скрыт в списке объектов 
                  const long              z_order=0)                // приоритет на нажатие мышью 
  { 

   ResetLastError(); 

   if(ObjectCreate(chart_ID,name,OBJ_BUTTON,sub_window,0,0)) 
     { 
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x); 
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y); 
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width); 
   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height); 
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner); 
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text); 
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font); 
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size); 
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr); 
   ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr); 
   ObjectSetInteger(chart_ID,name,OBJPROP_BORDER_COLOR,border_clr); 
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back); 
   ObjectSetInteger(chart_ID,name,OBJPROP_STATE,state); 
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection); 
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection); 
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden); 
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order); 
      }
  return(true); 
 }
 
 bool TextCreate(const long              chart_ID=0,              // ID графика 
                const string            name="Text",              // имя объекта 
                const int               sub_window=0,             // номер подокна 
                datetime                time=0,                   // время точки привязки 
                double                  price=0,                  // цена точки привязки 
                const string            text="Text",              // сам текст 
                const string            font="Arial",             // шрифт 
                const int               font_size=10,             // размер шрифта 
                const color             clr=clrBlack,             // цвет 
                const double            angle=0.0,                // наклон текста 
                const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // способ привязки 
                const bool              back=false,               // на заднем плане 
                const bool              selection=true,           // выделить для перемещений 
                const bool              hidden=true,              // скрыт в списке объектов 
                const long              z_order=0)                // приоритет на нажатие мышью 
  { 

   ResetLastError(); 

   if(ObjectCreate(chart_ID,name,OBJ_TEXT,sub_window,time,price)) 
     { 
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text); 
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font); 
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size); 
   ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle); 
   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor); 
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr); 
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back); 
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection); 
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection); 
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden); 
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order); 
     }
   return(true); 
 } 



  
  

write an EA which when I click the Button, it creates a Text in the place on the chart, as shown by the mouse. Now my Expert Advisor creates Text only in place of Button.

 

Hi, how do I make when I manually drag and drop a buystop pending order, the other sellstop pending order will move with it?

How can I bind one to the other, so that when I move one, it will move both of them at once?

 


Здравствуйте. У меня алгоритм такой 
//----------------------------------------------------------------------------
if (CountBuy() == 0)
{

BuySignal = h1; tttp=1; 
if (BuySignal > 0 && tttp==1 && h2>0)
{
CloseSell();
ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, comment, Magic, 0, Blue);

if (ticket > 0)
{

if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
{

SL = NormalizeDouble(Ask-StopLoss*Point, Digits);

TP = NormalizeDouble(Ask+TakeProfit*Point, Digits);

res = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0);
if (!res)

Print("Ошибка модификации ордера на покупку, ASK=" + DoubleToStr(Ask) + ", SL=" + DoubleToStr(SL) + ", TP=" + DoubleToStr(TP)); 
}
}
}
}
if (CountSell() == 0)
{
SellSignal= l1; tttp=-1;
if (SellSignal > 0 && tttp==-1 && l2>0)
{
CloseBuy();
ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, comment, Magic, 0, Red);
if (ticket > 0)
{
if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
{
SL = NormalizeDouble(Bid+StopLoss*Point, Digits);
TP = NormalizeDouble(Bid-TakeProfit*Point, Digits);
res = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0);
if (!res)
Print("Ошибка модификации ордера на продажу, BID=" + DoubleToStr(Bid) + ", SL=" + DoubleToStr(SL) + ", TP=" + DoubleToStr(TP)); 
}
}
}
}
} 
//------------------------------------------------------------------------------------------
Пытаюсь сделать, чтоб условия на открытие ордера проверялось бы через какое-то кол-во баров, а так сразу открывается другой ордер
 
Carcass77:

Why spam?

 
Konstantin Nikitin:

Why spam?

What you've written in several threads? I can take it down.
 
Ihor Herasko:

Write your own ticks and put them in the strategy tester.

Greetings. Can you tell me what the problem is and how to solve it?

I have changed some candlesticks in history for testing, for the 1st, 2nd, 3rd and 6th.

When testing, the second one is processed, the third one is drawn (without processing), and stops giving an error stopped because of Stop Out



 

Good afternoon

I have a very simple situation but I'm having trouble solving it.

Please tell me what I am doing wrong:

problem: i cannot bind custom indicator(i-DRP...) to the Expert Advisor (DR Test) via iCustom

thanks in advance