首页  

python3抽象类使用实例     所属分类 python 浏览量 10
from abc import ABC, abstractmethod
ABC是"Abstract Base Class"(抽象基类)的缩写,
它是Python标准库abc模块的一部分,用于定义抽象基类。


from abc import ABC, abstractmethod


class Indicator(ABC):
    """技术指标基类"""
    
    def __init__(self, name):
        self.name = name
    
    @abstractmethod
    def calculate(self, data):
        pass


class MovingAverage(Indicator):
    """移动平均线指标"""
  
    def __init__(self, window=20):
        super().__init__(f"MA{window}")
        self.window = window
    
    def calculate(self, data):
        return data['Close'].rolling(window=self.window, min_periods=1).mean()

上一篇     下一篇
Python量化交易实战指南

量化投资Python实战指南

python随机数种子作用及使用方法

WorldQuant 101 因子