[WARNING CLOSED!] Any newbie question, so as not to clutter up the forum. Professionals, don't go by. Can't go anywhere without you. - page 347

 

Please help me to improve this code to get a simple Expert Advisor. The essence is simple: we open 2 positions Sell and Bui with specified stops at the current price. Then when one or both of them trigger, we open new positions. The essence of it seems simple but it still does not work Thanks a lot!

//+------------------------------------------------------------------+
//| 2DiffOrders.mq4 |
//| Copyright © 2008, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
extern double lots=0.01;
extern int SL_in_pips;
extern int TP_in_pips;
int x2;
int init()
{
if(SL_in_pips<=MarketInfo(Symbol(),MODE_STOPLEVEL) && SL_in_pips>0)
{
SL_in_pips=MarketInfo(Symbol(),MODE_STOPLEVEL);
Alert("Stoploss установлен слишком близко к цене открытия. Должен быть минимум ",MarketInfo(Symbol(),MODE_STOPLEVEL),"pt.");
}
if(TP_in_pips<=MarketInfo(Symbol(),MODE_STOPLEVEL) && TP_in_pips>0)
{
TP_in_pips=MarketInfo(Symbol(),MODE_STOPLEVEL);
Alert("Takeprofit установлен слишком близко к цене открытия. Должен быть минимум ",MarketInfo(Symbol(),MODE_STOPLEVEL),"pt.");
}
x2=1;
while(x2!=0)
{
if(OrderSend(Symbol(),0,lots,Ask,2,Bid-15*Point,Bid+15*Point,0)!=-1)
{
Alert("Ордер типа BUY успешно открыт");
OrderSend(Symbol(),1,lots,Bid,2,Ask+15*Point,Ask-15*Point,0);
x2=GetLastError();
if(x2==0)
Alert("Ордер типа SELL успешно открыт");
if(x2!=0)
Alert("Ордер типа SELL не открыт. Ошибка №: ",x2);
}else
{
x2=GetLastError();
if(x2!=0)
Alert("Ордер типа BUY не открыт. Ошибка №: ",x2);
}
}
//----
return(0);


Files:
ala.mq4  3 kb
 
Reshetov >> :

This only appears to be exactly right, but in machine view the last bit can take on different values. I.e. 2.9999, 3 or 3.00000001


To get rid of such misunderstandings there is the NormalizeDouble() function in MQL.

Yes, NormalizeDouble() helps, thanks.

But it's still unclear how this is possible, it seems to be a simple division, and if you divide 0.5 or 0.9 or even 0.31 by 0.1, everything is fine.

and here he clearly has 2.9, it's not clear... the calculator is more accurate :-)

Thanks to Reshetov and splxgf for their help.

 
mukata >> :


But it's still not clear how it's possible, it seems to be a simple division. And if you divide 0.5 or 0.9 or even 0.31 by 0.1, it's OK.

and here it's 2.9, it's not clear... the calculator is more accurate :-)


Calculators have rounding functions built in. And they solve the same way because they use the same math coprocessor to calculate floating point numbers.

 

Hello!

Can you please give me a hint?!!!

I do not understand how to do it myself.

Task:

EA puts 1-2 orders once a day.

The initial value of the variable is set manually and can take a value from 1 to 10.

You need to change the previously set variable in increments of 1 to a value of - 10

on each working day of the EA, only by 1, if on that day TP is not executed.

When TP is executed, the variable returns value - 1.

Counting SL is not suitable, because it can be one or two, and the value can only be changed once a day.

I.e.

Variable = 1

Day 1 SL - variable =2

Day 2 SL - variable =3

Day 3 SL - variable =4

Day 4 TP - variable =1

Day 5 SL - variable =2

... And so on.

Help!

Thank you.

 
Enter писал(а) >>

Please help me to improve this code to get a simple Expert Advisor. The essence is simple: we open 2 positions Sell and Bui with specified stops at the current price. Then when one or both of them trigger, we open new positions. The essence of it seems simple but it still does not work Thanks a lot!

Well for expert's work, "meat" of advisor should be placed in start function all the same...
 

Connoisseurs, please help.

There is a code that calculates a single period momentum and integrates it with a large period, then outputs it as an indicator.

I need to write an indicator with a given period that finds the average of the resulting curve and outputs it as an indicator.

The code is attached:

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
#property indicator_width1 2

//------- External indicator parameters ----------------------------------------+
extern int MTM_Period = 1;
//extern int MTM_Integr_Period = 1000;

//extern int Applied_Price = 0; // Used price:
// 0 - PRICE_CLOSE
// 1 - PRICE_OPEN
// 2 - PRICE_HIGH
// 3 - PRICE_LOW
// 4 - PRICE_MEDIAN
// 5 - PRICE_TYPICAL
// 6 - PRICE_WEIGHTED
extern NumberOfBars = 1000; // Number of bars (0-all)

