Writing to File

Created at 05 Oct 2012, 11:55
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
admin's avatar

admin

Joined 30.09.2011

Writing to File
05 Oct 2012, 11:55


using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.IO;

namespace cAlgo.Robots
{
    [Robot]
    public class WriteToFileExample : Robot
    {
        StreamWriter _fileWriter;
    	
    	
        protected override void OnStart()
        {
			var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
			var filePath = Path.Combine(desktopFolder, "ServerTimeExample.txt"); 
			
			_fileWriter = File.AppendText(filePath);//creating file
			
			_fileWriter.AutoFlush = true;//file will be saved on each change
        }
        
        protected override void OnTick()
        {
			_fileWriter.WriteLine("Server Time: " + Server.Time);
        }
        
        protected override void OnStop()
        {
        	_fileWriter.Close();
        }
    }
}

 


@admin
Replies

Balena
27 Feb 2015, 16:21

RE:

 

Can you give the same type example of reading from a file back into cAlgo?

 

admin said:

using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.IO;

namespace cAlgo.Robots
{
    [Robot]
    public class WriteToFileExample : Robot
    {
        StreamWriter _fileWriter;
    	
    	
        protected override void OnStart()
        {
			var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
			var filePath = Path.Combine(desktopFolder, "ServerTimeExample.txt"); 
			
			_fileWriter = File.AppendText(filePath);//creating file
			
			_fileWriter.AutoFlush = true;//file will be saved on each change
        }
        
        protected override void OnTick()
        {
			_fileWriter.WriteLine("Server Time: " + Server.Time);
        }
        
        protected override void OnStop()
        {
        	_fileWriter.Close();
        }
    }
}

 

 


@Balena

Spotware
03 Mar 2015, 15:11

You can find an example there:

https://msdn.microsoft.com/en-us/library/ezwyzy7b.aspx


@Spotware

benco5000
01 May 2015, 08:00

RE:

When I run the Writing to File example I get an error message:

 

 

Backtesting Started

Crashed OnStart with Security Exception: Request for the permission of the type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Backtesting Stopped

Does anyone have any insight?

Thanks in advance.


@benco5000

benco5000
01 May 2015, 22:08

RE: RE:

Fixed:  AccessRights was set to AccessRights.none  changed to AccessRights.FullAccess and it works now.

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class myTest : Robot
    {...

 

benco5000 said:

When I run the Writing to File example I get an error message:

 

 

Backtesting Started

Crashed OnStart with Security Exception: Request for the permission of the type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Backtesting Stopped

Does anyone have any insight?

Thanks in advance.

 

 


@benco5000

bosma
05 May 2015, 23:56

RE: RE: RE:

 AccessRights = AccessRights.FileSystem

to adhere to principle of least privilege.

benco5000 said:

Fixed:  AccessRights was set to AccessRights.none  changed to AccessRights.FullAccess and it works now.

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class myTest : Robot
    {...

 

benco5000 said:

When I run the Writing to File example I get an error message:

 

 

Backtesting Started

Crashed OnStart with Security Exception: Request for the permission of the type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Backtesting Stopped

Does anyone have any insight?

Thanks in advance.

 

 

 


@bosma

iburakbirer
21 Aug 2015, 02:48

Hi is there anything like "FILE_SHARE_READ" that meta trader has? Because i cannot make the txt file readable by other programs while ctrader is running. Hope you reply. Thanks,


@iburakbirer

ClickAlgo
21 Aug 2015, 13:40

look at managing opening text file, load the resuts into an object in memory and then closing the connection to text file and releasing the handle on it, this should free up the text file to be used by other programs.

 

if you keep the handle open, it will be locked.


@ClickAlgo

ClickAlgo
21 Aug 2015, 13:46

RE:

Paul_Hayes said:

try this

var outStream = new FileStream(logfileName, FileMode.Open, 
                               FileAccess.Write, FileShare.ReadWrite)

 

 


@ClickAlgo

... Deleted by UFO ...