Fix indicator preset saving and update Hurst Bands defaults
- Include visual settings (line width, color, markers) in saved presets - Change default line width to 1px for indicators - Update Hurst Bands to use grey markers and custom shapes by default - Add robust userPresets initialization and error handling in savePreset
This commit is contained in:
@ -15,7 +15,16 @@ let indicatorPanes = new Map();
|
||||
let nextPaneIndex = 1;
|
||||
|
||||
// Presets storage
|
||||
let userPresets = JSON.parse(localStorage.getItem('indicator_presets') || '{}');
|
||||
let userPresets;
|
||||
try {
|
||||
userPresets = JSON.parse(localStorage.getItem('indicator_presets') || '{}');
|
||||
if (!userPresets || typeof userPresets !== 'object') {
|
||||
userPresets = { presets: [] };
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('Failed to parse presets:', e);
|
||||
userPresets = { presets: [] };
|
||||
}
|
||||
|
||||
// Categories
|
||||
const CATEGORIES = [
|
||||
@ -589,35 +598,78 @@ function getPresetsForIndicator(indicatorName) {
|
||||
}
|
||||
|
||||
window.savePreset = function(id) {
|
||||
console.log('[savePreset] Attempting to save preset for id:', id);
|
||||
const indicator = activeIndicators.find(a => a.id === id);
|
||||
if (!indicator) return;
|
||||
if (!indicator) {
|
||||
console.error('[savePreset] Indicator not found for id:', id);
|
||||
return;
|
||||
}
|
||||
|
||||
const presetName = prompt('Enter preset name:');
|
||||
if (!presetName) return;
|
||||
|
||||
const IndicatorClass = IR?.[indicator.type];
|
||||
if (!IndicatorClass) return;
|
||||
if (!IndicatorClass) {
|
||||
console.error('[savePreset] Indicator class not found for type:', indicator.type);
|
||||
return;
|
||||
}
|
||||
|
||||
const instance = new IndicatorClass({ type: indicator.type, params: indicator.params, name: indicator.name });
|
||||
const meta = instance.getMetadata();
|
||||
|
||||
const preset = {
|
||||
id: `preset_${Date.now()}`,
|
||||
name: presetName,
|
||||
indicatorName: meta.name,
|
||||
values: {}
|
||||
};
|
||||
|
||||
meta.inputs.forEach(input => {
|
||||
preset.values[input.name] = indicator.params[input.name];
|
||||
});
|
||||
|
||||
if (!userPresets.presets) userPresets.presets = [];
|
||||
userPresets.presets.push(preset);
|
||||
saveUserPresets();
|
||||
renderIndicatorPanel();
|
||||
|
||||
alert(`Preset "${presetName}" saved!`);
|
||||
try {
|
||||
const instance = new IndicatorClass({ type: indicator.type, params: indicator.params, name: indicator.name });
|
||||
const meta = instance.getMetadata();
|
||||
|
||||
const preset = {
|
||||
id: `preset_${Date.now()}`,
|
||||
name: presetName,
|
||||
indicatorName: meta.name,
|
||||
values: {}
|
||||
};
|
||||
|
||||
// Save standard inputs
|
||||
if (meta.inputs && Array.isArray(meta.inputs)) {
|
||||
meta.inputs.forEach(input => {
|
||||
preset.values[input.name] = indicator.params[input.name];
|
||||
});
|
||||
}
|
||||
|
||||
// Save visual settings (line width, type, colors)
|
||||
preset.values._lineWidth = indicator.params._lineWidth;
|
||||
preset.values._lineType = indicator.params._lineType;
|
||||
|
||||
// Save colors for each plot
|
||||
if (meta.plots && Array.isArray(meta.plots)) {
|
||||
meta.plots.forEach((plot, idx) => {
|
||||
const colorKey = `_color_${idx}`;
|
||||
if (indicator.params[colorKey]) {
|
||||
preset.values[colorKey] = indicator.params[colorKey];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Save marker settings
|
||||
const markerKeys = [
|
||||
'showMarkers',
|
||||
'markerBuyShape', 'markerBuyColor', 'markerBuyCustom',
|
||||
'markerSellShape', 'markerSellColor', 'markerSellCustom'
|
||||
];
|
||||
markerKeys.forEach(key => {
|
||||
if (indicator.params[key] !== undefined) {
|
||||
preset.values[key] = indicator.params[key];
|
||||
}
|
||||
});
|
||||
|
||||
if (!userPresets) userPresets = { presets: [] };
|
||||
if (!userPresets.presets) userPresets.presets = [];
|
||||
|
||||
userPresets.presets.push(preset);
|
||||
saveUserPresets();
|
||||
renderIndicatorPanel();
|
||||
|
||||
alert(`Preset "${presetName}" saved!`);
|
||||
} catch (error) {
|
||||
console.error('[savePreset] Error saving preset:', error);
|
||||
alert('Error saving preset. See console for details.');
|
||||
}
|
||||
};
|
||||
|
||||
window.applyPreset = function(id, presetId) {
|
||||
@ -688,15 +740,27 @@ function addIndicator(type) {
|
||||
|
||||
const params = {
|
||||
_lineType: 'solid',
|
||||
_lineWidth: 2,
|
||||
_lineWidth: 1,
|
||||
showMarkers: true,
|
||||
markerBuyShape: 'arrowUp',
|
||||
markerBuyColor: '#26a69a',
|
||||
markerBuyCustom: '◭',
|
||||
markerSellShape: 'arrowDown',
|
||||
markerSellColor: '#ef5350',
|
||||
markerBuyShape: 'custom',
|
||||
markerBuyColor: '#9e9e9e',
|
||||
markerBuyCustom: '▲',
|
||||
markerSellShape: 'custom',
|
||||
markerSellColor: '#9e9e9e',
|
||||
markerSellCustom: '▼'
|
||||
};
|
||||
|
||||
// Override with Hurst-specific defaults
|
||||
if (type === 'hurst') {
|
||||
params._lineWidth = 1;
|
||||
params.markerBuyShape = 'custom';
|
||||
params.markerSellShape = 'custom';
|
||||
params.markerBuyColor = '#9e9e9e';
|
||||
params.markerSellColor = '#9e9e9e';
|
||||
params.markerBuyCustom = '▲';
|
||||
params.markerSellCustom = '▼';
|
||||
}
|
||||
|
||||
metadata.plots.forEach((plot, idx) => {
|
||||
params[`_color_${idx}`] = plot.color || getDefaultColor(activeIndicators.length + idx);
|
||||
});
|
||||
@ -751,7 +815,7 @@ function renderIndicatorOnPane(indicator, meta, instance, candles, paneIndex, li
|
||||
indicator.series = [];
|
||||
|
||||
const lineStyle = lineStyleMap[indicator.params._lineType] || LightweightCharts.LineStyle.Solid;
|
||||
const lineWidth = indicator.params._lineWidth || 2;
|
||||
const lineWidth = indicator.params._lineWidth || 1;
|
||||
|
||||
const firstNonNull = results?.find(r => r !== null && r !== undefined);
|
||||
const isObjectResult = firstNonNull && typeof firstNonNull === 'object';
|
||||
|
||||
Reference in New Issue
Block a user