Aquí tienes un ejemplo de cBot en C# para cTrader que guarda los principales datos del Market Depth en un archivo:
csharp
using System;
using System.IO;
using cAlgo.API;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]
public class MarketDepthLogger : Robot
{
[Parameter("Log File Path", DefaultValue = @"C:\MarketDepthLogs\depth_log.csv")]
public string LogFilePath { get; set; }
[Parameter("Log Interval (seconds)", DefaultValue = 5)]
public int LogInterval { get; set; }
private DateTime _lastLogTime;
protected override void OnStart()
{
// Crear directorio si no existe
Directory.CreateDirectory(Path.GetDirectoryName(LogFilePath));
// Escribir encabezados del archivo CSV
if (!File.Exists(LogFilePath))
{
File.WriteAllText(LogFilePath, "Timestamp,Symbol,Bid,Ask,BidVolume,AskVolume\n");
}
}
protected override void OnBar()
{
LogMarketDepth();
}
private void LogMarketDepth()
{
// Verificar intervalo de tiempo
if ((Server.Time - _lastLogTime).TotalSeconds < LogInterval)
return;
try
{
// Obtener datos del Market Depth
var marketDepth = MarketData.GetMarketDepth(Symbol);
// Preparar línea de registro
string logEntry = string.Format(
"{0},{1},{2},{3},{4},{5}\n",
Server.Time.ToString("yyyy-MM-dd HH:mm:ss"),
Symbol.Name,
marketDepth.Bid,
marketDepth.Ask,
marketDepth.BidVolume,
marketDepth.AskVolume
);
// Agregar al archivo
File.AppendAllText(LogFilePath, logEntry);
// Actualizar último tiempo de log
_lastLogTime = Server.Time;
}
catch (Exception ex)
{
Print($"Error logging market depth: {ex.Message}");
}
}
}
}