Developing Python Library for Trading Automation

Raj232

Well-Known Member
#41
Whoa!! I think I missed a lot of action... I'm going thru it one by one now.

I've been trying some machine learning stuff lately.
Hi SJerry4U,

instead of storing data to an SQL db, can you push it to Amibroker directly e.g. exporting data to a file (OutPutFile.txt) and importing it thru Amibroker as per below:

boolUpdate = objAmibroker.Import(0, "D:\KITE_AMIB\OutPutFile.txt", "custom1.format")
boolUpdate = objAmibroker.RefreshAll()


Thanks in advance.
 

PreSap

Well-Known Member
#42
Hi All,

Has anyone been able to get realtime data from a datafeed to be used in a python program and then do data processing on these related to Nifty Futures?

Thanks!
 

chintan786

Well-Known Member
#43
Dear All, Nice thread.

I also get interested in Python because Globally kind of work being in FinTechs in Python.
Hope we all try to increase some interest among Indian members.

For Non-programmers and Starts can go through About Quantopian Zipline.

I have created small bolg post abt. installation part:
http://vikkas71.blogspot.in/2017/09/python-quantopian-zipline-installation.html

I am completely non-programmer & non-coder.
 

pannet1

Well-Known Member
#47
some bugs in the scripts is fixed and i am able to get some good results with my programming (simple wrapper around NSEPY). Here is a sample code through which I was able to get EOD option price of Bank Nifty and store it in a trade log (a csv file). I could do several iterations in a month and then check the results.
Code:
from datetime import date, time, datetime, timedelta
import math
from nsepy.derivatives import get_expiry_date
from nse_fno import *
import os
import sys
# -------------------- begin declaration section ------------------

# days between entry and exit
symbol = "BANKNIFTY"
lot_size = 40
# ------------------- end of declaration section --------------------
if len(sys.argv) < 4:
    exit

arg_mon = str(sys.argv[1])
arg_day = str(sys.argv[2])
arg_fun = str(sys.argv[3])


beg_date = date(2016,int(arg_mon),int(arg_day))
beg_time = datetime.combine(beg_date, time())
month_expiry = get_expiry_date(beg_time.year, beg_time.month)

leg_beg_time = next_working_day(beg_time)
weekly_expiry = next_weekly_expiry(leg_beg_time)

if len(sys.argv) == 5:
    base_strike = int(sys.argv[4])   
else:
    future = future_price(symbol, leg_beg_time, leg_beg_time, month_expiry, csvname="future")
    base_strike = (math.trunc(future / 100)) * 100

if arg_fun == "bc":
    monthly = option_price(symbol, beg_time, base_strike, month_expiry, str_trans_type="BUY :", str_option="CE", csvname="monthly")
    monthly *= lot_size
    write_to_csv([beg_date,"BUY", "CE", base_strike,monthly])

if arg_fun == "sc":
    monthly = option_price(symbol, beg_time, base_strike, month_expiry, str_trans_type="SELL:", str_option="CE", csvname="monthly")
    monthly *= lot_size
    write_to_csv([beg_date,"SELL", "CE", base_strike,monthly])

if arg_fun == "bp":
    monthly = option_price(symbol, beg_time, base_strike, month_expiry, str_trans_type="BUY :", str_option="PE", csvname="monthly")
    monthly *= lot_size
    write_to_csv([beg_date,"BUY" ,"PE", base_strike,monthly])

if arg_fun == "sp":
    monthly = option_price(symbol, beg_time, base_strike, month_expiry, str_trans_type="SELL:", str_option="PE", csvname="monthly")
    monthly *= lot_size
    write_to_csv([beg_date,"SELL", "PE", base_strike,monthly])
then you can run the program with arguments date, option type and transaction like this

Code:
python trade_from_shell.py 1 11 bc 26000
Note: I am using BANKNIFTY for testing and hence change the script to NIFTY 50 if you would like to. Also as mentioned I have made a wrapper around NSEPY and its not included in the above code. Its easy for someone to guess what code it will have. If you can't guess then mostly you can't program in python
 
#48
Hello Swapnil

Following are some of the answers to the questions you asked ?

