Sample Python script, and a question about returning values from that script

Here’s a sample Python script to calculate ticksize for a market:

import Tool

def Init():
    global bars
    Tool.Props.Name = 'TickSize'
    Tool.Props.Hint = 'Calculate tick size of a market from recent OHLC data'

def Process(start, end):
    # We're going to grab all the O,H,L,C values from the last 50 days into a set
    ohlcs = set()
    for i in range(end-50, end):
        ohlcs.add(Tool.Source.Row(i).open)
        ohlcs.add(Tool.Source.Row(i).high)
        ohlcs.add(Tool.Source.Row(i).low)
        ohlcs.add(Tool.Source.Row(i).close)

    # Now sort the set from lowest to highest values
    vals = sorted(ohlcs)
    print(vals)

    # Now set ticksize to a ridiculously large value, then iterate through
    # pairs of consecutive values in the sorted set to find the smallest difference.
    # That almost always returns the ticksize...
    ticksize = 10000000
    for first, second in zip(vals, vals[1:]):
        if second - first < ticksize:
            ticksize = second - first 

    print(ticksize)

    # This will print a line showing the ticksize value on a chart...
    for i in range(start, end):
        Tool.DataOut.Row(i).Close = ticksize

    # However, I want to return ticksize as a value I can consume rather than draw it on a chart
    # How do I do that?
    return ticksize

It’s not 100% guaranteed to give the right answer, but in practice I haven’t yet found a case where it doesn’t work. If there’s a problem, you can always work with more than the last 50 days’ data.

Couple of questions:

  • where can I view the output of print statements when I run this code inside Optuma?
  • when I insert this Python script in a bar chart, it draws a horizontal line at the "ticksize" value on the chart. In practice though, I don't want to draw a line on the chart; instead I want to use the value of "ticksize" in other scripts on that same chart. For example, I may want to print "Ticksize = value" in a TextBox on the chart. If I want to consume the value of "ticksize" from this script inside another Optuma script, how do I expose that value to the other script? "return ticksize" from the "Process" method as I'm doing above doesn't seem to work. For example, in Optuma's scripting language I can call "SCRIPT(scriptfile)" to use the output value of one Optuma script inside another Optuma script; how can I do that when the script I'm calling is in Python? I tried calling "SCRIPT(myscript.py)" from an Optuma script but it always seems to get a zero value back

Hi

Once you have set the ticksize in the dataout rows , you can use the PYTHON() function in Optuma scripting (remove the return ticksize line)

PYTHON(PYTHONFILE=TickSize.py)

So for example in a watchlist you can click on the + to add a column and choose new script column. Then in the script enter the above line.
The TickSize value should appear in the watchlist.

You can also use the last row value as a variable in scripts using the $ operator to change a list into a variable

$BARS = PYTHON(PYTHONFILE=bars.py); 
MA(BARS=$BARS)

The output of the print function in python scripts can be viewed using an outputdeubgstring viewer. DBGView is a good one.
https://docs.microsoft.com/en-us/sysinternals/downloads/debugview

Regards
Peter

Hi Peter,

Thanks for your reply. DBGView is going to come in very handy.

In the example you provided of using a watchlist to show the TickSize value, how does the PYTHON(PYTHONFILE=TickSize.py) call know to use the value of the ticksize variable in the Python script rather than e.g. ohlcs or vals.

I could understand if the last line was e.g. return(ticksize), but if I’m not returning anything from the Process() method, which variable gets consumed?

Hi David

By Default, the watchlist will use the Close of the most recent bar in the dataout list.
You can access previous bars using the array operator e.g. close of previous par is PYTHON(PYTHONFILE=TickSize.py)[1]

Thanks Peter,

Tools such as CANDLESTICKPATTERN() return string values, which can be displayed in a watchlist. Is it possible to have Python scripts return strings or booleans, or are they limited to returning numbers only?

For the use case I have in mind, ideally I’d like a Python script to return a small array of numbers and have them displayed as an array in a watchlist. I could easily convert that array to a string if that was going to work, but not sure how to implement it if the only option is to use the Close values (which I assume need to be numeric).

Currently only numbers can be returned, however if you create a script column in a watchlist that returns a number in a set range (e.g. 1-100), then right mouse click on the header of the column and select “Custom Labels” you can set different text and colors based on the numerical values, which might meet your needs.

here is a KB article to describe custom labels.
https://help.optuma.com/kb/faq.php?id=967