Python primer for Trading

UberMachine

Well-Known Member
#1
Hello everybody,

This is just a Python Primer for getting started with trading.
I would post a separate post in this thread for the basics so that you can reply back to the post in case of any details.
The main purpose of this thread is to improve our efficiency in trading so that we can automate some boring tasks and could spend more time in developing and testing more robust, profitable strategies.
I believe most traders use Excel for their calculation, feedback, position sizing and other things. You can always provide how you do it in Excel so that it could be easier to relate and learn.
This thread is the primer to how to build your own trading system from scratch.

I would keep the posts to bare minimum (10 or 12) at most to get started early.
So bear with me.
And this is not going to be a highly technical introduction; so you can follow it even if you are a non-tech guy.
The main purpose, as already told, is to be take your existing rule-based systems (successful traders do have rules) to a more sophisticated level

I assume you have already installed anaconda and ta-lib.
If not, you can follow this thread
 
Last edited:

UberMachine

Well-Known Member
#2
Running a jupyter notebook.
This is an interactive web notebook so that you can test your strategies.
So, after Python installation.
Open a command prompt and run
Code:
jupyter notebook
Or select Jupyter notebook from Windows Menu or Launch Anaconda Navigator and click Launch notebook
This will open a window similar to this
Screenshot from 2018-08-06 19-45-41.png





Click New->Python3 and this would open a new notebook similar to
Screenshot from 2018-08-06 19-48-46.png


You can rename untitled to any name
You are now good to go
 

UberMachine

Well-Known Member
#3
Running some simple comands
You can do any arithmetic. Just type in any calculation and then press CTRL+ENTER or click the RUN button.
You can also copy any Python code and copy here and press CTRL+ENTER or RUN it
Python:
10+10
Copy pasting the above, would give the expected result
Screenshot from 2018-08-06 19-53-50.png

You can try any calculation you want

By default, only the last value is printed
To print results, you must use the print function

Python:
print(10+15)
print(20-15)
print(15*4)
print(25/3)
print(2**4)
Copy paste the above and find out the results
 

UberMachine

Well-Known Member
#4
Calculating returns
So, you need to calculate return

What if I invest Rs.10000 for 10 years at a rate of 8%? Simple interest

Python:
10000 + (10000*8*10)/100
This gives you the result but clumsy

So, lets make it a bit better
Python:
principal = 10000
rate = 8
period = 10

interest = (principal * rate * period)/100
print(interest)

total = principal + interest
print(total)
Try out compound interest

Python:
principal = 10000
rate = 8
period = 10

principal * ((1 + (rate/100)) ** period)
 

MSN1979

Well-Known Member
#5
[I 08:50:54.538 NotebookApp] Interrupted...
[I 08:50:54.539 NotebookApp] Shutting down 0 kernels
(base) C:\Users\Manpreet>jupyter notebook
[I 08:51:11.787 NotebookApp] The port 8888 is already in use, trying another port.
[I 08:51:11.788 NotebookApp] The port 8889 is already in use, trying another port.
[I 08:51:11.843 NotebookApp] JupyterLab beta preview extension loaded from C:\Anaconda3\lib\site-packages\jupyterlab
[I 08:51:11.843 NotebookApp] JupyterLab application directory is C:\Anaconda3\share\jupyter\lab
[I 08:51:11.985 NotebookApp] Serving notebooks from local directory: C:\Users\Manpreet
[I 08:51:11.985 NotebookApp] 0 active kernels
[I 08:51:11.985 NotebookApp] The Jupyter Notebook is running at:
[I 08:51:11.985 NotebookApp] http://localhost:8890/?token=c70f8dc4c410418cf40bf70838845d309b5d86c9994d6990
[I 08:51:11.985 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 08:51:11.988 NotebookApp]
Copy/paste this URL into your browser when you connect for the first time,
to login with a token:
http://localhost:8890/?token=c70f8d...f8dc4c410418cf40bf70838845d309b5d86c9994d6990
[I 08:51:12.311 NotebookApp] Accepting one-time-token-authenticated connection from ::1


It doesn't open Jupyter in my computer. Can you please tell me why? It opens a web page but I cannot run anything from there
 

UberMachine

Well-Known Member
#6
[I 08:50:54.538 NotebookApp] Interrupted...
[I 08:50:54.539 NotebookApp] Shutting down 0 kernels
(base) C:\Users\Manpreet>jupyter notebook
[I 08:51:11.787 NotebookApp] The port 8888 is already in use, trying another port.
[I 08:51:11.788 NotebookApp] The port 8889 is already in use, trying another port.
[I 08:51:11.843 NotebookApp] JupyterLab beta preview extension loaded from C:\Anaconda3\lib\site-packages\jupyterlab
[I 08:51:11.843 NotebookApp] JupyterLab application directory is C:\Anaconda3\share\jupyter\lab
[I 08:51:11.985 NotebookApp] Serving notebooks from local directory: C:\Users\Manpreet
[I 08:51:11.985 NotebookApp] 0 active kernels
[I 08:51:11.985 NotebookApp] The Jupyter Notebook is running at:
[I 08:51:11.985 NotebookApp] http://localhost:8890/?token=c70f8dc4c410418cf40bf70838845d309b5d86c9994d6990
[I 08:51:11.985 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 08:51:11.988 NotebookApp]
Copy/paste this URL into your browser when you connect for the first time,
to login with a token:
http://localhost:8890/?token=c70f8d...f8dc4c410418cf40bf70838845d309b5d86c9994d6990
[I 08:51:12.311 NotebookApp] Accepting one-time-token-authenticated connection from ::1


It doesn't open Jupyter in my computer. Can you please tell me why? It opens a web page but I cannot run anything from there
Looks jupyter is already running.
Try http://localhost:8888 in your browser and post the output here

If you use internet explorer, kindly switch to Chrome or Firefox

Else you could create a new folder in C:\
Say C:\Jupyter (the folder could be any name)
Now open cmd and then type cd C:\Jupyter
Now the command prompt would change to C:\Jupyter
Now run jupyter notebook
 

MSN1979

Well-Known Member
#8
Ok its working now in Internet Explorer. I can see results by running them
 

UberMachine

Well-Known Member
#10
Reading files

The most common format is csv. Let's read it. You can download the file here.
Download and put it in the same folder from where you run your jupyter notebook

Python:
import pandas as pd
filename = 'SBIN.csv'
df = pd.read_csv(filename, parse_dates=['Date'])
df
Comments (single line) start with a # in python
I import the pandas library (required for reading files)
You could change the filename to anything. If its somewhere else in your computer, include the entire path
The read_csv function reads the file and parses it into a dataframe for easy analysis. parse_dates indicates that the Date column should be regarded as date

Running the above should give you the following output


Screenshot from 2018-08-07 17-51-23.png
 
Last edited: