Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. mr-m0nst3r
    3. Topics
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    • Profile
    • Following 0
    • Followers 0
    • Topics 2
    • Posts 9
    • Best 0
    • Controversial 0
    • Groups 0

    Topics created by mr-m0nst3r

    • mr-m0nst3r

      How to get MACD of 1 minute and 5 minute
      General Discussion • • mr-m0nst3r

      5
      0
      Votes
      5
      Posts
      1112
      Views

      mr-m0nst3r

      @backtrader said in How to get MACD of 1 minute and 5 minute:

      The period is what the indicators use as loo

      Hi @backtrader ,

      Thank you for your answer.
      First of all, this is my understanding of timeframe and period:
      timeframe is an attribute of the data feed; period is the "bar".
      If the data's timeframe is 1 minute, then 1 period is 1 minute's data (OCHLV); if data's timeframe is 5 minutes, then 1 bar is 5 minutes' data.

      I use bt.timeframe.Minutes all the time, so why bt.indicators.SMA(data, period=5) is not giving me the 5 minutes SMA value? I directly use the data, not a resampled one.
      I guess to get SMA of 5 minutes, I have to use 5 minutes timeframe data, seems clearer to me now. Thank you.

      And I used the Minutes data to resample to a 5 minutes timeframe using cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=5). Am I doing it right?

      And you're right, I'm confused when it comes to MACD.

      MACD=12-Period EMA − 26-Period EMA

      So, if the data's timeframe is 5 minutes, we'll get EMA(12) and EMA(26) of 5 minutes' timeframe, after calculation, it's the 5m MACD value, is that right?

      From the MACD equation, I see only period calculations, whether this period stands for 1 minute or 5 minutes, it's decided by the data's timeframe, which lead me to the conclusion that to get macd of 5m, the only way is to resample original 1m timeframe data to 5m timeframe data using compression=5. is that right?

      I'm stuck here, and forgive me if my expression confuses you.
      Thank you very much for your time.

    • mr-m0nst3r

      Resample live feed from CCXT problem
      Indicators/Strategies/Analyzers • • mr-m0nst3r

      4
      0
      Votes
      4
      Posts
      1276
      Views

      mr-m0nst3r

      @backtrader

      Here's the output of remove fromdate script and output:

      # !/usr/bin/env python # -*- coding: utf-8; py-indent-offset:4 -*- from __future__ import (absolute_import, division, print_function, unicode_literals) import sys import time from datetime import datetime, timedelta from datetime import datetime, timedelta import backtrader as bt import ccxt class TestStrategy(bt.Strategy): params = ( ('printlog', True), ) def log(self, txt, dt=None, doprint=False): ''' Logging function fot this strategy''' if self.params.printlog or doprint: dt = dt or bt.num2date(self.data.datetime[0]) print('%s, %s' % (dt, txt)) def start(self): self.counter = 0 print('START') def prenext(self): self.counter += 1 print('prenext len %d - counter %d' % (len(self), self.counter)) self.log("data#0: " + str(self.datas[0].datetime[0])) self.log("data#1: " + str(self.datas[1].datetime[0])) def __init__(self): self.macd = bt.indicators.MACDHisto(self.datas[0]) self.macd2 = bt.indicators.MACDHisto(self.datas[1]) def next(self): self.counter += 1 price_txt = "Counter: " + str(self.counter) + " Open/Close/High/Low/Volume: " + str(self.data0.open[0]) + " - "+ str(self.data0.close[0]) + " - " + str(self.data0.high[0]) + " - " + str(self.data0.low[0]) + " - " + str(self.data0.volume[0]) + " Len: "+ str(len(self.data0))# + " Time Frame:" + bt.TimeFrame.getname(self.data0._timeframe) + " Len: "+ str(len(self.data0)) self.log(price_txt) macd_txt = "MACD: {:.2f}, Histo: {:.2f}".format(self.macd.macd[0],self.macd.histo[0]) self.log("MACD#1: " + macd_txt) macd2_txt = "MACD: {:.2f}, Histo: {:.2f}".format(self.macd2.macd[0],self.macd2.histo[0]) self.log("MACD#2: " + macd2_txt) if __name__ == '__main__': cerebro = bt.Cerebro() #exchange = sys.argv[1] if len(sys.argv) > 1 else 'gdax' exchange = sys.argv[1] if len(sys.argv) > 1 else 'gateio' symbol = sys.argv[2] if len(sys.argv) > 2 else 'ETH/USDT' hist_start_date = datetime.utcnow() - timedelta(minutes=10) print('UTC NOW: ', datetime.utcnow()) print('hist_start_data: ', hist_start_date) print('Using symbol: ', symbol) # Create data feeds data_1m = bt.feeds.CCXT(exchange=exchange, symbol=symbol, name="1m", timeframe=bt.TimeFrame.Minutes, #fromdate=hist_start_date, compression=1) cerebro.adddata(data_1m) cerebro.replaydata(data_1m, timeframe=bt.TimeFrame.Minutes, name="1m", compression = 5) cerebro.addstrategy(TestStrategy) cerebro.run()

      Output:

      UTC NOW: 2019-06-24 02:30:45.980828 hist_start_data: 2019-06-24 02:20:45.980597 Using symbol: ETH/USDT START ... prenext len 163 - counter 163 2019-06-23 12:33:00, data#0: 737233.5229166667 2019-06-23 12:33:00, data#1: 737233.5229166667 dts[]: [737233.5236111111, 737233.5236111111] prenext len 164 - counter 164 2019-06-23 12:34:00, data#0: 737233.5236111111 2019-06-23 12:34:00, data#1: 737233.5236111111 dts[]: [737233.5243055555, 737233.5243055555] prenext len 165 - counter 165 2019-06-23 12:35:00, data#0: 737233.5243055555 2019-06-23 12:35:00, data#1: 737233.5243055555 dts[]: [737233.525, 737233.525] 2019-06-23 12:36:00, Counter: 166 Open/Close/High/Low/Volume: 312.57 - 312.45 - 312.71 - 312.45 - 99.16966564 Len: 166 2019-06-23 12:36:00, MACD#1: MACD: 0.07, Histo: -0.12 2019-06-23 12:36:00, MACD#2: MACD: 1.19, Histo: -0.09 dts[]: [737233.5256944444, 737233.5256944444] 2019-06-23 12:37:00, Counter: 167 Open/Close/High/Low/Volume: 312.45 - 312.54 - 312.54 - 312.36 - 21.3511 Len: 167 2019-06-23 12:37:00, MACD#1: MACD: 0.04, Histo: -0.12 2019-06-23 12:37:00, MACD#2: MACD: 1.19, Histo: -0.08 dts[]: [737233.526388889, 737233.526388889] 2019-06-23 12:38:00, Counter: 168 Open/Close/High/Low/Volume: 312.45 - 312.2 - 312.45 - 312.2 - 18.69802458 Len: 168 2019-06-23 12:38:00, MACD#1: MACD: -0.01, Histo: -0.13 2019-06-23 12:38:00, MACD#2: MACD: 1.17, Histo: -0.10 dts[]: [737233.5270833333, 737233.5270833333] 2019-06-23 12:39:00, Counter: 169 Open/Close/High/Low/Volume: 312.34 - 312.14 - 312.34 - 311.43 - 122.3391867563 Len: 169 2019-06-23 12:39:00, MACD#1: MACD: -0.05, Histo: -0.14 2019-06-23 12:39:00, MACD#2: MACD: 1.16, Histo: -0.11 dts[]: [737233.5277777778, 737233.5277777778] 2019-06-23 12:40:00, Counter: 170 Open/Close/High/Low/Volume: 311.44 - 311.79 - 311.79 - 311.43 - 22.0295 Len: 170 2019-06-23 12:40:00, MACD#1: MACD: -0.11, Histo: -0.16 2019-06-23 12:40:00, MACD#2: MACD: 1.13, Histo: -0.13 dts[]: [737233.5284722223, 737233.5284722223] ...

      You see the UTC NOW? I don't know why the MACDs' date is at around 2019-06-23 12:40:00.
      And I'm confused that:

      using fromdate will case error without fromdate, the script is still getting old data, but without error And I don't know why it's getting old data from that time

      How can these two happen in the same script?

      I'm just lost.

    • 1 / 1