Expression Overview


Logical expression are the most important component of Investfly. Logical expressions are used to define criteria for stock screener, stock alert, automated trade order and automated trading strategy. You will build logical expression using our widget called the Expression Builder

What is a logical expression?

A logical expression is a mathematical expression that evaluates to either TRUE or FALSE. A few examples

  • ((marketcap > 1B)) && ((LastPrice < 1.1 * Low50Week))
  • ((AVGVOLUME > 1M)) && ((rsi5 > 70) || (rsi5 < 30))
  • ((( prev_uband_20_2 - prev_lband_20_2 ) / PrevPrice > ( uband_20_2 - lband_20_2 ) / LastPrice))

A logical expression can contain four types of things:

  1. Variables ( marketcap, prev_uband_20_2 ).
  2. Constants ( 30, 1.1 ).
  3. Arithmetic Operators ( * + - / ).
  4. Logical Operators ( && || ).

When Investfly evaluates this expression, it inserts values for variables and evaluates it to either TRUE or FALSE.

  • Variables:

    Variables represent an indicator and associated parameters. When you define the variable, you map the variable to particular indicator and corresponding parameters

    • A variable named rsi5 could represent indicator=RSI, period=5, bar = 1-minute bars
    • A variable named Low50Week could represent indicator=MIN, period=50, field=close, bar = 1-day bars

    You can use any variable name but giving a meaningful name is recommended. The use of variables in the expression simplifies the expression because you can give a meaningful and short names to a combination of indicator and parameters.

  • Expressions in screener

    It is easiest to understand the use of expression in the context of a stock screener. When you specify logical expression in a stock screener, Investfly returns all stocks for which this logical expression evaluates TRUE. In simpler words, this means it lists all stocks that meet the criteria represented by this logical expression. Eg, when expression like rsi5 > 70 is evaluated, Investfly first maps the variable rsi5 to its full definition like [Indicator=RSI, period=5, bars=1-DAY]. The for each stock in the market, it computes value for rsi5 based on historical bars, inserts the computed value in the expression and finally evaluates the expression to TRUE or FALSE. All stocks for which the expression evaluates to TRUE are returned in the result set.

Expression Trigger Event

Another important concept to understand is the concept of an expression event. An event is triggered when a logical expression for a particular security transitions from FALSE to TRUE. By this definition, an event occurs at a specific point in time. After the event is triggered for particular security, the expression has to transition from TRUE back to FALSE and again to TRUE for the subsequent event to occur. Following table illustrates this

Time Expression result Event
9:00:00 FALSE No
9:00:01 FALSE No
9:00:02 TRUE Yes, Event Triggered
9:00:03 TRUE No
9:00:04 FALSE No
9:00:05 FALSE No
9:00:06 TRUE Yes, Event Triggered

Expression in Automated Trade

When logical expression is used to define criteria for a trade order, then trade signal is generated when the expression event is triggered as defined above by transitioning from false to true state. For example, if you used EMA5 > EMA15 as a criteria, a trade event is generated exactly when EMA5 crosses over EMA15 in the price chart. If EMA5 is already over EMA15, trade signal is not generated at that time. For an expression event to trigger, EMA5 must drop below EMA15 and then rise over EMA15 again, thus triggering an event. The same mechanism is used to evaluate open trade condition and close trade condition in automated trading strategy.