Skip to content

Instantly share code, notes, and snippets.

@dharma6872
dharma6872 / 백테스터 템플릿.py
Created February 16, 2021 10:54
[백테스터 템플릿] #퀀트
import backtrader as bt
class MyStrategy(bt.Strategy):
def next(self):
pass #Do something
#Instantiate Cerebro engine
cerebro = bt.Cerebro()
#Add strategy to Cerebro
@dharma6872
dharma6872 / 주가 데이터 다운로드.py
Created February 14, 2021 02:12
[주가 데이터 다운로드] #퀀트
import datetime
from pandas_datareader import data as pdr
import yfinance as yf
yf.pdr_override()
# 과거 주가 다운로드 함수
# stock_no: 한국 주식 번호, 현재는 한국 주식만 다운로드 가능
def get_stock_history_pdr(stock_no):
# 과거 주가 데이터, 기간은 3.5년으로 설정
DT_DIFF = 365.25*3.5
@dharma6872
dharma6872 / q_stick.py
Created February 10, 2021 07:55
[q_stick] #퀀트
def q_stick(Data, ema_lookback, opening, close, where):
# The variable Data refers to the OHLC array you are using
# The variable ema_lookback refers to the selected lookback period
# The variable opening refers to the open column in the OHLC array
# The variable close refers to the close column in the OHLC array
# The variable where refers to where the Q-Stick will be put
for i in range(len(Data)):
Data[i, where] = Data[i, close] - Data[i, opening]
@dharma6872
dharma6872 / 지수 이동평균.py
Created February 10, 2021 07:55
[지수 이동평균] #퀀트
# 가중 이동평균 함수
# 지수 이동평균 함수
def ema(Data, alpha, lookback, what, where):
# alpha is the smoothing factor
# window is the lookback period
# what is the column that needs to have its average calculated
# where is where to put the exponential moving average
alpha = alpha / (lookback + 1.0)
beta = 1 - alpha
@dharma6872
dharma6872 / 이동평균.py
Last active February 10, 2021 04:55
[이동평균] #권트
# 파라미터
# Data: 데이터(np.array), lookback: 이동 평균 기간(int), what: 대상 데이터 위치(int), where: 저장할 위치(int)
# ma: moving average
def ma(Data, lookback, what, where):
for i in range(len(Data)):
try:
Data[i, where] = (Data[i - lookback + 1:i + 1, what].mean())
except IndexError:
pass
return Data
@dharma6872
dharma6872 / Reinforcement Learning w Keras OpenAI DQNs.py
Created February 4, 2021 02:15
[Reinforcement Learning w Keras OpenAI DQNs] #gym #강화학습
import gym
import numpy as np
import random
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import Adam
from collections import deque
class DQN:
@dharma6872
dharma6872 / StockTradingEnv.py
Created February 3, 2021 09:50
[StockTradingEnv] #gym #강화학습 #퀀트
import random
import json
import gym
from gym import spaces
import pandas as pd
import numpy as np
from StockTradingGraph import StockTradingGraph
MAX_ACCOUNT_BALANCE = 2147483647
@dharma6872
dharma6872 / StockTradingGraph.py
Last active February 3, 2021 09:49
[StockTradingGraph] #gym #강화학습 #퀀트
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib import style
# finance module is no longer part of matplotlib
# see: https://github.com/matplotlib/mpl_finance
from mpl_finance import candlestick_ochl as candlestick
@dharma6872
dharma6872 / stable-baselines 설치.md
Last active February 4, 2021 02:07
[stable-baselines 설치] #gym #강화학습

stable-baselines 설치

Microsoft MPI v10.0 설치
https://www.microsoft.com/en-us/download/details.aspx?id=57467

pip install gym
pip install pandas
pip install tensorflow==1.14.0
pip install stable-baselines[mpi]==2.10.0
pip install mpl_finance
@dharma6872
dharma6872 / 가상환경 생성.md
Created February 3, 2021 05:25
[가상환경 생성] #python

가상환경 생성

conda create --prefix d:\conda\stable-baselines python=3.6