@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
376 lines • 17 kB
JavaScript
;
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.VolumeStrategy = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const indicatorts = __importStar(require("indicatorts"));
class VolumeStrategy {
constructor() {
this.description = {
displayName: 'Volume Strategy',
name: 'volumeStrategy',
icon: 'file:icon.svg',
group: ['transform'],
version: 1,
description: 'Execute volume trading strategies using the indicatorts library',
defaults: {
name: 'Volume Strategy',
},
inputs: ["main"],
outputs: ["main"],
properties: [
{
displayName: 'Strategy',
name: 'strategy',
type: 'options',
options: [
{
name: 'Chaikin Money Flow Strategy',
value: 'chaikinMoneyFlowStrategy',
description: 'Strategy based on Chaikin Money Flow (CMF) indicator',
},
{
name: 'Ease of Movement Strategy',
value: 'easeOfMovementStrategy',
description: 'Strategy based on Ease of Movement (EMV) indicator',
},
{
name: 'Force Index Strategy',
value: 'forceIndexStrategy',
description: 'Strategy based on Force Index (FI) indicator',
},
{
name: 'Money Flow Index Strategy',
value: 'moneyFlowIndexStrategy',
description: 'Strategy based on Money Flow Index (MFI) indicator',
},
{
name: 'Negative Volume Index Strategy',
value: 'negativeVolumeIndexStrategy',
description: 'Strategy based on Negative Volume Index (NVI) indicator',
},
{
name: 'Volume Weighted Average Price Strategy',
value: 'volumeWeightedAveragePriceStrategy',
description: 'Strategy based on Volume Weighted Average Price (VWAP) indicator',
},
],
default: 'moneyFlowIndexStrategy',
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: 'CMF Period',
name: 'cmfPeriod',
type: 'number',
default: 20,
description: 'Period for Chaikin Money Flow calculation',
displayOptions: {
show: {
strategy: ['chaikinMoneyFlowStrategy'],
},
},
},
{
displayName: 'EMV Period',
name: 'emvPeriod',
type: 'number',
default: 14,
description: 'Period for Ease of Movement calculation',
displayOptions: {
show: {
strategy: ['easeOfMovementStrategy'],
},
},
},
{
displayName: 'Force Index Period',
name: 'fiPeriod',
type: 'number',
default: 13,
description: 'Period for Force Index calculation',
displayOptions: {
show: {
strategy: ['forceIndexStrategy'],
},
},
},
{
displayName: 'MFI Period',
name: 'mfiPeriod',
type: 'number',
default: 14,
description: 'Period for Money Flow Index calculation',
displayOptions: {
show: {
strategy: ['moneyFlowIndexStrategy'],
},
},
},
{
displayName: 'NVI Period',
name: 'nviPeriod',
type: 'number',
default: 1,
description: 'Period for Negative Volume Index calculation',
displayOptions: {
show: {
strategy: ['negativeVolumeIndexStrategy'],
},
},
},
{
displayName: 'NVI Start Value',
name: 'nviStart',
type: 'number',
default: 1000,
description: 'Starting value for Negative Volume Index calculation',
displayOptions: {
show: {
strategy: ['negativeVolumeIndexStrategy'],
},
},
},
{
displayName: 'VWAP Period',
name: 'vwapPeriod',
type: 'number',
default: 14,
description: 'Period for Volume Weighted Average Price calculation',
displayOptions: {
show: {
strategy: ['volumeWeightedAveragePriceStrategy'],
},
},
},
{
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 'chaikinMoneyFlowStrategy':
config.period = this.getNodeParameter('cmfPeriod', i, 20);
actions = indicatorts.chaikinMoneyFlowStrategy(asset, config);
break;
case 'easeOfMovementStrategy':
config.period = this.getNodeParameter('emvPeriod', i, 14);
actions = indicatorts.easeOfMovementStrategy(asset, config);
break;
case 'forceIndexStrategy':
config.period = this.getNodeParameter('fiPeriod', i, 13);
actions = indicatorts.forceIndexStrategy(asset, config);
break;
case 'moneyFlowIndexStrategy':
config.period = this.getNodeParameter('mfiPeriod', i, 14);
actions = indicatorts.moneyFlowIndexStrategy(asset, config);
break;
case 'negativeVolumeIndexStrategy':
config.period = this.getNodeParameter('nviPeriod', i, 1);
config.start = this.getNodeParameter('nviStart', i, 1000);
actions = indicatorts.negativeVolumeIndexStrategy(asset, config);
break;
case 'volumeWeightedAveragePriceStrategy':
config.period = this.getNodeParameter('vwapPeriod', i, 14);
actions = indicatorts.volumeWeightedAveragePriceStrategy(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.VolumeStrategy = VolumeStrategy;
//# sourceMappingURL=VolumeStrategy.node.js.map