Writing to File

admin's avatar

admin since: 30 Nov -0001;

  05 Oct 2012, 11:55
Writing to File
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();
        }
    }
}
TRADERS FIRSTâ„¢

Balena since: 17 Dec 2012;

  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();
        }
    }
}
Spotware's avatar

Spotware since: 23 Sep 2013;

  03 Mar 2015, 15:11
TRADERS FIRSTâ„¢ Vote for your favorite features: https://ctrader.com/forum/suggestions

benco5000 since: 01 May 2015;

  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 since: 01 May 2015;

  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.

bosma's avatar

bosma since: 25 Dec 2014;

  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.

iburakbirer since: 11 Mar 2015;

  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,

ClickAlgo's avatar

ClickAlgo since: 05 Feb 2015;

  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.

~ Contact us for all your cBot & Indicator development ~
ClickAlgo's avatar

ClickAlgo since: 05 Feb 2015;

  21 Aug 2015, 13:46
RE:

Paul_Hayes said:

try this

var outStream = new FileStream(logfileName, FileMode.Open, 
                               FileAccess.Write, FileShare.ReadWrite)
~ Contact us for all your cBot & Indicator development ~