If you’ve been writing trading algorithms for a while, you’ve probably run into the classic "works on my machine, fails on another broker" scenario. One of the most common—and fatal—mistakes I see in community code and commercial EAs is hardcoding pip, point, or tick values.
When you are scalping, precision is everything. A single decimal place error can turn a 5-pip tight stop loss into a 50-pip disaster or a 0.5-pip micro-stop that triggers instantly.
Let's break down why hardcoding is dangerous and look at the proper ways to handle dynamic pip calculations across MetaTrader, cTrader, and TradingView.
The Trap: Why Hardcoding Breaks Your Code
Many beginners write code that assumes a fixed decimal structure for currency pairs. It usually looks something like this:
The Bad Way (Do NOT do this):
Code: Select all
// Assuming a 4-digit broker or a non-JPY pair
double stopLossPrice = Ask - 0.0050; // Hardcoding 50 pips (or 500 points)4-Digit vs. 5-Digit Brokers: If you move from a traditional 4-digit broker to a 5-digit broker, your 50-pip stop loss suddenly becomes a 5-pip stop loss.
JPY Pairs: USDJPY is priced with 2 or 3 decimals (e.g., 150.25). Subtracting 0.0050 from a JPY pair does absolutely nothing useful.
Asset Class Hopping: If you decide to test your forex scalper on XAGUSD (Silver) or an index like US30, hardcoded zeroes will instantly break your logic due to entirely different tick sizes.