Download software Tutorial videos
Subscription & data-feed pricing Class schedule


New account application Trading resources
Margin rates Stock & option commissions

Attention: Discussion forums are read-only for extended maintenance until further notice.
Welcome Guest, please sign in to participate in a discussion. Search | Active Topics |

How to create a boolen expression with memory Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
sttstt
Posted : Saturday, April 22, 2006 11:21:58 AM
Registered User
Joined: 10/7/2004
Posts: 39
Hi,
I am looking for a way to set and reset an indicator with two separate conditions. For example,
if a>0 and b>0 and c>0, set an indicator to True and it will stay True until
a<0 and b<0 and c<0, then it is reset to False and stay False until the True condition is met.
In essence, a set/reset flip-flip.

The closest function I can find on TC is the cumulative indicator. But its value will change (increment if the set condition stays true, decrement if the reset condition is true, unchanged if neither), making it visually more difficult to spot the change in condition. Any clear ways to achieve what I want to do?

Thanks.
diceman
Posted : Saturday, April 22, 2006 11:36:51 AM
Registered User
Joined: 1/28/2005
Posts: 6,049
sttstt

The simplest solution could be to use a percent true indicator for the true condition and a second percent true indicator for the false condition.

You could make true "green" and false "red" put them both in the same window.

It will change red or green depending on the conditions.

Hope that helps
Good Luck



jynkin
Posted : Saturday, April 22, 2006 11:47:28 AM
Registered User
Joined: 10/7/2004
Posts: 286

Here's a methadolgy you might pursue that can open up new true/false possibilities for switchover situations, though I'm not sure it can do all that you're looking for.

When a boolean expression is True, the mathematical result is -1. So if you have three boolean expressions that you want all true or false, put them in parenthesis and require them to equal -3. Such as: ((a>0) + (b>0) + (c>0)) = -3 You'll get a True only when all three are true. If you made it ((a>0) + (b>0) + (c>0)) < 0, then it would still result in a True when only 1 or 2 of the three was true.

jynkin
Craig_S
Posted : Saturday, April 22, 2006 12:04:04 PM


Worden Trainer

Joined: 10/1/2004
Posts: 18,819
I like diceman's idea. Check out this video to get an idea of how it will work:

Visually Backtesting Specific Symbols

You could use the custom cumulative. When it goes up, it is true. When it goes down, it is false. When it moves sideways neither is the case.

- Craig
Here to Help!
bustermu
Posted : Saturday, April 22, 2006 3:44:44 PM
Registered User
Joined: 1/1/2005
Posts: 2,645
sttstt,

Yes. We can write a PCF to fulfill your needs. The only problem is that its length may have to be adjusted to fit the particular WatchList on which you wish to use it. That can be a minor problem.

Suppose we have two conditions P and Q. We can construct a PCF whose output is:

a) +1 if P was most recently True,
b) -1 if Q was most recently True,
c) 0 if it is not long enough to determine whether P or Q was most recently True,
d) nothing if it is too long to be calculated.

If you wish to control the state of some two state indicator, such a PCF is necessary. If you wish to mark the occurrences of P and Q, diceman's suggestion is much simpler.

Thanks,
Jim Murphy
sttstt
Posted : Saturday, April 22, 2006 8:39:35 PM
Registered User
Joined: 10/7/2004
Posts: 39
Hi guys,

I appreciate all the inputs. I think most of you missed that what I am looking for is not just the "combinatinal" boolen expression, that's the easy part and the percent true takes care of that. What is missing in the current built-in function is a "memory" or "state", i.e. once a condition sets it to True, it will stay True state until a reset condition, which can be totally independent of the set condition, resets it to False, and stay in False state until it is set by set condition to turn into the True state again.

As to creating the two percent true graphs, I already did that. As you might know the problem is that because the set and reset conditions are not compelementary, therefore, the combo graphs still require manual interpretation as to when is the real set state and reset state. This requires looking at the set and reset conditions in addtition to the combo graphs and there it's only an incremental help.

Regards.
sttstt
Posted : Saturday, April 22, 2006 8:52:22 PM
Registered User
Joined: 10/7/2004
Posts: 39
I retract the words "most of you missed" as I reread the comments. However, these are still true:
1. the percent true combo graph is not the solution.
2. cumulative function is usable but I have to spot when it changes direction, when used with the combo graph, it is marginally useful.
3. a state function would be useful for this application.

