Article Contents
Print

Persistent Terms

The values, conditional states, securities and security lists defined in your spec’s named terms are calculated, as required, usually based on some data associated with a security, which is often the security whose data is currently being evaluated (the ‘evaluated security’). This data is lost when you shut down ShareScope or switch between the RuleTrader Live or Test sessions. When you restart a session, it is recalculated, as it is needed, based on the current data.

However, sometimes it is useful to have a memory of past data. For example, if you want to count the number of times some market event happens, you’ll need a counter whose value persists between ShareScope and RuleTrader sessions.

The answer is Persistent Terms, which are Value Terms, Condition Terms, Security Terms or Security List terms, whose data is saved between RuleTrader and ShareScope sessions. If a share is deselected from a trading list, and then reselected at a later date, its persistent term data will be preserved from the earlier time. Similarly, if a soft-reset occurs in the Live session, then all persistent terms data will be preserved.

Because the data in Persistent Terms must persist, it cannot be calculated ‘on the fly’ like other terms. Instead a method is required to set, update and reset the data in Persistent Terms. This is achieved using Action Rules, which are described below.

An example of the use of Persistent Terms is provided in the Example Trading Spec’s Momentum strategy’s ‘Days Waiting To Sell‘ persistent term. This counts the number of days that the Spec has been waiting for a good price to sell a position. The ‘Increment Days Waiting To Sell’ and ‘Reset Days Waiting To Sell’ Action Rules in that strategy are used to update and reset the term’s data.

Creating A Persistent Term

Persistent Terms are created in exactly the same way as a regular term (see the Named Terms help topic for instructions on how to do this). The only difference is that, instead of selecting value, condition, security or security list as the term’s type, you’ll select one of the persistent value, persistent condition, persistent security or persistent security list options.

With a regular term, you’d then be asked to define an expression that returns that term’s data. With Persistent Terms it’s exactly the same, except that this data will be used to initialise the first value the term will contain.

As these expressions usually derive their data from the information associated with a share (most usually the evaluated share), separate persistent data stores are created for each instance of such a share.

Setting, Updating and Resetting A Persistent Term’s Data

As mentioned above, this is achieved using Action Rules, which are defined in the same way as regular Trading Rules but, instead of selecting the rule’s type as ‘signals a trade’ or ‘generates an alert’, one of the following types are selected:

  • Sets a persistent term: sets the data in a specified persistent term to the result from some expression that you define
  • Resets a persistent term: resets the data in a specified persistent term to its initialisation value (see Creating A Persistent Term, above)
  • Increments/decrements a persistent value: adds or subtracts one to the value of a persistent value term. Use this when you need to count events. Because Action Rules are only evaluated once per day, this effectively acts as a day counter.
  • Toggles a persistent condition: inverts the true/false state of a persistent condition term i.e. true become false; false becomes true
  • Adds/removes shares in a persistent share list: adds or removes a specified share to/from a persistent security list

Unlike Trading Rules, Action Rules are always evaluated (even if a Trading Rule has triggered) but they are only evaluated once per day, making them useful for e.g. counting daily events.

Also, Trading Rules are never evaluated when a share is being backfilled, but when you define an Action Rule you can specify that it should be evaluated during the backfill. This can be very useful if, for example, you are counting events and you do not want to miss any events that occurred between when a share was previously in a trading list and when it is reselected for a new list.

Scroll to Top