Json data and general programming question



  • 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?



  • 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()```


  • 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).



  • 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.


  • administrators

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

    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


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.