재무와 차트

반응형

Backtrader는 투자전략을 세우고 과거데이터들을 통해 투자전략을 검증하기 위해 사용되는 파이썬 프레임워크 입니다.

 

 

Backtrader 설치

pip install backtrader

 

 

Backtrader 시작

 

기본적으로 Backtrader를 실행시켜보는 단계입니다.

 

from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import backtrader as bt

if __name__ == '__main__':
    cerebro = bt.Cerebro()

    print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())

    cerebro.run()

    print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())

 

이를 통해 기본적으로 설정되어 있는 값들이 있다는것을 알 수 있습니다.

 

 

초기자금설정

 

cerebro.broker.getvalue()는 현금을 의미합니다.

from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import backtrader as bt

if __name__ == '__main__':
    cerebro = bt.Cerebro()
    cerebro.broker.setcash(100000.0)

    print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())

    cerebro.run()

    print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())

 

초기자금 설정은 아래 함수를 통해 설정할 수 있습니다.

 cerebro.broker.setcash(초기자금)

 

 

데이터 입력

테스트를 해볼 데이터 파일을 입력하는 방법입니다. 아래는 예제데이터 입니다.

 

orcl-1995-2014.txt
0.33MB

 

from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import datetime  # For datetime objects
import os.path  # To manage paths
import sys  # To find out the script name (in argv[0])

# Import the backtrader platform
import backtrader as bt

if __name__ == '__main__':
    # Create a cerebro entity
    cerebro = bt.Cerebro()

    # Datas are in a subfolder of the samples. Need to find where the script is
    # because it could have been called from anywhere
    modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
    datapath = os.path.join(modpath, 'orcl-1995-2014.txt')

    # Create a Data Feed
    data = bt.feeds.YahooFinanceCSVData(
        dataname=datapath,
        # Do not pass values before this date
        fromdate=datetime.datetime(2000, 1, 1),
        # Do not pass values after this date
        todate=datetime.datetime(2000, 12, 31),
        reverse=False)

    # Add the Data Feed to Cerebro
    cerebro.adddata(data)

    # Set our desired cash start
    cerebro.broker.setcash(100000.0)

    # Print out the starting conditions
    print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())

    # Run over everything
    cerebro.run()

    # Print out the final result
    print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())

 

adddata 함수를 통해 데이터를 적용시킬 수 있습니다.

cerebro.adddata(추가할 데이터)

 

파일의 구성은 Date,Open,High,Low,Close,Adj Close, Volume 으로 이루어져 있으며, 위 경우 야후에서 주식데이터를 가져왔을 경우의 형식을 적용시킨것입니다.

frodate의 경우 분석시작일 todate는 분석종료일을 의미하며 데이터가 내림차순으로 되어 있을시 reverse=True를 통해 뒤집어서 데이터를 넣어줍니다.

 

 

참고사이트

 

Quickstart Guide - Backtrader

Quickstart Note The data files used in the quickstart guide are updated from time to time, which means that the adjusted close changes and with it the close (and the other components). That means that the actual output may be different to what was put in t

www.backtrader.com

반응형