Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Building GenAI Applications. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

Using r”” Raw Strings in Amibroker for Algo Trading – Amibroker Tutorial

2 min read

Starting from AmiBroker 6.41.0 BETA, raw string literals (r"" and r'') were introduced to the AFL language. This powerful feature makes life much easier when you’re working with automated trading systems that involve HTTP requests, JSON payloads, or file handling.

If you’ve struggled with escaping quotes, backslashes, or newline characters inside strings, raw strings are here to make your AFL coding cleaner and error-free.

What Are Raw Strings?

Raw strings allow you to write strings without worrying about escape sequences. That means:

  • No need to double backslashes (\\)
  • No need to escape quotes (\")
  • No surprise behavior from \n, \r, \t, or similar characters

In short, everything inside a raw string is taken literally.

A raw string starts with the letter r followed by a normal string in either double or single quotes:

path = r"C:\Trading\Logs\order.json";
json = r'{"symbol": "SAIL", "action": "BUY"}';

Why Raw Strings Matter in Algo Trading

In automated trading, we often build JSON payloads to send to a broker’s order API. These payloads are full of quotes, colons, commas, and sometimes newline sequences. Writing this manually with proper escaping becomes messy.

Here’s a sample JSON for placing an order:

{
  "apikey": "34fr3edr45hyt5",
  "strategy": "Test Strategy",
  "symbol": "SAIL",
  "action": "BUY",
  "exchange": "NSE",
  "pricetype": "MARKET",
  "product": "MIS",
  "quantity": "1"
}

Using raw strings in AFL, you can store this as:

data = r'{"apikey": "34fr3edr45hyt5", "strategy": "Test Strategy", "symbol": "SAIL", "action": "BUY", "exchange": "NSE", "pricetype": "MARKET", "product": "MIS", "quantity": "1"}';

This is much cleaner than manually escaping every quote and colon.

Examples for Algo Trading

  1. JSON payload for placing an order:
payload = r'{"symbol": "SAIL", "action": "BUY", "quantity": "1"}';
  1. JSON market quote response:
quote = r'{"ltp": 863.65, "volume": 14520568, "status": "success"}';
  1. HTTP headers:
headers = r"Content-Type: application/json\r\nAccept-Encoding: gzip, deflate\r\n";
  1. Log file path:
logfile = r"D:\AmiBroker\AlgoLogs\orders.txt";
  1. API endpoint:
url = r"http://127.0.0.1:5000/api/placeorder";
  1. Mixed dynamic string building using variables:
symbol = "SAIL";
action = "BUY";
qty = 1;

json = r"{" + 
       "\"symbol\": \"" + symbol + "\", " +
       "\"action\": \"" + action + "\", " +
       "\"quantity\": \"" + NumToStr(qty, 1.0) + "\"" +
       "}";

Do’s When Using Raw Strings in AFL

  • Do use r"" when you’re dealing with file paths, HTTP headers, or JSON blocks.
  • Do use r'...' if your content has double quotes inside.
  • Do build dynamic JSON using raw strings and string concatenation if needed.
  • Do keep raw strings to a single line. If your content is multiline, split it and join using +.

Don’ts When Using Raw Strings

  • Don’t escape quotes or backslashes inside a raw string. For example, r"This \"will\" fail" will throw an error.
  • Don’t use multiline blocks inside a raw string. Raw strings in AFL must be single-line.
  • Don’t assume \n will break a line. In raw strings, \n is treated as literal text — it won’t create a newline.

Final Thoughts

The addition of raw string support in AFL is a quality-of-life improvement, especially for developers building systems that interact with external services. Whether you’re placing orders through a REST API, logging to files, or working with JSON responses, raw strings will make your AFL code cleaner, more readable, and less error-prone.

If you’re running AmiBroker 6.41.0 BETA or later, take advantage of this feature in all your automated trading scripts.

Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Building GenAI Applications. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More