1. Yes algotrading is useful in intraday and many use it for HFT but HFT requires the execution time of the code to be also low.Python can be used for Low frequency trading , intraday trading on a minute based data or seconds also but for nano second and millisecond execution time not the best.Either C++ or Scala will have to be used

2. Need third party news api provider for that.

3. Historical intraday minute level data(real time feeds) have to be purchased from authorized data vendors of NSE like Global Data feeds

4. Here is one strategy written in Python code functioning is explained in comments.It was written in quantopian.If you can try to convert the equivalent code.There are comments in the code denoted by "#".

#Import packages numpy , scipy and pandas dataframe
import numpy as np
from scipy import optimize
import pandas as pd
#Initialize function will run only once when the algorithm starts running.
def initialize(context):
#Define a universe of stocks that the algorithm will trade.Include all Finance , Technology , Energy , Healthcare , Industrial and
context.stocks = [ sid(8554),sid(42950),sid(700),sid(24),sid(36628),sid(5061)]
''' sid(19662), # XLY Consumer Discrectionary SPDR Fund
sid(19656), # XLF Financial SPDR Fund
sid(19658), # XLK Technology SPDR Fund
sid(19655), # XLE Energy SPDR Fund
sid(19661), # XLV Health Care SPRD Fund
sid(19657), # XLI Industrial SPDR Fund] # XLU Utilities SPRD Fund'''
#Define maximum leverage to which algorithm can accelerate
context.leverage_buffer = 2.45
#Define number of days to lookback which retrieving price and volume history
context.lookback = 30
#Stop the algorithm when loss > value*100 in percentage
context.stop_portfolio = 0.10
#Define a dictionary to store whether a particular instrument has to be bought or sold
context.B_OR_S = {}
#Store true if any position is taken in a particular stock or not false means no positions
context.invested = {}
#Store
context.stop_price = {}
context.stop_pct = 0.95
#This is a recurring function call which runs the function "monthly_rebal" every week start 3 minutes before market close
schedule_function(monthly_rebal,
date_rules.week_start(),
time_rules.market_close(minutes=3))
#Run a loop which sets default values of Buy or Sell , invested or not and stop price
for stock in context.stocks:
context.B_OR_S[stock] = False
context.invested[stock] = False
context.stop_price[stock] = 0.0

#This function runs every time the datafeed comes.If the datafeed is daily then function is called daily at market open
def handle_data(context, data):
leverage = context.account.leverage
record(levrage = context.account.leverage)
record(equity = context.portfolio.cash)
#Check existing open positions everytime datafeed comes to set a trailing check for the positions.Exit positions
set_trailing_stop(context, data)
#Iterate stock list to check existing positions and exiting sell positions if leverage greater than max leverage
for stock in context.stocks:
if stock in data:
if leverage > context.leverage_buffer:
if context.invested[stock] == True and context.B_OR_S[stock] == False:
log.info("Liquidating position %s" % (stock))
order_target_percent(stock, 0)
context.invested[stock] = False
return

def monthly_rebal(context, data):
#Store no of stocks in the universe
l = len(context.stocks)
#If no stocks exit
if l == 0:
return
#else if there are stocks calculate weight of each stock by dividing by number of stocks
else:
weight = 1.0/l
#Traverse list of all stocks
for stock in context.stocks:
if stock in data:
#Fetch close prices of the particular stock denoted by "stock" with number of days as context.lookback value
close = history(context.lookback,'1d', field='close_price')[stock]
#Store the maximum close price of the stock in the specified period
max_high = np.max(close)
#Store the minimum close price of the stock in the specified period
min_low = np.min(close)
#Store the current price of stock in variable
cur_p = data[stock].price
#Calculate the mid point of maximum high close and minumum low close
mid_range = (max_high+min_low)/2
#Calculate the mean of mid range and maximum high
upp_limit = (max_high+mid_range)/2
#Calculate the mean of mid range and minimum low
low_limit = (min_low+mid_range)/2
#If current price of instrument > upper limit check cash available for trade and sell
if cur_p > upp_limit:
portfolio_check(context,data,stock,weight,1)
#If current price of instrument < lower limit check cash available for trade and buy
elif cur_p < low_limit:
portfolio_check(context,data,stock,weight,0)