Regards.
Craig_S
Posted : Saturday, April 22, 2006 9:08:00 PM


Worden Trainer

Joined: 10/1/2004
Posts: 18,819
I understood what you were asking for. I believe jynkin, bustermu and diceman also understood 100%. We are all trying to offer something to get close to what you are looking for.

I can create an indicator/PCF that will do what you want BUT it must be limited by a certain time frame. Give me the true and false trigger conditions and let me write up a version of what I mean. (this is the bustermu idea).

What you want is not exactly available but we might be able to do a great job faking it.

There is no lack of understanding of your request.

- Craig
Here to Help!
sttstt
Posted : Thursday, May 4, 2006 3:09:46 AM
Registered User
Joined: 10/7/2004
Posts: 39
Hi Craig,

Sorry for the belated reply. I forgot about the post.
Here is the specification:
set %true to true if c>c30 and c>c65 and c>avgc50
and stay true until
set %true to false if c<c30 and c<c65 and c<avgc50
and stay false until the first condition set it to true again.

Thanks.
Craig_S
Posted : Thursday, May 4, 2006 6:40:26 AM


Worden Trainer

Joined: 10/1/2004
Posts: 18,819
No problem!

I still need a sense of time frame from you. What is your look-back time in days?

- Craig
Here to Help!
bustermu
Posted : Thursday, May 4, 2006 7:57:37 AM
Registered User
Joined: 1/1/2005
Posts: 2,645
QUOTE (Craig_S)
No problem!

I still need a sense of time frame from you. What is your look-back time in days?


Craig,

Suppose you were told that the "look-back time", whatever that is, was 2 days or 200 days. What would be the difference in your PCF?

I do not see how that would effect the PCF.

It seems to me that for a particular WatchList and observation interval, the PCF has to be long enough to determine the present state and not so long as to not calculate.

Thanks,
Jim Murphy
Craig_S
Posted : Thursday, May 4, 2006 8:00:21 AM


Worden Trainer

Joined: 10/1/2004
Posts: 18,819
It would be a huge difference in the length of the PCF. I don't want to create a functional (yet very long and involved) PCF that falls way short of his look back expectations (thus is not used). I am not sure he understands the time-frame limitation.

- Craig
Here to Help!
bustermu
Posted : Thursday, May 4, 2006 9:13:41 AM
Registered User
Joined: 1/1/2005
Posts: 2,645
QUOTE (Craig_S)
It would be a huge difference in the length of the PCF.


Craig,

The PCF would have to be long enough to determine the state. That is all.

Probably the problem concerns what is meant by "look-back period".

You may be asking the user to tell you how long the PCF should look into the past without accepting a failure? If so, then his response should probably be that he does not want it to ever fail. That problem can be addressed.

The length of a PCF to plot the state for the last 200 days is not necessarily any longer than the minimum length of one required to determine the state today only. Of course, it is never any shorter.

Thanks,
Jim Murphy
Craig_S
Posted : Thursday, May 4, 2006 9:16:34 AM


Worden Trainer

Joined: 10/1/2004
Posts: 18,819
I don't think I understand the method you are thinking of. Can you post your solution for his above request so I can learn from it?

- Craig
Here to Help!
bustermu
Posted : Thursday, May 4, 2006 10:27:39 AM
Registered User
Joined: 1/1/2005
Posts: 2,645
QUOTE (Craig_S)
I don't think I understand the method you are thinking of. Can you post your solution for his above request so I can learn from it?


Craig,

I have not written a PCF for the problem.

For simplicity, let us say that the two states are determined by C up 1% and by C down 1% from C1. The PCF only has to be long enough to determine whether the last occurence of either event was up or down. Correct?

My question is the "look-back time" the number of days the PCF must use or is it the number of days desired to observe a plot of the PCF or something else?

Thanks,
Jim Murphy
Craig_S
Posted : Thursday, May 4, 2006 10:29:32 AM


Worden Trainer

Joined: 10/1/2004
Posts: 18,819
QUOTE (bustermu)
The PCF only has to be long enough to determine whether the last occurence of either event was up or down.


