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

 

hello all!

Please advise what should be added to the code to shift the MA not only forward (positive value) but also backward (negative value)

Here is the code from the Expert Advisor:

// Параметры советника
input string  sParametersEA = "";     // Параметры советника
input double  Lot           = 0.01;   // Количество лотов
input int     StopLoss      = 30;     // Стоп (SL)
input int     TakeProfit    = 30;     // Тейк (TP)
input int     Slippage      = 3;      // Проскальзование (в пунктах)
input int     Magic         = 1;      // Индентификатор советника
input double  K_Martin1     = 0.01;    // Множитель мартин 1
input double  K_Martin2     = 1.9;    // Множитель мартин 2
input double  K_Martin3     = 1.4;    // Множитель мартин 3
input int     OrdersClose   = 3;      // Ограничение лотности мартин1
input int     OrdersClose2  = 5;      // Ограничение лотности мартин2
input int     DigitsLot     = 2;      // Точность лотности
// Параметры индикатора
input string  sParametersMA = "";     // Параметры индикатора
input int     PeriodMA      = 14;     // Период мувинга
input int     MovingShift   = 1;      // Сдвиг мувинга
// Глобальные переменные
datetime Start,newbar;
double dMA;
double MaxMartinLot;
double MaxMartinLot2;
//+-----------------------------------------------------------------------------------------------+
int OnInit()
  {
Start          = TimeCurrent();
MaxMartinLot   = Lot*MathPow(1.4,OrdersClose);
MaxMartinLot2  = Lot*MathPow(K_Martin2,OrdersClose2);
int Y          = 15;
DrawLABEL("nextlot",1,5,Y,clrLime,""); 
Y += 20;
DrawLABEL("currentlot",1,5,Y,clrLime,"");
Y += 30;
return(INIT_SUCCEEDED);
  }
//+-----------------------------------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+-----------------------------------------------------------------------------------------------+
void OnTick()
  {
// Получим значение индикатора
   dMA = iMA(Symbol(), 0,PeriodMA, MovingShift, MODE_SMA, PRICE_CLOSE, 0); // MODE_SMA - простое усреднение , значение 0. PRICE_CLOSE- цена закрытия, значение 0.

// Если нет открытых ордеров, то входим в условие
      if(CountOrders()==0)
     {
// Если появился сигнал на покупку, то откроем ордер на покупку
      if(bSignalBuy() == true)
         vOrderOpenBuy();

// Если появился сигнал на продажу, то откроем ордер на продажу
      if(bSignalSell() == true)
         vOrderOpenSell();
     }
// Пишем какой лот текущий и какой следующий
      DrawLABEL("nextlot",1,5,0,Color1(),StringConcatenate("CURRENT LOT: ",DoubleToStr(LOT(),2)));
      DrawLABEL("currentlot",1,5,0,Color2(),StringConcatenate("NEXT LOT: ",DoubleToStr(LOT(),2))); 
 
   }
 
законопослушный гражданин #:

hello all!

Please advise what should be added to the code to shift the MA not only forward (positive value) but also backward (negative value)

Here is the code from the Expert Advisor:

Add to the shift the desired negative value, but the value + shift should not be less than zero.

 
ALEKSANDR SOKOLOV #:

Add the desired negative value to the shift, but make sure the value + shift is at least zero.

Thanks, I'll give it a try.

 

Good afternoon.

Help with the code, maybe there are examples.
Task:

There are several open orders, with different lots, in different directions. One of them is buried with profit, let us assume 20. However, there is an open order with the negative profit, let us assume -55 with 0.05 lots. I want to close a part of a negative order by the amount of the profit of a positively closed order to decrease the drawdown.

I have tried to write this using the while operator, but it did not go well.

Thank you in advance.

 

Hello, this code is from Fedoseev's book. Why is the indicator not drawn in the window, the log says that it is running, no errors during compilation, an additional window opens (empty), this code is from the book, with mine is exactly the same result. What is wrong?

