admin管理员组

文章数量:1356515

I've been trying to port a strategy visualization from Pine Script to Indie that requires drawing rectangle-like structures. In Pine Script, this is straightforward with the box function, but Indie doesn't have a direct equivalent. My attempt to create rectangle-like visualizations in Indie is failing with this error:

Error: 23:8 assignment to `result` failed: the value has a generic type, please instantiate it first or add a type annotation

Code with error:

# indie:lang_version = 5
from indie import indicator, param, color, plot, MainContext, MutSeriesF
from indie.algorithms import Highest, Lowest

@indicator('Multiple Rectangle Zones', overlay_main_pane=True)
@param.int('lookback_period', default=30, min=5, title='Lookback Period')
@param.int('num_zones', default=3, min=1, max=5, title='Number of S/R Zones')
@param.float('zone_width', default=0.5, min=0.1, max=2.0, title='Zone Width (%)')
class Main(MainContext):
    def __init__(self):
        # Initialize arrays to store support and resistance levels
        self.resistance_levels = [0.0] * 5
        self.support_levels = [0.0] * 5
        self.zone_colors = [
            color.rgba(255, 0, 0, 0.2),    # Red
            color.rgba(0, 128, 0, 0.2),    # Green
            color.rgba(0, 0, 255, 0.2),    # Blue
            color.rgba(255, 165, 0, 0.2),  # Orange
            color.rgba(128, 0, 128, 0.2)   # Purple
        ]
        
    def calc(self, lookback_period, num_zones, zone_width):
        result = []
        
        # Detect new pivot highs and lows
        for i in range(1, num_zones + 1):
            period = lookback_period * i
            
            # Find local highs and lows
            highest = Highest.new(self.high, period)[0]
            lowest = Lowest.new(self.low, period)[0]
            
            # Update levels with decay factor for older levels
            decay_factor = 0.95
            self.resistance_levels[i-1] = max(highest, self.resistance_levels[i-1] * decay_factor)
            self.support_levels[i-1] = min(lowest, self.support_levels[i-1] / decay_factor) if self.support_levels[i-1] > 0 else lowest
            
            # Calculate zone thickness
            res_thickness = self.resistance_levels[i-1] * (zone_width / 100)
            sup_thickness = self.support_levels[i-1] * (zone_width / 100)
            
            # Create resistance zone (top rectangle)
            res_top = self.resistance_levels[i-1] + res_thickness
            res_bottom = self.resistance_levels[i-1] - res_thickness
            
            # Create support zone (bottom rectangle)
            sup_top = self.support_levels[i-1] + sup_thickness  
            sup_bottom = self.support_levels[i-1] - sup_thickness
            
            # For each zone, we need 8 plot elements:
            # - 4 lines for resistance zone (left, right, top, bottom)
            # - 4 lines for support zone (left, right, top, bottom)
            
            # We'll use horizontal lines with limited width to simulate rectangle edges
            # The left edge is at current bar, right edge is 10 bars to the right
            bar_width = 10
            
            # Add resistance zone rectangle
            result.append(plot.Line(res_top))                    # Top horizontal
            result.append(plot.Line(res_bottom))                 # Bottom horizontal
            result.append(plot.Line(res_top, offset=bar_width))  # Right top
            result.append(plot.Line(res_bottom, offset=bar_width)) # Right bottom
            
            # Fill for resistance zone
            result.append(plot.Fill(color=self.zone_colors[i-1]))
            
            # Add support zone rectangle
            result.append(plot.Line(sup_top))                    # Top horizontal
            result.append(plot.Line(sup_bottom))                 # Bottom horizontal
            result.append(plot.Line(sup_top, offset=bar_width))  # Right top
            result.append(plot.Line(sup_bottom, offset=bar_width)) # Right bottom
            
            # Fill for support zone
            result.append(plot.Fill(color=self.zone_colors[i-1]))
        
        return tuple(result)
  1. I've tried using an explicit list initialization instead:
result = list()
  1. I attempted to provide a type annotation:
result: list = []

Both approaches still produce the same error about generic types.

  1. What's the correct way to instantiate a list in Indie that can hold plot elements?
  2. Is there an alternative approach to creating rectangle-like visualizations in Indie without using lists to collect the plot elements?
  3. Does anyone have a working example of simulating rectangles or boxes in Indie v5?

本文标签: tradingDrawing Rectangles Handling Generic Type Assignment ErrorStack Overflow