# Charting: add-fieldset examples

This page provides three examples of the Charting blade’s `add-fieldset` configuration item.

For information on the values used in the configuration examples, see Intraday Parameter Codes and Values.

## Equity (Building block) / Cash (Instrument type)

```# Equity cash instrument
recordtype 113

# last price fields
add-field      OPEN_PRC              10       <<          TRDPRC_1        TRDPRC_1
add-field      HST_CLOSE             10        =          TRDPRC_1        TRDPRC_1
add-field      HIGH_1                10        >          TRDPRC_1        TRDPRC_1
add-field      LOW_1                 10        <          TRDPRC_1        TRDPRC_1
add-field      TRDVOL_1              10       +%          TRDVOL_1        TRDPRC_1

# bid price fields
add-field      BID_OPEN_PRC          10       <<          BID BID
add-field      BID_HST_CLOSE         10       =           BID BID
add-field      BID_HIGH_1            10       >           BID BID
add-field      BID_LOW_1             10       <           BID BID
add-field      BID_TRDVOL_1          10       +%          TRDVOL_1          BID

# ask price fields

# level-1 fields
cache-field    PROD_PERM
cache-field    DSPLY_NAME

rules          "$TRDPRC_1 0 =" # filter 0 price updates rules "$BID 0 ="        # filter 0 price updates
rules          "$ASK 0 =" # filter 0 price updates rules "$TRDPRC_1 $TRDPRC_2$TRDPRC_3 $TRDPRC_4$TRDPRC_5 + + + 4 / / 1 - ~ 0.4 }"  # filter >= 40% (four period moving average)
rules          "$BID$BID_1 $BID_2 + 2 / / 1 - ~ 0.4 }" # filter >= 40% (two period moving average) rules "$ASK $ASK_1$ASK_2 + 2 / / 1 - ~ 0.4 }"  # filter >= 40% (two period moving average)

end-fieldset```

## Equity (Building block) / Market statistics (Instrument type)

```# Equity/Market stats
recordtype 117

# last price fields
add-field      OPEN_PRC              10       <<          TRDPRC_1        TRDPRC_1
add-field      HST_CLOSE             10       =           TRDPRC_1        TRDPRC_1
add-field      HIGH_1                10       >           TRDPRC_1        TRDPRC_1
add-field      LOW_1                 10       <           TRDPRC_1        TRDPRC_1
add-field      TRDVOL_1              10      +%           TRDVOL_1        TRDPRC_1

# level-1 fields
cache-field    PROD_PERM
cache-field    DSPLY_NAME

rules          "$TRDPRC_1 0 =" # filter 0 price updates rules "$TRDPRC_1 $TRDPRC_2$TRDPRC_3 $TRDPRC_4$TRDPRC_5 + + + 4 / / 1 - ~ 0.4 }" # filter >= 40% (four period moving average)
end-fieldset```

## Equity (Building block) / Market Indices (Instrument type)

```# Equity Market indices
recordtype 118
# last price fields
add-field      OPEN_PRC              10      <<            TRDPRC_1       TRDPRC_1
add-field      HST_CLOSE             10       =            TRDPRC_1       TRDPRC_1
add-field      HIGH_1                10       >            TRDPRC_1       TRDPRC_1
add-field      LOW_1                 10       <            TRDPRC_1       TRDPRC_1
add-field      TRDVOL_1              10      +%            TRDVOL_1       TRDPRC_1

# level-1 fields
cache-field    PROD_PERM
cache-field    DSPLY_NAME
rules          "$TRDPRC_1 0 =" # filter 0 price updates rules "$TRDPRC_1 $TRDPRC_2$TRDPRC_3 $TRDPRC_4$TRDPRC_5 + + + 4 / / 1 - ~ 0.4 }" # filter >= 40% (four period moving average)
end-fieldset```

## Validating instrument values in RPN or in Lua

