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

SVM possible in NT8?

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

    #16
    Originally posted by jortuzar View Post
    Most of the wrappers for Tensorflow are for .NetStandard V2.0 which is not yet supported by Ninjatrader. I integrated tensorflow throw a restful api. It works but it is very slow for back-testing purpose. If anyone knows of a wrapper compatible with .net framework 4.5 please post.
    jortuzar With the restful api I get a latency of ~10ms when running a project with identical code in visual studio. However, when running the NT script I get a latency >100ms with cases >500ms. (see images attached)

    Did you experienced the same?
    In my case the line:
    Code:
     response = client.PostAsync(url, stringContent).Result;
    is taking too long.

    I'm attaching the cs file as reference. TestingTensorflowModel.cs

    I couldn't try the grpc communication due to the .net 4.5 framework. :/

    Comment


      #17
      It's curious that you are posting and parsing the response asynchronously but your code is synchronous. Obviously you're getting a result so everything might be fine.
      Could be that asynchronous part is being ignored or that I am missing something?

      Comment


        #18
        Originally posted by MojoJojo View Post
        It's curious that you are posting and parsing the response asynchronously but your code is synchronous. Obviously you're getting a result so everything might be fine.
        Could be that asynchronous part is being ignored or that I am missing something?
        From what I understand is that adding the .Result at the end does the trick.
        See https://stackoverflow.com/a/31394044/12234828

        But maybe there's a better way to do it.

        Comment


          #19
          Originally posted by Capablanca View Post
          I also intend to use a tensorflow model and I imagine a model deployed with tensorflow services via grpc would be able to do predictions, but may not be the optimal solution for backtesting the strategy in NT.


          jortuzar, can you elaborate more on the wrapper needed? I've done plenty of indicators and strategies in NT8, but I'm no expert in C# and I don't know the differences of the .net frameworks.
          Do you know if these libraries work for deploying a Tensorflow model in NT8? Which wrapper did you use?
          Keras.NET is a high-level neural networks API for C# and F#, with Python Binding and capable of running on top of TensorFlow, CNTK, or Theano. - GitHub - SciSharp/Keras.NET: Keras.NET is a high-le...

          .NET Standard bindings for Google's TensorFlow for developing, training and deploying Machine Learning models in C# and F#. - SciSharp/TensorFlow.NET


          Thanks in advance
          Scisharp won't work. Those are .net standard 2.0

          Wrapper means you wrap the c++ tensor flow libraries so they can be used by a certain framework (.net 4.5 for example compatible with Ninjatrader).

          Easiest is to build your models in Python and then integrate them with Ninjatrader through a restful API such as Flask.

          Python is really the way to go if you want to lower your R&D time to find an optimal model. There is just a world of tools you can use in python scikit-learn, xgboost, plotting utils, and many many data manipulation tools.

          If you are not familiar with data science there is a steep learning curve up ahead, but there could be a huge reward also.
          ​​​​​​
          Last edited by jortuzar; 07-06-2020, 09:15 AM.

          Comment


            #20
            Originally posted by Capablanca View Post

            jortuzar With the restful api I get a latency of ~10ms when running a project with identical code in visual studio. However, when running the NT script I get a latency >100ms with cases >500ms. (see images attached)

            Did you experienced the same?
            In my case the line:
            Code:
             response = client.PostAsync(url, stringContent).Result;
            is taking too long.

            I'm attaching the cs file as reference. [ATTACH]n1107593[/ATTACH]

            I couldn't try the grpc communication due to the .net 4.5 framework. :/
            I haven't really measured it. More involved in the R&D phase right now. The current latency I have with Flask is good enough to do Playback at 100x

            Comment


              #21
              I wanted to let anyone who is interested in using tensorflow or any other machine learning library available in Python, that you can seamlessly integrate them to Ninjatrader NT8 using Python.Net. Here is an example of the code I used and it works like a charm.

              Link to pythonnet -> https://github.com/pythonnet/pythonnet/wiki

              In order to install it in Visual Studio I had to:
              - download the nuget package directly from the binary sources. https://ci.appveyor.com/project/pyth.../branch/master
              - select the one that matches with your hardware and Python version then go to artifacts and download the nuget package. example "pythonnet_py37_x64.2.5.0.nupkg".
              - install the package in visual studio
              - then you will have to reference the Python.Runtime.dll (that you get from the above installation) in your Ninjatrader project.

              I'm using python through CONDA and environment so your code in C# will have to look something like this:


              Code:
              //USE PYTHON.NET
              using Python.Runtime;
              ...
              //NEED TO LET PYTHON.NET KNOW THE ENVIRONMENT VARIABLES OF YOUR PYTHON SETTINGS
              private string pathToVirtualEnv = @"C:\ProgramData\Anaconda3\envs\NinjatraderAI"
              Environment.SetEnvironmentVariable("PATH", pathToVirtualEnv, EnvironmentVariableTarget.Process);
              Environment.SetEnvironmentVariable("PYTHONHOME", pathToVirtualEnv, EnvironmentVariableTarget.Process);
              Environment.SetEnvironmentVariable("PYTHONPATH ", pathToVirtualEnv+"/Lib/site-packages"+";"+ pathToVirtualEnv+"/Lib/", EnvironmentVariableTarget.Process);
              PythonEngine.PythonHome = pathToVirtualEnv;
              PythonEngine.PythonPath = Environment.GetEnvironmentVariable("PYTHONPATH", EnvironmentVariableTarget.Process);
              
              //IMPORT WHATEVER LIBRARIES YOU NEED
              using (Py.GIL()) { sys = Py.Import("sys"); }
              using (Py.GIL()) { np = Py.Import("numpy"); }
              using (Py.GIL()) { tf = Py.Import("tensorflow"); }
              ...
              
              //LOAD YOUR MODEL
              using (Py.GIL()) { model = tf.keras.models.load_model(model_path); }
              ...
              
              //PREDICT!
              private double Python_GetPrediction(List<List<List<double>>> tensor)
              {
                         double ret = -1;
                          using (Py.GIL())
                          {
                              dynamic np_tensor = np.array(tensor, dtype: "float64");
                              dynamic pred = model.predict(np_tensor)[0][0];
                              ret = (double) pred;
                          }
                          return ret;
              }
              Last edited by jortuzar; 07-16-2020, 05:03 PM.

              Comment


                #22
                Awesome info, thanks. Python.Net looks like a good choice for quick results. As someone obsessed with performance I think I'll reluctantly take a break from curly braces and give Cython, mypy and/or Numba a shot. Looks to be too early for using Julia. NT will handle the live trading once all the pieces are in place.
                Last edited by MojoJojo; 07-16-2020, 05:19 AM.

                Comment


                  #23
                  Nice! Got my model running using python.net code. Thanks jortuzar!
                  The latency however is x5 times larger than using the restful api.
                  ...quite disappointing

                  Comment


                    #24
                    Originally posted by Capablanca View Post
                    Nice! Got my model running using python.net code. Thanks jortuzar!
                    The latency however is x5 times larger than using the restful api.
                    ...quite disappointing
                    That is very strange. I haven't measured it since I'm more involved in the modeling right now, but when i tried it I did think it was slow. If you find any reason let us know.

                    Comment


                      #25
                      Originally posted by Capablanca View Post
                      Nice! Got my model running using python.net code. Thanks jortuzar!
                      The latency however is x5 times larger than using the restful api.
                      ...quite disappointing
                      Just a quick look for info on this indicates that the lag is likely due to the instantiation and overhead added by pythonnet. Here a some comments on this https://medium.com/scisharp/using-py...n-11124d6190cf

                      Any ideas or alternatives on this matter are very welcome!
                      Last edited by jortuzar; 07-20-2020, 11:17 AM.

                      Comment


                        #26
                        For anyone that is interested, the best solution I've found so far is using sockets to communicate to python. I has the advantage that it's easy to customize the server side in python to add more models/calculations (eg. sklearn models). I also found that the tensorflow model predictions are up to x3 faster using the tensorflow/serving with dockers, than loading the model in python with tf.keras.models.load_model().

                        My final approach was:

                        Socket Connect (C#) <-> Server Calculations (Python)
                        Inside the python server the tensorflow predictions are done with tensorflow/serving
                        Server Calculations (Python) <-> Tensorflow Restful API (Dockers)

                        My latency is now <15 ms. That's x100 times better than using the Tensorflow Restful API directly from C# and x500 times better than python.net.

                        The client code in c# and the server code in python are attached as files.

                        Code:
                        using System.Net;
                        using System.Net.Sockets;
                        
                        private static double[] getInstructions(IPAddress ipAddr, IPEndPoint localEndPoint, string X)
                                {
                                    double[] response=null;
                                     Socket sender = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                                            sender.Connect(localEndPoint);
                                            byte[] messageSent = Encoding.ASCII.GetBytes(X);
                                            int byteSent = sender.Send(messageSent);
                                            byte[] messageReceived = new byte[1024];
                                            int byteRecv = sender.Receive(messageReceived);
                                            string messageReceived_str = Encoding.ASCII.GetString(messageReceived, 0, byteRecv);
                                            response = Array.ConvertAll(messageReceived_str.Split(','), double.Parse);
                                            sender.Shutdown(SocketShutdown.Both);
                                            sender.Close();     
                                            return response;
                                        }
                        
                        protected override void OnBarUpdate()
                                {            
                                    data = getInstructions(ipAddr, localEndPoint, X_str);
                                }
                        Attached Files

                        Comment


                          #27
                          Just to give some light about this topic, I recently published this article:

                          https://medium.com/@mcostajr/applied...es-304e9f434c8
                          mcosta72
                          NinjaTrader Ecosystem Vendor - Quant-Wise

                          Comment


                            #28
                            Hi LunarDiplomacy,

                            We created an extension of NinjaTrader that will create machine learning models based on indicator or instrument data that can be used in trading. We're using Microsoft's AutoML library which searches for the best machine learning algo for the given data set. If you want to develop it yourself that's probably the direction I would go based on several other machine learning libraries we tried. Or if you want to try our deep signal library it has a free two week trial.

                            Cheers,
                            Eric

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by alifarahani, Today, 09:40 AM
                            6 responses
                            36 views
                            0 likes
                            Last Post alifarahani  
                            Started by Waxavi, Today, 02:10 AM
                            1 response
                            17 views
                            0 likes
                            Last Post NinjaTrader_LuisH  
                            Started by Kaledus, Today, 01:29 PM
                            5 responses
                            14 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by Waxavi, Today, 02:00 AM
                            1 response
                            12 views
                            0 likes
                            Last Post NinjaTrader_LuisH  
                            Started by gentlebenthebear, Today, 01:30 AM
                            3 responses
                            17 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Working...
                            X