Problem with FileDelete  [SOLVED]

Questions about MultiCharts and user contributed studies.
Wally_AD
Posts: 47
Joined: 28 Jan 2014
Has thanked: 8 times
Been thanked: 7 times

Problem with FileDelete

Postby Wally_AD » 15 Jan 2015

I have a problem deleting files. OS is Windows 7.
The show code runs as a signal and shall monitor the current order status.
Is was before part of the system itself, but I found it can run independently. Same problem was already before.

Code: Select all

[IntrabarOrderGeneration = false]

{ Name: UW_ATS_Monitor

Task: Supervise broker status. Show current order status by
means of three files:
S2.txt for Short
L2.txt for Long
S2.txt or Flat
files are copied afterwards by means of robocopy into a cloud folder
start Robocopy %Source_Path1% %Target_Path2% /PURGE /MOT:5 /mon:2 /RH:0745-2230 /PF

last change 15.01.2015 }

Vars:
Path1("D:\Users\myName\Documents\Multicharts\Debug\mySystem\"),
Trace_Path("D:\Users\\myName\Documents\Multicharts\Debug\HmySystem\"),
Lenovo_Path3("C:\Users\\myName\Documents\Multicharts\Debug\mySystem"),
FileName(""),
tmpName("ATS_Monitor"),
PValue(false),
Status_FileName("F.txt"),
Debug_T(True), Debug_F(False), Lenovo(false);

if CurrentBar=1 Then begin

// to identify the product machine (I use 2 computers)
PValue=ELC.PathExists(Lenovo_Path3);
If PValue then Lenovo=true;
if Lenovo Then begin
Trace_Path = Lenovo_Path3 + "\";
end;

end;

// +-+-+-+-+-+-+-+-+- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
if Date = CurrentDate and BarStatus(1) = 2 and time > 0830 and time < 1700 then begin
// If ELC.PathExists(Trace_path + Status_FileName) Then
// FileDelete(Trace_path + Status_FileName);
if MarketPosition_at_Broker < 0 then Status_FileName = "S2.txt";
if MarketPosition_at_Broker = 0 then Status_FileName = "F2.txt";
if MarketPosition_at_Broker > 0 then Status_FileName = "L2.txt";
//
if Debug_T then begin // instead of FileAppend
FileDelete(Trace_path + "F2.txt");
FileDelete(Trace_path + "L2.txt");
FileDelete(Trace_path + "S2.txt");
Print(File(Trace_path + Status_FileName),
DateToString(DateToJulian(Date)) + Spaces(2) +
TimeToString(ELTimeToDateTime(Time)) + " " +
NumtoStr(Close,2) + " Profit/Loss: " +
NumtoStr(i_ClosedEquity,2) + Newline);
end;
end; // CurrentDate
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
Debug_T is true. The if block contains three FileDelete statements and then one file is created.
So always only 1 file should exist. F2.txt, S2.txt or L2.txt.
In fact the deleting works when starting the program, but during run time I can see that files are created or updated, but after a while I have three files instead of 1. Very surprising.
Does anybody have an idea for this behaviour?
regards
Wally_AD

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Problem with FileDelete

Postby TJ » 15 Jan 2015

Maybe this is an obvious question... have you ever had success in deleting a file? or is this a new problem?

Have you checked the access right to delete files in your directory?


Have you ever attempt to delete a file that does not exist? What happens next?

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Problem with FileDelete

Postby JoshM » 16 Jan 2015

I have a problem deleting files. OS is Windows 7.
(...)

Code: Select all

(...)
Vars:
Path1("D:\Users\myName\Documents\Multicharts\Debug\mySystem\"),
Trace_Path("D:\Users\\myName\Documents\Multicharts\Debug\HmySystem\"),
Lenovo_Path3("C:\Users\\myName\Documents\Multicharts\Debug\mySystem"),
(...)
Don't know if the problem is caused by this, but the slashes (\) in the file names are inconsistent. Does the problem also occur when you use the following paths?

Code: Select all

Vars:
Path1("D:\Users\myName\Documents\Multicharts\Debug\mySystem\"),
Trace_Path("D:\Users\myName\Documents\Multicharts\Debug\HmySystem\"),
Lenovo_Path3("C:\Users\myName\Documents\Multicharts\Debug\mySystem\"),

Wally_AD
Posts: 47
Joined: 28 Jan 2014
Has thanked: 8 times
Been thanked: 7 times

Re: Problem with FileDelete

Postby Wally_AD » 16 Jan 2015

PValue=ELC.PathExists(Lenovo_Path3);

ELC.PathExists works only with no backslash at the end. This is the reason for the slash.

I wrote: In fact the deleting of files works when starting the program, ..

So in principle FileDelete works.
There are no issues with access rights.
The program always tries to delete not existing files. No problem with it.

I can only imagine that the problem has to do with the robocopy job, which also accesses the files but shall only copy them. But there is a two minute time interval. But as it runs independently from the system now I see now that I can perform some manual tests - I mean running the above script without robocopy.
Thank you JosM and TJ

Wally_AD
Posts: 47
Joined: 28 Jan 2014
Has thanked: 8 times
Been thanked: 7 times

Re: Problem with FileDelete

Postby Wally_AD » 16 Jan 2015

I stopped robycopy - it did not help.
Added a time delay after the filedelete. It did not help.

Each filedelete is executed only once. After starting the signal.

orion
Posts: 250
Joined: 01 Oct 2014
Has thanked: 65 times
Been thanked: 104 times

Re: Problem with FileDelete

Postby orion » 16 Jan 2015

My two cents (wild guesses) below.

1. Windows will not allow you to delete a file if there is an open handle to the file. Perhaps MC or Windows hangs on to file handles longer than it should?
2. Are there multiple symbols being traded with market position >0, =0, and <0 as a result of which you have all 3 files being written?

Wally_AD
Posts: 47
Joined: 28 Jan 2014
Has thanked: 8 times
Been thanked: 7 times

Re: Problem with FileDelete

Postby Wally_AD » 17 Jan 2015

1) A delay of 1 second after the Filedelete does not help.
Multicharts Team should confirm that all handles are released after a Print(file(..) or a FileDelete.

2) No other symbols. There were 2 active chart windows: one only trading the ES, and the ATS_monitor from above. Used filenames are different in both signals. And many symbols would make no difference.

3) I would need a File Rename, but this function does not exist in Easylanguage or MC.

orion
Posts: 250
Joined: 01 Oct 2014
Has thanked: 65 times
Been thanked: 104 times

Re: Problem with FileDelete

Postby orion » 17 Jan 2015

1) A delay of 1 second after the Filedelete does not help.
Multicharts Team should confirm that all handles are released after a Print(file(..) or a FileDelete.
MC is unlikely to be able to confirm what the underlying Windows filesystem may be doing. I have run into Windows filesystem handle bugs when a lot of Windows files have been opened and written. My app was in C++ using Windows filesystem API and doing lots of small frequent writes to files for all equities in US equities universe.

Wally_AD
Posts: 47
Joined: 28 Jan 2014
Has thanked: 8 times
Been thanked: 7 times

Re: Problem with FileDelete

Postby Wally_AD » 19 Jan 2015

I made a test by means of a simple signal script:

Code: Select all

Print(File(x), ".."));
DeleteFile(x);
Now do it again.

Code: Select all

Print(File(x), ".."));
DeleteFile(x);
The file is deleted only once;
Executing the 2 lines more often shows that each time one line is added to the file. It is only deleted when the system is started.

orion
Posts: 250
Joined: 01 Oct 2014
Has thanked: 65 times
Been thanked: 104 times

Re: Problem with FileDelete  [SOLVED]

Postby orion » 19 Jan 2015

I made a test by means of a simple signal script:
The file is deleted only once;
Executing the 2 lines more often shows that each time one line is added to the file. It is only deleted when the system is started.
Indeed. My tests show its not that the file is deleted only once. What I found is:
1. File can't be deleted after you have done a print() to file (you can't even delete it once)
2. File can be deleted any number of times if you use fileAppend() instead of print().
So it seems that print() hangs on to file handle while fileAppend() doesn't.

Wally_AD
Posts: 47
Joined: 28 Jan 2014
Has thanked: 8 times
Been thanked: 7 times

Re: Problem with FileDelete

Postby Wally_AD » 19 Jan 2015

Thanks orion.

I think FileAppend is the same, but I have to test it.

orion
Posts: 250
Joined: 01 Oct 2014
Has thanked: 65 times
Been thanked: 104 times

Re: Problem with FileDelete

Postby orion » 19 Jan 2015

Thanks orion. I think FileAppend is the same, but I have to test it.
No its different. One more thing: even if fileAppend works, you should redo your code so that is is not doing a zillion file writes and deletes. Trust me, that Windows file sytem bug that I mentioned in one of the earlier posts is real. You will not encounter it in small test cases but when you get to real trading with zillion file writes and deletes you are going to hit that Windows bug.

Wally_AD
Posts: 47
Joined: 28 Jan 2014
Has thanked: 8 times
Been thanked: 7 times

Re: Problem with FileDelete

Postby Wally_AD » 19 Jan 2015

FileAppend works in my script as expected.

orion, thanks for the warning.

With a filter only changes of the order status lead to a file access. It will not be more than 20 per day. For about 100 file accesses per week I don't expect a problem.


Return to “MultiCharts”