Is this not a finite period contraint or does your method look back forever?

- Craig
Here to Help!
bustermu
Posted : Thursday, May 4, 2006 11:03:18 AM
Registered User
Joined: 1/1/2005
Posts: 2,645
QUOTE (Craig_S)
QUOTE (bustermu)
The PCF only has to be long enough to determine whether the last occurence of either event was up or down.


Is this not a finite period contraint or does your method look back forever?


Craig,

What do you mean "look-back period"?

I do not see why a method for writing a state PCF is relevant.

No PCF can cover an infinite number of days. Any state PCF has to be adjusted for the WatchList and time interval for which you wish to use it. A user likely cannot supply you that information. You have to supply him the instructions and PCF's necessary to determine the number of days the state PCF must use.

Thanks,
Jim Murphy
Craig_S
Posted : Thursday, May 4, 2006 11:38:55 AM


Worden Trainer

Joined: 10/1/2004
Posts: 18,819
QUOTE
You have to supply him the instructions and PCF's necessary to determine the number of days the state PCF must use.

Your order (or assignment) is noted.

I still have no idea if we are plannning on using the same method to solve this request. You are welcome to post your solution. Otherwise, please allow me to handle this as I see fit. You are welcome to critique my method once I finish.

- Craig
Here to Help!
bustermu
Posted : Thursday, May 4, 2006 12:18:30 PM
Registered User
Joined: 1/1/2005
Posts: 2,645
QUOTE (Craig_S)
QUOTE
You have to supply him the instructions and PCF's necessary to determine the number of days the state PCF must use.

Your order (or assignment) is noted.


Craig,

I apologize that my statement seemed to be giving an order or assingnment.

It was intended to merely convey that if a user knew how many days a state PCF must use, he would likely already know how to write the PCF. I would not be able to tell you that in a reasonable amount of time without doing essentially the equivalent of writing the state PCF.

By the way, what do you mean by "look-back time"?

Sorry,
Jim Murphy
Craig_S
Posted : Thursday, May 4, 2006 12:34:49 PM


Worden Trainer

Joined: 10/1/2004
Posts: 18,819
I mean how far back the PCF should look to figure out if there was a previous event ("c>c30 and c>c65 and c>avgc50" or "c<c30 and c<c65 and c<avgc50")

I still don't understand your proposed method. The length of the WatchList being a factor confuses me.

- Craig
Here to Help!
bustermu
Posted : Thursday, May 4, 2006 2:13:59 PM
Registered User
Joined: 1/1/2005
Posts: 2,645
QUOTE (Craig_S)
I mean how far back the PCF should look to figure out if there was a previous event ("c>c30 and c>c65 and c>avgc50" or "c<c30 and c<c65 and c<avgc50")


Thanks. Now we know.

The PCF should look far enough back to determine the last occurence of "c>c30 and c>c65 and c>avgc50" or "c<c30 and c<c65 and c<avgc50" for the WatchList and time interval of interest and not so far as to preclude computation because of lack of sufficient data. The WatchList may have to be partitioned in order to accomplish this.

It is not the case that a state PCF using a fixed number of bars will work on every WatchList.

Thanks,
Jim Murphy
Craig_S
Posted : Thursday, May 4, 2006 2:46:11 PM


Worden Trainer

Joined: 10/1/2004
Posts: 18,819
I need to know, from sttstt what he want as the "time interval of interest" (as you call it).

- Craig
Here to Help!
bustermu
Posted : Thursday, May 4, 2006 5:08:55 PM
Registered User
Joined: 1/1/2005
Posts: 2,645
QUOTE (Craig_S)
I need to know, from sttstt what he want as the &quot;time interval of interest&quot; (as you call it).


Craig,

The "time interval of interest" to which I refer is the number of days of the observation interval for the members of the WatchList. It is only indirectly related to the required number of days used for computation in a state PCF.

For example. suppose the WatchList is the Dow Jones 30 Industrial Component Stocks and the observation interval is that of Zoom 9. In that instance, it may be that a state PCF requires computation on 60 days to determine the last state switching condition. If the observation interval is that of Zoom 6, it may require computation on 70 days.

