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

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

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #68181
    David
    • Topics: 9
    • Replies: 16
    • Posts: 25

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

    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
    • This topic was modified 2 weeks ago by David.
    #68191
    Peter
    • Topics: 0
    • Replies: 208
    • Posts: 208

    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)

    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

    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

    1 user thanked author for this post.
    #68219
    David
    • Topics: 9
    • Replies: 16
    • Posts: 25

    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?

    #68233
    Peter
    • Topics: 0
    • Replies: 208
    • Posts: 208

    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]

    #68237
    David
    • Topics: 9
    • Replies: 16
    • Posts: 25

    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).

    #68239
    Peter
    • Topics: 0
    • Replies: 208
    • Posts: 208

    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

    1 user thanked author for this post.
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

Pin It on Pinterest