How to create a share file that multiple instances can access?

Created at 03 Feb 2023, 07:51
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!
CT

ctid2434759

Joined 02.08.2022

How to create a share file that multiple instances can access?
03 Feb 2023, 07:51


Hi,

I have been at this problem for months now.
I've tried many different solution which work but do not solve my problem.

In order for it to work I think I need to create a share file that my cBot can access so that multiple instances can read/write to it and share the data.

Essentially what I need it to do is place and order and write that order to the "order_status.txt" file located in what I presume is the cBot's working directory and then check the file to see if an order has been placed.

The final version is more complex than this simplified version however this one piece of code 'file sharing' I cannot solve.
The bot just crashes no matter what I try here.

Any help with providing cBot access to a file to read/write and share with other instances would be much appreciated.

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.SingaporeStandardTime, AccessRights = AccessRights.FullAccess)]
    public class CUSTOMCBOT : Robot
    {

        //private string _shareFilePath = @"order_status.txt";
        //private string _shareFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "order_status.txt");
        private string _shareFilePath = @"Z:\Documents\cAlgo\Sources\Robots\TESTING\TESTING\order_status.txt";
        //private string _shareFilePath = @"C:\Users\<username>\AppData\Roaming\cAlgo\Sources\Robots\order_status.txt";
        //private string _shareFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "cAlgo", "Sources", "Robots", "order_status.txt");
        //private string _shareFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "order_status.txt");

        protected override void OnTick()
        {
            if (!File.Exists(_shareFilePath))
            {   
                Print("Not Placed");
                File.WriteAllText(_shareFilePath, "Not placed");
            }

            string orderStatus = File.ReadAllText(_shareFilePath);
            if (orderStatus != "Placed")
            {
                ExecuteMarketRangeOrder(TradeType.Buy, SymbolName, 1000, 5, Symbol.Ask, "2", null, 25);
                File.WriteAllText(_shareFilePath, "Placed");
                Print("Order Placed");
            }
        }

        protected override void OnStop()
        {
            File.WriteAllText(_shareFilePath, string.Empty);
        }
    }
}


 


@ctid2434759
Replies

ctid2434759
03 Feb 2023, 09:20 ( Updated at: 03 Feb 2023, 09:31 )

RE:

Every single time I write the question on this forum that I've been stuck on for days, weeks or months I figure it out about an hour after I ask the question.

So if anyone ever want's the code. What I have written above works.

I'm very very happy.

I'de like to take the time to say thanks to.....ABSOLUTE NO BODY, the coding champ does whatever the f the coding champ wants.


@ctid2434759

ZORDAX
14 Aug 2023, 14:04 ( Updated at: 14 Aug 2023, 15:18 )

Hi, do you still have the problem? I was like you hours ago, but I found the solution. Change AccessRights from None to FullAcess, I hope that it helped you if you didn't find the solution.


@ZORDAX