The minimum length of a state PCF is determined by the maximum distance between successive occurrences of state switching conditions, which, in turn, depends on the WatchList and "the interval of observation". In general, if a user can write a PCF to determine that distance, he can also write a state PCF.

Thanks,
Jim Murphy
sttstt
Posted : Thursday, May 4, 2006 5:20:48 PM
Registered User
Joined: 10/7/2004
Posts: 39
Looks like that I may have inadvertently created a problem for Craig by using large numbers like 65, 50, and 30. What I intended to do is a simple rate of change where each bar is a daily. I agree with Jim that the interval should not matter. It is just a condition that sets or resets a state. Once a state is set, it stays set indefinitely, until it is reset by the reset condition. Therefore, I don't see why the lookback interval matters.
Unless you plan to somehow create a sequential state machine state with combinatorial logic expressions.

So let's simplify the conditions:
set if c > avgc14 and rsi21 > 50 and tsv16 >50
reset if c < avgc14 and rsi21 <50 and tsv16 <50

or even simpler
set if c>c1 and c1>c2 and c2>c3
reset if c<c1 and c1<c2 and c2<c3
bustermu
Posted : Thursday, May 4, 2006 6:08:59 PM
Registered User
Joined: 1/1/2005
Posts: 2,645
QUOTE (sttstt)
I agree with Jim that the interval should not matter. It is just a condition that sets or resets a state. Once a state is set, it stays set indefinitely, until it is reset by the reset condition. Therefore, I don't see why the lookback interval matters.


sttstt,

Jim did not say that at all. Whatever "interval" you are referring to matters. There are no loops, user defined variables, and no flags that can be set in the PCF Language. A state PCF has to be calculated on a sufficient number of days that it can determine the last state switching condition that occurred. If it is calculated on too many days it may run out of data and fail to calculate on some Symbols. It is no big deal to create a state PCF, but its length may have to be adjusted for the WatchList and observation interval for which you wish to use it, and that is also no big deal.

Thanks,
Jim Murphy
sttstt
Posted : Friday, May 5, 2006 2:48:24 AM
Registered User
Joined: 10/7/2004
Posts: 39
After re-read the postings, here is what I like to see:
1. the pcf should be as short as possible. A long pcf slows down the computation to an unacceptable crawl.
2. I don't really care about the previous event or state prior to what is displayed on the chart as long as a triggering events get shown as transitions and are visible on the chart. So you can have three states, -1, 0, and +1. 0 being the unkown state so that you don't need to look back, to make it simpler. Then if a triggering event takes place, it will change the state to either -1 or +1. From there on the state is deterministic.

Regards.
Craig_S
Posted : Friday, May 5, 2006 6:43:38 AM


Worden Trainer

Joined: 10/1/2004
Posts: 18,819
QUOTE

true if c>c30 and c>c65 and c>avgc50
false if c<c30 and c<c65 and c<avgc50


Plot this as a CUSTOM INDICATOR with the CENTER ZERO LINE optino checked.


ABS(C>C30 AND C>C65 AND C>AVGC50)+(C<C30 AND C<C65 AND C<AVGC50)

- Craig
Here to Help!
bustermu
Posted : Friday, May 5, 2006 7:34:24 AM
Registered User
Joined: 1/1/2005
Posts: 2,645
QUOTE (sttstt)
After re-read the postings, here is what I like to see:
1. the pcf should be as short as possible. A long pcf slows down the computation to an unacceptable crawl.
2. I don't really care about the previous event or state prior to what is displayed on the chart as long as a triggering events get shown as transitions and are visible on the chart. So you can have three states, -1, 0, and +1. 0 being the unkown state so that you don't need to look back, to make it simpler. Then if a triggering event takes place, it will change the state to either -1 or +1. From there on the state is deterministic.

Regards.


If you require that the conditions in 2. be satisfied, the State PCF is very long and much more difficult to plot. It requires a different PCF for each symbol in the WatchList. Please don't do that to us!

Thanks,
Jim Murphy
bustermu
Posted : Friday, May 5, 2006 8:14:20 AM
Registered User
Joined: 1/1/2005
Posts: 2,645
It may be helpful to give a simple example of a State PCF. Let the conditions for determining the two states be a greater than 1% change in the closing price. The State PCF is to return +1 if the last greater than 1% change was up and -1 if it was down. The construction of the PCF's is not described here.

