My EA opens many order one after another instead of one - page 2

 

Thanx guys for helping. I think I could solve the problem. I have merged the conditions of closed order profit into one. After closing (trigger or SL,TP) the code will check the last closed order. And this checking is independent from the closing criteria.

if (liveorder==false) {
   if (last_profit==false) {
   for (counter2=OrdersHistoryTotal()-1;counter2 >= 0;counter2--) {
   if (OrderSelect(counter2,SELECT_BY_POS,MODE_HISTORY)==true && OrderSymbol()==szimbolum && OrderMagicNumber()==magic_number) {
        close_order_profit=OrderProfit() + OrderSwap() + OrderCommission();

Conditions: liveorder=false: no running trade ; last_profit=false: profit of closed trade did not get earlier ;

The first closed trade (magic number, symbol) is the trade what I want to get, so with OrderProfit I will get what I am looking for.

Thx for hint (conditions, loop, decrement loop etc.)!

 

I have another problem. I don't want to start for this a new topic, I share here:

I want to open(create) a file for writing & reading, but it doesn't work

terminal_data_path=TerminalInfoString(TERMINAL_DATA_PATH);
filename_temp=Symbol()+"_"+EnumToString(ENUM_TIMEFRAMES(_Period))+".txt";
filename=terminal_data_path+"\\MQL4\\Files\\"+filename_temp;
filehandle=FileOpen(filename,FILE_READ|FILE_WRITE|FILE_CSV,';');

I get always errorcode. What could be wrong?

 
pecskeke1976: What could be wrong?
  1. You didn't use SEARCH
  2. We just went over this six (6) days ago. Is the File Name Wrong? - MQL4 forum
    filename=terminal_data_path+"\\MQL4\\Files\\"+filename_temp;

 
WHRoeder:
  1. You didn't use SEARCH
  2. We just went over this six (6) days ago. Is the File Name Wrong? - MQL4 forum

Ty WHRoeder!

I used search, but this topic what you have linked I did not find. Thats why I asked. Btw thx for help.

 
Its solved.
 

I am confused. What could be wrong with file write?

filehandle=FileOpen(filename,FILE_READ|FILE_WRITE|FILE_BIN);
        uint byteswritten=FileWriteDouble(filehandle,total_profit,DOUBLE_VALUE);
        FileClose(filehandle);

total_profit is double. It's writing something out, I see 8 bytes filelength. It's recomended to close file after each write and read? Or could be stay opened?

 

if I don't use:

uint byteswritten=FileWriteDouble

but:

FileWriteDouble(filehandle,total_profit,DOUBLE_VALUE);

It's working.

 
pecskeke1976: I am confused. What could be wrong with file write?
uint byteswritten=FileWriteDouble(filehandle,total_profit,DOUBLE_VALUE);

total_profit is double. It's writing something out, I see 8 bytes filelength. It's recomended to close file after each write and read? Or could be stay opened?

  1. You wrote a double, (total_profit,) and the size of a double is 8. What's the problem?
  2. If you close after the write the next open is rewriting the file, unless you seek to the end.
  3. If you keep it open, flush after each write.
 
WHRoeder:
  1. You wrote a double, (total_profit,) and the size of a double is 8. What's the problem?
  2. If you close after the write the next open is rewriting the file, unless you seek to the end.
  3. If you keep it open, flush after each write.

Ty, from your answer the 3nd I did not know. In first version I have kept open and I got 16 bytes. If I read it back got wrong result. Now I close it after each write and as you wrote I got a rewrited file. Thats what I want.

WHRoeder I like to ask something about indicators crossing. Example: moving average. How can I avoid the off-key crossing? Sometimes the indicators are crossing each other but after the candle closing I see no crossing. If my EA under the candle time after "crossing" take a trade, it could be off-key after the candle closing. I hope it is understandable. As I am thinking in my EA I will check the 0. and the 1st shifted indicators if I want to get the crossing. But this thinking leads sometimes off-key crossing. Could you please help me?

Reason: