UNPKG

@dendaio/n8n-nodes-collection

Version:

🚀 Comprehensive n8n node collection for financial analysis and automation. Features 55+ technical indicators (RSI, MACD, Bollinger Bands), 32+ candlestick patterns (Doji, Hammer, Engulfing), automated trading strategies, RSS feed processing, and OAuth2 v

394 lines • 17.6 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.MomentumStrategy = void 0; const n8n_workflow_1 = require("n8n-workflow"); const indicatorts = __importStar(require("indicatorts")); class MomentumStrategy { constructor() { this.description = { displayName: 'Momentum Strategy', name: 'momentumStrategy', icon: 'file:icon.svg', group: ['transform'], version: 1, description: 'Execute momentum trading strategies using the indicatorts library', defaults: { name: 'Momentum Strategy', }, inputs: ["main"], outputs: ["main"], properties: [ { displayName: 'Strategy', name: 'strategy', type: 'options', options: [ { name: 'Awesome Oscillator Strategy', value: 'awesomeOscillatorStrategy', description: 'Strategy based on Awesome Oscillator momentum indicator', }, { name: 'Ichimoku Cloud Strategy', value: 'ichimokuCloudStrategy', description: 'Strategy based on Ichimoku Cloud indicator', }, { name: 'RSI2 Strategy', value: 'rsi2Strategy', description: 'Strategy based on 2-period RSI (deeply oversold/overbought)', }, { name: 'Stochastic Oscillator Strategy', value: 'stochasticOscillatorStrategy', description: 'Strategy based on Stochastic Oscillator', }, { name: 'Williams %R Strategy', value: 'williamsRStrategy', description: 'Strategy based on Williams %R indicator', }, ], default: 'rsi2Strategy', noDataExpression: true, }, { displayName: 'OHLCV Data', name: 'ohlcvData', type: 'string', default: '={{ $json.ohlcv }}', description: 'JSON string containing OHLCV data array', }, { displayName: 'Date Data', name: 'dateData', type: 'string', default: '={{ $json.dates }}', description: 'JSON string containing date array (optional, will use index if not provided)', }, { displayName: 'AO Fast Period', name: 'aoFast', type: 'number', default: 5, description: 'Fast period for Awesome Oscillator calculation', displayOptions: { show: { strategy: ['awesomeOscillatorStrategy'], }, }, }, { displayName: 'AO Slow Period', name: 'aoSlow', type: 'number', default: 34, description: 'Slow period for Awesome Oscillator calculation', displayOptions: { show: { strategy: ['awesomeOscillatorStrategy'], }, }, }, { displayName: 'Ichimoku Short Period', name: 'ichimokuShort', type: 'number', default: 9, description: 'Short period for Ichimoku Cloud calculation', displayOptions: { show: { strategy: ['ichimokuCloudStrategy'], }, }, }, { displayName: 'Ichimoku Medium Period', name: 'ichimokuMedium', type: 'number', default: 26, description: 'Medium period for Ichimoku Cloud calculation', displayOptions: { show: { strategy: ['ichimokuCloudStrategy'], }, }, }, { displayName: 'Ichimoku Long Period', name: 'ichimokuLong', type: 'number', default: 52, description: 'Long period for Ichimoku Cloud calculation', displayOptions: { show: { strategy: ['ichimokuCloudStrategy'], }, }, }, { displayName: 'Ichimoku Close Period', name: 'ichimokuClose', type: 'number', default: 3, description: 'Close period for Ichimoku Cloud calculation', displayOptions: { show: { strategy: ['ichimokuCloudStrategy'], }, }, }, { displayName: 'Stochastic K Period', name: 'stochKPeriod', type: 'number', default: 14, description: 'K period for Stochastic Oscillator calculation', displayOptions: { show: { strategy: ['stochasticOscillatorStrategy'], }, }, }, { displayName: 'Stochastic D Period', name: 'stochDPeriod', type: 'number', default: 3, description: 'D period for Stochastic Oscillator calculation', displayOptions: { show: { strategy: ['stochasticOscillatorStrategy'], }, }, }, { displayName: 'Williams %R Period', name: 'willrPeriod', type: 'number', default: 14, description: 'Period for Williams %R calculation', displayOptions: { show: { strategy: ['williamsRStrategy'], }, }, }, { displayName: 'Options', name: 'options', type: 'collection', placeholder: 'Add Option', default: {}, options: [ { displayName: 'Return Type', name: 'returnType', type: 'options', options: [ { name: 'Actions Only', value: 'actions', description: 'Return only the strategy actions (BUY/SELL/HOLD)', }, { name: 'With Metadata', value: 'metadata', description: 'Return actions with additional metadata and analysis', }, ], default: 'actions', }, { displayName: 'Include Gains Calculation', name: 'includeGains', type: 'boolean', default: false, description: 'Whether to calculate and include strategy gains based on actions', displayOptions: { show: { returnType: ['metadata'], }, }, }, ], }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; for (let i = 0; i < items.length; i++) { try { const strategy = this.getNodeParameter('strategy', i); const ohlcvDataString = this.getNodeParameter('ohlcvData', i); const dateDataString = this.getNodeParameter('dateData', i, ''); const options = this.getNodeParameter('options', i, {}); let ohlcvData; try { ohlcvData = JSON.parse(ohlcvDataString); } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Failed to parse OHLCV data: ${error instanceof Error ? error.message : 'Unknown error'}`); } if (!Array.isArray(ohlcvData) || ohlcvData.length === 0) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'OHLCV data must be a non-empty array'); } let dates = []; if (dateDataString) { try { const dateArray = JSON.parse(dateDataString); if (Array.isArray(dateArray)) { dates = dateArray.map((date) => new Date(date)); } } catch (error) { console.warn('Failed to parse date data, using index-based dates'); } } if (dates.length === 0) { dates = ohlcvData.map((_, index) => new Date(Date.now() - (ohlcvData.length - index - 1) * 24 * 60 * 60 * 1000)); } const opens = ohlcvData.map((candle) => candle[0]); const highs = ohlcvData.map((candle) => candle[1]); const lows = ohlcvData.map((candle) => candle[2]); const closes = ohlcvData.map((candle) => candle[3]); const volumes = ohlcvData.map((candle) => candle[4] || 0); const asset = { dates, openings: opens, highs, lows, closings: closes, volumes, }; let actions; let config = {}; switch (strategy) { case 'awesomeOscillatorStrategy': config.fast = this.getNodeParameter('aoFast', i, 5); config.slow = this.getNodeParameter('aoSlow', i, 34); actions = indicatorts.awesomeOscillatorStrategy(asset, config); break; case 'ichimokuCloudStrategy': config.short = this.getNodeParameter('ichimokuShort', i, 9); config.medium = this.getNodeParameter('ichimokuMedium', i, 26); config.long = this.getNodeParameter('ichimokuLong', i, 52); config.close = this.getNodeParameter('ichimokuClose', i, 3); actions = indicatorts.ichimokuCloudStrategy(asset, config); break; case 'rsi2Strategy': actions = indicatorts.rsi2Strategy(asset); break; case 'stochasticOscillatorStrategy': config.kPeriod = this.getNodeParameter('stochKPeriod', i, 14); config.dPeriod = this.getNodeParameter('stochDPeriod', i, 3); actions = indicatorts.stochasticOscillatorStrategy(asset, config); break; case 'williamsRStrategy': config.period = this.getNodeParameter('willrPeriod', i, 14); actions = indicatorts.williamsRStrategy(asset, config); break; default: throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unsupported strategy: ${strategy}`); } const outputData = { strategy, parameters: config, actions: actions.map((action) => { switch (action) { case indicatorts.Action.BUY: return 'BUY'; case indicatorts.Action.SELL: return 'SELL'; case indicatorts.Action.HOLD: return 'HOLD'; default: return 'UNKNOWN'; } }), actionValues: actions, }; if (options.returnType === 'metadata') { const buyCount = actions.filter((a) => a === indicatorts.Action.BUY).length; const sellCount = actions.filter((a) => a === indicatorts.Action.SELL).length; const holdCount = actions.filter((a) => a === indicatorts.Action.HOLD).length; const metadata = { totalActions: actions.length, buySignals: buyCount, sellSignals: sellCount, holdSignals: holdCount, buyPercentage: ((buyCount / actions.length) * 100).toFixed(2) + '%', sellPercentage: ((sellCount / actions.length) * 100).toFixed(2) + '%', holdPercentage: ((holdCount / actions.length) * 100).toFixed(2) + '%', inputLength: ohlcvData.length, calculationTimestamp: new Date().toISOString(), }; outputData.metadata = metadata; if (options.includeGains) { try { const gains = indicatorts.applyActions(closes, actions); outputData.gains = gains; outputData.totalGain = gains[gains.length - 1] || 0; metadata.totalGain = gains[gains.length - 1] || 0; } catch (error) { outputData.gainsError = error instanceof Error ? error.message : 'Unknown error'; } } } returnData.push({ json: outputData, }); } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { error: error instanceof Error ? error.message : 'Unknown error occurred', }, }); continue; } throw error; } } return [returnData]; } } exports.MomentumStrategy = MomentumStrategy; //# sourceMappingURL=MomentumStrategy.node.js.map