Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Pinescript to Python conversion issues

    Indicators/Strategies/Analyzers
    1
    1
    192
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      Chricco94 last edited by

      Hi, i'im trying to convert WaveTrendStrategy from Pinescript to Python to backtest it more in the past, but i have some issues. I hope someone can give me a hand.

      import requests
      import backtrader as bt
      import backtrader.analyzers as btanalyzers
      import json
      import pandas as pd
      import datetime as dt
      import matplotlib.pyplot as plt
      import qgrid
      
      
      def get_binance_bars(symbol, interval, startTime, endTime):
          
          url = "https://api.binance.com/api/v3/klines"
          
          startTime = str(int(startTime.timestamp() * 1000))
          endTime = str(int(endTime.timestamp() * 1000))
          limit='1000'
          
          req_params={"symbol" : symbol, 'interval' : interval, 'startTime' : startTime, 'endTime' : endTime, 'limit' : limit}
          df=pd.DataFrame(json.loads(requests.get(url, params=req_params).text))
          
          if(len(df.index)==0):
              return None
          df=df.iloc[:, 0:6]
          df.columns=[ 'datetime', 'open', 'high', 'low', 'close', 'volume']
          
          df.open=df.open.astype("float")
          df.high=df.high.astype("float")
          df.low=df.low.astype("float")
          df.close=df.close.astype("float")
          df.volume=df.volume.astype("float")
          
          df['adj_close']=df['close']
          
          df.index=[dt.datetime.fromtimestamp(x / 1000.0) for x in df.datetime]
          
          return df
      

      here i want to change BTCUSDT in BTCUSD_PERP but it doesn't work

      df_list = []
      last_datetime = dt.datetime(2019, 1, 1)
      while True:
          new_df = get_binance_bars('BTCUSDT', '5m', last_datetime, dt.datetime.now())
          if new_df is None:
              break
          df_list.append(new_df)
          last_datetime=max(new_df.index)+dt.timedelta(0,1)
          
      df=pd.concat(df_list)
      df.shape
      
      class WaveTrendStrategy(bt.Strategy):
          
          def __init__(self) :
              n1 = 21
              n2 = 14
              obLevel1 = 60
              obLevel1 = 60
              obLevel2 = 53
              osLevel1 = -60
              osLevel2 = -53
              hlc3 = (self.data.high+self.data.low+self.data.close)/3
              ap = hlc3 
              esa = bt.ind.EMA(ap,period=n1)
              d = bt.ind.EMA(abs(ap - esa), period=n1)
              ci = (ap - esa) / (0.015 * d)
              tci = bt.ind.EMA(ci, period=n2)
              wt1 = tci
              wt2 = bt.ind.SMA(wt1,period=4)
              self.longCondition  = bt.ind.CrossUp(wt2,osLevel2)
              self.shortCondition = bt.ind.CrossDown(wt2,obLevel2)
              
              
          def next(self):
              if self.longCondition:
                      self.buy()
              
              elif self.shortCondition:
                  self.sell()
      

      here i want to add commissions

      cerebro=bt.Cerebro()
      data=bt.feeds.PandasData(dataname=df)
      cerebro.adddata(data)
      
      cerebro.addstrategy(WaveTrendStrategy)
      cerebro.broker.setcash(10000)
      
      cerebro.addsizer(bt.sizers.PercentSizer, percents=100)
      cerebro.addanalyzer(btanalyzers.SharpeRatio,_name="sharpe")
      cerebro.addanalyzer(btanalyzers.Transactions,_name="trans")
      
      back=cerebro.run()
      
      cerebro.broker.getvalue()
      

      but i get this output: 17014500.431190684 and i think it's a little bit too much... don't know what i have to change

      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors