Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatically determine If statement from multiple conditions sele

Status
Not open for further replies.

Marco123

Programmer
Dec 31, 2010
23
GB
Hi All,

I’m trying to code an automated trading system that will perform a trade if certain conditions are met. Assume there are 5 conditions, and the user can select any one, two, three, four or five of the conditions together. This means the user could select any number of conditions, and any possible permutation of conditions of that size. Hence, there are dozens of distinct possible choices.

In my code, each condition is being determined by a function, where the function will return a true or false value. Let’s call them FunctionCondition1, FunctionCondition2, FunctionCondition3….etc.

For example, if the user selects condition 1 and condition 3, the vba would be as follows:

Code:
If FunctionCondition1 = True And FunctionCondition3 = True Then etc

If the user selects conditions 3,4 and 5, the vba would be:

Code:
If FunctionCondition3 = True And FunctionCondition4 = True And FunctionCondition5 Then etc

I’m wondering if it’s possible to code something that will be able to automatically construct or use an If statement (depending on the selected conditions) without having to list all the possible combinations of conditions in If statements. Therefore, at the start of the code it would pick up (using simple Booleans) how many and which conditions have been selected by the user, and from that it would be able to construct (or work out) the appropriate If statement to use.

Any ideas?
 
Store the user selections as bMyCondition1 etc

bTrade=false
If bMyCondition1 then bTrade = bFunctionCondition1
If bMyCondition2 then bTrade = bTrade and bFunctionCondition2
If bMyCondition3 then bTrade = bTrade and bFunctionCondition3

If bTrade then 'perform a trade

Gavin
 
Thanks for your answer. I tested it and it is actually nearly correct except for one tiny thing. If the btrade boolean is initially set to false, then it will always remain false unless the bFunctionCondition1 is selected, and is true. This is because if the btrade is initially set to false, it will remain false when used in any statement that contains btrade on the other side of the equals sign, irrespective of what the function returns. E.g.

If bMyCondition2 then bTrade = bTrade and bFunctionCondition2

Here bTrade will remain false even if bFunctionCondition2 is true.

In order to get round this problem, I simply set the btrade to true at the beginning. I’m surprised how the solution for this is so simple.
 
Glad to have helped. I should have said it was untested! Not sure if it matters but if all bMyConditions are False then you will perform a trade. You need an OR to overcome that.

Gavin
 
Yeah that’s true, if my model went into a live trading environment having no conditions selected would be hazardous! Think I’ll put in some clause to give a warning if no conditions are selected.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top