Validation rules determine whether a fieldset caches a instrument update. Rules can be defined using Reverse Polish notation (RPN) expressions or a Transformer pipeline function (Lua or JavaScript).

This section looks at the rules below and contrasts their implementation in RPN and in Lua:

• Rule one: reject an update for a fieldset if the value of the TRADPRC_1 field is zero

• Rule two: reject an update for a fieldset if the value of the TRADPRC_1 field is different from the four-period moving average by 40% or more

 Do not define both RPN rules (`rules`) and a Transformer pipeline function (`pipeline-file` and `pipeline-func`) in the same `add-fieldset` stanza.

### Validating using RPN expressions

To define a validation rule for a fieldset in Reverse Polish notation (RPN), write the RPN expression in a `rules` option. If the expression evaluates to a non-zero value, then the update for the fieldset will not be cached.

Define one RPN expression per `rules` option. Add as many `rules` options as you need.

Rule one: to reject an update for a fieldset if the value of the TRADPRC_1 field is zero, add the following `rules` option to the fieldset’s `add-fieldset` configuration item:

`rules "$TRDPRC_1 0 ="` Rule two: to reject an update for a fieldset if the value of the TRADPRC_1 field is different from the four-period moving average by 40% or more, add the following `rules` option to the fieldset’s `add-fieldset` configuration item. The moving average is calculated from last four cached values for the field, which are held in fields TRDPRC_2, TRDPRC_3, TRDPRC_4, and TRDPRC_5. `rules "$TRDPRC_1 $TRDPRC_2$TRDPRC_3 $TRDPRC_4$TRDPRC_5 + + + 4 / / 1 - ~ 0.4 }"`

### Validating using a Lua function

To define one or more validation rules in Lua for a fieldset, follow the instructions below:

1. Create a Lua script with a single function in the `global_overrides/ChartingService/Transformer/etc/pipeline` directory of your Deployment Framework.

2. Add the following options to the fieldset’s `add-fieldset` configuration item. The example below assumes the name of the Lua script file is `validate.js` and the name of the Lua function is `validate`:

```pipeline-file   validate.lua
pipeline-func   validate```

The content of the Lua script `validate.lua` is quoted in full below, with comments highlighting the code for rule one and rule two:

``````require("log")
require("math")

function initialise()
log.log(log.INFO,"Normalisation initialisation happening\n")
end

--
-- Apply these RPN rules to an update
--
--   Rule 1 filter 0 price updates
--   Rule 2 filter >= 40% (four period moving average)
--
-- param update - Current update
-- param cache - Last update
--
-- retval 0 - Rules passed or can't be applied
-- retval 1 - Rule failed
--
function validate(update, cache)

local trdprc1 = update:getfield("TRDPRC_1")
if trdprc1 == nil then
return 0
end

-- Rule 1: reject the update if TRDPRC_1 is zero
-- RPN equivalent: "$TRDPRC_1 0 =" if tonumber(trdprc1) == 0 then return 1 end local trdprc2 = update:getfield("TRDPRC_2") if trdprc2 == nil then return 0 end local trdprc3 = update:getfield("TRDPRC_3") if trdprc3 == nil then return 0 end local trdprc4 = update:getfield("TRDPRC_4") if trdprc4 == nil then return 0 end local trdprc5 = update:getfield("TRDPRC_5") if trdprc5 == nil then return 0 end -- Rule 2: reject the update if the difference between TRDPRC_1 and -- the four-period moving average is 40% or greater. -- RPN equivalent: "$TRDPRC_1 $TRDPRC_2$TRDPRC_3 $TRDPRC_4$TRDPRC_5 + + + 4 / / 1 - ~ 0.4 }"
local moving_average = math.abs( ( trdprc1 / ( (trdprc2 + trdprc3 + trdprc4 + trdprc5 ) / 4 ) ) - 1 )
if  moving_average >= 0.4 then
return 1
end

return 0
end``````