logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
Systematic  
#1 Posted : Monday, November 8, 2010 10:49:41 AM(UTC)
Systematic

Rank: Member

Groups: Registered, Registered Users, Subscribers
Joined: 9/15/2005(UTC)
Posts: 24

How does one emulate various "states" using MetaStock formula language without running into circular references? For example, I want to create custom indicator with variable called "state" that has the following properties: state = 0 initially state transitions from 0 to 1 when variable "buy1" is TRUE and retains that value until affected by other variables, as noted below state transitions from 1 to 2 when variable "buy2" is TRUE and retains that value until affected by other variables state transitions from either 1 or 2 to 0 when variable "sell" is TRUE and stay at zero until affect by other variables state transitions from 0 to -1 when variable "short" is TRUE and retains that value...(as discussed above) state transitions from -1 to -2 when variable "short2" is TRUE... state transitions from either -1 or -2 to 0 when variable "cover" is TRUE... A key element here is the ability to define the "state" variable in terms of both other variables and prior values of itself. Can someone provide some simple code for the "state" variable as defined above that does NOT make use of PREV? All input welcome! Thanks, Systematic
henry1224  
#2 Posted : Monday, November 8, 2010 11:50:54 AM(UTC)
henry1224

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 10/29/2004(UTC)
Posts: 1,394
Location: Glastonbury, CT

Was thanked: 2 time(s) in 2 post(s)
State:=If(buy2,2,If(Sell2,-2,If(Buy1,1,If(Sell1,-1,0))));
Systematic  
#3 Posted : Monday, November 8, 2010 1:52:24 PM(UTC)
Systematic

Rank: Member

Groups: Registered, Registered Users, Subscribers
Joined: 9/15/2005(UTC)
Posts: 24

Thanks for your reply. Unfortunately, this code does not meet the requirement that "state" retain its value after being triggered to change by the values of the other variables, such as "buy", and those trigger variables revert to FALSE. For example, in your formula, when buy=TRUE, state=1 but then it immediately reverts to state=0 after the buy signal goes away instead of staying state=1 until some other input affects it. Ditto for the other inputs. What I need is a way for state to change its value as the result of various trigger variables but it must continue to maintain that new value until there is another trigger. And the new value of state when a trigger occurs also depends upon the old value of state. Here's a more detailed example: state=0 initially. At some point buy=TRUE causing state=1 Later buy=FALSE but state=1 is maintained state=1 is maintained until either buy2=TRUE or sell=TRUE. In the first case, state=2 becomes the new value and stays there until sell=TRUE. In either case (i.e., state=1 or state=2), when sell=TRUE state reverts to 0 as stays there until some other trigger occurs. I hope I am communicating this clearly! Any other suggestions? Thanks, Systematic
henry1224  
#4 Posted : Monday, November 8, 2010 4:55:02 PM(UTC)
henry1224

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 10/29/2004(UTC)
Posts: 1,394
Location: Glastonbury, CT

Was thanked: 2 time(s) in 2 post(s)
without your code,it is hard to design a latch system

You could try the forum latch using the forum dll.

look into the files section for the forum dll

as for my suggestion

State:=If(buy2,2,If(Sell2,-2,If(Buy1,1,If(Sell1,-1,0))));

it all depends on how you code the buy and sell conditions as to when the state retains a signal or not IE: cross(A,B) will only give a +1 on the crossing but A>B will continue to plot +1 as long as A is greater than B
Systematic  
#5 Posted : Tuesday, November 9, 2010 8:43:06 PM(UTC)
Systematic

Rank: Member

Groups: Registered, Registered Users, Subscribers
Joined: 9/15/2005(UTC)
Posts: 24