//+------------------------------------------------------------------+

//| 015 Symbol.mq5 |

//+------------------------------------------------------------------+

#property copyright "Dmitry Fedoseev"

#property link "mailto:for-good-letter@yandex.ru"

#property version "1.00"

#property indicator_separate_window

#property indicator_buffers 4

#property indicator_plots 1

//--- plot bar

#property indicator_label1 "bar"

#property indicator_type1 DRAW_CANDLES

#property indicator_color1 clrGray,clrBlue,clrRed

#property indicator_style1 STYLE_SOLID

#property indicator_width1 1

//--- input parameters


input string symbol="";

input bool drawBars=false;


string sym;


//--- indicator buffers

double barBuffer1[];

double barBuffer2[];

double barBuffer3[];

double barBuffer4[];

//+------------------------------------------------------------------+

//| Custom indicator initialisation function |

//+------------------------------------------------------------------+

int OnInit()

{

sym=symbol;

StringTrimLeft(sym);

StringTrimRight(sym);

if(sym==""){

sym=Symbol();

}

if(!SymbolSelect(sym,true)){

Alert("Unknown symbol ",sym);

return(INIT_FAILED);

}

if(drawBars){

PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_BARS);

}

else{

PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_CANDLES);

}

//--- indicator buffers mapping

SetIndexBuffer(0,barBuffer1,INDICATOR_DATA);

SetIndexBuffer(1,barBuffer2,INDICATOR_DATA);

SetIndexBuffer(2,barBuffer3,INDICATOR_DATA);

SetIndexBuffer(3,barBuffer4,INDICATOR_DATA);

//---

return(INIT_SUCCEEDED);

}


void OnDeinit(const int r){

Comment(");

EventKillTimer();

}

//+------------------------------------------------------------------+

//| Custom indicator iteration function |

//+------------------------------------------------------------------+

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[])

{

int bars=Bars(sym,Period());

if(bars==0){

Comment("Forming timeframe ",sym,"...");

EventSetTimer(1);

return(0);

}

int start;

if(prev_calculated==0){

Comment(");

EventKillTimer();

datetime tm[1];

if(CopyTime(sym,Period(),bars-1,1,tm)==-1){

return(0);

}

start=0;

for(;start<rates_total;start++){

if(time[start]>=tm[0]){

break;

}

}

}

else{

start=prev_calculated-1;

}

MqlRates r[1];

for(int i=start;i<rates_total;i++){

if(CopyRates(sym,Period(),time[i],1,r)==-1){

return(0);

}

barBuffer1[i]=r[0].open;

barBuffer2[i]=r[0].high;

barBuffer3[i]=r[0].low;

barBuffer4[i]=r[0].close;

}

return(rates_total);

}


void OnTimer()

{

ChartSetSymbolPeriod(0,Symbol(),Period());

}


//+------------------------------------------------------------------+

Files:
 
MatveySt #:

Hello, this code is from Fedoseev's book. Why is the indicator not drawn in the window, the log says that it is running, no errors during compilation, an additional window opens (empty), this code is from the book, with mine is exactly the same result. What is wrong?


Alt s paste the code.

And when you cast the inductor do you enter the tool?

 
Valeriy Yastremskiy #:

Alt s to insert the code.

And when you cast the inductor, do you enter the tool?

I don't quite understand you, I've just started studying, I start the indicator in the terminal from the navigator, all the timeframes are in the pop-up window. When on historical data profiling, it is drawn but when it gets to the end it disappears.
 
Hi, I have a problem with the indicator but it goes to the Expert Advisors section when loading the demo version. What is the problem?
 
Maksim Kalachev #:
Hi, I have a problem, I have placed an indicator, but when I load the demo it goes to the EA section. What is the problem?

Before displaying products,you should read the rules of the marketplace.

 
Vitaly Muzichenko #:

Before displaying products,you should read the market rules.

I have read, only the indicator is set up as an indicator. But when I download the demo it appears in EAs

Reason: