Type Converters and NinjaScript
Learn how to create custom Type Converters to be used with NinjaScript UI’s property grid. Notes on using traditional coding statements, ternary operators, and LINQ queries.
You read it right: type converters. That’s where we are going today. Why? Because I like to suffer and because it shouldn’t be too hard to make a property read-only. Right? Well, it shouldn’t have been. Hot take: this would have been easier in JavaScript (TypeScript). But this isn’t a SubStack about web (or web-adjacent) development, is it?
In NinjaScript, we use NinjaScriptProperties
to expose user-definable variables such as stop (SL) and profit (TP) information. What if we have more than one exit method in our strategy, and when we switch it to trail or none, we want to make the SL/TP fields read-only? That’s what we will be discussing today.
This post will cover how to turn the SL/TP profits fields in a NinjaScript strategy to read-only when they aren’t necessary. We will also cover different techniques to make the code more concise and readable (subjective). The code will be broken into parts to make it easier to understand what each function is doing.
Disclaimer: the following post is an organized representation of my research and project notes. It doesn’t represent any type of advice, financial or otherwise. Its purpose is to be informative and educational. Backtest results are based on historical data, not real-time data. There is no guarantee that these hypothetical results will continue in the future. Day trading is extremely risky, and I do not suggest running any of these strategies live.
Code
Creating this Type Converter took a lot more research and testing than I anticipated, but in the process, I learned some new ways to code that I want to share. I also believe I made something that should be easily convertible for future needs. I will admit that I don’t know if there are any merits in getting this detailed with strategy development in the research phase, as it was time-consuming and did nothing to help the strategy itself. It is a nice feature, though, and if you want to share strategies (or sell them if that’s your goal), it becomes more beneficial. The complete code will be available at the end of the article. The code will be added to GitHub once I have finalized January’s strategy. This type converter was made for the upcoming strategy, 9X. Followers of ATS might know that this is one of the Twilight Strategies, a strategy that never worked in the forward test. This strategy drop will bring updates to the repository’s code style and folder structure.