PythonTrading – 1- Learning basic data types and control flows

Python is used for several purposes, so it is not very easy to find really targeted information about trading related topics.

 

There are some good sources out there, one of them is the Youtube channel from Sentdex.

 

I have seen lots of other video channels about Python.

 

But those videos are often outdated – and there is always old Python2 content involved.

 

Interactive brokers so far seems to be the one and only solution for real accounts. I have found a promising video from Jens Rabe.

 

He talks about the huge volumes that are traded with IB. He has about 10 accounts with IB, but my current information is, that a real account needs to be capitalized with at least $10000.

 

That is a lot of money to find out if Python is a good solution to achieve the results that I already have with MQL4 and MQL5.

 

To get started I go to Udemy and buy a Python course for finance. This one contains a Python crash course and it uses the Jupyter Notebook system.

 

I remember that I didn’t like Jupyter notebooks before, but there are some nice functions.

 

Shift and Enter will run a line of code and Shift and Tab will show the reference for a function.

 

 

I can also access functions and properties for a variable when I type a dot and press Shift and TAB.

 

 

I would prefer a complete IDE like Pycharm as I am used to Eclipse when I was programming in Java, but for now Jupyter should do the trick.

 

Some things in Python are a little strange for me, for example how to output variables inside of strings.

 

 

Arrays are called lists in Python. And they can contain different types of data, for example strings and numbers can be mixed. To get the item in a list we use an index (starting at 0 as in Java or MQL) and if we want to get the last item we use minus one for the index number.

 

 

Getting everything from or above to a certain index is done by using a colon.

 

Combined lists are also possible, you can grab an item by using the index for multiple dimensions.

 

Write protected lists are called tuples and use round braces instead of square braces.

 

 

And there also so called sets, where you can put in unordered data and get back ordered,unique items – without duplicates.

 

 

Math calculations are like in other programming languages, but we can also import the math library. It comes with Python and we can use it to use things like the square root function.

 

 

We can check conditions like greater, less, equal, unequal and we will get a boolean return value, either True or False.

 

Like in other languages we have things like if, else (Python contains even elif instead of “else if”) and we can use for loops and while loops.

 

Instead of curly braces functions and control flows are defined by indents. That reminds me of the old days when we used to program our pocket calculators.

 

Python also offers so called ranges. A range can be used to define a start value, a step value and a size for the range of values that should be used.

 

It is possible to use a for loop to fill a new list with calculated values based on the values of the original list. It is also possible to use the list values for dynamic calculations (comprehensive lists).