# Self Referencing Scripts

Author: Optuma Team Last updated: Apr 3, 2020 05:23

## Overview

The SELF() function allows a script to reference itself to get its previous value for use in the current calculation. Alternativley you can reference the function with [] as you would with an offset value.

E.g., a simple script that increments 1 on each bar process

A1 = 1 + Self();
A1

Or

A1 = 1 + A1;
A1

In this script example you can see A1 is equal to a standard moving average MA() plus 0.2, then multiplies by the previous scripts value.

A1 = MA() + 0.2 * Self();
A1;

or this script would output the same result…

A1 = MA() + 0.2 * A1;
A1;

In this final example this script returns the accumulated turnover divided by the accumulated volume…

t1 = TO();
v1 = VOLUME();
t2 = t1 + t2;
v2 = v1 + v2;
t2/v2
{"ft_lang_label":"__label__en","ft_lang_prob":0.7925541,"math_prob":0.9889362,"size":751,"snap":"2023-40-2023-50","text_gpt3_token_len":219,"char_repetition_ratio":0.121820614,"word_repetition_ratio":0.028571429,"special_character_ratio":0.322237,"punctuation_ratio":0.12820514,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99161065,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-24T14:11:39Z\",\"WARC-Record-ID\":\"<urn:uuid:ead70b4f-e55b-49ab-8f7e-a4f70e290b08>\",\"Content-Length\":\"31100\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:22196468-ee5b-42f4-ab85-8614a6e90a48>\",\"WARC-Concurrent-To\":\"<urn:uuid:e3e17cd0-7319-43fd-b204-39cef9032546>\",\"WARC-IP-Address\":\"141.193.213.10\",\"WARC-Target-URI\":\"https://www.optuma.com/kb/optuma/scripting/self-referencing-scripts\",\"WARC-Payload-Digest\":\"sha1:BMAF6SMMRRZ6PQSSJYTZXO3ZTKYFYUTR\",\"WARC-Block-Digest\":\"sha1:VXFSWH3UTXT44ZEY6BXFLVQB5AEP4UPQ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233506646.94_warc_CC-MAIN-20230924123403-20230924153403-00790.warc.gz\"}"}