Name: ____________________________
Adm No.: __________________________
Language Name: PRLANG (Pips Risk Language)
PRLANG is a tiny domain-specific language for calculating stop-loss distance and leveraged risk in trading. A user writes a command with symbol, order type, entry price, stop-loss, leverage, and optional lot size. The PRLANG runner evaluates the command and returns the SL distance in price, pips, estimated loss, required margin, and loss as a percentage of margin.
My prompt:
"Give me BNF grammar rules for a tiny trading language that accepts symbol, order type, entry, SL, leverage, and lot size."
AI response (important part):
<command> ::= <symbol> <order_type> <price> "sl" <price> "leverage" <number> ["lot" <number>]
<symbol> ::= "XAUUSD" | "EURUSD" | "GBPUSD" | "USDJPY"
<order_type> ::= "buy" | "sell" | "buy limit" | "sell limit"
<price> ::= <number>
<number> ::= <digit> | <digit> <number> | <digit> "." <digit_sequence>
<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
My prompt:
"Write Python code to parse a command like XAUUSD buy limit 3110.00 sl 3109.95 leverage 100 lot 1 and compute pips and SL-risk metrics."
AI response (important part):
COMMAND_PATTERN = re.compile(
r"^\\s*(?P<symbol>[A-Za-z]{6})\\s+"
r"(?P<order_type>buy\\s+limit|sell\\s+limit|buy|sell)\\s+"
r"(?P<entry>\\d+(?:\\.\\d+)?)\\s+"
r"sl\\s+(?P<sl>\\d+(?:\\.\\d+)?)\\s+"
r"leverage\\s+(?P<leverage>\\d+(?:\\.\\d+)?)"
r"(?:\\s+lot\\s+(?P<lot>\\d+(?:\\.\\d+)?))?\\s*$",
re.IGNORECASE,
)
price_diff = abs(entry - sl)
pips = price_diff / pip_size
loss_quote = price_diff * contract_size * lot
margin_required = (entry * contract_size * lot) / leverageProblem identified:
The first version only handled hardcoded examples and did not read commands from a source file.
My prompt:
"Please update it so I can execute commands from a .prl file, skip comments, and print a report for each line."
AI response (important part):
def run_prl_file(file_path: str):
with open(file_path, "r", encoding="utf-8") as f:
for line_no, raw_line in enumerate(f, start=1):
line = raw_line.strip()
if not line or line.startswith("#"):
continue
result = run_pips_language(line)
print(line_no, result)- It generated a practical parser pattern for command-based trading syntax quickly.
- It provided a clear risk-calculation model linking price distance, pips, margin, and leverage.
- Initial output mode relied on built-in examples instead of processing
.prlinput files. - Validation and usability details (for example comments, empty lines, and CLI flow) needed refinement.
I learned that domain-specific languages are most useful when syntax matches real user tasks. In this project, simple keywords (sl, leverage, lot) made the language readable, while semantic rules produced useful risk metrics beyond basic parsing.
Single inline command:
python pips_risk_language.py --command "XAUUSD buy limit 3110.00 sl 3109.95 leverage 100 lot 1"From a .prl source file:
python pips_risk_language.py pips_demo.prlExample .prl source file: pips_demo.prl