C# to python OPEN API
C# to python OPEN API
04 Nov 2024, 17:55
I have this C# sharp code that save market data (only last Bar Close) in csv file on the local PC:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using cAlgo.API;
namespace cTrader
{
[Robot(TimeZone = TimeZones.EasternStandardTime, AccessRights = AccessRights.FullAccess)]
public class DataLogger : Robot
{
[Parameter("Log Directory Path", DefaultValue = "c:\\\\ClickAlgo\\logs\\D1")]
public string LogDirPath { get; set; }
[Parameter("Tickers", DefaultValue= "EURUSD,GBPUSD,USDJPY,USDCHF,"+
"USDCAD,AUDUSD,NZDUSD,USDSEK,"+
"USDNOK,USDZAR,USDSGD,USDMXN,"+
"USDCNH,USDPLN,"+
"EURGBP,EURCAD,EURNZD,EURAUD,EURJPY,EURCHF,"+
"GBPJPY,GBPAUD,GBPCAD,GBPNZD,GBPCHF,"+
"AUDCAD,AUDNZD,AUDJPY,"+
"NZDJPY,NZDCAD,"+
"CHFJPY,"+
"CADJPY,"+
"US500,GER40,NAS100,US30,UK100,FRA40,AUS200,JPN225,USDX,"+
"BTCUSD,ETHUSD,XRPUSD,BCHUSD,SOLUSD,DOGEUSD,ADAUSD,"+
"SpotBrent,SpotCrude,NatGas,"+
"USTN10YR,EUBund,"+
"AAPL.US-24,AMZN.US-24,MSFT.US-24,NVDA.US-24,TSLA.US-24,"+
"BA.US-24,META.US-24,GOOG.US-24,XOM.US-24,CVX.US-24,"+
"BAC.US-24,GM.US-24,DIS.US-24,KO.US-24,V.US-24,WMT.US-24,"+
"PFE.US-24,ORCL.US-24,MCD.US-24,BABA.US-24,C.US-24,NKE.US-24,"+
"XAUUSD,XAUEUR,XAGUSD,XAGEUR,Copper"
)]
public string TickerList { get; set; }
private string[] tickers;
private bool isFirstTick = true;
protected override void OnStart()
{
Print("Starting script...");
try
{
tickers = TickerList.Split(',');
Directory.CreateDirectory(LogDirPath);
foreach (var ticker in tickers)
{
Print($"Processing ticker: {ticker.Trim()}");
LogLast10BarsData(ticker.Trim());
}
Print("Waiting for the next bar close...");
}
catch (Exception ex)
{
Print($"Error in OnStart: {ex.Message}");
}
}
private void LogLast10BarsData(string ticker)
{
try
{
var bars = MarketData.GetBars(TimeFrame.Daily, ticker);
if (bars == null || bars.Count < 1000)
{
Print($"Insufficient data found for ticker {ticker}.");
return;
}
string logFilePath = Path.Combine(LogDirPath, $"{ticker}_D1.csv");
using (var logWriter = new StreamWriter(logFilePath, false))
{
logWriter.WriteLine("Date,Open,High,Low,Close");
for (int i = 1000; i >= 1; i--)
{
var date = Server.Time.ToString("yyyy-MM-dd");
var O = bars.OpenPrices.Last(i).ToString();
var H = bars.HighPrices.Last(i).ToString();
var L = bars.LowPrices.Last(i).ToString();
var C = bars.ClosePrices.Last(i).ToString();
var OHLC = $"{date},{O},{H},{L},{C}";
logWriter.WriteLine(OHLC);
}
Print($"Logged data for ticker {ticker} into {logFilePath}.");
}
}
catch (Exception ex)
{
Print($"Error logging data for ticker {ticker}: {ex.Message}");
}
}
protected override void OnBar()
{
if (isFirstTick)
{
Print("First tick received.");
isFirstTick = false;
}
try
{
foreach (var ticker in tickers)
{
var bars = MarketData.GetBars(TimeFrame.Daily, ticker.Trim());
if (bars == null || bars.Count < 1000)
continue;
string logFilePath = Path.Combine(LogDirPath, $"{ticker}_D1.csv");
var lines = new List<string>();
if (File.Exists(logFilePath))
{
using (var logReader = new StreamReader(logFilePath))
{
string line;
while ((line = logReader.ReadLine()) != null)
{
lines.Add(line);
}
}
}
using (var logWriter = new StreamWriter(logFilePath, false))
{
if (lines.Count == 0)
{
logWriter.WriteLine("Date,Open,High,Low,Close");
}
else
{
logWriter.WriteLine(lines.First());
}
for (int i = Math.Max(1, lines.Count - 1000); i < lines.Count; i++)
{
logWriter.WriteLine(lines[i]);
}
var date = Server.Time.ToString("yyyy-MM-dd");
var O = bars.OpenPrices.Last(1).ToString();
var H = bars.HighPrices.Last(1).ToString();
var L = bars.LowPrices.Last(1).ToString();
var C = bars.ClosePrices.Last(1).ToString();
var OHLC = $"{date},{O},{H},{L},{C}";
logWriter.WriteLine(OHLC);
Print($"Logged new bar data for ticker {ticker}: {OHLC}");
}
}
Print("Waiting for the next bar close...");
}
catch (Exception ex)
{
Print($"Error in OnBar: {ex.Message}");
}
}
}
}
and this is the .bat file that run this robot:
REM Start the cTrader CLI process
start "" "C:\Users\User\AppData\Local\Spotware\cTrader\abb70432efbee65d18af69e79fe8efe1\ctrader-cli.exe" run "C:\Users\User\Documents\cAlgo\Sources\Robots\D1.algo" --ctid=user@gmail.com --pwd-file="D:\password.pwd" --account=3706404 --symbol=EURUSD --period=D1 --full-access
timeout /t 300 /nobreak
taskkill /IM "ctrader-cli.exe" /F
Is there a way to have python code that run independent of the ctrader software to do the same job as C# robot is doing??? I have an application in Open API portal but all my efforts dont gave any results to save data from the server. How much wil cost to develop such a script?