Python primer for Trading

MSN1979

Well-Known Member
#51
i want to find out duplicate stocks in 2 Different columns
I want duplicate stocks to be printed
I want unique stocks to be printed
Also Lets say I would like price columns to be picked up with stock name

Example

Stock list 1
HCL
ICICI
ITC
ZEE
COLPAL
INFY
TCS
HDFC
Britannia

Stock List 2

HCL
ICICI
ITC
ZEE
COLPAL
INFY
axy
xyz
abc
TCS
HDFC
Britannia
 
#52
A few more code examples to try.

Python:
import pandas as pd
filename = 'cm13AUG2018bhav.csv.zip' # Replace with your filename
df = pd.read_csv(filename, parse_dates=['TIMESTAMP'])  # Replace Date with the date column in your file[/SIZE][/SIZE]

[SIZE=5][SIZE=5]# Calculate one-day returns
df['RET'] = (df['CLOSE']/df['PREVCLOSE'])-1[/SIZE][/SIZE]

[SIZE=5][SIZE=5]# Get all stocks with open equals high price
df.query('OPEN == HIGH')[/SIZE][/SIZE]

[SIZE=5][SIZE=5]# Get all stocks with open equals low price
df.query('OPEN == LOW')[/SIZE][/SIZE]

[SIZE=5][SIZE=5]# Get all stocks with returns greater than 15 percent
df.query('RET > 0.15')[/SIZE][/SIZE]

[SIZE=5][SIZE=5]# Get all stocks with close price greater than 10000
df.query('CLOSE > 10000')[/SIZE][/SIZE]

[SIZE=5][SIZE=5]
And finally a compound query to try
Get all stocks with more than 10 percent returns and close price greater than 100
Python:
df.query('(RET > 0.1) & (CLOSE > 100)')
And as a bonus, you can make your condition as a string. Leave this if this is confusing
Python:
cond = '(RET > 0.1) & (CLOSE > 100)'
df.query(cond)

Awesome.. Everything is clear till here.. Can't wait for more this is going to be interesting. Just a quick question here.. How to access all the queries we have defined here. IDLE does not shows them.. Does this require to learn SQL too? If yes then would you mind providing the link to learn the basics of SQL.
 

UberMachine

Well-Known Member
#53
A few more examples to try
This is just a repetition of what is already posted without the size


Python:
import pandas as pd
filename = 'cm13AUG2018bhav.csv.zip' # Replace with your filename
df = pd.read_csv(filename, parse_dates=['TIMESTAMP'])  # Replace Date with the date column in your file

# Calculate one-day returns
df['RET'] = (df['CLOSE']/df['PREVCLOSE'])-1

# Get all stocks with open equals high price
df.query('OPEN == HIGH')

# Get all stocks with open equals low price
df.query('OPEN == LOW')

# Get all stocks with returns greater than 15 percent
df.query('RET > 0.15')

# Get all stocks with close price greater than 10000
df.query('CLOSE > 10000')
 

UberMachine

Well-Known Member
#54
Awesome.. Everything is clear till here.. Can't wait for more this is going to be interesting. Just a quick question here.. How to access all the queries we have defined here. IDLE does not shows them.. Does this require to learn SQL too? If yes then would you mind providing the link to learn the basics of SQL.
Its not SQL. Its just simple comparisons we would use in excel.
 

lvgandhi

Well-Known Member
#55
Is there any possibility to download intra-day data from nest or now and chart it with python? If so will it be faster than the RTD by Josh1?
 

UberMachine

Well-Known Member
#56
Is there any possibility to download intra-day data from nest or now and chart it with python? If so will it be faster than the RTD by Josh1?
Techincally, you can use the kite connect API to download real time data for a select number of instruments (I am successful implementing it).
But creating and updating your own charts for live trading is a bit too much complex and I see the existing tools are good enough.
Is there any specific use case for implementing this?
 

lvgandhi

Well-Known Member
#57
Techincally, you can use the kite connect API to download real time data for a select number of instruments (I am successful implementing it).
But creating and updating your own charts for live trading is a bit too much complex and I see the existing tools are good enough.
Is there any specific use case for implementing this?
Thanks for the response.
I am ok with RTD from Josh. just wanted to know whether charting using Python will be faster. Further for those who don't have amibroker, it will be useful.
 
#58
A few more examples to try
This is just a repetition of what is already posted without the size


Python:
import pandas as pd
filename = 'cm13AUG2018bhav.csv.zip' # Replace with your filename
df = pd.read_csv(filename, parse_dates=['TIMESTAMP'])  # Replace Date with the date column in your file

# Calculate one-day returns
df['RET'] = (df['CLOSE']/df['PREVCLOSE'])-1

# Get all stocks with open equals high price
df.query('OPEN == HIGH')

# Get all stocks with open equals low price
df.query('OPEN == LOW')

# Get all stocks with returns greater than 15 percent
df.query('RET > 0.15')

# Get all stocks with close price greater than 10000
df.query('CLOSE > 10000')
What is to be done in case of running multiple queries in single line ?

e.g. OPEN=CLOSE and CLOSE>3000
 

UberMachine

Well-Known Member
#60
i want to find out duplicate stocks in 2 Different columns
I want duplicate stocks to be printed
I want unique stocks to be printed
Also Lets say I would like price columns to be picked up with stock name

Example

Stock list 1
HCL
ICICI
ITC
ZEE
COLPAL
INFY
TCS
HDFC
Britannia

Stock List 2

HCL
ICICI
ITC
ZEE
COLPAL
INFY
axy
xyz
abc
TCS
HDFC
Britannia
A bit of math. Remember sets.
Say, your stock list 1 as S1 and stock list 2 as S2
Duplicate stock mean stock found in both sets - INTERSECTION
Unique stock mean stock found in either one of the sets - UNION
Python has a build in set command to do this
Python:
# Assume you have named your dataframes df1 and df2

stock_list_1 = df1['symbol']
stock_list_2 = df2['symbol']
S1 = set(stock_list_1)
S2 = set(stock_list_2)

S1.union(S2) # List of unique stocks
S1.intersection(S2) # List of duplicate stocks
If your stock is simply a list just replace stock_list_1 and stock_list_2 with your list