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

 
Maxim Kuznetsov:

if (time[i]>=time_m1 && time[i]<time_m1+PeriodSeconds(PERIOD_M1) {

  // время time[i] попало внутрь бара открытого в time_m1

}

OK) one arrow, but by this condition at M15 and higher the arrow is set at 21:00

 if(time[i]>=StringToTime("2019.04.22 20:55:00") && time[i]<StringToTime("2019.04.22 20:55:00")+Period()*PeriodSeconds(PERIOD_M1))

and if you just use+PeriodSeconds(PERIOD_M1) withoutPeriod()*, it is not set at all)

 
yiduwi:

OK) one arrow, but by this condition at M15 and higher the arrow is set at 21:00

And if you just use+PeriodSeconds(PERIOD_M1) withoutPeriod()*, it is not set at all)

Read the documentation :-) Period() returns just the id of the current period - why are you multiplying by it?

instead of PERIOD_M1 (which is given as an example), pass it to PeriodSeconds( Period() ) - then get how many seconds in 1 bar of the current period.

 
kopeyka2:

So much for the "Bermuda parallelepiped" conundrum

It works for me :) Even tried opening/closing the terminal.

PS: Artyom, you moved the question on the five to the four topic... Found it by accident.

 
kopeyka2:


EMA line 20 23.04.2019 00:00

When turning on MT5, WITHOUT online connection, the message "array out...." appears immediately.

Errors vary, but always present. Can replicate online, but more often present when MT is switched on.


Such is the mystery of the "Bermuda parallelepiped".

the error message shows the line number where the error occurred. Start digging from there
 
Igor Zakharov:

It works for me :) Even tried opening/closing the terminal.

PS: Artyom, you moved the question on the five to the four topic... Found it by accident.

This is a common theme - we help here not only with MQL4, but also for migration to MQL5. So it's in the theme.

 
Please advise how to conceptually write EA code, which takes prices of trades from text .csv file. Why this question arises: on each tick the EA will compare the current price with the price in the .csv file, which is read entirely by a loop in the fileopen function, if I understand correctly. But the file contains more than 5000 lines for last year, each line has name of instrument, price, type of deal (buy/sell), date of recording, date of order cancellation. When testing, the Expert Advisor will loop through all lines in the file on every tick to understand if it is time to place an order? Or, for testing, should we make our EA set all orders with the date of cancellation at once during initialization and check the actual orders by their expiry date on every tick for the real trading? Maybe, this is not what I expected at all. May be this is the right thing to do from the point of view of resources or there are other variants (for example, we should make graphical objects and compare the current price with them, but it seems to be the same cycle); I don't understand, please advise.
 

Hello!


I downloaded the MQL4 programming video tutorial.

I created an Expert Advisor according to the lesson.

But it doesn't work when I'm trading.

I don't have any errors when compiling it.

Since I'm at the beginning of my journey, it's hard to find the error so far.

I am asking for help if anybody can help me.

Thank you!

Code:

/+----Входные параметры----------------+

extern inttern BarCount=10;

extern int int HourStart=14;

extern double Lots=0.1;

extern int StopLoss=120;

extern int TakeProfit=300;

extern int Magic=1456;

//+------------Глобальные переменные----------------+

double minprice=999999,mp,

maxprice=-99999,SL,TP;

int ticket;

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

//| Expert initialization function |

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

int OnInit()

{

return(INIT_SUCCEEDED);

}

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

//| Expert deinitialization function |

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

void OnDeinit(const int reason)

{


}

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

//| expert tick function |

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

void OnTick()

