trading-core-lib
v0.0.7
Published
A trading platform core library
Maintainers
Readme
Trading Core Library
A comprehensive TypeScript library for trading platform development with utilities for market data, order management, and financial calculations.
Features
- Trading Services: Order placement, cancellation, and status tracking
- Market Data: Real-time and historical market data retrieval
- Validation: Comprehensive input validation for trading operations
- Calculations: Financial calculations including P&L, percentages, and averages
- Formatting: Number and currency formatting utilities
- TypeScript: Full TypeScript support with type definitions
Installation
npm install trading-core-libUsage
Basic Setup
import { TradingService, MarketDataService } from 'trading-core-lib';
// Initialize services
const tradingService = new TradingService({
apiKey: 'your-api-key',
apiSecret: 'your-api-secret',
baseUrl: 'https://api.example.com'
});
const marketDataService = new MarketDataService('https://api.example.com');Placing Orders
import { TradingService } from 'trading-core-lib';
const tradingService = new TradingService();
const order = {
symbol: 'BTC/USD',
side: 'buy',
type: 'market',
quantity: 1
};
const result = await tradingService.placeOrder(order);
if (result.success) {
console.log('Order placed:', result.data);
} else {
console.error('Error:', result.error);
}Getting Market Data
import { MarketDataService } from 'trading-core-lib';
const marketDataService = new MarketDataService();
// Get current market data
const marketData = await marketDataService.getMarketData('BTC/USD');
// Get historical data
const historicalData = await marketDataService.getHistoricalData(
'BTC/USD',
new Date('2023-01-01'),
new Date('2023-01-31')
);Using Utilities
import {
isValidSymbol,
calculateProfitLoss,
formatPrice,
formatCurrency
} from 'trading-core-lib';
// Validation
const isValid = isValidSymbol('BTC/USD'); // true
// Calculations
const profit = calculateProfitLoss(100, 110, 1); // 10
// Formatting
const formattedPrice = formatPrice(12345.6789); // "12345.68"
const formattedCurrency = formatCurrency(1234.56, 'USD'); // "$1,234.56"API Reference
TradingService
placeOrder(order: Partial<TradeOrder>): Promise<TradingResult>cancelOrder(orderId: string): Promise<TradingResult>getOrderStatus(orderId: string): Promise<TradingResult>
MarketDataService
getMarketData(symbol: string): Promise<TradingResult>getHistoricalData(symbol: string, startDate: Date, endDate: Date): Promise<TradingResult>getAvailableSymbols(): Promise<TradingResult>
Validation Utils
isValidSymbol(symbol: string): booleanisValidPrice(price: number): booleanisValidQuantity(quantity: number): booleanisValidTradeOrder(order: Partial<TradeOrder>): boolean
Calculation Utils
calculatePercentageChange(oldValue: number, newValue: number): numbercalculateTradeValue(price: number, quantity: number): numbercalculateProfitLoss(buyPrice: number, sellPrice: number, quantity: number): numbercalculateProfitLossPercentage(buyPrice: number, sellPrice: number): numbercalculateAveragePrice(trades: Array<{price: number, quantity: number}>): numberroundToDecimals(value: number, decimals: number): number
Formatting Utils
formatNumber(value: number, decimals?: number): stringformatPrice(price: number): stringformatQuantity(quantity: number): stringformatPercentage(value: number, decimals?: number): stringformatDate(date: Date): stringformatCurrency(amount: number, currency?: string): string
Development
Prerequisites
- Node.js >= 16.0.0
- npm or yarn
Setup
# Install dependencies
npm install
# Build the library
npm run build
# Run tests
npm test
# Run linting
npm run lint
# Format code
npm run formatDeployment
Automatic Deployment (Recommended)
This project uses GitHub Actions for automatic deployment to NPM.
Set up NPM Token:
- Go to NPM Settings
- Create a new access token
- Add it to your GitHub repository secrets as
NPM_TOKEN
Deploy:
# Patch version (1.0.0 → 1.0.1) npm run version:patch # Minor version (1.0.0 → 1.1.0) npm run version:minor # Major version (1.0.0 → 2.0.0) npm run version:major
Manual Deployment
# Build and publish
npm run build
npm publishScripts
npm run build- Build the librarynpm run dev- Watch mode for developmentnpm test- Run testsnpm run test:watch- Run tests in watch modenpm run lint- Run ESLintnpm run lint:fix- Fix ESLint issuesnpm run format- Format code with Prettiernpm run clean- Clean build directory
Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
If you have any questions or need help, please open an issue on GitHub.
