Saving to files from Indicators and Strategies
- Tantalus
- Posts: 302
- Joined: Fri Mar 23, 2007 3:51 pm
- Contact:
Saving to files from Indicators and Strategies
I couldn't find a function to save information to a file from a strategy or an indicator, like you can do in MT4. Is such a function available, and if not, can you consider adding this feature in the next release?
Thanks!
Thanks!
- Terranin
- Site Admin
- Posts: 833
- Joined: Sat Oct 21, 2006 4:39 pm
Re: Saving to files from Indicators and Strategies
Tantalus wrote:I couldn't find a function to save information to a file from a strategy or an indicator, like you can do in MT4. Is such a function available, and if not, can you consider adding this feature in the next release?
Thanks!
There are a lot of functions to work with files in Delphi and C++. You can use them in strategies or indicators.
For Delphi see:
AssignFile
open/rewrite
write/writeln/read/readln
CloseFile
These are not an API functions, it is just common functions of programming language.
Hasta la vista
Mike
Mike
- Tantalus
- Posts: 302
- Joined: Fri Mar 23, 2007 3:51 pm
- Contact:
OK, I'm trying to figure out how to use these commands, but it keeps saying file doesn't exist (that's the message that shows in the Journal).
Here's the code I'm using:
I couldn't find a command to create a file, so I'm assuming that if I try to access a file that doesn't exist, it will be created automatically. Can someone who knows Delphi tell me what's wrong? Basically, I just want to be able to save information about my strategy while it's executing. I need to be able to open it when necessary and add a line of info to it.
Thanks,
B-
Here's the code I'm using:
Code: Select all
fTradeRecord: TextFile;
AssignFile(fTradeRecord, 'C:\TestFile.txt');
Append(fTradeRecord);
WriteLn(fTradeRecord, 'Text line added');
CloseFile(fTradeRecord);
I couldn't find a command to create a file, so I'm assuming that if I try to access a file that doesn't exist, it will be created automatically. Can someone who knows Delphi tell me what's wrong? Basically, I just want to be able to save information about my strategy while it's executing. I need to be able to open it when necessary and add a line of info to it.
Thanks,
B-
- Terranin
- Site Admin
- Posts: 833
- Joined: Sat Oct 21, 2006 4:39 pm
Tantalus wrote:OK, I'm trying to figure out how to use these commands, but it keeps saying file doesn't exist (that's the message that shows in the Journal).
Here's the code I'm using:Code: Select all
fTradeRecord: TextFile;
AssignFile(fTradeRecord, 'C:\TestFile.txt');
Append(fTradeRecord);
WriteLn(fTradeRecord, 'Text line added');
CloseFile(fTradeRecord);
I couldn't find a command to create a file, so I'm assuming that if I try to access a file that doesn't exist, it will be created automatically. Can someone who knows Delphi tell me what's wrong? Basically, I just want to be able to save information about my strategy while it's executing. I need to be able to open it when necessary and add a line of info to it.
Thanks,
B-
Code: Select all
fTradeRecord: TextFile;
AssignFile(fTradeRecord, 'C:\TestFile.txt');
if FileExists('C:\TestFile.txt') then
Append(fTradeRecord)
else
rewrite(fTradeRecord);
WriteLn(fTradeRecord, 'Text line added');
CloseFile(fTradeRecord);
Hasta la vista
Mike
Mike
-
- Posts: 40
- Joined: Wed Sep 15, 2010 6:52 am
writing trade data to a text file
I assumed that the sample code above needs to be split into different locations within a strategy for efficient execution. For example,
I put the following statement at the top of the source code under the "var" command:
fTradeRecord: TextFile
Then I put the following statements under InitStrategy so they would only open the file one time:
AssignFile(fTradeRecord, 'C:\TradeData.txt');
if FileExists('C:\TradeData.txt') then
Append(fTradeRecord)
else
Rewrite(fTradeRecord);
Then I put the following statement immediately after executing an order (the point in time where I want to capture certain data to a text file):
WriteLn(fTradeRecord, 'Text line added');
And finally, I added the following statement to the DoneStrategy section so I will only close the file once:
CloseFile(fTradeRecord);
My compile looked good and the install of the DLL into FT worked. However, I must have put the code in the wrong place within the strategy because FT2.6 will not even launch without blowing up. I assume I will have to re-install FT2.6, but could use your advise on rearranging the location of the statements so it is compatible with FT2.6's expectations.
Thanks for any help or ideas.
I put the following statement at the top of the source code under the "var" command:
fTradeRecord: TextFile
Then I put the following statements under InitStrategy so they would only open the file one time:
AssignFile(fTradeRecord, 'C:\TradeData.txt');
if FileExists('C:\TradeData.txt') then
Append(fTradeRecord)
else
Rewrite(fTradeRecord);
Then I put the following statement immediately after executing an order (the point in time where I want to capture certain data to a text file):
WriteLn(fTradeRecord, 'Text line added');
And finally, I added the following statement to the DoneStrategy section so I will only close the file once:
CloseFile(fTradeRecord);
My compile looked good and the install of the DLL into FT worked. However, I must have put the code in the wrong place within the strategy because FT2.6 will not even launch without blowing up. I assume I will have to re-install FT2.6, but could use your advise on rearranging the location of the statements so it is compatible with FT2.6's expectations.
Thanks for any help or ideas.
creativity + willful purpose
-
- Posts: 905
- Joined: Sat Jul 11, 2009 10:54 am
Hello,
Actually it is not a good idea to put this code:
into InitStrategy method.
and into DoneStrategy method.
Please try to move these parts of code into Calculate method.
Actually it is not a good idea to put this code:
Code: Select all
AssignFile(fTradeRecord, 'C:\TradeData.txt');
if FileExists('C:\TradeData.txt') then
Append(fTradeRecord)
else
Rewrite(fTradeRecord);
into InitStrategy method.
and
Code: Select all
CloseFile(fTradeRecord);
Please try to move these parts of code into Calculate method.
Check our other product here:
http://www.forexcopier.com
http://www.forexcopier.com
-
- Posts: 40
- Joined: Wed Sep 15, 2010 6:52 am
Trouble writing to a text file from within a Strategy
I tried moving all of my code to a procedure which is called from the GetSingleTick procedure. It kept blowing up, so I isolated the problem to the Rewrite command using the following code (I got the rewrite error message):
I also confirmed the problem by inserting a Print command immediately after the Rewrite command -- it never Printed to the Journal.
Finally, I noticed that no trades execute after the first trade because the Rewrite command blows up before the next trade.
Note that I use Lazarus 0.9.26.2 beta dated 2010-09-18 with
FPC Version 2.2.2, SVN Revision 18980, i386-win32-win32/win64
on Vista platform. Forex Tester is v2.6
Thanks
Code: Select all
// inserted near the top of the file after 'uses'...
var
fTradeDataFile: TextFile;
// in a procedure which is called within GetSingleTick...
AssignFile(fTradeDataFile, 'C:\TradeData.txt');
If FileExists('C:\TradeData.txt') then
Append(fTradeDataFile)
else
begin
{$I-}
Rewrite (fTradeDataFile);
{$I+}
If (IOResult = 0) then
Print('trade data file opened correctly')
else
Print ('file rewrite error');
end;
Writeln(fTradeDataFile, 'Text line added to file');
CloseFile (fTradeDataFile);
I also confirmed the problem by inserting a Print command immediately after the Rewrite command -- it never Printed to the Journal.
Finally, I noticed that no trades execute after the first trade because the Rewrite command blows up before the next trade.
Note that I use Lazarus 0.9.26.2 beta dated 2010-09-18 with
FPC Version 2.2.2, SVN Revision 18980, i386-win32-win32/win64
on Vista platform. Forex Tester is v2.6
Thanks
creativity + willful purpose
-
- Posts: 905
- Joined: Sat Jul 11, 2009 10:54 am
-
- Posts: 40
- Joined: Wed Sep 15, 2010 6:52 am
I pulled a lot of strategy logic out to simplify for illustration. This code will buy and sell on each subsequent candle -- after the first "sell", I try to write data to a text file. That's when the program crashes. If you watch the Journal while it executes, you can see the Print commands I have embedded which confirm it is abending on the Rewrite command.
The source code...
The source code...
Code: Select all
library SampleFileIO;
uses
SysUtils, Classes, Graphics, Windows, StrategyInterfaceUnit;
var
// output file
fTradeDataFile: TextFile;
// External parameters
Currency: PChar = nil;
TimeFrame: integer;
LotSize: double = 0.1;
info: PCurrencyInfo = nil;
// OTIS variables
MA1, MA2, MA3: double;
MA3period: integer;
PrevBars: integer;
// custom variables
OrderHandle: integer;
OrderStyle: TTradePositionType;
OpenTime: TDateTime;
//-----Init strategy-------------------
procedure InitStrategy; stdcall;
begin
StrategyShortName('SampleFileIO');
StrategyDescription('Program blows up on Rewrite statement');
RegOption('Currency', ot_Currency, Currency);
ReplaceStr(Currency, 'EURUSD');
RegOption('Timeframe', ot_Timeframe, TimeFrame);
TimeFrame := PERIOD_M30;
end;
//-----Done strategy--------------
procedure DoneStrategy; stdcall;
begin
FreeMem(Currency);
end;
//-----Reset strategy--------------------
procedure ResetStrategy; stdcall;
begin
OrderHandle := -1;
MA3period := 5;
end;
// Write trade data to a text file
Procedure CaptureTradeData;
begin
AssignFile(fTradeDataFile, 'C:\TradeData.txt');
If FileExists('C:\TradeData.txt') then
Append(fTradeDataFile)
else
begin
{$I-}
Print ('this print command works');
Rewrite (fTradeDataFile);
Print ('this print command does not work');
{$I+}
If (IOResult = 0) then
Print('trade data file opened correctly')
else
Print ('file rewrite error');
end;
Writeln(fTradeDataFile, 'text line added to file');
CloseFile (fTradeDataFile);
end;
// TestCloseLong
Procedure TestCloseLong;
begin
if (MA1 = MA1) then
begin
CloseOrder(OrderHandle);
OrderHandle := -1;
CaptureTradeData;
exit;
end;
end;
// TestCloseShort
Procedure TestCloseShort;
begin
if (MA2 = MA2) then
begin
CloseOrder(OrderHandle);
OrderHandle := -1;
CaptureTradeData;
exit;
end;
end;
// TestOpenLong
Procedure TestOpenLong;
begin
if (MA1>MA2) and (MA2>MA3) then
begin
OrderStyle := tp_Buy;
SendInstantOrder(Symbol, op_Buy, LotSize, 0, 0, '', 0, OrderHandle);
OpenTime := Time(0);
exit;
end;
end;
// TestOpenShort
Procedure TestOpenShort;
begin
if (MA1<MA2) and (MA2<MA3) then
begin
OrderStyle := tp_Sell;
SendInstantOrder(Symbol, op_Sell, LotSize, 0, 0, '', 0, OrderHandle);
OpenTime := Time(0);
exit;
end;
end;
//-----Process single tick-------------------
procedure GetSingleTick; stdcall;
begin
// check our currency
if Symbol <> string(Currency) then exit;
// set currency and timeframe
SetCurrencyAndTimeframe(Symbol, TimeFrame);
GetCurrencyInfo(Symbol, info);
// check number of bars and MA periods
if (Bars < MA3period*3) then exit;
if (Bars <> PrevBars) then PrevBars := Bars else exit;
// Get EMAs
MA1 := 2;
MA2 := 1;
MA3 := 0;
// Open & Close Logic
if (OrderHandle = -1) then TestOpenLong;
if (OrderHandle = -1) then TestOpenShort;
if (OrderHandle<> -1) and (OrderStyle=tp_Buy) and (OpenTime<>Time(0)) then
TestCloseLong;
if (OrderHandle<> -1) and (OrderStyle=tp_Sell) and (OpenTime<>Time(0)) then
TestCloseShort;
end;
exports
InitStrategy,
DoneStrategy,
ResetStrategy,
GetSingleTick;
end.
creativity + willful purpose
-
- Posts: 905
- Joined: Sat Jul 11, 2009 10:54 am
Hello I've just tried your code and it works fine on my computer.
Probably your Forex Tester do not enough permissions to perform IO operations on your machine or maybe some another error happened. Please add "try..catch" block and see what error is thrown there.
Probably your Forex Tester do not enough permissions to perform IO operations on your machine or maybe some another error happened. Please add "try..catch" block and see what error is thrown there.
Check our other product here:
http://www.forexcopier.com
http://www.forexcopier.com
-
- Posts: 40
- Joined: Wed Sep 15, 2010 6:52 am
I changed my procedure which attempts file I/O to the following to capture the error message:
... and here is a screen shot of the Journal output:
Code: Select all
Procedure CaptureTradeData;
begin
try
AssignFile(df,'C:\TradeData.txt');
Rewrite(df);
CloseFile(df);
except
on E : Exception do
Print(E.ClassName+' error raised, with message: '+E.Message);
end;
end;
... and here is a screen shot of the Journal output:
- Attachments
-
- A reminder: I am compiling using FPC via the Lazarus IDE in a Win32 MS Vista environment.
What should I do next? - error message.jpg (28.91 KiB) Viewed 17101 times
- A reminder: I am compiling using FPC via the Lazarus IDE in a Win32 MS Vista environment.
creativity + willful purpose
-
- Posts: 40
- Joined: Wed Sep 15, 2010 6:52 am
I solved my problem.
This solution pertains to Windows Vista -- other versions of Windows may be slightly different, but the concept is the same.
First, I ran into some trouble trying to do the 8 steps below to the root directory C:\. So, I changed my filename to a location in my user area: C:\users\username\textfile.txt
Then, I just followed these steps:
1. Log on as a user with administrative rights.
2. Right click on Start to launch Explore and then right click on the folder where you want to create a file.
3. Select "Properties"
4. Click the "Security" tab at the top of the window.
5. In the "Group or User Names" box, click on your currently logged on, user name.
6. Click the "Edit" button to change permissions.
7. Check all of the boxes under the "Allow" column heading (this will give you read/write access permission).
8. Click "Apply" then click "OK"
This solution pertains to Windows Vista -- other versions of Windows may be slightly different, but the concept is the same.
First, I ran into some trouble trying to do the 8 steps below to the root directory C:\. So, I changed my filename to a location in my user area: C:\users\username\textfile.txt
Then, I just followed these steps:
1. Log on as a user with administrative rights.
2. Right click on Start to launch Explore and then right click on the folder where you want to create a file.
3. Select "Properties"
4. Click the "Security" tab at the top of the window.
5. In the "Group or User Names" box, click on your currently logged on, user name.
6. Click the "Edit" button to change permissions.
7. Check all of the boxes under the "Allow" column heading (this will give you read/write access permission).
8. Click "Apply" then click "OK"
creativity + willful purpose
Who is online
Users browsing this forum: No registered users and 12 guests