Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
in order to test various trendfilters within my strategy (condition1 && condition2 && ...), I would like to be able to activate/deactivate them in the strategy menu (without having to change the code everytime).
So Iīm looking for the best way to integrate a bool for each one to switch it on and off.
Can anyone help me please?
Thank you,
seeker
Can you help answer these questions from other members on NexusFi?
I will try this.
Btw. I forgot to mention, the goal is not only to activate the several filters separately, but also in combination (for instance 2 + 3 + 5 are working - whatever I select in the menu).
That really does add complexity, I don't think I realized the scope of your plan with my first post.
First if you plan to use the optimizer then use 0/1 instead of true/false since the optimizer does not work on booleans. Second I envision a structure like this:
Parameter screen:
Filter1 = Input 0 or 1 here
Filter2 = Input 0 or 1 here
Filter3 = Input 0 or 1 here
Now in the actual code each condition&& will include an ||. In case you did not know || = OR. Below is pseudo-code with an example.
Example: You want to test with filters 1 & 3 active. Ignoring filter 2.
Parameter Input
Filter1 = 1
Filter2 = 0
Filter3 = 1
Basically, when evaluating, either the filter condition has to evaluate as true OR the filter has to be turned off to continue down the if statement. The example above would enter the trade because filter2, although in a no trade state, was turned off.
When I want complex rules to be parameterized, I use string matching, and I can change the rule by changing the regular expression in a string parameter. You can apply very different and complex rules without changing the code:
string chosen_filters = "(Trending.*Reversal)"; //### default to Bar Reversal in a trend
void OnBarUpdate () {
string marketState="";
if ( ...Trending ...) marketState += "Trending ";
if ( ...MACD condition ...) marketState += "MACD ";
if ( ...RSI condition ...) marketState += "RSI ";
if ( ...Bar reversal ...) marketState += "Reversal ";
if ( ...Other condition ...) marketState += "Other ";
if ( Regex.Match(marketState, chosen_filters).Success {
//### Take the trade
}
}
Now I can go into the Indicator parameters window and change chosen_filters to:
chosen_filters: "(RSI|Reversal)" ---> Bar Revesal or RSI condition met
chosen_filters: "(Trending.*MACD.*Reversal)" ---> Trending and MACD condition and Bar reversal
chosen_filters: "RSI.*Reversal" ---> RSI condition with a Bar reversal
chosen_filters: "Reversal" ---> Just Bar reversals
chosen_filters: "Trending.*RSI.*Reversal" ---> Trending RSI Reversals
chosen_filters: "Trending RSI Reversal" ---> Trending RSI Reversal ONLY, but not if MACD condition is met
or any complex condition involving unlimited AND's, OR's etc, limited only by your proficiency with regular expressions.
looks very plausible.
However, since Iīve just started learning ninjascript, I donīt know how to use 1/0 instead of a bool (true/false) to get a switch within the menu. Could you please explain it with a sample?
... and monpere,
looks very interesting, although Iīm not quite sure if I will get baked this. Iīm afraid I will have to learn a bit before ...
Those variables that you had set as booleans, just need to be set as integers (int).
In the "Variables" region:
Way down at the bottom, there is a "Properties" region that you will have to expand:
You will want a section like that for each of the filter variables. Do make note that the Public variable starts with a capital letter, and the others do not, including the private one that was initialized in the "Variable" region.
There were already few valid examples on how to achieve this.
Attached is sample strategy using ternary operators for filtering between conditions, e.g.:
Together with conditionCounter (which you could achieve simpler than I did by using reflection and simply add all values of useCondXX) you are ready to use it also for optimization and let NT show you which combination of conditions is beneficial for your strategy