This document explains the enhanced CFD trading system with proper risk ratios, pip calculations, and lot size management.
Instead of fixed percentages, the system now uses proper risk:reward ratios:
- 1:3 Ratio: For every 1 pip risked, aim for 3 pips profit
- 1:5 Ratio: For every 1 pip risked, aim for 5 pips profit
- 1:7 Ratio: For every 1 pip risked, aim for 7 pips profit
function getOptimalRiskRatio(accountBalance) {
if (accountBalance < 100) return '1:3'; // Small accounts
if (accountBalance < 500) return '1:5'; // Medium accounts
if (accountBalance < 2000) return '1:7'; // Large accounts
return '1:5'; // Default for larger accounts
}Different assets have different pip values:
function getPipValue(symbol) {
switch (symbol) {
case 'X:BTCUSD': return 0.01; // $0.01 per pip
case 'X:ETHUSD': return 0.01; // $0.01 per pip
default: return 0.01; // Default for other crypto pairs
}
}{
"symbol": "X:BTCUSD",
"accountBalance": 1000,
"lotSize": 0.01, // Required: Lot size in standard lots
"riskPercentage": 2, // Risk percentage of account
"riskRatio": "1:3", // Risk:Reward ratio (1:3, 1:5, 1:7)
"maxLeverage": 10, // Maximum leverage allowed
"useAdvancedModel": true,
"enableSelfLearning": true
}{
"status": "success",
"symbol": "X:BTCUSD",
"timestamp": "2025-07-28T12:03:33.141Z",
"signal": {
"signal": "BUY",
"entry": 45000.00,
"predictedPrice": 46500.00,
"stopLoss": 44800.00,
"takeProfit": 45600.00,
"priceChangePercent": 3.33,
"confidence": 0.75,
"riskRewardRatio": 3.0,
"leverage": 7,
"pips": {
"stopLoss": 200,
"takeProfit": 600,
"riskReward": 3.0
},
"positionSize": {
"units": 1000,
"marginRequired": 6428.57,
"leverage": 7,
"riskAmount": 20,
"priceDifference": 200,
"pipsRisked": 200,
"riskPerPip": 10,
"lotSize": 0.01
},
"potentialProfit": 6000,
"potentialLoss": 2000,
"marginRequired": 6428.57,
"freeMargin": 3571.43
},
"prediction": {
"currentPrice": 45000.00,
"predictedPrice": 46500.00,
"confidence": 0.75,
"individualPredictions": {
"lstm": [46500.00],
"randomForest": [46450.00]
}
},
"analysis": {
"signalStrength": 2.22,
"riskAssessment": "MEDIUM",
"confidenceScore": 0.75
}
}- Entry: $45,000
- Stop Loss: $44,800 (200 pips risk)
- Take Profit: $45,600 (600 pips reward)
- Risk/Reward: 200:600 = 1:3 ratio
- Entry: $3,000
- Stop Loss: $2,980 (200 pips risk)
- Take Profit: $3,100 (1000 pips reward)
- Risk/Reward: 200:1000 = 1:5 ratio
// 1 standard lot = 100,000 units
const units = lotSize * 100000;
// Examples:
// 0.01 lot = 1,000 units
// 0.05 lot = 5,000 units
// 0.1 lot = 10,000 units
// 1.0 lot = 100,000 units// Profit = Take Profit Pips ร Units ร Pip Value
const potentialProfit = takeProfitPips * units * pipValue;
// Loss = Stop Loss Pips ร Units ร Pip Value
const potentialLoss = stopLossPips * units * pipValue;const leverage = Math.min(maxLeverage, Math.floor(confidence * 10));
// Higher confidence = higher leverage (up to max)const marginRequired = (units * entryPrice) / leverage;
const freeMargin = accountBalance - marginRequired;function assessRisk(confidence, priceChangePercent) {
const riskScore = (1 - confidence) + (Math.abs(priceChangePercent) / 10);
if (riskScore < 0.3) return 'LOW';
if (riskScore < 0.6) return 'MEDIUM';
return 'HIGH';
}npm run test:cfd// Small account test
{
symbol: 'X:BTCUSD',
accountBalance: 100,
lotSize: 0.01,
riskPercentage: 2,
riskRatio: '1:3'
}
// Large account test
{
symbol: 'X:ETHUSD',
accountBalance: 2000,
lotSize: 0.1,
riskPercentage: 1.5,
riskRatio: '1:7'
}- Before:
confidence: null - After: Proper confidence calculation with bounds (0-1)
- Before: Fixed 1.5x take profit ratio
- After: Configurable 1:3, 1:5, 1:7 ratios
- Before: ML-generated position sizing
- After: User-provided lot size with proper calculations
- Before: Percentage-based calculations
- After: Proper pip-based calculations for different assets
- Before: Generic calculations
- After: CFD-specific pip-based profit/loss calculations
- Proper Risk Management: Real risk:reward ratios
- Accurate Calculations: Pip-based instead of percentage-based
- User Control: Lot size provided by user, not ML
- Better Confidence: Proper confidence calculation
- CFD Compliance: Follows standard CFD trading practices
- Flexible Ratios: Different ratios for different account sizes
- Accurate P&L: Realistic profit and loss calculations
# Trading Configuration
DEFAULT_LEVERAGE=10
MAX_POSITION_SIZE=1000
RISK_PERCENTAGE=2- Small Accounts (< $100): Use 1:3 ratio
- Medium Accounts ($100-$500): Use 1:5 ratio
- Large Accounts ($500-$2000): Use 1:7 ratio
- Very Large Accounts (> $2000): Use 1:5 ratio (conservative)
This enhanced system provides proper CFD trading functionality with accurate risk management, pip calculations, and user-controlled position sizing.