OK. Let's start with a simpler example. Initially, state=0. We want state=0 to change to state=1 when buy=TRUE. Suppose that buy := Cross(Mov(C,n1,S),Mov(C,n2,S) for some specified n1 ands n2. Thereafter, state=1 until the following condition occurs: sell := Cross(Mov(C,n2,S), Mov(C,n1,S) Then state=1 changes to state=0. No other conditions can cause state=1 to change to state=0 or any other value. With state=0 again, another buy signal will cause it to change to state=1. Alternatively, state=0 can change to state=-1 if the following condition occurs: short := Cross(Mov(C,n3,S), Mov(C,n4,S) for some specified n3 and n4. Thereafter, state=-1 until the following condition occurs: cover := Cross(Mov(C,n4,S), Mov(C,n3,S) Then state=-1 changes to state=0. No other conditions can cause state=-1 to change to state=0 or any other value. The objective of the code is to ensure that state always has the proper value in accordance with these rules with all possible combinations of the inputs buy, sell, short and cover. We then display a chart of state in an inner window below the underlying security. The important point is that the effect upon state of any input depends upon state's current value. How can I most efficiently code this example, preferably without using PREV? Thanks for your assistance. Systematic
wabbit  
#6 Posted : Tuesday, November 9, 2010 10:53:25 PM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
Although a trinary state can be "relatively easily" implemented using BarsSince(), +2,+1,0,-1,-2 is a lot more complicated (although probably not impossible): Either use PREV or write external functions; that's what they're for.

As Henry has already pointed out, you can achieve results more easily by using (in)equality comparisons instead of the Cross() function.



wabbit [:D]

Systematic  
#7 Posted : Wednesday, November 10, 2010 8:42:49 PM(UTC)
Systematic

Rank: Member

Groups: Registered, Registered Users, Subscribers
Joined: 9/15/2005(UTC)
Posts: 24

Thanks for your reply. I was hoping to avoid the route of creating external functions because that's another bailiwick altogether. I've actually created a five state version using PREV but it has some problems that I find difficult to resolve because of the dependencies between the current state and the state change triggers. It is also very slow (as expected). The objective is to create "indicators" showing trade by trade results displayed graphically along with the price chart -- e.g., buy, sell, etc triggers for trade entries and exits; the current trading state (out, long, short, scaled-in long, etc); provisions for adding to (or subtracting from) an ongoing trade ("scaling in" or "scaling out"); and the %gain(loss) upon exiting the trade, in addition to any indicators upon which the triggers are based. A general framework based upon the "trade state" would accommodate and facilitate the visual assessment of a large number of different trading systems, each with different buy, sell, etc triggers and rules. I find the visual aspect to be a valuable supplement that is nearly as important as trading system performance statistics... Systematic
wabbit  
#8 Posted : Wednesday, November 10, 2010 9:10:39 PM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
Generally, if I have think longer than a minute or two how I am going to have to circumvent the limitations of MSFL, I just write an external function.

Another generalisation: if you have more than 1 PREV in a function, or any nested PREVs then external functions are much faster to execute.

[edit] You should also consider writing out a complete truth table for how your state machine should deal with every permutation of signal inputs.



wabbit [:D]

Systematic  
#9 Posted : Wednesday, November 10, 2010 9:41:28 PM(UTC)
Systematic

Rank: Member

Groups: Registered, Registered Users, Subscribers
Joined: 9/15/2005(UTC)
Posts: 24

Re: "You should also consider writing out a complete truth table for how your state machine should deal with every permutation of signal inputs." I've actually implemented this in Excel using VBA code to generate detailed trade summaries, statistical reports and complete time series of all the signals and states. But Excel's graphical abilities are far inferior to MS for stock charts, so I was hoping to do the same thing using MSFL for that reason and as an expedient means to help validate the VBA code. So maybe I should just link some Excel workbooks to MS via OLE... Systematic
Systematic  
#10 Posted : Monday, November 22, 2010 8:19:56 PM(UTC)
Systematic

Rank: Member

Groups: Registered, Registered Users, Subscribers
Joined: 9/15/2005(UTC)
Posts: 24

Just curious: Is there a document that provides a step by step procedure for translating a state transition table to MetaStock code? My main problems with MS have been not fully understanding what's happening "under the hood" -- esp. in terms of the order of internal calculations. Such a doc would be very helpful.... Systematic
wabbit  
#11 Posted : Monday, November 22, 2010 9:19:06 PM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
henry1224  
#12 Posted : Tuesday, November 23, 2010 8:35:23 AM(UTC)
henry1224

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 10/29/2004(UTC)
Posts: 1,394
Location: Glastonbury, CT

Was thanked: 2 time(s) in 2 post(s)
A:=Cross(mov(C,5,S),Mov(C,10,S));

A will plot +1 on the bar that the 2 MA's cross

A:=if(Mov(C,5,S)>Mov(C,10,S),1,
If(Mov(C,5,S)<Mov(C,10,S),-1,0));

Now A will plot +1 on the bar that the 2 MA's Cross and will continue to Plot +1 as long as the condition remains true
Users browsing this topic
Guest (Hidden)
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.