我需要执行从 Ninja Trader 到 Interactive Broker 的 Api C# 的 JSON CSharp Post,使用以下内容:
{
"Instrument": "SIE",
"Side": "BUY",
"Volume": "200",
"Price": "100",
"Type": "LMT",
"Accounts": ["DU1107110", "DU1107170", "DU1107180", "DU1107190"],
"Currency": "EUR",
"Exchange": "SMART",
"SecType": "STK",
"PrimaryExch": "",
"Gives you": ""
}
您需要先使用数据创建一个c#对象。 那么您需要序列化该对象,例如 var json = JsonConvert.SerializeObject(数据);
现在您需要做的就是将字符串传递给 post 方法。
var stringContent = new StringContent(json, UnicodeEncoding.Utf8, "application/json");
var client = new HttpClient(); var response = await client.PostAsync(uri, stringContent);
非常感谢!我能够做到这一点:
dynamic jsonObject = new ExpandoObject();
jsonObject.Instrument= "SIE"; //Instrument.FullName
jsonObject.Side= "BUY";
jsonObject.Volume=quantity;
jsonObject.Price= "100";
jsonObject.Type= "LMT";
jsonObject.Accounts = new string[1];
jsonObject.Accounts[0] = "DU1107160";
jsonObject.Currency= "EUR";
jsonObject.Exchange= "SMART";
jsonObject.SecType= "STK";
jsonObject.PrimaryExch= "";
jsonObject.Date= "";
postToInteractiveBroker(jsonObject);
/--------------------/
protected void postToInteractiveBroker(ExpandoObject jsonObject)
{
var json = JsonConvert.SerializeObject(jsonObject);
var stringContent = new StringContent(json, UnicodeEncoding.UTF8, "application/json");
var client = new HttpClient();
var response = client.PostAsync("http://localhost:59251/api/trades", stringContent);
Print("- - - - - - - | POST TO INTERACTIVE BROKER | - - - - - - ");
Print(response);
Print("- - - - - - - | END POST TO INTERACTIVE BROKER | - - - - - - ");
}
你如何使用 JsonConvert.SerializeObject 我收到消息名称 JsonConvert 在当前上下文中不存在我尝试直接在 ninjacript nt8 上执行此操作