Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Here's a script to convert indicators and strategies

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

    Here's a script to convert indicators and strategies

    Attached is a C# script to convert NT7 indicators and strategies to NT8. It should convert most indicators without them needing any modifications. See below for limitations.

    The start of the script looks as follows:
    Code:
    bool testing = false; // "true" to convert test scripts when making changes.
    bool convertIndicators = true; // "true" to convert indicators.
    bool convertStrategies = true; // "true" to convert strategies.
    string indicatorSourceFolder = @"C:\Users\UserName\Documents\NinjaTrader 7\bin\Custom\Indicator";
    string indicatorDestinationFolder = @"C:\Users\UserName\Documents\NinjaTrader 8\bin\Custom\Indicators";
    string indicatorFilesToConvert = "*.cs";
    
    string strategySourceFolder = @"C:\Users\UserName\Documents\NinjaTrader 7\bin\Custom\Strategy";
    string strategyDestinationFolder = @"C:\Users\UserName\Documents\NinjaTrader 8\bin\Custom\Strategies";
    string strategyFilesToConvert = "*.cs";
    Steps for conversion:
    1. Backup your NT7 indicators and scripts in case you enter the wrong folders below.
    2. Download LINQPad 5 at www.linqpad.net. It's a free C# (and VB, F#, SQL) script runner - if you don't need intellisense. The script is a text file with full source (using .Net 4.6). If you don't want to use LINQPad, you can put the code in a console application (or WPF or whatever) instead - it needs the Microsoft code analysis Nuget package.
    3. Download the attached script, remove the ".txt" extension, open in LINQPad and make the following changes:
      • Change the indicatorSourceFolder variable to match where you have your NT7 indicators
      • Change the indicatorDestinationFolder variable to match where you have your NT8 indicators.
      • Change the strategySourceFolder variable to match where you have your NT7 strategies
      • Change the strategyDestinationFolder variable to match where you have your NT8 strategies.
    4. Click the green right arrow button to run (or press F5).


    Notes on conversion:
    • UserDefinedMethods.cs will need manual updating. If you don't have any changes to it that you need, you can just delete the NT8 converted version. If you have made changes to it that you need, you'll need to manually move the NT8 converted version into the AddOns folder and make the appropriate changes as stated in the NT8 documentation.
    • The script uses only syntactic analysis - no semantic analysis is done so there are a few things it won't be able to convert and others it may get wrong. It worked for all the stock indicators (with the exceptions listed below) so I didn't feel the need to go through the extra hassle.
    • Statements such as "Plots[0].Pen.DashStyle = DashStyle.Dash;" will convert to "Plots[0].Pen.DashStyle = DashStyleHelper.Dash;". This is one of the cases that would need semantic analysis to fix. Simply remove the "Helper" text.
    • Some functions have changed signatures that will require simple manual fixing after conversion. Mostly the Draw* functions (16 overloads have changed - the rest are fine) are affected. Look at the sample indicator text at the bottom of the script to get an idea of what changes are needed.
    • If the indicator overrides Plot, it will most likely require manual changes. There is no conversion to the DX API. This means classes such as StringFormat and SolidBrush won't convert since they are normally used for painting and the API is significantly different.
    • The indicator/strategy must be the first or only class in the file.
    • If something doesn't convert that you believe should have, there is a list of lookups below the initial setup portion of the script. You can try adding the appropriate lookup values.


    Notes on making script changes:

    If you want to make modifications to the script, you should probably set the "testing" variable to true. This will cause the indicators and strategies to be ignored and instead convert the sample indicator and strategy that are in string variables at the bottom of the script. These samples will allow you to quickly know if your changes have broken anything else. You can modify these samples to include code that allows testing the changes you are making. The output will go into the appropriate NT8 folder so you will need to remove these once you are done. The files will be named "ConversionSample.cs" for both indicator and strategy.

    This was quickly put together to convert my code. I then went back and added the rest of the changes listed in the documentation. If you run into any bugs, feel free to make changes or post here and I will fix it if I have time. If you've made changes, it will probably be a good idea to post here so others may benefit.
    Attached Files
    Last edited by wbennettjr; 10-25-2015, 01:11 PM. Reason: Updated per Taddypole's request

    #2
    Help with Conversion Error Messages

    I followed the procedure to convert some of my indicators but ran into some problems I can't solve. I looked in the mentioned references but could not find any reference to:

    1. StringFormat
    2. SolidBrush

    How do I correct these errors?

    regards,
    taddypole...

    Also in the procedure, "Steps for Conversion" 4 - 7... it took me a bit of time to figure out those steps referred to lines of code in the "NT7toNT8" file loaded into LINQPad5. Maybe a few more words in the procedure to help those like me.
    Attached Files

    Comment


      #3
      Hi Taddypole,

      Thanks for the suggestions. I've updated the procedures accordingly.

      I've also added a note about StringFormat and SolidBrush. These are normally used with the Plot (OnRender) method to paint the graphics of the indicator. The API for doing this has changed significantly in NT8 - they are now using the SharpDX API. As a result, StringFormat is no longer being used - it's in System.Windows.Drawing which is not used in NT8. They have switched, it seems, to a TextLayout class for which there isn't a simple one to one mapping. StringFormat was being used with Graphics.DrawString(). Now there is a RenderTarget.DrawTextLayout() method that is being used in NT8. You can have a look at the @BarTimer.cs file for an example of how it's used (or do a search through the indicators for DrawTextLayout).

      As stated in the post, there is no conversion to the SharpDX API - would be much too complicated and error prone. Plot (OnRender) methods will need to be manually updated to the new API.

      Changes you need to make:

      In your example, you would need to change:
      Code:
      private SolidBrush barBrush = new SolidBrush(Brushes.FromArgb(127, Brushes.Violet));
      To:
      Code:
      private SolidColorBrush barBrush = new SolidColorBrush(Color.FromArgb(127, Brushes.Violet.Color.R, Brushes.Violet.Color.G, Brushes.Violet.Color.B));
      Your StringFormat variable will need to be removed since you'll need to update to use the SharpDX API.

      More on SolidBrush:

      SolidBrush is also a little more complicated to convert. In NT7 (System.Windows.Drawing), it's possible to create these with the following:
      Code:
      var solidBrush1 = new SolidBrush(Color.White);
      var solidBrush2 = new SolidBrush(Color.FromName("ActiveBorder"));
      var solidBrush3 = new SolidBrush(Color.FromKnownColor(KnownColor.ActiveBorder));
      var solidBrush4 = new SolidBrush(Color.FromArgb(1, 1, 1, 1));
      var solidBrush5 = new SolidBrush(Color.FromArgb(1, 1, 1));
      var solidBrush6 = new SolidBrush(Color.FromArgb(1, Color.White));
      With NT8 (System.Windows.Media), the corresponding class, SolidColorBrush, only has the following:
      Code:
      var solidBrush1 = new SolidColorBrush();
      var solidBrush2 = new SolidColorBrush(Color.FromArgb(1, 1, 1, 1));
      var solidBrush3 = new SolidColorBrush(Color.FromRgb(1, 1, 1));
      That being said, if I have time, I'll see if I can add a special case to convert SolidBrush. I think I can get it to translate to a compilable version of SolidColorBrush.
      Last edited by wbennettjr; 10-24-2015, 10:16 AM.

      Comment


        #4
        OnOrderUpdate, OnPositionUpdate

        Hi wbennettjr.
        For Strategy not converted:
        OnOrderUpdate
        OnPositionUpdate

        Comment


          #5
          Joni2 I don't think any changes need to be made see ninjatrader.com/support/helpGuides/nt8/en-us/
          for additional confirmation in the ninja script section

          Comment


            #6
            I've uploaded a new version that converts SolidBrush.

            Comment


              #7
              Thanks Joni! I've added those (and OnExecution) to the _renamedVirtualMethods lookup and uploaded the new file.

              Comment


                #8
                Originally posted by wbennettjr View Post
                Thanks Joni! I've added those (and OnExecution) to the _renamedVirtualMethods lookup and uploaded the new file.
                Where is the new file.

                Comment


                  #9
                  Originally posted by wbennettjr View Post
                  Attached is a C# script to convert NT7 indicators and strategies to NT8. It should convert most indicators without them needing any modifications. See below for limitations.

                  The start of the script looks as follows:
                  Code:
                  bool testing = false; // "true" to convert test scripts when making changes.
                  bool convertIndicators = true; // "true" to convert indicators.
                  bool convertStrategies = true; // "true" to convert strategies.
                  string indicatorSourceFolder = @"C:\Users\UserName\Documents\NinjaTrader 7\bin\Custom\Indicator";
                  string indicatorDestinationFolder = @"C:\Users\UserName\Documents\NinjaTrader 8\bin\Custom\Indicators";
                  string indicatorFilesToConvert = "*.cs";
                  
                  string strategySourceFolder = @"C:\Users\UserName\Documents\NinjaTrader 7\bin\Custom\Strategy";
                  string strategyDestinationFolder = @"C:\Users\UserName\Documents\NinjaTrader 8\bin\Custom\Strategies";
                  string strategyFilesToConvert = "*.cs";
                  Steps for conversion:
                  1. Backup your NT7 indicators and scripts in case you enter the wrong folders below.
                  2. Download LINQPad 5 at www.linqpad.net. It's a free C# (and VB, F#, SQL) script runner - if you don't need intellisense. The script is a text file with full source (using .Net 4.6). If you don't want to use LINQPad, you can put the code in a console application (or WPF or whatever) instead - it needs the Microsoft code analysis Nuget package.
                  3. Download the attached script, remove the ".txt" extension, open in LINQPad and make the following changes:
                    • Change the indicatorSourceFolder variable to match where you have your NT7 indicators
                    • Change the indicatorDestinationFolder variable to match where you have your NT8 indicators.
                    • Change the strategySourceFolder variable to match where you have your NT7 strategies
                    • Change the strategyDestinationFolder variable to match where you have your NT8 strategies.
                  4. Click the green right arrow button to run (or press F5).


                  Notes on conversion:
                  • UserDefinedMethods.cs will need manual updating. If you don't have any changes to it that you need, you can just delete the NT8 converted version. If you have made changes to it that you need, you'll need to manually move the NT8 converted version into the AddOns folder and make the appropriate changes as stated in the NT8 documentation.
                  • The script uses only syntactic analysis - no semantic analysis is done so there are a few things it won't be able to convert and others it may get wrong. It worked for all the stock indicators (with the exceptions listed below) so I didn't feel the need to go through the extra hassle.
                  • Statements such as "Plots[0].Pen.DashStyle = DashStyle.Dash;" will convert to "Plots[0].Pen.DashStyle = DashStyleHelper.Dash;". This is one of the cases that would need semantic analysis to fix. Simply remove the "Helper" text.
                  • Some functions have changed signatures that will require simple manual fixing after conversion. Mostly the Draw* functions (16 overloads have changed - the rest are fine) are affected. Look at the sample indicator text at the bottom of the script to get an idea of what changes are needed.
                  • If the indicator overrides Plot, it will most likely require manual changes. There is no conversion to the DX API. This means classes such as StringFormat and SolidBrush won't convert since they are normally used for painting and the API is significantly different.
                  • The indicator/strategy must be the first or only class in the file.
                  • If something doesn't convert that you believe should have, there is a list of lookups below the initial setup portion of the script. You can try adding the appropriate lookup values.


                  Notes on making script changes:

                  If you want to make modifications to the script, you should probably set the "testing" variable to true. This will cause the indicators and strategies to be ignored and instead convert the sample indicator and strategy that are in string variables at the bottom of the script. These samples will allow you to quickly know if your changes have broken anything else. You can modify these samples to include code that allows testing the changes you are making. The output will go into the appropriate NT8 folder so you will need to remove these once you are done. The files will be named "ConversionSample.cs" for both indicator and strategy.

                  This was quickly put together to convert my code. I then went back and added the rest of the changes listed in the documentation. If you run into any bugs, feel free to make changes or post here and I will fix it if I have time. If you've made changes, it will probably be a good idea to post here so others may benefit.

                  Getting the error as attached.
                  Last edited by skann29; 10-25-2015, 11:58 AM.

                  Comment


                    #10
                    Originally posted by skann29 View Post
                    Getting the error as attached.
                    This is the error

                    "InvalidCastException: Unable to cast object of type 'Microsoft.CodeAnalysis.CSharp.Syntax.QualifiedNam eSyntax' to type 'Microsoft.CodeAnalysis.CSharp.Syntax.SimpleNameSy ntax'."

                    Comment


                      #11
                      Sorry to hear that Skann .

                      I need a bit more context to diagnose. I've uploaded a new version of the script that adds an exception handler so I can get a stacktrace. Please download, run and send me the output so I can fix.

                      Thanks!

                      Comment


                        #12
                        I replaced the original.

                        Comment


                          #13
                          Hello, thank wbennettjr for the script, though some files falls out with errors, it would be nice this thing to visualize, as a variant of working not with the file once and for example by line, made a small workpiece in windose forms, open and save files one by one, can someone would be so convenient.
                          Excuse my bad English.

                          Reference to the file and source code

                          Last edited by Gellyus; 10-28-2015, 08:52 AM.

                          Comment


                            #14
                            Thanks Gellyus!

                            If I understand correctly, you are asking for a GUI version? That should be simple enough to do but unless this is requested by a lot of people, I'm not going to devote the time to it. I thought about it before but since this is going to be a one time thing for most people, I did not think it was worth it. Also, having the code and LINQPad allows people to make changes and test really quickly if they like.

                            Comment


                              #15
                              Once again, I apologize for my bad english.
                              No, not really true, it would be great to add the name of the return type, or function, it is in the converted text file in which the script falls. The rest will be a lot easier.
                              Just programming my hobby, I'm not a professional.
                              Once again thank you for your work!

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by PaulMohn, Today, 12:36 PM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by yertle, Yesterday, 08:38 AM
                              8 responses
                              36 views
                              0 likes
                              Last Post ryjoga
                              by ryjoga
                               
                              Started by rdtdale, Today, 01:02 PM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Started by alifarahani, Today, 09:40 AM
                              3 responses
                              16 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by RookieTrader, Today, 09:37 AM
                              4 responses
                              19 views
                              0 likes
                              Last Post RookieTrader  
                              Working...
                              X