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/

    Json data and general programming question

    General Code/Help
    4
    5
    1787
    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.
    • H
      hippylover last edited by

      Hi, i have json data with open, close, high, low and time in unix time. How would i use backtrader to take the data as i feed it and get an alert every time there's a buy signal(for example ema goes over sma)?

      Also, does backtester only take open and close data or can i feed it just last bid for example?

      H 1 Reply Last reply Reply Quote 0
      • H
        hippylover @hippylover last edited by

        Well, i managed to get it to show on plot, but had to convert it to csv and put it in a file first... Is there no way to take the data directly from an array or something? Also, if i want each "period" for calculating/plotting ema/sma/etc to be 30-minutes, is it possible to set this or do i have to feed it half-hour data?

        import backtrader as bt
        import requests
        from jq import jq
        import csv
        import backtrader.feeds as btfeeds
        
        class smaEmaCross(bt.SignalStrategy):
            def __init__(self):
                sma = bt.ind.SMA(period=50)
                ema = bt.ind.EMA(period=20)
                crossover = bt.ind.CrossOver(ema,sma)
                self.signal_add(bt.SIGNAL_LONG, crossover)
        
        
        cerebro = bt.Cerebro()
        cerebro.addstrategy(smaEmaCross)
        
        #get data
        currency = "MAID"
        symbol = "BTC"
        requestLine = 'https://min-api.cryptocompare.com/data/' + "histominute?fsym=" + currency + "&tsym=" + symbol + "&allData=True&e=Poloniex"
        res = requests.get(requestLine)
        start = str(int((datetime.datetime.now() - datetime.timedelta(minutes=10000)).timestamp()))
        
        dataDownload = jq('.Data[]|select(.time > ' + start + ')|{date: .time |strftime("%d/%m/%y"), time: .time |strftime("%H:%M:%S"), open: .open, close: .close, high: .high, low: .low, volume: .volumeto}').transform(res.json(), multiple_output=True)
        csvData = []
        for i in dataDownload:
            csvData.append(str(i['date']) + "," + str(i['time']) + "," + str(i['open']) + "," + str(i['close']) + "," + str(i['high']) + "," + str(i['low']) + "," + str(i['volume']))
        
        print(csvData)
        
        #write file
        with open("ohlc.txt","w") as outf:
        	outf.write("date,time,open,close,high,low,volume\n")
        	for x in csvData:
        		outf.write(x + "\n")
        
        data = btfeeds.GenericCSVData(
            dataname="ohlc.txt",
            dtformat='%d/%m/%y',
            #date=0,
            time=1,
            open=2,
            close=3,
            high=4,
            low=5,
            volume=6,
            openinterest=-1,  # -1 for not present
            #fromdate=datetime.datetime(2017, 1, 1),
            #todate=datetime.datetime(2017, 12, 31),
            reverse=False)
        
        cerebro.adddata(data)
        
        cerebro.run()
        cerebro.plot()```
        1 Reply Last reply Reply Quote 0
        • Curtis Miller
          Curtis Miller last edited by

          Since you can create a data feed with a pandas DataFrame, perhaps consider using the pandas function from_json() to convert the JSON data to a DataFrame then use that DataFrame as your data feed.

          I'm also curious about data that isn't OHLC (like tick data).

          1 Reply Last reply Reply Quote 0
          • A
            ab_trader last edited by

            Maybe you want to try to search community using words bid or tick, you may find several posts that can help. As remember it was also a blog post about it. Maybe not single post.

            1 Reply Last reply Reply Quote 0
            • B
              backtrader administrators last edited by

              You can create your own data feed and fill the values from your JSON data. See

              • Docs - Binary Datafeed Development

              You basically subclass and override _load to deliver your set of OHLC prices each time.

              @hippylover said in Json data and general programming question:

              Also, does backtester only take open and close data or can i feed it just last bid for example?
              @Curtis-Miller said in Json data and general programming question:
              I'm also curious about data that isn't OHLC (like tick data).

              You can extend the data feed to pass any values you wish. See: Docs - Extending a Datafeed

              In any case, bid-ask are values tied to ticks and not to larger timeframes (mostly resampled) which would only have the latest of the bid-ask values to act upon or else the next incoming. This platform doesn't use bid-ask for anything.

              @hippylover said in Json data and general programming question:

              Also, if i want each "period" for calculating/plotting ema/sma/etc to be 30-minutes, is it possible to set this or do i have to feed it half-hour data?

              With no time sample it is impossible to know what timeframe your data is in. But you can resample the data (and even replay it). See Docs - Data Resampling

              You may also want to read the FAQ, to remind yourself of setting the right timeframe and compression for your data. See Community - FAQ

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
              $(document).ready(function () { app.coldLoad(); }); }