如何从Ninja Trader的操作中使用C#制作JSON帖子?

jm81lzqq  于 2023-05-13  发布在  C#
关注(0)|答案(2)|浏览(172)

我需要从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": ""
}
wkyowqbh

wkyowqbh1#

你需要首先使用数据创建一个C#对象。然后你需要序列化这个对象例如var json = JsonConvert.SerializeObject(data);
现在你需要做的就是将字符串传递给post方法。
var stringContent = new StringContent(json,UnicodeEncoding.Utf8,“application/json”);
var client = new HttpClient(); var response = await client.PostAsync(uri,stringContent);

soat7uwm

soat7uwm2#

非常感谢!我能够做到这一点:

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 | -  - - - - -  ");
}

相关问题