Skip to content

LocalStorage

interface cAlgo.API

DesktopCLICloud

Represents the local storage API.

Environment behaviour

Cloud — works differently. Values are kept in memory for the current run only; nothing is saved to or read from disk, and auto-flush is off.

Definition

public interface LocalStorage

Namespace: cAlgo.API · Group: Local storage

Examples

Example 1

using cAlgo.API;

namespace cAlgo.Robots;

public partial class TestExample : Robot
{
    protected override void OnStart()
    {
        const string key = "Key";
        const string expectedData = "Data";

        LocalStorage.SetString("Key", expectedData);

        Print($"Actual Date: {LocalStorage.GetString(key)} | Expected Data: {expectedData}");
    }
}
import clr

clr.AddReference("cAlgo.API")

# Import cAlgo API types
from cAlgo.API import *

# Import trading wrapper functions
from robot_wrapper import *

class Test():
    def on_start(self):
        key = "Key"
        expectedData = "Data"
        api.LocalStorage.SetString("Key", expectedData)
        print(f"Actual Date: {api.LocalStorage.GetString(key)} | Expected Data: {expectedData}")
{
  "AccessRights": "None"
}

Example 2

using cAlgo.API;

namespace cAlgo.Robots;

public partial class TestExample : Robot
{
    public record Point(double X, double Y);

    protected override void OnStart()
    {
        const string key = "Key";

        var expectedPoint = new Point(10, 12);

        LocalStorage.SetObject(key, expectedPoint);

        Print($"Actual Point: {LocalStorage.GetObject<Point>(key)} | Expected Point: {expectedPoint}");

        LocalStorage.Remove(key);
        LocalStorage.Flush(LocalStorageScope.Instance);

        Print($"Point After Remove: {LocalStorage.GetObject<Point>(key)}");
    }
}
import clr

clr.AddReference("cAlgo.API")

# Import cAlgo API types
from cAlgo.API import *

# Import trading wrapper functions
from robot_wrapper import *

class Test():
    def on_start(self):
        key = "Key"
        expectedValue = 10.2
        api.LocalStorage.SetObject(key, expectedValue)
        print(f"Actual: {api.LocalStorage.GetString(key)} | Expected: {expectedValue}")
        api.LocalStorage.Remove(key)
        api.LocalStorage.Flush(LocalStorageScope.Instance)
        print(f"After Remove: {api.LocalStorage.GetString(key)}")
{
  "AccessRights": "None"
}

Methods

Name Description
FlushDesktopCLICloudvoid Saves all data to the disk. When using local storage, data is automatically saved every minute. Use this method if you want to save data without delays.
GetObject 2 overloadsDesktopCLICloudT Gets an object from local storage. This method will search all available scopes (Instance -> Type -> Device) and stops on first found value.
GetString 2 overloadsDesktopCLICloudstring Gets a string value from local storage. This method will search all available scopes (Instance -> Type -> Device) and stops on first found value.
ReloadDesktopCLICloudvoid Reloads all values from the disk. When calling this method, unsaved pending changes may be lost. To avoid losing information, call the Flush() method first.
Remove 2 overloadsDesktopCLICloudvoid Removes an element from local storage. This method will search all available scopes (Instance -> Type -> Device) and stops on first found value.
SetObject 2 overloadsDesktopCLICloudvoid Saves an object to local storage. The storage scope is LocalStorageScope.Instance.
SetString 2 overloadsDesktopCLICloudvoid Saves a string value to local storage. The storage scope is LocalStorageScope.Instance.