Menu

1 / 2

Writing a forex position size calculator in JavaScript: the pip value bugs

DEV Community: beginners·Edgelog·4 days ago
#xurjqpc6
#devtobeginners#devto#dev#account#lots
Reading 0:00
15s threshold

Most forex position size calculators online are one formula:

lots = risk / (stop_pips × pip_value)

Enter fullscreen mode

Exit fullscreen mode

That formula is right. The bugs live in the two inputs nobody explains: the pip value, and what you do with the number that comes out. I hit all of these while building a calculator, so here is the version that survives real pairs, written in plain JavaScript.

1. Pip size is not always 0.0001

A pip is the fourth decimal place on most pairs, but the second decimal place on pairs quoted in Japanese yen:

function pipSize ( pair ) { return pair . endsWith ( 'JPY' ) ? 0.01 : 0.0001 ; }

Enter fullscreen mode

Exit fullscreen mode

Hard-code 0.0001 and every USDJPY or GBPJPY result is off by a factor of 100. That is not a rounding error; that is a blown account.

Gold, silver, indices and crypto CFDs have no universal pip.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.