Developing Python Library for Trading Automation

#21
Well, NEST offers APIs. We can get our orders fired from AmiBroker (most are using it), for which you'd need to confirm before sending the order to the exchange.
http://www.globaldatafeeds.in/trading_plugins.html
https://plus.omnesysindia.com/NestPlus/download/NestPlusAPI.pdf

If one has a dealer terminal i.e. they are registered as an authorised person, they won't need the confirmation, the order would be placed automatically. But, for client terminals, they need to confirm the order. We need that UI automation to be done. PyWinAuto/SWAPY should do it, I guess.
Looking at this, it seems that it is possible to write plugins for amibroker and fire orders directly to kite, but one needs to know visual C++, which I dont have much of experience. I've managed to install it and taken few baby steps.

So its not anytime soon, unless someone with VC++ comes along. Meanwhile I'm writing and back-testing strategies in python (life is much much easier here)
 

NJ23

Well-Known Member
#22
Looking at this, it seems that it is possible to write plugins for amibroker and fire orders directly to kite, but one needs to know visual C++.
You can write your code in Python and we can convert it into C/C++ using Cython or whatever.

Meanwhile I'm writing and back-testing strategies in python (life is much much easier here)
That's true. Flexibility is there. Which libraries are you using to backtest? Zipline is good, I suppose.

BTW, R has pretty robust set of packages available for backtesting, specially options strategies. R isn't a lot different from Python.
 
#23
I'm trying zipline but I wrote a small simulator as well (very basic not so fancy).

Regarding C++, still need to experiment how to use dll with python etc..
 

revendar

Well-Known Member
#24
Im thinking on investing some time in exploring zipline.io.
I'm not comfortable with python, but an experienced programmer.
Have a few questions. Does zipline has most of basic indicators integrated?
I'm interested in EMA, RSI, stochastic. Do we have 5min data for indian markets?
If so for how long. Can someone post some sample python client to integrate with zipline for indian scrips, so that I can try out.
 
#25
Zipline and even quantopian do not provide technical analysis out of box. But hey its Python and we have lot of free stuff. There's an opensource library for technical analysis called TA-Lib. which provides all the indicators you mentioned.
Even Zipline and Quantopian have support for TA-Lib. Here's an example
https://www.quantopian.com/help#sample-talib

It uses yahoo finance to get stock data so stock symbols will become SBIN.NS, RELIANCE.NS and this should work as expected, just replace AAPL with SBIN.NS or SYMBOL.NS in below example

import pytz
from datetime import datetime

from zipline.algorithm import TradingAlgorithm
from zipline.utils.factory import load_bars_from_yahoo

# Load data manually from Yahoo! finance
start = datetime(2000, 1, 1, 0, 0, 0, 0, pytz.utc)
end = datetime(2012, 1, 1, 0, 0, 0, 0, pytz.utc)
data = load_bars_from_yahoo(stocks=['AAPL'], start=start,
end=end)

# Define algorithm
def initialize(context):
pass

def handle_data(context, data):
order(symbol('AAPL'), 10)
record(AAPL=data[symbol('AAPL')].price)

# Create algorithm object passing in initialize and
# handle_data functions
algo_obj = TradingAlgorithm(initialize=initialize,
handle_data=handle_data)

# Run algorithm
perf_manual = algo_obj.run(data)

So from my experience, if you are starting with Python for data analysis and stuff like that-
1. Install Anaconda (it bundles numpy, scipy libraries required for analysis) if you are using a Windows machine as installing these libraries by yourself is a pain. No problem if you are using Linux, just ensure that you have GCC compiler and build utilities installed.
2. Python is really easy. It'll just take few days to get basics clear, advanced usage will become clear when you start coding
3. After getting a bit of hands on with Python, play with "Pandas" library for data analytic, this is a basic building blocks for analytic in python

I'll try to post a zipline example with Indian stocks
 

revendar

Well-Known Member
#26
Thanks bro. I have explored a little bit if TA-lib with Perl.
Will check the same. Hope you have tried it extensively. How is the performance if we backtest huge data with TA lib?
Also are there any open source libraries/algorithms which can find the channels, triangles, etc.
 

TradeOptions

Well-Known Member
#27
Pi bridge – gateway to trade using other programs -

Pi bridge, which earlier had capability of only accepting orders via AmiBroker is now enabled for two-way communication (firing orders, and getting response in terms of order updates, positions, holdings, and more) not just for AmiBroker but from any programming languages such as C#, PERL, Python and more.


http://zerodha.com/z-connect/tradezerodha/pi-bridge/pi-bridge-gateway-to-trade-using-other-programs

That page has got some links, which might be helpful, so please have a look.

Thanks and regards
 
#28
quoting from the above link-
Everything explained above is semi-automated, that means there will need to be a human intervention to place an order.
This one is fully automatic without any intervention-
https://github.com/swapniljariwala/zerodha
But the above library does not support any charting software yet, you have to rely on python and C libraries only, (there are number of good libraries like scipy, pandas (analytics on data series), skit-learn (machine learning), TA-Lib (Technical analysis))
 
#29
Hello Sjerry

Quantopian doesn't use data from yahoo.It is getting the price feeds from Nanex.I have built some trading strategies using www.quantopian.com.They are not good for HFT(High Frequency Trading) only for LFT.The customization possible in python are good.Quantopian has recently launched pipe also makes it lot easier to trade.

Just a question have you ever tried automate using the (Interactive Brokers API) IbPy though it is not official.Also have you tested the automation code on a real account in zerodha ?
 
Last edited:
#30
I've used my library - Zerodha github to fully automatically place buy/sell orders in stocks/derivatives in my zerodha account (both in MIS and CNC)
I usually just run my python script from my laptop and the order is placed (i get order ID acknowledgment) in anywhere between 0.5 secs to 1.5 sec max over my broadband connection. I did not try from my AWS server, but it would be similar considering the distance. If you want still lesser time then you can happily go for co-location :p , I hope this would serve most of our purposes :D
So here are some issues I'm facing-
1. I'm good at web-scraping and hence writing a library which can do such automatic execution, but not good at making profitable strategies. So I dont have any live system running.
2. The current code just works, Its not tested rigorously for extreme situations and currently I'm more in a process of learning and backtesting strategies, so expanding and proving the auto-execution library is on hold, because I dont have any incentive to develop this if I do not see any outcome and moreover I'm a married man with a day job :D :p so its really not that easy to find the time.
3. There's still a legal issue with running only approved strategies.
4. I would still be happy to contribute if people are using and benefiting from this library and in process it will be tested in the live system and eventually bugs get reported and corrected. But again there's a trust issue, why would someone put their money at risk with free unofficial library without support. I would still rely on enthusiasts like you and me though ;). You can read the whole code as it is open and there's no gimmick to steal your password :D
5. There's a risk that Zerodha may change the way their website works. If they introduce something like captcha for confirming order placement, then all our efforts are in vain and this is very likely as they are targeting institutions with their API interface

So you can go ahead and try out the example that I've posted on Readme page, putting a limit order of say 1 SBIN share wont cost you anything or at around rs 240 at max if you are unlucky :p, But still - Be aware that this exercise should be on your own risk.

Edit-
Again using webscraping, I can get quotes from sources like Zerodha and ICICIDirect which are realtime (If you call 1 min quotes without delay "realtime" then I can do much better because Brokers can provide data at higher frequency.) I could only get last traded price info from Zerodha, but from ICICI I could get last traded, volume and even market depth etc.
Even then integrating these sources with Zipline would be a big task and still it does not support execution frequency less than 1min, So I would rather not use it or else develop some simpler backtesting framework with greater freedom
 
Last edited: