Python Operators + if-else + Loops | 2/100 Days of Python Algo trading

Algorithmic Trading and Python: The Technology of the Future

Algorithmic trading is rapidly transforming the financial markets. Automating trading strategies through Python has now become extremely beneficial for quantitative traders. In this session, we will not only learn Python Operators and Conditional Statements (If-Else) but also touch upon the tools used in Freqtrade Strategy, Crypto Trading Strategies, and Algo Trading Software USA.

Why strong fundamentals are important?

Before we begin, it is important to understand that if your fundamental knowledge is not strong, it will be difficult to solve big problems. Understanding the role of Operators and Conditional Statements in Python will give you confidence in coding and help in large automated trading projects.

Join the Community
If you have any questions – you can reach out to us on our social media handles. Also, after each session you are given resources like:

  • MCQs (Multiple Choice Questions)
  • Jupyter Notebook
  • Task Sheet & Mini Project

All these links are available in the video description.

Complete information about Python Operators

Operators in Python are special symbols with which we can perform calculations on variables and values.

1. Arithmetic Operators

+ (Addition)

– (Subtraction)

* (Multiplication)

/ (Division – Float)

// (Integer Division – Integer Division only)

% (Modulo – Remainder)

** (Power – Exponent)

Example:

a = 5

b = 2

print(a + b) # 7

print(a // b) # 2

print(a % b) # 1

2. Relational Operators

With these you can compare two values:

== (Equals)

!= (Not Equals)

> (Greater)

< (Smaller)

>= (Greater or Equal)

<= (Smaller or Equal)

3. Logical Operators

To check logical conditions For:

and

or

not

Example:

a = True
b = False
print(a and b) # False
print(a or b) # True
print(not a) # False

4. Bitwise Operators (Operations at bit level)

Less commonly used but important:

& (AND)

| (OR)

^ (XOR)

<< (Left Shift)

>> (Right Shift)

5. Assignment Operators (Assigning Values)

=

+=, -=, *=, /=, //=, %=
Example:

a = 5
a += 2 # now a = 7

6. Membership Operators (Checking Membership)

These are used to check whether a value is in a sequence (such as list, string) or not:

in

not in

Example:

‘BTC’ in [‘BTC’, ‘ETH’] # True
‘USD’ not in [‘BTC’, ‘ETH’] # True

Conditional Statements (if, else, elif) in Python
✅ if Statement:

if a > b:

print(“a is greater”)

if-else Statement:

if a > b:
print(“a is bigger”)
else:
print(“b is bigger”)

if-elif-else Statement:

if a > b:
print(“a is bigger”)
elif a == b:
print(“a and b are equal”)
else:
print(“b is bigger”)

Python Operators

Real-world Use: Decision Making in Algo Trading

In trading, we use these operators and conditions in many places like:

  • To trigger a strategy
  • To give buy/sell signal
  • To compare indicator threshold
  • To take action on bot rules.

For example:

if price > moving_average and volume > threshold:
signal = “Buy”

Watch this Day 2 video tutorial

Example: Basic If-Else for Trade Execution

# Sample trade data

price = 45000

moving_average = 46000

if price > moving_average:

print(“Sell BTC-USD”)

else:

print(“Buy BTC-USD”)

Using If-Elif-Else for Multi-Condition Trading

Why Use If-Elif-Else?

Handles multiple trade conditions

Improves quantitative analysis for trading in Singapore

Example: Multi-Condition Trading Strategy

# RSI indicator values

rsi = 28

if rsi < 30:

print(“Strong Buy Signal”)

elif 30 <= rsi <= 70:

print(“Hold Position”)

else:

print(“Sell Position”)

Using Loops for Trade Automation

Why Use Loops in Trading?

Automates bulk order execution

Improves Freqtrade strategy efficiency

Example: Looping Through Multiple Trade Signals

# List of trade signals

signals = [“BUY BTC”, “SELL ETH”, “BUY ADA”]

for signal in signals:

print(f”Executing Trade: {signal}”)

While Loop for Real-Time Market Monitoring

Why Use While Loops?

Continuously monitors market trends

Helps execute algorithmic trading without interruptions

Example: While Loop for Price Monitoring

price = 45000

target_price = 46000

while price < target_price:

print(“Waiting for Price to Reach Target…”)

price += 1000

Go ahead—challenge yourself and solidify your learning!

Day 2: Python Operators + if-else + Loops

1. What are the two main categories of operators in Python?

2. What does the following code output: result = 5 + 3 * 2

3. How do you represent exponentiation (a to the power of b) in Python?

4. What is the output of the following code: x = 10 if x > 5: print(“x is greater than 5”)

5. What is the correct syntax to start an if-else statement in Python? 

6. How can you add an else block to an if-else statement?

7. What does the following code do?

number = 10

while number > 0:

print(number)

number -= 1

8. How can you exit a loop early in Python?

9. What is the difference between a for loop and a while loop?

10. What is the correct syntax to start a for loop iterating over a list?

11. What does the % operator do in Python?

12. Which comparison operator checks if two values are not equal?

13. How would you check if the variables x and y hold the same value in Python?

14. Which of the following represents the logical “AND” operator in Python?

15. Which statement is used to skip to the next iteration in a loop?

16. What is the output of this code?

for i in range(5):

if i == 3:

break

print(i)

17. What’s the output of this code?

x = 5

if x > 2:

print(“Bigger”)

elif x < 5:

print(“Smaller”)

18. What’s a common use case for a while loop?

19. How would you iterate through the characters in the string “Hello”?

20. What is the purpose of the pass statement in Python?






 

1 thought on “Python Operators + if-else + Loops | 2/100 Days of Python Algo trading”

Comments are closed.

SekabetSekabetSekabet GirişSekabet Güncel GirişSekabetSekabet GirişSekabet Güncel Giriş