@run-out yes indeed, when the stdev is small, which means that the three MAs are very close, which could be a good buy point or breakup point. I am testing my theory. and thanks for the help with the codes, chief. =)
Best posts made by Mike Leighton
-
RE: how to setup a standard deviation in def __init__ (self)?
Latest posts made by Mike Leighton
-
RE: how to setup a standard deviation in def __init__ (self)?
@run-out yes indeed, when the stdev is small, which means that the three MAs are very close, which could be a good buy point or breakup point. I am testing my theory. and thanks for the help with the codes, chief. =)
-
how to setup a standard deviation in def __init__ (self)?
this is what I have for now, which is working fine outside of btr system. I have to be honest that I am pretty shady with class writing.
def __init__(self): # Keep a reference to the "close" line in the data[0] dataseries self.dataclose = self.datas[0].close # To keep track of pending orders self.order = None self.buyprice = None self.buycomm = None # Add a MovingAverageSimple indicator self.sma5 = bt.talib.SMA(self.datas[0], timeperiod=self.p.ma5period) self.sma20 = bt.talib.SMA(self.datas[0], timeperiod=self.p.ma20period) self.sma60 = bt.talib.SMA(self.datas[0], timeperiod=self.p.ma60period) self.stdev = stats.pstdev((self.sma5, self.sma20, self.sma60)) self.MACDhisto = bt.indicators.MACDHisto(self.datas[0])
DOM
File "P:/LEIGH PYTHON/Codes/Quant/backtrader_initiation.py", line 67, in __init__ self.stdev = stats.pstdev((self.sma5, self.sma20, self.sma60)) File "C:\Users\Mike_Leigh\.conda\envs\LEIGH\lib\statistics.py", line 666, in pstdev var = pvariance(data, mu) File "C:\Users\Mike_Leigh\.conda\envs\LEIGH\lib\statistics.py", line 637, in pvariance T, ss = _ss(data, mu) File "C:\Users\Mike_Leigh\.conda\envs\LEIGH\lib\statistics.py", line 535, in _ss c = mean(data) File "C:\Users\Mike_Leigh\.conda\envs\LEIGH\lib\statistics.py", line 312, in mean T, total, count = _sum(data) File "C:\Users\Mike_Leigh\.conda\envs\LEIGH\lib\statistics.py", line 148, in _sum for n,d in map(_exact_ratio, values): File "C:\Users\Mike_Leigh\.conda\envs\LEIGH\lib\statistics.py", line 230, in _exact_ratio raise TypeError(msg.format(type(x).__name__)) TypeError: can't convert type 'SMA' to numerator/denominator
would anyone please give me a direction on this? much appreciated!
-
RE: Multiple Data Feeds (3555 csv files) on a single strategy
@run-out ok, the codes I presented is just a practice work. Im trying to get to know BTr and get a hold of it. here is what im trying to deal with. Im trying to set up a way to find MA combo pattern, and then run all 3555 csv files to match the pattern, then trigger trades. This is the reason why i have 3555 csv files on my HDD. =D
-
RE: Multiple Data Feeds (3555 csv files) on a single strategy
it runs separately, one by one each.
-
Multiple Data Feeds (3555 csv files) on a single strategy
I am new here, just wondering if i could do such performance with backtrader.
I have 3555 good csv files laying there.here is my codes:
# -*- coding:utf-8 -*- # env python 3.6 # 从兴趣开始学习Python # 我是pepCoder & pepTrader # email: mike_leigh@qq.com # Blog: https://me.csdn.net/weixin_44736043 # Date: 2020/5/4 10:49 from __future__ import (absolute_import, division, print_function, unicode_literals) import datetime import os.path import sys import pandas as pd import backtrader as bt # Create a Strategy class TestStrategy(bt.Strategy): params = ( ('maperiod', 30), ) def log(self, txt, dt=None): """ Logging function fot this strategy""" dt = dt or self.datas[0].datetime.date(0) print('%s, %s' % (dt.isoformat(), txt)) def __init__(self): # Keep a reference to the "close" line in the data[0] dataseries self.dataclose = self.datas[0].close # To keep track of pending orders self.order = None self.buyprice = None self.buycomm = None # Add a MovingAverageSimple indicator self.sma = bt.indicators.SimpleMovingAverage( self.datas[0], period=self.params.maperiod) # Indicators for the plotting show bt.indicators.ExponentialMovingAverage(self.datas[0], period=90) bt.indicators.WeightedMovingAverage(self.datas[0], period=200).subplot = True # bt.indicators.StochasticSlow(self.datas[0]) bt.indicators.MACDHisto(self.datas[0]) rsi = bt.indicators.RSI(self.datas[0]) bt.indicators.SmoothedMovingAverage(rsi, period=10) bt.indicators.ATR(self.datas[0]).plot = False def notify_order(self, order): if order.status in [order.Submitted, order.Accepted]: # Buy/Sell order submitted/accepted to/by broker - Nothing to do return # Check if an order has been completed # Attention: broker could reject order if not enough cash if order.status in [order.Completed]: if order.isbuy(): self.log( 'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm)) self.buyprice = order.executed.price self.buycomm = order.executed.comm else: # Sell self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm)) self.bar_executed = len(self) elif order.status in [order.Canceled, order.Margin, order.Rejected]: self.log('Order Canceled/Margin/Rejected') self.order = None def notify_trade(self, trade): if not trade.isclosed: return self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' % (trade.pnl, trade.pnlcomm)) def next(self): # Simply log the closing price of the series from the reference self.log('Close, %.2f' % self.dataclose[0]) # Check if an order is pending ... if yes, we cannot send a 2nd one if self.order: return # Check if we are in the market if not self.position: # Not yet ... we MIGHT BUY if ... if self.dataclose[0] > self.sma[0]: # BUY, BUY, BUY!!! (with all possible default parameters) self.log('BUY CREATE, %.2f' % self.dataclose[0]) # Keep track of the created order to avoid a 2nd order self.order = self.buy() else: if self.dataclose[0] < self.sma[0]: # SELL, SELL, SELL!!! (with all possible default parameters) self.log('SELL CREATE, %.2f' % self.dataclose[0]) # Keep track of the created order to avoid a 2nd order self.order = self.sell() if __name__ == '__main__': cerebro = bt.Cerebro() cerebro.addstrategy(TestStrategy) root_path = '.\\__stock__' for root, dirs, files in os.walk(root_path): for csv in files: if csv.endswith('.csv'): csv_path = os.path.join(root, csv) df = pd.read_csv("{}".format(csv_path), encoding='gbk', index_col=0, parse_dates=True) df = df.sort_values(by=['日期'], ascending=True) df = df.loc[:, ['日期', '开盘价', '收盘价', '最高价', '最低价', '成交量']] df = df.rename(columns={ '开盘价': 'open', '收盘价': 'close', '最高价': 'high', '最低价': 'low', '成交量': 'volume'}) df['openinterest'] = 0 data = bt.feeds.PandasData(dataname = df, fromdate = datetime.datetime(2015, 1, 1), todate = datetime.datetime(2020, 4, 15)) cerebro.adddata(data) cerebro.broker.setcash(1000000.0) # Add a FixedSize sizer according to the stake cerebro.addsizer(bt.sizers.FixedSize, stake=10) # Set the commission - 0.1% ... divide by 100 to remove the % cerebro.broker.setcommission(commission=0.001) print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) cerebro.run() print('Final Portfolio value: %.2f' % cerebro.broker.getvalue()) cerebro.plot()
I know it looks hilarious, plz give me hint where I should be heading. lolz
-
RE: AttributeError: module 'backtrader' has no attribute 'Cerebro'
@backtrader thanks chief, I made the same insane mistake lolz. changed to "backtrader_initiation", problem solved.