{

GetMinPrice();

GetMaxPrice();


if(TimeHour(TimeCurrent())==HourStart)

{

if(BuyLimitCount()&& BuyCount() ==0)

{

SL=NormalizeDouble(minprice-StopLoss*Point,5);

TP=NormalizeDouble(minprice+TakeProfit*Point,5);

ticket=OrderSend(Symbol(),OP_BUYLIMIT,Lots,minprice,5,SL,TP,"",Magic,0,Blue);

if(ticket<0)

Print("Failed to open buy limit");

}

if(SellLimitCount()&& SellCount()==0)

{

SL=NormalizeDouble(maxprice+StopLoss*Point,5);

TP=NormalizeDouble(maxprice-TakeProfit*Point,5);

ticket=OrderSend(Symbol(),OP_SELLLIMIT,Lots,maxprice,5,SL,TP,"",Magic,0,Red);

if(ticket<0)

Print("Failure to open Sell Limit");

}

}



Comment("MinPrice: "+DoubleToStr(minprice,5)+"\n "+"MaxPrice: "+DoubleToStr(maxprice,5));


}

//+FUNCTION FOR DEFINING MINIMUM PRICE ON BARCOUNT BAR NUMBER

void GetMinPrice()

{

for(int i=0; i<BarCount; i++)

{

mp=iLow(Symbol(),PERIOD_CURRENT,i);

if(mp<minprice)

minprice=mp;

}

return;

}

//+FUNCTION FOR DETERMINATION OF MAXIMUM PRICE ON BARCOUNT NUMBER

void GetMaxPrice()

{

for(int i=0; i<BarCount; i++)

{

mp=iHigh(Symbol(),PERIOD_CURRENT,i);

if(mp>maxprice)

maxprice=mp;

}

return;

}

//+FUNCTION OF NUMBER OF OPEN LIMIT ORDERS TO BUY

int BuyLimitCount()

{

int count=0;

for(int i=OrdersTotal()-1; i>=0; i--)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true && OrderMagicNumber()==Magic && OrderType()==OP_BUYLIMIT)

{

count++;

}

}

return(count);

}

//+-FUNCTION OF NUMBER OF OPEN SALE LIMIT ORDERS

int SellLimitCount()

{

int count=0;

for(int i=OrdersTotal()-1; i>=0; i--)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true && OrderMagicNumber()==Magic && OrderType()==OP_SELLLIMIT)

{

count++;

}

}

return(count);

}

//+FUNCTION OF NUMBER OF OPEN MARKET BOOK ORDERS

int BuyCount()

{

int count=0;

for(int i=OrdersTotal()-1; i>=0; i--)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true && OrderMagicNumber()==Magic && OrderType()==OP_BUY)

{

count++;

}

}

return(count);

}

//+-FUNCTION OF NUMBER OF OPEN MARKET SALE ORDERS

int SellCount()

{

int count=0;

for(int i=OrdersTotal()-1; i>=0; i--)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true && OrderMagicNumber()==Magic && OrderType()==OP_SELL)

{

count++;

}

}

return(count);

}

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


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

 

Seric29

Thank you, I will add your information to my luggage.

 
WinProject:
Can you please advise how to conceptually write EA code, which takes trade prices from .csv file. Why this question arises: On every tick the EA will compare the current price with the price in the .csv file, which is read entirely by the fileopen function, if I understand it correctly. But the file contains more than 5000 lines for last year, each line has name of instrument, price, type of deal (buy/sell), date of recording, date of order cancellation. When testing, the Expert Advisor will loop through all file lines on every tick to understand if it is time to place an order? Or, for testing, should we make our EA set all orders with the date of cancellation at once during initialization and check the actual orders by their expiry date on every tick for the real trading? Maybe, this is not what I expected at all. May be this is the right thing to do from the point of view of resources or there are other variants (for example, we should make graphical objects and compare the current price with them, but it seems to be the same cycle); please advise me.

Usually one tries to read (write) to the file as few times as possible.

For your task, it would be better to read the data into an array at loading (though probably more conveniently into a structure) and then compare the current price and time values with the array values

SZY: search on codebase "file" or "csv" was once such ready-made Expert Advisors - read from a file trade on the data

 
Igor Makanu:

Usually one tries to read (write) to the file as few times as possible.

For your task, it would be better to read the data into an array at loading (though probably more conveniently into a structure) and then compare the current price and time values with the array values

ZS: search on codebase "file" or "csv" was once such ready-made EAs - read from a file trade on that data

Thank you very much, got the answer I wanted.