Define the PCF's:

UpPCF:
(-1)
*((C0/C1>1.01)+(C0/C1<=1.01)*(C0/C1>=0.99)
*((C1/C2>1.01)+(C1/C2<=1.01)*(C1/C2>=0.99)
*((C2/C3>1.01)+(C2/C3<=1.01)*(C2/C3>=0.99)
*((C3/C4>1.01)+(C3/C4<=1.01)*(C3/C4>=0.99)
*((C4/C5>1.01)+(C4/C5<=1.01)*(C4/C5>=0.99)
*((C5/C6>1.01)+(C5/C6<=1.01)*(C5/C6>=0.99)
*((C6/C7>1.01)+(C6/C7<=1.01)*(C6/C7>=0.99)
*((C7/C8>1.01)+(C7/C8<=1.01)*(C7/C8>=0.99)
*((C8/C9>1.01)
)))))))))

DnPCF:
(-1)
*((C0/C1<0.99)+(C0/C1>=0.99)*(C0/C1<=1.01)
*((C1/C2<0.99)+(C1/C2>=0.99)*(C1/C2<=1.01)
*((C2/C3<0.99)+(C2/C3>=0.99)*(C2/C3<=1.01)
*((C3/C4<0.99)+(C3/C4>=0.99)*(C3/C4<=1.01)
*((C4/C5<0.99)+(C4/C5>=0.99)*(C4/C5<=1.01)
*((C5/C6<0.99)+(C5/C6>=0.99)*(C5/C6<=1.01)
*((C6/C7<0.99)+(C6/C7>=0.99)*(C6/C7<=1.01)
*((C7/C8<0.99)+(C7/C8>=0.99)*(C7/C8<=1.01)
*((C8/C9<0.99)
)))))))))

Under appropriate conditions, UpPCF will return 1 if the last more than 1% change was up and 0 otherwise. Similarly, DnPCF will return 1 if the last more than 1% change was down and 0 otherwise.

Under appropriate conditions, the PCF of the form:

StatePCF:
UpPCF - DnPCF

will return the desired result. If it returns 0, it is likely too short and if it is not calculated, it is too long.

There is a procedure for determining its minimum required length.

Thanks,
Jim Murphy
sttstt
Posted : Saturday, May 6, 2006 12:51:36 PM
Registered User
Joined: 10/7/2004
Posts: 39
[quote=Craig_S]
QUOTE

Plot this as a CUSTOM INDICATOR with the CENTER ZERO LINE optino checked.
ABS(C&gt;C30 AND C&gt;C65 AND C&gt;AVGC50)+(C&lt;C30 AND C&lt;C65 AND C&lt;AVGC50)


I tried this. It has some quirkiness. I think I will draw this discussion to a close. Since all I want is a simple visual to capture the condition. I am really not interested in any convoluted expressions to reconstruct an SR flip-flop when a simple addition of a state construct in the PCF language should have been the solution.

For now I will stick with the "cumulative" construct, which has the concept of state.

Thanks both Craig and Jim for the amont of time you spent on this.
marcj
Posted : Friday, June 1, 2007 2:37:48 PM

Registered User
Joined: 5/31/2006
Posts: 377
here is the BLOCKS code for a simple flip-flop
---------------------------
<WBIGuid("c46da947-89c2-49cc-9951-de263d9e1af4"),FriendlyName("Flip Flop"), _
ClassAuthor("Quick Page", "Line1 set TRUE and Line2 resets it.", "3/31/2007")> _
Public Class Flip_Flop
inherits BaseTemplateDLBAndDLBToDLB
Public Overrides Sub calculate()
'************ Your Code Here ***************
Dim Line1 As Boolean
Dim Line2 As Boolean
Dim OutState As Boolean

OutState = False

For i As Integer = 1 To Me.CodeBlock.InputCount -1
Line1 = Me.CodeBlock.Input1Value(i)
Line2 = Me.CodeBlock.Input2Value(i)

If Line1 = True Then
OutState = True
End If

If Line2 = True Then
OutState = False
End If

Me.CodeBlock.AddToOutput(Me.CodeBlock.InputDate(i), OutState )

Next
End Sub
End Class
Users browsing this topic
Guest-1

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.