AlgoJi APIBridge documentation contains setup instructions and sample code with explanation. We have primarily used Amibroker in setup example; however, for Excel, Python or MT4 integration, contact us through email.
Table of Content
- 1 APIBridge Features
- 2 APIBridge System Requirements
- 3 APIBridge Setup Instructions for Amibroker
- 4 APIBridge Mock Trade Settings
- 5 Trading Without Third-party Platform
- 6 APIBridge AFL Coding Guide
- 7 Coding Guide for LE, LX, SE, SX
- 8 Amibroker Troubleshooting:
- Supported APIs: Zerodha Kite API, Upstox API, Interactive Brokers, Sharekhan, Omnesys API
- Integration with Amibroker, Excel, Python, MT4 etc
- Allows trading for Multi-Strategies in Multi-Timeframe and Multi-Markets in Multi-Client Account
Allows Multi-Order Types: L, M, SL-L, SL-M, BO, CO etc.
- Send custom order quantity for each trade based on your risk management rules
- Automatically select Multi-Strikes through your code for options trading
- Automatically select multi-expiry contracts through your code for options trading
- Trade using charts or without charts through scanner
- Does not needs VPS, reconnects to internet after internet interruption
- Custom Target, SL, time-based signals etc. for your code
- Maintain independent cycle of Long Entry, Long Exit, Short Entry and Short Exit signals.
Limitless possibilities with custom development…
APIBridge System Requirements
- Amibroker 32-bit. Any version upwards from 5.50 will work. Amibroker should be connected with live data.
- VC++ runtime environment 2015 (32 bit). Windows-10 has this pre-installed, or you can install fresh from: https://www.microsoft.com/en-in/download/details.aspx?id=48145
- Upstox API: Login to upstox developer https://developer.upstox.com/#/login ->
Click Create New App -> App Name: anything you want, Redirect Url: https://upstox.com/
Subscribe to Interactive API. Once you click create, you will get API key and secret.
For Zerodha API or Omnesys API setup instructions, contact your administrator
- APIBridge integrated AFL.
If any of above 4 items are missing, you will not be able to start auto trading.
APIBridge Setup Instructions for Amibroker
- This software does not require installation! Simply download zipped folder, and extract it. Right-click on APIBridgeLauncher.exe -> Send To->Desktop (create shortcut)
- Login with your APIBridge ID
- Go to Tools->Application Settings. Under API, put your upstox api key, secret and redirect url. Important: make sure you copy-paste these settings correctly, or it will not work. Click Save after filling up this information
- Under Amibroker tab, give the installation path for Amibroker. Usually this is C:\Program Files (x86)\AmiBroker
- Login to APIBridge and Start Amibroker.
- In Amibroker, go to Tools->Preferences->Intraday. Check the option START time of interval (recommended). Click Apply.
- In your strategy AFL from which you want to fire signals, append the code autotrade.afl
That’s all! these are one-time settings only.
APIBridge Mock Trade Settings
– Before trading live, mock trading is recommended to ensure everything is working correctly. Mock trading should be done after market hours to ensure no orders are actually placed on exchange. Example for NSE after 3:30pm
– You can trade through charts or through scanner (without using charts)
– Mock Trading after market hours, or during exchange mock trading session is preferable to Paper Trading mode. In Paper Trading mode, actual orders are not sent to exchange. There is semi-automatic mode also available which can be used as required.
Mock Trading through charts:
- Setup multi-chart layout just as you would like to trade in live market. Ensure that APIBridge integrated AFL is used on each chart, and that chart parameters are correctly set.
- Under the settings tab in APIBridge, add scrips with correct settings for trading
- Click ‘Start Trading’ button in APIBridge
- Start bar-replay in amibroker. The step-interval should the minimum interval your data vendor allows- typically 1-minute. You can increase the Speed (steps/sec) to 5.
Mock Trading through Scanner:
- Setup scanner with correct settings. Refer article: https://algoji.com/auto-trading-amibroker-scanner/ . You can select date range to trigger signals on particular date.
- Ensure that all stocks are added under APIBridge Settings, and Start Trading
Live Trade Settings:
– If there is no issue in mock trading, you can move forward to live trading. Amibroker will automatically trigger signals as per new data
Trading Without Third-party Platform
Trading strategies which are based directly on Price or Quantitative Analysis (and which do not require charting), can be programmed directly in APIBridge without using a third-party commercial software such as Amibroker, Excel or MT4. For such development requirement contact us https://algoji.com/trading-strategy-development/
APIBridge AFL Coding Guide
Use the Amibroker Autotrade AFL Toolkit for set of pre-built code snippets.
all parameters should be passed as string.
- ID (mandatory): unique id for each signal sent via Amibroker. If two signals have the same id, the status for the respective order placed may be wrong.
- Type (mandatory): can be either LE, LX, SE or SX. LE: Long Entry, LX: Long Exit, SE: Short Entry, SX: Short Exit
- Symbol (mandatory): symbol name which will be matched to Input Symbol in API Bridge
- OrderType (mandatory): can me L, M, SLL or SLM
- TrigPrice (optional): should be 0 or Null for market/limit orders
- Price (optional): should be 0 or Null for market orders
- Qty (optional): Order Quantity
- InstrumentName (mandatory): For upstox API, it should be any of the following EQ|FUTIDX|FUTSTK|OPTIDX|OPTSTK|FUTCOM
- Strike (optional): used only for options trading. Eg. 10500.00
- StopLoss (optional): for only OCO/BO. It should be absolute value from Price. Eg. if OCO order price is Rs. 100, SL is 98, then this value should be 2
- SquareOff (optional): for only OCO/BO. It should be absolute value from Price. Eg. if OCO order price is Rs. 105, TGT is 105, then this value should be 5
- Trailing Ticks (optional): For Trail SL in OCO/BO.
- StrategyTag (optional): It can be any string to identify which particular Amibroker strategy is sending the signals.
- Port: default port is 300001. You need NOT change unless facing issues.
Example 1: Send Market Order in Equities:
For this signal to execute, APIBridge settings windows should have the stock SBIN with defined ordertype.
Example 2: Send Stop Limit Order in Equities
Example 3: Send Stop Market Order in Equities
Example 4: Send OCO Order in Equities
Example 5: Send Market Order in Nifty Futures
Example 6: Send Limit Order in Nifty Options
Note: Option Strike should be called without decimals if it does not have fractional part.
Testing & Debugging:
- Login to APIBridge. Login again to your upstox account.
- Go to Tools->Application Settings and verify all settings are correct.
- Go to Settings window in APIBridge and check that stock has been added with proper parameters.
- Go to Amibroker->Tools->Formula Editor. This will open new blank AFL file.
- Copy only the function call you want to test. Example: AlgojiBroadcastSignal(“5”, “LE”,”SBIN”,””,””,””,”1″,”EQ”,””,””,””,””,””);
- In AFL Editor, click Tools->Verify Syntax. This will execute function call and order will be placed.
Coding Guide for LE, LX, SE, SX
APIBridge takes 4 types of signals, LE: Long Entry, LX: Long Exit, SE: Short Entry, SX: Short Exit. Please note:
- Exit signal is matched for initial Entry signal coming from the same strategy. If no Entry position was taken, the Exit signal is ignored. If, entry was taken for quantity Q, then exit order is also fired for same quantity Q. This is to prevent any code misfiring.
- For scale-in, scale-out strategies, you should fire signals with only LE and SE (do not use LX or SX). This is a simple way to avoid conditional checks applied by APIBridge.
Note that all Amibroker related issues are independent of APIBridge, or it’s plugin. These issues should be identified/resolved without integrating with APIBridge.
How to Check repainting/fake signals.
To check fake signals, add plotALLsignals.afl from the AFL Toolkit
Many a times one may see that a buy / sell signal appears on the chart, and vanishes before the completion of the candle. This happens when the conditions for buy & sell are satisfied, but changes before the closing of the current candle.
This occurrence and then vanishing of the signal is called re-painting.
In some cases, repainting can occur in other forms as well, for example those who use the zigzag indicator to identify the highs & lows or any such condition, then a buy / sell signal may appear a few candles before the current candle and even that signal may not sustain, as it has the tendency to shift itself as per price movement.
Let us understand this with the help of an pictorial example. To check repainting, you need to bar-replay with minimum time interval. Example, bar-replay with 1-min time interval even if the chart time frame is 15-minute.
Is some other cases of repainting, signal will appear on previous/historical candle, but not on current candle.
(Optional) In Amibroker top menu click Windows->Log. Then click on Trace tab and right click to enable Internal Outputs. You can bar-replay AFL and check whether all signals are displayed properly in Trace output.
Signal comes in Amibroker but no trade placed
Check the following settings one-by-one
- Amibroker chart parameters or scanner parameters should show the correct market segment
- Scrips should be added in APIBridge with correct settings
- Start Trading button should be turned on
- If all above settings are correct, problem is with AFL code. 90% of AFLs found over internet have repainting/fake signals. (separate short tutorial- how to check repainting signals)
Order placed but no signal in Amibroker
This can happen only if AFL has fake signals. The actual signals triggered by AFL may be different then the signals plot on chart. To check signals, use the AFL plotALLsignals.afl with bar-replay (download from AFL Toolkit)
Wrong price sent in order
APIBridge picks the price defined from AFL in the variables bp/sp etc.. If these are not defined properly it may pick wrong price. It is recommended to use market orders if price is not defined properly in AFL.