简体   繁体   中英

How to make a JSON post using C# from the operations of Ninja Trader?

I need to perform a JSON CSharp Post from the Ninja Trader to one for Api C# of the Interactive Broker, using following:

{
"Instrument": "SIE",
"Side": "BUY",
"Volume": "200",
"Price": "100",
"Type": "LMT",
"Accounts": ["DU1107110", "DU1107170", "DU1107180", "DU1107190"],
"Currency": "EUR",
"Exchange": "SMART",
"SecType": "STK",
"PrimaryExch": "",
"Gives you": ""
}

You need to create ac# object using data first. then you need to serialize that object eg var json = JsonConvert.SerializeObject(data);

Now all you need to do is pass the string to the post method.

var stringContent = new StringContent(json, UnicodeEncoding.Utf8, "application/json");

var client = new HttpClient(); var response = await client.PostAsync(uri, stringContent);

Thank you very much! I was able to do this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM