UNPKG

n8n-nodes-fxcm

Version:
148 lines (147 loc) 6.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Fxcm = void 0; const FxcmClient_1 = require("./FxcmClient"); const TradingDescription_1 = require("./descriptions/TradingDescription"); const MarketDataDescription_1 = require("./descriptions/MarketDataDescription"); class Fxcm { constructor() { this.description = { displayName: 'FXCM', name: 'fxcm', icon: 'file:fxcm.svg', group: ['transform'], version: 1, subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Execute FXCM trading operations', defaults: { name: 'FXCM', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'fxcmApi', required: true, }, ], properties: [ { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, options: [ { name: 'Trading', value: 'trading', }, { name: 'Market Data', value: 'marketData', }, { name: 'Account', value: 'account', }, ], default: 'trading', }, ...TradingDescription_1.tradingOperationFields, ...MarketDataDescription_1.marketDataOperationFields, ], }; } async execute() { const returnData = []; const credentials = await this.getCredentials('fxcmApi'); const client = new FxcmClient_1.FxcmClient(credentials); const resource = this.getNodeParameter('resource', 0); const operation = this.getNodeParameter('operation', 0); try { if (resource === 'trading') { switch (operation) { case 'placeMarketOrder': { const instrument = this.getNodeParameter('instrument', 0); const orderType = this.getNodeParameter('orderType', 0); const amount = this.getNodeParameter('amount', 0); const stopLoss = this.getNodeParameter('stopLoss', 0); const takeProfit = this.getNodeParameter('takeProfit', 0); const result = await client.placeMarketOrder({ instrument, isBuy: orderType === 'buy', amount, stopLoss, takeProfit, }); returnData.push({ json: result }); break; } case 'placeLimitOrder': { const instrument = this.getNodeParameter('instrument', 0); const orderType = this.getNodeParameter('orderType', 0); const amount = this.getNodeParameter('amount', 0); const price = this.getNodeParameter('price', 0); const stopLoss = this.getNodeParameter('stopLoss', 0); const takeProfit = this.getNodeParameter('takeProfit', 0); const result = await client.placeLimitOrder({ instrument, isBuy: orderType === 'buy', amount, price, stopLoss, takeProfit, }); returnData.push({ json: result }); break; } case 'closePosition': { const positionId = this.getNodeParameter('positionId', 0); const result = await client.closePosition(positionId); returnData.push({ json: result }); break; } case 'modifyPosition': { const positionId = this.getNodeParameter('positionId', 0); const stopLoss = this.getNodeParameter('stopLoss', 0); const takeProfit = this.getNodeParameter('takeProfit', 0); const result = await client.modifyPosition(positionId, stopLoss, takeProfit); returnData.push({ json: result }); break; } } } else if (resource === 'marketData') { switch (operation) { case 'getCurrentPrice': { const instrument = this.getNodeParameter('instrument', 0); const result = await client.getPrices(instrument); returnData.push({ json: result }); break; } case 'getHistoricalData': { const instrument = this.getNodeParameter('instrument', 0); const startDate = this.getNodeParameter('startDate', 0); const endDate = this.getNodeParameter('endDate', 0); const result = await client.getHistoricalData(instrument, new Date(startDate), new Date(endDate)); returnData.push({ json: { result } }); break; } } } } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { error: error.message } }); } else { throw error; } } finally { client.disconnect(); } return [returnData]; } } exports.Fxcm = Fxcm;