Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Sending POST request to localhost server

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Sending POST request to localhost server

    I am attempting to send an input to a local server that will then return an output. When I run the code outside of NinjaTrader it works great. When I run the code (as indicator) inside of NinjaTrader though, it crashes the chart. I do see the POST request went through properly on the servers end. If anyone could help me identify what is causing this issue I would greatly appreciate it!
    My code is as follows:

    Code:
    public class TensorTest : Indicator
    {
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Description                                    = @"";
                Name                                        = "Test";
                Calculate                                    = Calculate.OnBarClose;
                IsOverlay                                    = true;
            }
            else if (State == State.Configure)
            {
                Task<string> response = SendPOST();
                Print(response.Result);
            }
        }
    
        private static async Task<string> SendPOST()
        {
            var responseContent = "";
            var url = "http://localhost:5000/app";
            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("input", "val1,val2"),
            });
    
            using (var httpClient = new HttpClient())
            {
                var response = await httpClient.PostAsync(url, content);
                responseContent = await response.Content.ReadAsStringAsync();
            }
    
            return responseContent;
        }
    }​
    Last edited by Kreyenhagen; 05-08-2023, 03:35 PM.
    Kreyenhagen
    NinjaTrader Ecosystem Vendor - Tyche Trading

    #2
    It appears to me that you are awaiting with the NinjaTrader UI thread during State.Configure? That is not a good idea - the indicator instance gets to that state repeatedly when it isn't even on a chart, such as when you are pulling up the list of indicators.

    Why don't you consider something like starting up a timer, and running the things that have to await from there? That way it would be on the timer thread, and will not halt the UI. It is considered undesireable to sleep or await any NinjaTrader threads - that has significant impact on other running charts and indicators and can lead the whole platform to become unstable.

    You may want to read and consider this post: https://forum.ninjatrader.com/forum/...66#post1129166

    Look at the DelayedEventExample and how it does something using a TimerEventProcessor. You could put your tasks there.

    You should also read https://ninjatrader.com/support/help...fecycle_of.htm to understand why there are instances in State.Configure all the time and not just when it is running the one time on the chart.
    Last edited by QuantKey_Bruce; 05-08-2023, 03:47 PM.
    Bruce DeVault
    QuantKey Trading Vendor Services
    NinjaTrader Ecosystem Vendor - QuantKey

    Comment


      #3
      Hello Kreyenhagen and Bruce,

      Thank you for your notes.

      Bruce, you have mentioned the following: "It appears to me that you are awaiting with the NinjaTrader UI thread during State.Configure? That is not a good idea - the indicator instance gets to that state repeatedly when it isn't even on a chart, such as when you are pulling up the list of indicators."
      While it is important to understand the NinjaScript Lifecycle, it is not true that the indicator instance gets to State.Configure repeatedly. State.Configure is only called once for the life of the object and this is described on the help guide page for OnStateChange():


      The Lifecycle page also mentions that, "Resources should only be set up once an object has reached State.Configure or State.DataLoaded" and refers to the NinjaScript Best Practices page for more information:


      Kreyenhagen, do you see any errors on the screen or on the Log tab of the Control Center when the chart crashes? What version of NinjaTrader are you using? Please provide the entire version number. This can be found under Help -> About (Example: 8.?.?.?)

      You may need to consider how NinjaTrader is multi-threaded in order to prevent locking/crashing behaviors from your script. The following page might be helpful to review for a better understanding:
      https://ninjatrader.com/support/help...-threading.htm

      I look forward to your reply.
      Emily C.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by JoMoon2024, Today, 06:56 AM
      0 responses
      6 views
      0 likes
      Last Post JoMoon2024  
      Started by Haiasi, 04-25-2024, 06:53 PM
      2 responses
      17 views
      0 likes
      Last Post Massinisa  
      Started by Creamers, Today, 05:32 AM
      0 responses
      5 views
      0 likes
      Last Post Creamers  
      Started by Segwin, 05-07-2018, 02:15 PM
      12 responses
      1,786 views
      0 likes
      Last Post Leafcutter  
      Started by poplagelu, Today, 05:00 AM
      0 responses
      3 views
      0 likes
      Last Post poplagelu  
      Working...
      X