New functions SetVar() and FCount()

We added two new powerful functions:

In FUNCTIONS->MISC

SetVar(@myvar,expression) Sets the variable ‘@myvar’ to the value of the expression. For example to store the 1-week %return in a variable:

SetVar(@1wkRet, 100*(Close(0)-Close(5))/Close(5))

You can then use the new variable in sub-sequent rules as in:

@1wkRet > 10

Note that you can name the variable it whatever you want

In FUNCTIONS->RANKING & SORTING

FCount(“expression”[,type,universe]) Counts the number of stocks where expression is true.

FCount operates as follows:

  1. The expression is evaluated for all stocks
  2. The number of stocks whose expression evaluates to TRUE (1)is counted
  3. This count is stored in each stock

We’ll post some examples of cool things you can do with these functions shortly

Marco - is the variable contents maintained through rebalance periods or only in the current period?

Here is an example of what I wish to do:

  1. get into the market only if the Bench closes above its SMA(200) + 5%

  2. continue to stay in the market even after the market drop below that level but higher than SMA(200) -5%

To accomplish this I want to have a variable that is set to 1 when the bench is greater than SMA(200)*1.05.

I would like to reset the variable to 0 when the bench is less than SMA(200)*0.95.

Then I would like to use this variable to determine whether or not to hold stocks.

Steve

In portfolio simulations that’s easy: two separate rules one for buy one for sell, but you don’t really need SetVar for this.

You buy rule should be something like

Close(0,#Bench) >SMA(200,0,#Bench) *1.05

Your sell rule shuold be

Close(0,#Bench) < SMA(200,0,#Bench) *0.95

Not possible in the screener since you can’t separate buy/sell. In the screener a buy is everything that passes the screen, a sell if everything that was held before the rebalance that does not pass the screen.

Marco - it would only be case for Ports if stocks were only bought when the rule is satisfied: Close(0,#Bench) >SMA(200,0,#Bench) *1.05

But stocks should be bought (and sold) any time after that condition occurs but prior to Close(0,#Bench) < SMA(200,0,#Bench) *0.95 .

So, it can’t be done in Ports without using a variable. But you didn’t answer the question - is the variable maintained through rebalance periods?

Steve

You bring up an interesting case Steve. I too would like to know the answer to this question. If the variable is maintained that is great. If it isn’t, it serves to highlight the need for a BarsSince function.

The setvar is only for the current rebalance. There’s a neat way to do what I think you want to do…

We need a new Variable: NoPos - Number of positions

Your buy rule would then be:

NoPos>0 Or Close(0,#Bench) >SMA(200,0,#Bench) *1.05

So the first time a stock is bought it’s when

Close(0,#Bench) >SMA(200,0,#Bench) *1.05

After that NoPos makes sure the rule evaluates to true, until all stocks are sold due to the sell rule

Close(0,#Bench) < SMA(200,0,#Bench) *0.95

Would that work?

Marco - the proposed solution works in the simple case. But if one wants to have another condition for entering/exiting the market on a shorter (or longer) term basis then the idea falls apart.

Perhaps the answer is to have a function called PrevSetVar(). This would allow the following states for the variable:

If SMA( …, 200) > 1.05 then set @variable = 1
If (SMA( …,200) < 0.95 then set @variable = 0
If neither of the above conditions are met then set @variable to Previous @variable (from last rebalance period).

I assume the logic could be handled via the IF statements already present with P123. So you would only need to have access to the value of the variable from the previous rebalance.

Steve

Not sure it falls apart. Can you give an example?

the SetVar is meant to set a variable for each stock, so what you are proposing is a bit of a hack.

Maybe SetGlobalVar is more like it… but the rule is still run for every stock, so it’s still a bit of a hack…

What you are asking is an independent rule run only once, before the buy/sells are executed. It belongs in a separate area than the rules… In other words, quite a bit of work…

This kind falls into a more generic concept: adding a market timing module to sims/port, separate from buy/sell rules.

It can have it’s own macro economic data signals and ranking system, and index rules such as the one you describe…

Marco - Like the concept of a market timing module and macro economic data signals. I would certainly welcome seeing more items for reference.

Current available economic data signals I see are
Interest: #TNX - 10Y Treasury Note
Earnings: #SPRP - SP500 Risk Premium, #SPEPSCY - SP500 EPS CurrentY, #SPEPSNY - SP500 EPS NextY, #SPEPSTTM - SP500 EPS Trailing 12 months, #SPYield - SP500 Yield

I would welcome items like
Interest: 13 week Treasury Bill, 5 year Treasury Note, 30 year Treasury Bond
Currency: Euro to US Dollar

The solution proposed by Steve using a global variable is one way to handle the problem. A conceptually different way that might also work would be using a BarsSince function.

If I was using a charting program with a BarsSince function, the generic way I would handle this problem is as follows:

A = Close(0,#Bench) >SMA(200,0,#Bench) *1.05
B = Close(0,#Bench) < SMA(200,0,#Bench) *0.95

Buy: BarsSince(A)<=BarsSince(B)
Sell: BarsSince(A)>BarsSince(B)

Since this is the route most charting applications I am familiar with provide I’m guessing it may be the easier one to implement. I present it as another option to consider in case that is so.

Sterling’s suggestion would work and may be easier to implement.

Steve

Steve, I’m still wondering why my proposed solution would not work. It’s much easier to think of enhancements when exact examples of strategies people are trying to do. It can guide us to the right answer as to what solution is the best, most generic and most useful. Once a few examples are examined the answer becomes more obvious.

Everybody has very specific requirements, but once all are looked together the generalities and the true ‘function’ comes to the surface. For example I still think what everybody is talking about here is a separate market timing module. However since that is quite a project to design and implement, a simple solution might do for now. But we want to stay away from hacks…

Marco - I agree with everything you are saying. We are just on the wrong side of the page of what constitutes a hack.

You own the website and are a lot more aware of how complex various solutions are. My gut feel is that what you are proposing is a hack that will work in the simple case but not in general.

Steve

From how I understand the problem, which I’m not necessarily sure is the same as how Steve meant it, the proposed NoPos solution wouldn’t work.

Take for example the case where Close(0,#Bench) >SMA(200,0,#Bench)*1.05 occurred recently but is currently only Close(0,#Bench) = SMA(200,0,#Bench) *1.0. Is the stock bought or not? Using the NoPos solution it is not bought. However, some systems would still consider this in buy mode would buy the stock. I’m not sure how one would code it without going into contortions using multiple offsets and only arriving at an imprecise approximation.

I have no idea how difficult the technical aspects behind implementing the GlobalVar solution are, but from what I can tell the BarsSince solution seems very close to the already implemented HighestBar and LowestBar functions.

HighestBar(#High,10,0) is basically BarsSince(High=Highest(#High,10,0))

With the implementation of the HighestBar function I presume the code necessary for counting the bars is already coded. The modification that would make it more powerful is if the built-in statically defined true/false condition of “High=Highest(x,y,z)” could be turned into a user definable true/false condition like in the Eval function.

Anyway I’m just exploring ideas. Thank you for all the other improvements you’ve made to the site! :slight_smile:

Marco, Dadoz, and All,

For some new data streams to do timing with how about NASDAQ new highs - new lows, and NASDAQ advances - declines?

Bill

On Aug 13 Marco wrote (see http://www.portfolio123.com/mvnforum/viewthread?thread=4044#18909 )

Marco, do we have the NoPos variable now available, or anything else that achieves a similar goal?
Thanks,
Z.