def portfolio_check(context,data,stock,weight,trig):
#Store available free cash to place orders
equity = context.portfolio.cash
#Starting portfolio value
portfolio_size = context.portfolio.portfolio_value
#Portfolio value at which the algorithm will stop if free cash reaches below it
stop_portfolio = (1.0 - context.stop_portfolio)*portfolio_size
#Stop if equity reaches portfolio value is lower is than stop_portfolio
if equity < stop_portfolio:
return
#If there is no position in the stock and sell signal generated place buy order
if context.invested[stock] == False and trig == 1:
# Buy stock with weight by allocating 1/len(stock_universe) portfolio in percent.
# If there are 10 stocks in the universe allocate 1/10 th of the portfolio to buy the stock
order_target_percent(stock, -weight)
# Set invested or not to true
context.invested[stock] = True
#If there is no position in the stock and buy signal generated place sell order
elif context.invested[stock] == False and trig == 0:
order_target_percent(stock, weight)
# Set invested or not to true
context.invested[stock] = True

def set_trailing_stop(context, data):
# retrieve all the open orders and log the total open amount
# Add this guard to prevent over-ordering
open_orders = get_open_orders()
# open_orders is a dictionary keyed by sid, with values that are lists of orders.
if open_orders:
# Traverse all the open orders every time a new data feed comes
for security, orders in open_orders.iteritems():
# Traverse the open orders each specific open order denoted by oo
for oo in orders:
#Store the price at which open order was executed
p = oo.price
#Store the amount of stock bought or sold
am = oo.amount
#Get the symbol of the stock of which open order is placed
stock = security.sid
#Get the current price of stock in open order
cur_price = data[stock].price
#Trailing stoploss 0.1 indicates 10 percent
trailing_stop = context.trailing_stop
#If amount of stock > 0 it is bought and if current price is also greater than 1.1*price of purchase exit positions
if am > 0 and cur_price > (p+p*trailing_stop):
order_target_percent(stock,0)
#If amount of stock < 0 it is sold and if current price is lesser than 0.9*selling price exit positions
elif am < 0 and cur_price < (p-p*trailing_stop):
order_target_percent(stock,0)



sir,

I used to trade intraday in crude oil with sma5 and sma8 crossover strategy sir.Could you please give me sample code to zerodha kite in python to automate my strategy sir.
Thanks in Advance,
Radha
 
#49
How can i get the bhav copy for fno data, in get_price_list function in history.py module file it takes in default as segment="EQ", so can we specify a different segment and how would you do that for FNO data in bhav copy. Please suggest
 

pannet1

Well-Known Member
#50
How can i get the bhav copy for fno data, in get_price_list function in history.py module file it takes in default as segment="EQ", so can we specify a different segment and how would you do that for FNO data in bhav copy. Please suggest
I have created a wrapper function then i call this in my actual back testing programme.

Python:
from nsepy import get_history
from nsepy.derivatives import get_expiry_date
import pyexcel as pyexcel
from datetime import datetime, time, timedelta
import math

# some code left here for brevity

# wrapper function to find the base strike price and future price (approx)
def future_price(symbol,beg_date,end_date,expiry_date,csvname="tmp",str_trans_type="NA",bln_index=True, bln_futures=True):
    """Obtain price of future from NSEpy, save it to CSV and retreive it."""
    filename = csvname+".csv"
    if bln_futures:
        csvname = get_history(symbol=symbol,
                            start=beg_date,
                            end=end_date,
                            index=bln_index,
                            futures=bln_futures,
                            expiry_date=expiry_date)
    else:
        csvname = get_history(symbol=symbol,
                            start=beg_date,
                            end=end_date,
                            index=bln_index,
                            futures=bln_futures)

    csvname.to_csv(filename)
    sheet = pyexcel.get_sheet(file_name=filename)
    fut_price = sheet[1, 8]

    if(str_trans_type=="BUY :"):
        fut_price *= -1

    beg_date = datetime.date(beg_date)
    fut_price=math.trunc(fut_price)
    print(beg_date,str_trans_type,"Future at",fut_price)
    if(str_trans_type=="NA"):
        return fut_price
    else:
        print(beg_date,str_trans_type,"Future at",fut_price)
        return fut_price;