D

@run-out alright... I think I got it!
class Win_Buy(bt.Strategy):
params = (("period", 5),)
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):
self.risk = 0.01
self.diff = self.data - self.data(-1)
self.win_record = self.data(0) > self.data(-1)
self.prob_win = bt.ind.SumN(self.win_record, period=self.p.period)
self.prob_loss = self.p.period - self.prob_win
self.wins = bt.ind.SumN(
bt.If(self.diff >= 0, self.diff, 0), period=self.p.period)
self.losses = bt.ind.SumN(
bt.If(self.diff <0, -self.diff,0), period=self.p.period)
self.avg_win = bt.DivByZero(self.wins, self.prob_win, 0)
self.avg_loss = bt.DivByZero(self.losses, self.prob_loss, 0)
self.avg_risk = bt.ind.Average(self.data.close*self.risk, period=self.p.period)
def next(self):
self.dsharpe = self.avg_win[0] / (self.avg_risk[0])
if self.dsharpe > 1:
# previous close less than the previous close
# BUY, BUY, BUY!!! (with all possible default parameters)
self.log('BUY CREATE, %.2f' % self.data[0])
self.buy()
else:
pass
With Prints...
2018-01-09, c 24.37 diff 0.02, winr 1.00, pwin 3.00, ploss 2.00, wins 0.25, plosses 0.14, avg_win 0.08, avg_loss 0.07, avg_risk 0.24, DSHAREP 0.34
2018-01-10, c 24.25 diff -0.12, winr 0.00, pwin 2.00, ploss 3.00, wins 0.12, plosses 0.26, avg_win 0.06, avg_loss 0.09, avg_risk 0.24, DSHAREP 0.25
2018-01-11, c 24.32 diff 0.07, winr 1.00, pwin 2.00, ploss 3.00, wins 0.09, plosses 0.26, avg_win 0.05, avg_loss 0.09, avg_risk 0.24, DSHAREP 0.18
2018-01-12, c 24.34 diff 0.02, winr 1.00, pwin 3.00, ploss 2.00, wins 0.11, plosses 0.18, avg_win 0.04, avg_loss 0.09, avg_risk 0.24, DSHAREP 0.15
2018-01-15, c 24.40 diff 0.06, winr 1.00, pwin 4.00, ploss 1.00, wins 0.17, plosses 0.12, avg_win 0.04, avg_loss 0.12, avg_risk 0.24, DSHAREP 0.17
2018-01-16, c 24.31 diff -0.09, winr 0.00, pwin 3.00, ploss 2.00, wins 0.15, plosses 0.21, avg_win 0.05, avg_loss 0.11, avg_risk 0.24, DSHAREP 0.21
2018-01-17, c 24.35 diff 0.04, winr 1.00, pwin 4.00, ploss 1.00, wins 0.19, plosses 0.09, avg_win 0.05, avg_loss 0.09, avg_risk 0.24, DSHAREP 0.20
Does this seem right?
Also if I just wanted to use DSharpe as a bt.Indicator would i just change the class from Strategy to Indicator? and the purchase metrics?