For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Custom Indicator-from Tradingview



  • Hi
    I am new to backtrader , I am trying to create the following Tradingview indicator
    https://uk.tradingview.com/script/nl8OAzka/

    My code is below, dont receive any error but just does not work,
    appreciate your help?

    class maw(bt.Indicator):

    alias = ('mawilim',)
    lines = ('maw', 'M1', 'M2', 'M3', 'M4', 'M5', 'M6')
    params = (
        ('mal1', 1),
        ('mal2', 3),
    )
    plotinfo = dict(subplot=False)
    plotlines = dict(maw=dict(alpha=1.0, linestyle='-', linewidth=2.0, 
                              color='magenta'))
    
    def __init__(self):
        self.p.mal3 = self.p.mal1 + self.p.mal2
        self.p.mal4 = self.p.mal3 + self.p.mal2
        self.p.mal5 = self.p.mal4 + self.p.mal3
        self.p.mal6 = self.p.mal5 + self.p.mal4
    
    def next(self):
        self.lines.M1 = bt.ind.WMA(self.data.close(-1), period=self.p.mal1)
        self.lines.M2 = bt.ind.WMA(self.lines.M1[0], period=self.p.mal2)
        self.lines.M3 = bt.ind.WMA(self.lines.M2[0], period=self.p.mal3)
        self.lines.M4 = bt.ind.WMA(self.lines.M3[0], period=self.p.mal4)
        self.lines.M5 = bt.ind.WMA(self.lines.M4[0], period=self.p.mal5)
        self.lines.maw = bt.ind.WMA(self.lines.M5[0], period=self.p.mal6)
    

    class TestStrategy(bt.Strategy):
    def init(self):
    # Indicators for the plotting show
    myind = maw(self.data)

    cerebro = bt.Cerebro()
    cerebro.addstrategy(TestStrategy)

    store = alpaca_backtrader_api.AlpacaStore(
    key_id=
    secret_key=
    paper=
    )

    if not ALPACA_PAPER:
    broker = store.getbroker() # or just alpaca_backtrader_api.AlpacaBroker()
    cerebro.setbroker(broker)

    DataFactory = store.getdata # or use alpaca_backtrader_api.AlpacaData
    data = DataFactory(dataname='AAPL', historical=True, fromdate=datetime(
    2019, 1, 1), timeframe=bt.TimeFrame.Days)
    cerebro.adddata(data)



  • @yanke_zulu said in Custom Indicator-from Tradingview:

    self.lines.M1[0]

    Resolved

    self.lines.M1[0]
    should be self.lines.M1(0)


Log in to reply
 

});