Pi Bridge for Amibroker
Pi, our proprietary trading platform is now accessible to everyone who has an account with Zerodha. It has all the features you could expect in a modern trading platform. Check out https://zerodha.com/pi.
Why Pi Bridge?
With Pi we are trying to introduce the concept of coding and backtesting strategies. “Tradescript” the scripting language on Pi is intended for traders who want to code their own trading strategies but don’t know how to program in low-level languages like C, C++, and others. Without a programming language traders can’t develop automated trading systems or perform backtesting of strategies. Tradescript, a vector programming language, offers extreme flexibility with a minimal learning curve. So for example, the code for a 14-candle moving average strategy is Close>EMA(Close,14). Quite simple, right?
Tradescript is good for traders who don’t know programming, and for those who are looking at it as a stepping stone to learn. But for professional traders out there who are adept at coding and run complex trading strategies, Tradescript will not fit the bill. We ran a survey few months back to find out what such advanced traders like to use. The answer was: AFL, Easylanguage, Python, C++, etc. Tradescript is within our trading platform, so executing trades based on signal/alerts is quite easy. But what about traders who are using pure charting, analytic, or programming tools like the ones above which have no trading capability?
This is why we (Zerodha + Tradelab) have decided to build Pi Bridge to give seamless and reliable connectivity from the charting or programming tool to the trading engine on Pi. The plan is, whatever be your front end, we will help you fire orders using the bridge. This will definitely be the first of its kind in India. We started off by enabling the bridge for the most popular request we got – AFL or AmiBroker Formula Language. We also made the bridge compatible Ninjascript (Ninja Trader) and other programming languages like C, C++, C#, Python, R programming etc. Check this link to know more about them.
Following are the details on getting started with Pi bridge for AmiBroker:
Enabling Pi Bridge
You can place a request for the bridge on the Pi page of Q by clicking on Start the Pi bridge trial .
After a 7 day free trial we you will automatically be charged Rs 500/month + taxes (which will be automatically debited from your trading account on the first week of every month). Enabling the bridge might take up to 24 hours from the time you have put in a request.
If you wish to unsubscribe, you can disable access from from Q itself, as manual requests will not be accepted.
Make sure both Pi and AmiBroker applications are closed. Download the installer from Q after you are enabled for the bridge.
Double click on the Pibridgeinstaller.exe file from the installation file for Bridge that you would have downloaded from Q. You should get a message saying Pi bridge installed successfully within a few seconds. Once done you should be able to see pibridge.net.dll in installed Amibroker folder on your computer.
Launch Pi and select Two-way Semi-auto or Mock trading
Make sure you first launch Pi before AmiBroker (every time you want to use the bridge). You can go to View -> User Settings on Pi, and under Pi Bridge choose either Mock , Semi Auto, or Two-way Semi-auto trading (will explain both in a bit).
If everything has happened correctly, as soon as you launch AmiBroker, you should see the Pi Bridge button turn from red to green at the bottom of the Pi screen.
*Even if you don’t see a green light, we have added a small check in the AFL if(!brd.GetConnectionStatus()) brd.Reconnect(); to reconnect the bridge before the order is fired.
Difference between Mock and Two-way Semi-auto trading
When you select mock, orders are logged as a file and this can be used to test and improve your trading system. You can find the log under Pi folder as PiBridge.log; see the image below.
In Two-way Semi-auto trading, the orders instead of getting logged onto a file, get logged into a window called “generated alerts” from where it can be very easily traded.
Firing order from AmiBroker using AFL
To place orders, you have to first create a Pi Bridge object using CreateStaticObject function (AmiBroker function to create a global instance) and place orders using the PlaceOrder function.
API Details –
PlaceOrder(string pExchnge, string pTrdSymbol, string pSymbol, string pUserStrategyName, short
pOrderSide, int pInitialQty, int pDiscQty, double pLimitPrice, double pTriggerPrice, string pOrderType, string
pProdType, string pClientCode, string pValidity)
//COM Calling function:
brd = CreateStaticObject(“pibridge.Bridge”);
//AFL Buy Logic
brd.PlaceOrder(“NFO”, “NIFTY15SEPFUT”, “NIFTY”,”STRATEGYNAME”, 1, 50, 50, 8500.05, 0, “L”, “NRML”,
pExchange = “NFO” or “BFO” or “CDS” or “MCX”
pTrdSymbol = “NIFTY15SEPFUT”
(you could see from Pi Marketwatch Trading symbol columns)
pSymbol = “NIFTY” or “BANKNIFTY” etc. should be within 10 characters
pUserStrategyName = strategy name through which orders are put to distinguish orders from different strategies..
pOrderSide = 1 or2 . 1 for Buy or 2 for sell
pInitialQty = 1 (put actual quantities to trade)
pDiscQty = 0 for default
pLimitPrice = 8500.05 (price in INR for NIFTY15SEPFUT)
pTriggerPrice = 0 price in INR (send non-zero values in case of SL and SL-M order type)
pOrderType = “L”, “MKT, “SL”, “SL-M” for Limit Market, StopLoss and StopLoss Market ordertypes.
pProdType = “NRML” or “MIS” or “CNC”
pClientCode = “DN0005” Your Pi User id like DN0005
pValidity = “DAY” or “IOC”
Here is an example on a simple strategy, sending a buy/sell order every 15 seconds at a predetermined price.
Write the AFL and take it live on AmiBroker
Generated alerts window on Pi
As soon as you take the AFL live, every time your AFL gives a buy/sell, the alert is logged into the generated alerts window (under the Alerts menu on Pi) along with a Buy/Sell Trade button. When you click on this, the buy/sell order window opens with your predetermined product type, quantity, and order type. See the pic below where you will see a buy/sell alert generated every 20 seconds.
Here is a short Video of the Pi Bridge for Amibroker in action:
For all those looking to get started coding on AmiBroker, here is a link to download a database of over 5000 AFLs. Make sure to backtest before taking any strategy live. If you have any coding related queries related to the Pi Bridge you can post it here.
Note: The Pi bridge will work with AmiBroker irrespective of where you source the data feeds from. As a Zerodha client you can get a special offer on real-time data feeds from Neotrade Analytics.