Broker API HubMenu

Stage 1 of 4, lesson 2 of 31. No coding experience needed

Variables and types: what a value can be

Meet the four kinds of value every script is made of, see why text does not do arithmetic, and convert between them. It introduces str, int, float, bool and f-strings.

Independent and unofficialCode run

Risk warning: CFDs and spread bets are complex instruments and come with a high risk of losing money rapidly due to leverage. You can lose money on these products, and each provider publishes its own loss figure under FCA rules. You should consider whether you understand how they work and whether you can afford to take the high risk of losing your money. Automated and API trading can amplify losses through bugs, connectivity failures and runaway orders.

You need: The first lesson. No broker account.

A variable is a name for a value. Every value has a type, and the type decides what you are allowed to do with it. Four types cover most of what a trading script handles: text, whole numbers, numbers with a decimal point, and true or false.

The type matters most at the edges of a program. Anything read from a file, typed by a person or sent by an API arrives as text, even when it looks like a number. Multiply the text 2 by three and you get 222, not 6. Python does not guess what you meant. You convert on purpose, with int or float.

Run the script and read each line of output against the line of code that printed it. The figures are invented.

The code

# Variables and types: every value has a type, and the type decides what you can do with it.
# No broker, no account, no internet. The figures are invented.

market = "FTSE 100 (mock)"   # str: text
size = 2                     # int: a whole number
entry_price = 7650.2         # float: a number with a decimal point
is_open = True               # bool: True or False

print(market, "is a", type(market).__name__)
print(size, "is a", type(size).__name__)
print(entry_price, "is a", type(entry_price).__name__)
print(is_open, "is a", type(is_open).__name__)

# Numbers read from a file or typed by a person arrive as text. Text does not do arithmetic.
typed_size = "2"
print("Text times 3:", typed_size * 3)
print("Number times 3:", int(typed_size) * 3)

# Converting between types is explicit: float() turns text into a number.
exit_price = float("7662.7")
points = exit_price - entry_price
print("Points:", round(points, 1))
print("As whole points:", int(points))

# An f-string builds text from values. :.2f means two decimal places.
print(f"{market}: {size} at {entry_price:.2f}, {points * size:.2f} points in all")

What you should see

FTSE 100 (mock) is a str
2 is a int
7650.2 is a float
True is a bool
Text times 3: 222
Number times 3: 6
Points: 12.5
As whole points: 12
FTSE 100 (mock): 2 at 7650.20, 25.00 points in all

Run with Python 3.12 on Windows. No network and no account.

Check yourself

Answer each question from the code above before you open it. The answers were written from the script on this page and what it printed.

Why does the script print 222 for one line and 6 for the next?

typed_size holds the text 2, not the number. Multiplying text repeats it, giving 222. int(typed_size) turns it into a number first, and 2 times 3 is 6.

The points come to 12.5. What does int(points) print, and is that rounding?

It prints 12. int cuts off everything after the decimal point. It does not round. Use round when you want rounding.

In the last line, what does :.2f do to entry_price?

It formats the number with exactly two decimal places, so 7650.2 is printed as 7650.20. The value itself is not changed.

Exercise

Change typed_size to the text 5 and the exit price to the text 7640.2, so the trade ends below its entry. Predict every line that will change, then run it.

What the changed script prints
FTSE 100 (mock) is a str
2 is a int
7650.2 is a float
True is a bool
Text times 3: 555
Number times 3: 15
Points: -10.0
As whole points: -10
FTSE 100 (mock): 2 at 7650.20, -20.00 points in all

Captured from a run of the solution on . Run with Python 3.12 on Windows. No network and no account.

One solution
# Exercise solution: the lesson script with the one change the exercise asks for.
# Variables and types: every value has a type, and the type decides what you can do with it.
# No broker, no account, no internet. The figures are invented.

market = "FTSE 100 (mock)"   # str: text
size = 2                     # int: a whole number
entry_price = 7650.2         # float: a number with a decimal point
is_open = True               # bool: True or False

print(market, "is a", type(market).__name__)
print(size, "is a", type(size).__name__)
print(entry_price, "is a", type(entry_price).__name__)
print(is_open, "is a", type(is_open).__name__)

# Numbers read from a file or typed by a person arrive as text. Text does not do arithmetic.
typed_size = "5"
print("Text times 3:", typed_size * 3)
print("Number times 3:", int(typed_size) * 3)

# Converting between types is explicit: float() turns text into a number.
exit_price = float("7640.2")
points = exit_price - entry_price
print("Points:", round(points, 1))
print("As whole points:", int(points))

# An f-string builds text from values. :.2f means two decimal places.
print(f"{market}: {size} at {entry_price:.2f}, {points * size:.2f} points in all")

Your progress

Next: If and comparisons

Your marks are kept in this browser only, in its local storage. They are not sent anywhere, there is no account, and they will not follow you to another browser or device. To remove them, use Clear my progress on the learning track page, or clear this site's data in your browser's settings. Marking needs JavaScript. With scripts off the track reads the same, without the ticks.

Where this sits in the path

Stage 1 of 4: First steps. Values, decisions, loops and functions, and the habit of testing what you wrote. No files and no account.

  1. First Python script
  2. Variables and typesYou are here
  3. If and comparisons
  4. Lists and loops
  5. Functions and sizing
  6. Test your functions

The whole path, every stage

Back: First Python scriptNext: If and comparisons