AFL Coding Guide Algo Trading

NOTE: You should first study the pre-built AFL templates before writing your own. You can get them here:

You can write advanced automated trading code in Amibroker itself for custom order management, position management, and risk management. One single function “AlgoJi_Signal()” is sufficient to place custom orders across all brokers for both APIBridge and Robo.

AlgoJI_Signal() takes a total of 9 parameters, but only 4 are mandatory. All parameters should be passed as string.

Syntax: Algoji_Signal(ID,Type,Symbol,OrderType,TrigPrice,Price,Qty,InstrumentName,StrategyTag)

• 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. If you want to send strike/expiry also through Amibroker, you can combine it in syntax “Symbol | Expiry | Strike | OptionType”
• OrderType (optional): can be L, M, SLL or SLM
• TrigPrice (optional): should be 0 or Null for market/limit orders. If you want to send extra parameters for OCO order, you can combine it in syntax “TrigPrice | StopLoss | SquareOff | TrailingTicks”
• 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
• StrategyTag (optional): It can be any string to identify which particular Amibroker strategy is sending the signals.

Example 1: Send Market Order in Equities to create Short position:
AlgoJi_Signal(“8”, “SE”,”SBIN”,”L”,””,””,”1″,”EQ”,”STAG_1″);

Example 2: Send Stop Limit Order in Equities to create Short position:
AlgoJi_Signal(“8”, “SE”,”SBIN”,”SLL”,”302.50″,”302″,”1″,”EQ”,”STAG_1″);

Example 3: Send Stop Market Order in Equities to create Long position:
AlgoJi_Signal(“8”, “LE”,”SBIN”,”SLM”,”302″,”302.50″,”1″,”EQ”,”STAG_1″);

Example 4: Send OCO Order in Equities for Long trade:
AlgojiBroadcastSignal(“6”, “LE”,”SBIN”,”SLL”,”302|1.5|3″,”302.10″,”1″,”EQ”,”STG1″);

Example 5: Send Limit Order in BankNifty Futures to create Short position:

Example 6: Send Limit Order in BankNifty Options to exit Long position:

Example 7: Send Market order in Currency Futures to exit Long position:

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:

  1. 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.
  2. 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.

Testing & Debugging:

  1. Login to APIBridge. Login again to your upstox account.
  2. Go to Tools->Application Settings and verify all settings are correct.
  3. Go to Settings window in APIBridge and check that stock has been added with proper parameters.
  4. Go to Amibroker->Tools->Formula Editor. This will open new blank AFL file.
  5. Copy only the function call you want to test. Example: AlgojiBroadcastSignal(“5”, “LE”,”SBIN”,””,””,””,”1″,”EQ”,””,””,””,””,””);
  6. In AFL Editor, click Tools->Verify Syntax. This will execute function call and order will be placed.