|
| 1 | +# Forex Calculator [](https://github.com/NeaByteLab/Forex-Calculator) [](https://www.npmjs.org/package/@neabyte/forex-calculator) [](https://jsr.io/@neabyte/forex-calculator) |
| 2 | + |
| 3 | +Simple and reliable Forex Calculator tools for trading analysis. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Choose your preferred package manager: |
| 8 | + |
| 9 | +```bash |
| 10 | +# npm package |
| 11 | +npm install @neabyte/forex-calculator |
| 12 | + |
| 13 | +# Deno module |
| 14 | +deno add jsr:@neabyte/forex-calculator |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +### Position Sizing |
| 20 | + |
| 21 | +Calculate optimal lot size based on account risk and stop loss distance. |
| 22 | + |
| 23 | +```typescript |
| 24 | +import { calculateLotSize } from '@neabyte/forex-calculator' |
| 25 | + |
| 26 | +const result = calculateLotSize({ |
| 27 | + pairName: 'XAUUSD', |
| 28 | + openPrice: 2000, |
| 29 | + stopPrice: 1990, |
| 30 | + leverage: 100, |
| 31 | + riskUSD: 100 |
| 32 | +}) |
| 33 | +console.log(result) |
| 34 | +/* |
| 35 | +{ |
| 36 | + info: { symbol: 'XAUUSD', timestamp: '...', type: 'METAL' }, |
| 37 | + margin: { leverage: '1:100', required: 200 }, |
| 38 | + position: { lot: 0.1, pipValue: 1, units: 10 }, |
| 39 | + risk: { amount: 100, stopDistance: 10, stopPoints: 100 } |
| 40 | +} |
| 41 | +*/ |
| 42 | +``` |
| 43 | + |
| 44 | +### Profit & Loss |
| 45 | + |
| 46 | +Calculate the exact profit or loss in USD and pips for any position. |
| 47 | + |
| 48 | +```typescript |
| 49 | +import { calculateProfit } from '@neabyte/forex-calculator' |
| 50 | + |
| 51 | +const result = calculateProfit({ |
| 52 | + pairName: 'EURUSD', |
| 53 | + sidePosition: 'BUY', |
| 54 | + lotSize: 1.0, |
| 55 | + entryPrice: 1.1, |
| 56 | + exitPrice: 1.105 |
| 57 | +}) |
| 58 | +console.log(result) |
| 59 | +/* |
| 60 | +{ |
| 61 | + isProfit: true, |
| 62 | + pips: 50, |
| 63 | + profitUSD: 500, |
| 64 | + sidePosition: 'BUY', |
| 65 | + symbol: 'EURUSD', |
| 66 | + type: 'FOREX' |
| 67 | +} |
| 68 | +*/ |
| 69 | +``` |
| 70 | + |
| 71 | +### Pivot Points |
| 72 | + |
| 73 | +Support for multiple methodologies to find support and resistance. |
| 74 | + |
| 75 | +```typescript |
| 76 | +import { calculatePivotPoints } from '@neabyte/forex-calculator' |
| 77 | + |
| 78 | +const result = calculatePivotPoints({ |
| 79 | + high: 1.1, |
| 80 | + low: 1.09, |
| 81 | + close: 1.095, |
| 82 | + method: 'FIBONACCI' |
| 83 | +}) |
| 84 | +console.log(result) |
| 85 | +/* |
| 86 | +{ |
| 87 | + method: 'FIBONACCI', |
| 88 | + p: 1.095, |
| 89 | + r1: 1.09882, |
| 90 | + r2: 1.10118, |
| 91 | + r3: 1.105, |
| 92 | + s1: 1.09118, |
| 93 | + s2: 1.08882, |
| 94 | + s3: 1.085 |
| 95 | +} |
| 96 | +*/ |
| 97 | +``` |
| 98 | + |
| 99 | +### Strategy Simulations |
| 100 | + |
| 101 | +Project account growth or analyze risk with built-in simulators. |
| 102 | + |
| 103 | +```typescript |
| 104 | +import { simulateCompounding, calculateExpectancy } from '@neabyte/forex-calculator' |
| 105 | + |
| 106 | +// Project compounding growth |
| 107 | +const compound = simulateCompounding({ |
| 108 | + startBalance: 1000, |
| 109 | + profitPerTrade: 10, |
| 110 | + totalTrades: 2 |
| 111 | +}) |
| 112 | +console.log(compound) |
| 113 | +/* |
| 114 | +[ |
| 115 | + { period: 1, startBalance: 1000, endBalance: 1100, profit: 100 }, |
| 116 | + { period: 2, startBalance: 1100, endBalance: 1210, profit: 110 } |
| 117 | +] |
| 118 | +*/ |
| 119 | + |
| 120 | +// Calculate mathematical edge |
| 121 | +const edge = calculateExpectancy({ |
| 122 | + winRate: 50, |
| 123 | + avgWinUSD: 100, |
| 124 | + avgLossUSD: 50 |
| 125 | +}) |
| 126 | +console.log(edge) // { expectancy: 25, potentialProfit: 1250, status: 'POSITIVE' } |
| 127 | +``` |
| 128 | + |
| 129 | +### Backtesting |
| 130 | + |
| 131 | +Simulate trading a strategy over a specific period of time. |
| 132 | + |
| 133 | +```typescript |
| 134 | +import { calculateBacktest } from '@neabyte/forex-calculator' |
| 135 | + |
| 136 | +const result = calculateBacktest({ |
| 137 | + initBalance: 1000, |
| 138 | + riskAmount: 10, |
| 139 | + tradePerDay: 1, |
| 140 | + totalDays: 5, |
| 141 | + winRate: 60, |
| 142 | + rewardRatio: 2 |
| 143 | +}) |
| 144 | +console.log(result.summary) |
| 145 | +/* |
| 146 | +{ |
| 147 | + actualWinRate: 60, |
| 148 | + finalBalance: 1040, |
| 149 | + initBalance: 1000, |
| 150 | + isProfitable: true, |
| 151 | + netProfit: 40, |
| 152 | + totalTrades: 5, |
| 153 | + totalWins: 3, |
| 154 | + ... |
| 155 | +} |
| 156 | +*/ |
| 157 | +``` |
| 158 | + |
| 159 | +## API Reference |
| 160 | + |
| 161 | +### Core Calculators |
| 162 | + |
| 163 | +- **`calculateLotSize(params)`**: Returns `LotSizeResult`. Calculates margin, lot size, and risk metrics. |
| 164 | +- **`calculateProfit(params)`**: Returns `ProfitResult`. Calculates PnL and pip distance. |
| 165 | +- **`calculatePivotPoints(params)`**: Returns `PivotResult`. Supports `CLASSIC`, `FIBONACCI`, `CAMARILLA`, `WOODIES`, `DEMARKS`. |
| 166 | + |
| 167 | +### Strategy Simulations |
| 168 | + |
| 169 | +- **`simulateCompounding(params)`**: Project account growth over multiple trades. |
| 170 | +- **`calculateDrawdown(params)`**: Analyze potential balance decay during losing streaks. |
| 171 | +- **`calculateExpectancy(params)`**: Determine strategy edge (Statistical Expectancy). |
| 172 | + |
| 173 | +### Backtesting |
| 174 | + |
| 175 | +- **`calculateBacktest(params)`**: Comprehensive strategy simulation over a time period. |
| 176 | + |
| 177 | +## Supported Instruments |
| 178 | + |
| 179 | +The library supports a wide range of symbols across different asset classes. |
| 180 | + |
| 181 | +### Forex |
| 182 | + |
| 183 | +| Symbol | Contract Size | Pip Size | |
| 184 | +| :--------------------------------------------------------------------------------------------- | :------------ | :------- | |
| 185 | +| AUDJPY, CADJPY, CHFJPY, EURJPY, GBPJPY, NZDJPY, USDJPY | 100,000 | 0.01 | |
| 186 | +| AUDNZD, AUDUSD, EURAUD, EURGBP, EURNZD, EURUSD, GBPCAD, GBPNZD, GBPUSD, NZDUSD, USDCAD, USDCHF | 100,000 | 0.0001 | |
| 187 | + |
| 188 | +### Metals |
| 189 | + |
| 190 | +| Symbol | Type | Contract Size | Pip Size | |
| 191 | +| :----- | :----- | :------------ | :------- | |
| 192 | +| XAUUSD | Gold | 100 | 0.1 | |
| 193 | +| XAGUSD | Silver | 5000 | 0.01 | |
| 194 | + |
| 195 | +### Indices & Commodities |
| 196 | + |
| 197 | +| Symbol | Contract Size | Pip Size | |
| 198 | +| :----------------------------------------- | :------------ | :------- | |
| 199 | +| US30, NAS100, SPX500, GER40, JPN225, UK100 | 1 | 1.0 | |
| 200 | +| USOIL, UKOIL | 100 | 0.01 | |
| 201 | +| XNGUSD | 10,000 | 0.001 | |
| 202 | + |
| 203 | +### Crypto |
| 204 | + |
| 205 | +| Symbol | Contract Size | Pip Size | |
| 206 | +| :------------- | :------------ | :------- | |
| 207 | +| BTCUSD, ETHUSD | 1 | 1.0 | |
| 208 | +| SOLUSD | 1 | 0.01 | |
| 209 | + |
| 210 | +## Error Handling |
| 211 | + |
| 212 | +The library validates inputs and throws clear error messages: |
| 213 | + |
| 214 | +```typescript |
| 215 | +try { |
| 216 | + calculatePivotPoints({ high: 0, low: 100, close: 105, method: 'CLASSIC' }) |
| 217 | +} catch (e) { |
| 218 | + console.error(e.message) // "Prices must be greater than 0" |
| 219 | +} |
| 220 | +``` |
| 221 | + |
| 222 | +## License |
| 223 | + |
| 224 | +This project is licensed under the MIT license. See the [LICENSE](LICENSE) file for more info. |
0 commit comments