【发布时间】:2016-04-26 06:58:20
【问题描述】:
我正在尝试使用 C# 中的基础 Interactive Broker API 来处理外汇市场数据。我的目标是获得多个货币对的买入价和卖出价。
这就是我现在所拥有的。主要:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBApi;
namespace TWS_Data
{
class Program
{
static void Main(string[] args)
{
//IB's main object
EWrapperImpl ibClient = new EWrapperImpl();
//Connect
ibClient.ClientSocket.eConnect("127.0.0.1", 7497, 0);
//Creat and define a contract to fetch data for
Contract contract = new Contract();
contract.Symbol = "EUR";
contract.SecType = "CASH";
contract.Currency = "USD";
contract.Exchange = "IDEALPRO";
// Create a new TagValue List object (for API version 9.71)
List<TagValue> mktDataOptions = new List<TagValue>();
// calling method every X seconds
var timer = new System.Threading.Timer(
e => ibClient.ClientSocket.reqMktData(1, contract, "", true, mktDataOptions),
null,
TimeSpan.Zero,
TimeSpan.FromSeconds(10));
}
}
}
API 函数“reqMktData”调用以下两个函数:
public virtual void tickSize(int tickerId, int field, int size)
{
Console.WriteLine("Tick Size. Ticker Id:" + tickerId + ", Field: " + field + ", Size: " + size + "\n");
}
public virtual void tickPrice(int tickerId, int field, double price, int canAutoExecute)
{
Console.WriteLine("Tick Price. Ticker Id:" + tickerId + ", Field:" + field + ", Price:" + price + ", CanAutoExecute: " + canAutoExecute + "\n");
string str = Convert.ToString(price);
System.IO.File.WriteAllText(@"C:\Users\XYZ\Documents\price.txt", str);
}
在这段代码中,我试图将市场数据保存在我的硬盘上作为虚拟测试。但是,这里我的问题出现了。在一次运行中,函数 tickPrice(..) 被调用 6 次并提供 6 种不同的价格(请参阅此处的 API 指南:https://www.interactivebrokers.com/en/software/api/apiguide/java/tickprice.htm)
我现在需要知道的是如何在 C# 中以某种方式保存这些单独的结果? (它们都被发布到控制台,但显然只有最后一个价格是保存在文件中的价格)。
我对 Matlab 编码非常熟悉,但对 C# 语法不熟悉。所以我以某种方式思考向量或 for 循环的形式来解决这个问题,但它不是那样工作的。
感谢任何帮助或提示
【问题讨论】:
标签: c# data-structures algorithmic-trading