Sending HTTP request from Indicator does not work

Created at 03 Aug 2022, 11:11
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
FR

frantisek-fanda

Joined 03.08.2022

Sending HTTP request from Indicator does not work
03 Aug 2022, 11:11


Hi,
I am trying to send POST request with INdicator in cTrader, unfortunatelly it does not work, but I don't know why. 
Code is ok, there is not any error in Journal or Log, but the external website just don't receive any request. Could anyone push me to the right direction to solve this problem? Thank you.
My code is in Calculate(), after some conditions are met, this code is invoked:
var endpoint = "requestcatcher.com"; // this url was modified because of spam filter in forum (i guess) it is normally with https
var client = new HttpClient();
client.PostAsync(endpoint, new StringContent("{'test':'test'}", Encoding.UTF8, "application/json"));

I am using requestcatcher which is solid working website for testing sending requests. It is working when I send request manually from browser or from python, php etc ...
Thank you


@frantisek-fanda
Replies

firemyst
04 Aug 2022, 04:07

Try this as I believe you need to include/set the headers:

var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var result = client.PostAsync(endpoint, content).Result;

 


@firemyst

paolo.panicali
05 Aug 2022, 00:36 ( Updated at: 21 Dec 2023, 09:22 )

Hi, I would leave 3 lines of code into the bot and let a Web App doing the job

Because you have a simple code which guarantees you compatibility (3 lines of code using net framework system library no nuget library) , not crashing prone, minimal workload for Ctrader (if anything goes wrong into your bot, it s over) , reusable (few lines to add to any bot) and you have some privacy because you are making the requests from a web application installed on an external server.(you can deploy the webapp on a VPS or using pages like pythonanywhere as you wish, they will get an IP that does not belong to you)

In this example the Bot will buy if yesterdays temperature is higher than 2 days ago temperature in a city you can choose, the Web app send a request to a free weather api  (www.weather something.com/api......"  to get the temperatures.

At the end of the day the trade is closed and a new one is opened. You can change the city using CityName parameter.

The bot passes the City name and the current date to the web app, so that it can work also on backtesting.

............................ 

protected override void OnBar()
        {
            Data_WebApp OutPutData = new Data_WebApp();
            OutPutData.CityName = City_Name;
            OutPutData.Date = Bars.OpenTimes.LastValue.ToString("yyyy-MM-dd");
            Print(OutPutData.Date);

            //THIS IS THE REQUEST
            var json = new JavaScriptSerializer().Serialize(OutPutData);
            var url = "http://127.0.0.1:5000/api";
            string response = new WebClient().UploadString(url, json);

            // If yesterday temperature in Paris was higher than 2 days ago then buy otherwise sell
            // close prev day trade
            if (Bars.OpenTimes.Last(1).Day == Bars.OpenTimes.LastValue.AddDays(-1).Day)
            {
                Closealltrades();

                if (Int32.Parse(response) == 1)
                {
                    ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, "Buy" + Symbol.Name, 50, 75);
                }
                if (Int32.Parse(response) == 0)
                {
                    ExecuteMarketOrder(TradeType.Sell, Symbol, 1000, "Sell" + Symbol.Name, 50, 75);
                }
            }
        }

   public class Data_WebApp
        {
            public string CityName;
            public string Date;
        }

....................

then you deploy your webapp consuming a weather api in this case it is a free one, weather visual cross...

Hope this will help, bye.


@paolo.panicali

frantisek-fanda
14 Aug 2022, 15:47

Thank you guys. I tried both of them (compiled with older .NET and .NET 6) but none of them works. :(


@frantisek-fanda