//------- Indicator buffers ---------------------------------------------------+
double buf0[];

//+----------------------------------------------------------------------------+
//| Custom indicator initialisation function |
//+----------------------------------------------------------------------------+
void init() {
SetIndexBuffer (0, buf0);
SetIndexEmptyValue(0, 0);
SetIndexLabel (0, "i-mtm_integr");
SetIndexStyle (0, DRAW_LINE);
}

//+----------------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+----------------------------------------------------------------------------+
void deinit() {
Comment(");
}

//+----------------------------------------------------------------------------+
//| Custom indicator iteration function |
//+----------------------------------------------------------------------------+
void start() {
int LoopBegin, i,j, per=3;
double mtm, mtmsum;

if (NumberOfBars==0) LoopBegin=Bars-1;
else LoopBegin=NumberOfBars;
LoopBegin=MathMin(LoopBegin, Bars-1);

for (i=LoopBegin; i>=0; i--) {
mtm=Close[i]-Close[MTM_Period+i];
mtmsum=mtmsum+mtm;
buf0[i]=mtmsum;
}
/* for (i=LoopBegin-per;i>=per;i--){
mtm=Close[i]-Close[MTM_Period+i];
mtmsum[i]=0+mtm;
for (j=per;j<=0;j--){
mtmsum[i]=0+mtmsum[i+j];
mtmsum[i]=mtmsum[i]/per;
buf0[i]=mtmsum[i];
}
}*/
}
//+----------------------------------------------------------------------------+

 
VNG писал(а) >>

Connoisseurs, please help.

There is a code that calculates a single period momentum and integrates it with a large period, then outputs it as an indicator.

I need to write an indicator with a given period that finds the average of the resulting curve and outputs it as an indicator.

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 DodgerBlue
#property indicator_color2 Red
#property indicator_width1 2

//------- Внешние параметры индикатора ----------------------------------------+
extern int MTM_Period = 1; 
//extern int MTM_Integr_Period = 1000; 

//extern int Applied_Price = 0; // Используемая цена:
// 0 - PRICE_CLOSE
// 1 - PRICE_OPEN
// 2 - PRICE_HIGH
// 3 - PRICE_LOW
// 4 - PRICE_MEDIAN
// 5 - PRICE_TYPICAL
// 6 - PRICE_WEIGHTED
extern int NumberOfBars = 1000; // Количество баров обсчёта (0-все)
extern int MA_Period=3;
extern int MA_Mode=0;

//------- Буферы индикатора ---------------------------------------------------+
double buf0[];
double MA[];
//+----------------------------------------------------------------------------+
//| Custom indicator initialization function |
//+----------------------------------------------------------------------------+
void init() {
  SetIndexBuffer (0, buf0);
  SetIndexEmptyValue(0, 0);
  SetIndexLabel (0, "i-mtm_integr");
  SetIndexStyle (0, DRAW_LINE);
  SetIndexBuffer (1, MA);
  SetIndexLabel (1, "MA");
  SetIndexStyle (1, DRAW_LINE);

}

//+----------------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+----------------------------------------------------------------------------+
void deinit() {
Comment("");
}

//+----------------------------------------------------------------------------+
//| Custom indicator iteration function |
//+----------------------------------------------------------------------------+
void start() {
  int LoopBegin, i, j, per=3;
  double mtm, mtmsum;

  if ( NumberOfBars==0) LoopBegin=Bars-1;
  else LoopBegin= NumberOfBars;
  LoopBegin=MathMin( LoopBegin, Bars-1);

  for ( i= LoopBegin; i>=0; i--) {
    mtm=Close[ i]-Close[ MTM_Period+ i];
    mtmsum= mtmsum+ mtm;
    buf0[ i]= mtmsum;
  }
  for ( i= LoopBegin- per; i>= per; i--){
     MA[ i]= iMAOnArray( buf0, 0, MA_Period, 0, MA_Mode, i);
  }
}
//+----------------------------------------------------------------------------+
Maybe this variant will do. Didn't check the code, wrote it in the browser
 
Reshetov >> :

This only appears to be exactly right, but in machine view the last bit can take on different values. I.e. 2.9999, 3 or 3.00000001

To get rid of such misunderstandings, MQL has the NormalizeDouble() function


Why does not the NormalizeDouble() function run by default for all values of Ask, Bid, Low, etc.?

Everyone has fallen for it and then has to fix it "manually"... Strange that MT can't read what it itself spawned.

 
Silen >> :

Thanks, is there any guarantee that the data will be available in start()?

you can try using RefreshRates() - see examples in the standard MetaEditor help

 

There is a "Save as report" option in the EA optimisation, but the "Input Parameters" are not shown in the internet explorer,

although, judging by the html code, they are present there. In general, it was for the sake of these parameters that the optimisation was started.

Maybe someone knows how to see them in Explorer?


(There is also possibility to copy them to text file by separate command, but it's preferable to see them in IE)

Reason: