Python Tool

Author: Optuma Team Last updated: May 10, 2022 11:01

Overview

Python is a popular programming language that now has limited support within Optuma (from versions 2.0 and later).

Requirements

There are a number of prerequisites that must be met before the Python tool will display…

  1. You need an Active Trader Services (or higher) subscription
  2. You must have a version of Python 3.9 installed. If you require a copy you can download a free version here. (NOTE: This Module is currently not compatible with Python 3.10).
  3. Python must be added to your Windows Path (this option is provided as part of the standard Python installation).

Python1

*Note: If you already have Python installed and it is not part of the Windows Path you can set it up manually using the process outlined here.

Once all prerequisites have been met you will have access to the Python tool in Optuma.

Using the Python Tool

Right-click on the chart, go to the Tool (spanner) icon, and search for Python. Left-click on the option that is found in the list.

Python2

The tool will be added to the chart (but will display not plots by default). On the left-side Actions panel click the Create New Python Script option.

Python3

A pop-up window will appear where you can enter in a new name for the script.

Python4

Enter a name and press Ok and the Python editor will appear.

Python5

Example Python Script

The following is an example Python script you can use in Optuma…

</span> ``` import Tool from datetime import datetime def Init(): global bars,date,list,real Tool.Props.Name = ‘My Tool’ Tool.Props.Hint = ‘My Tool Hint [REAL]’ Tool.Props.PlotColor = 0x0000ff #BBGGRR bars= Tool.AddInteger(‘BARS’, ‘Bars’, 12) date= Tool.AddDate(‘DATE’,’Date’, 1545730073) list= Tool.AddList(‘LIST’,’List’,’One|Two|Three’) real= Tool.AddReal(‘REAL’,’Real’,23.5) timestamp = 1545730073 dt_object = datetime.fromtimestamp(timestamp) print(“dt_object =”, dt_object) def Process(start, end): real.Value = real.value + 1 m = Tool.RunScript(‘MA(BARS=’+str(bars.value)+’, STYLE=Simple, CALC=Close)’, Tool.Source) print(date.Value) for i in range(start, end): Tool.DataOut.Row(i).close = m.Row(i).close def OnPropertyChange(var): print(var.Name) print(var.Value)



Once the code has been saved, go to the **Actions** panel of the Python tool again and click the **Reprocess Python Script** action. Any time you wish to adjust the script, click the **Edit Python Script** in the same **Actions** panel.

### Selecting an Existing Python Script

To access a Python script you have already coded, apply the Python tool to the chart. Go to the left side Properties panel and you will see a Drop Down selector called Python File.

![Python6](../images/python-tool-python6.png)

From here you can select all previously created Python scripts for use in Optuma.

### Python Tool Properties

**![Ex6](../images/python-tool-ex6.png)
**Tool Name:** This allows you to adjust the name of the tool, as it's displayed in the Structure Panel.

**Python File:** Use this drop-down to select the Python Script file you want to display on the chart.

*Note: Any user-controlled properties you have set up in the Python code (a date selector for example) will appear in this section.*

<div>**Plot Style:** Adjusts the display style of the Python tools default plot. There are 6 options available: Line, Dot, Histogram, Step, Shaded, Shaded Step.</div><div>**Line Style:** When Line is selected as the Plot Style, the Line Style property allows you to adjust the type of line displayed. There are 8 options available: Solid, Dots, Dash, Dash Dots, Long Dash, Long Dash Dot, Long Dash Dot Dot, Stippled.
</div><div>**Line Width:** This allows you to adjust the width of the Python Tools default line. Moving the slider bar to the right increases the thickness of the line.
</div><div>**Colour Scheme:** This allows you to select the colour scheme being used by the Python Tools default plot. Single Colour, Copy Colours from Price Chart, Positive / Negative, and Custom options are available.
</div><div><div>**Line Colour:** This allows you to adjust the colour of the Python Tools default line.</div><div>**Tool Transparency:** Use this slider bar to adjust the transparency of the tool. Moving the slider to the left will increase the transparency of the tool.
</div><div>**Visible:** Un-tick this checkbox to hide the tool from the chart.
</div><div>**Show in Price Scale:** When selected the Python Tools plot lines value(s) will be displayed in the Price Scale.
</div></div><div>**Lock Tool:** When enabled this tool can't be deleted from the chart or have any properties adjusted.</div>