How to count multiple conditions in a system?
Author: mikesblack
Creation Date: 4/7/2016 7:38 PM
profile picture

mikesblack

#1
I'd like to create a system that references specific conditions and adds a value, e.g. a 1 for each time such a condition is true. For example, say there are 5 conditions, price is greater than 200 day m.a., the rsi is lower than some value, the price closes three consecutive days, the day is last trading day of the month and the day is Tuesday. The value would be 5 if all conditions are true. I would buy if this value is greater than or equal to 3.

How can I write code that will do this?

Thank you..
profile picture

Eugene

#2
Answered here (for both code- and rule-based strategies):

Multiple indicator confirmation

P.S. For more on mentioned Multi-Condition Groups in Rule-based strategies see:

Using Multi-Condition Group for consecutive condition "Volume above/below SMA"

MC Groups come in especially handy when you don't require all the 5 conditions to be met precisely on the same bar:

Condition true today and yesterday
Rules: Condition happened X bar ago / Condition Active X bars in a row
profile picture

mikesblack

#3
Thank you.
profile picture

mikesblack

#4
I would like to use triggers as per the example you showed. Below is the code I have that I wish to make changes on. How will I get from here to a program that adds up the triggers that will then become the buy rule. E.g. if(trigger value > 3) BuyAtMarket; Right now, I'll plan on using an exit based on the code as is. I just need a starter for coding the buy rules for this system. Thank you in advance.

CODE:
Please log in to see this code.
profile picture

Cone

#5
Based on the setup requirements in the first post and the script above, here's your solution.

CODE:
Please log in to see this code.
profile picture

Eugene

#6
Since the strategy clearly comes from the Rules Wizard, I wonder if it would be natural to simply adjust Required Conditions to a desired number and limit the Lookback Period to 1?
profile picture

mikesblack

#7
Thank you Robert. Eugene, I'm not sure I understand what you mean at this time. At the end of the day, I wish to do something similar to what Robert has provided, except I would change the weights and the conditions accordingly.
profile picture

Eugene

#8
Perhaps I must have overlooked the weights.
profile picture

mikesblack

#9
Robert. Can you explain cntConditions++ and bcd += 1-> 1000000? I am trying to understand your methodology. Thanks.
profile picture

Eugene

#10
Increment operators are C# 101.

http://www.dotnetperls.com/increment

By the way this resource is on our FAQ.
profile picture

mikesblack

#11
I understand that, but why are the bcds incremented by a factor of 10? The cntConditions seems most obvious in terms of adding up to a value that would make the trade, but bcd is confusing me.
profile picture

Eugene

#12
QUOTE:
why are the bcds incremented by a factor of 10?

Why not? You can assign different weights to the "bcd". For example, the DayOfWeek in Cone's code is the strongest signal.
profile picture

mikesblack

#13
I'm not sure why there are both cntConditions++ and bcd += . Please clarify how both of these work together?
profile picture

Eugene

#14
"Not sure" is vague. And you already know that the "bcd" is weight - a double value. Assuming that you can experiment with the code, evaluating PrintDebug() output and assigning different weights while keeping a C# 101 book open as reference, I wonder what do you think requires clarification exactly?
profile picture

mikesblack

#15
So one is a counter and the other assigns weight. When I write if( cntConditions > = 4) Buy at Market. it seems only the cntConditions is applicable. What is happening with bcd as it relates to the buy signal?
profile picture

Eugene

#16
Whatever you code. It's up to you.
profile picture

Cone

#17
Michael, the following comment is in the script explaining what bcd is:
// bcd is the "code" of the condition that is true.

Using this, you can see which conditions triggered the entry. It's informational only.

For example: 01101
The unit position "1" corresponds to (Close[bar] > ma[bar])
The hundreds position "100" corresponds to (CumDown.Value(bar, Close, 1) >= 3)
The thousands position "1000" corresponds to (Date[bar].GetRemainingTradingDays() == 0)


profile picture

mikesblack

#18
Ahh I understand now. Thanks Robert!
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).