For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
More elegant way in strategy
-
Hi
is there a way to improve the backorder strategy code? Especially the summation method used?
i converted the indicator:
https://www.prorealcode.com/prorealtime-indicators/reversal-point-indicator/to this code:
class strategy(bt.Strategy): def __init__(self): self.sto = btind.Stochastic(period=8, period_dfast=3) def next(self): if (len(self.data) < 54): return c1 = self.data.close[-1] < self.data.open[-1] and self.data.close[0] > self.data.open[0] c2 = self.data.close[0] > self.data.open[-1] low3 = min(self.data.low.get(size=3)) c3 = low3 < min(self.data.low.get(ago=-1, size=50)) or low3 < min(self.data.low.get(ago=-2, size=50)) or low3 < min(self.data.low.get(ago=-3, size=50)) c4 = False for i in self.sto.get(size=3): if i < 20: c4 = True break long = c1 and c2 and c3 and c4 c5 = self.data.close[-1] > self.data.open[-1] and self.data.close[0] < self.data.open[0] c6 = self.data.close[0] < self.data.open[-1] high3 = max(self.data.high.get(size=3)) c7 = high3 > max(self.data.high.get(ago=-1, size=50)) or high3 > max(self.data.high.get(ago=-2, size=50)) or high3 > min(self.data.high.get(ago=-3, size=50)) c8 = False for i in self.sto.get(size=3): if i > 80: c8 = True break short = c5 and c6 and c7 and c8 if long: print("LONG", self.data[0]) if short: print("SHORT", self.data[0])
-
@dasch said in More elegant way in strategy:
c1 = self.data.close[-1] < self.data.open[-1] and self.data.close[0] > self.data.open[0]
You could pack those calculations in individual indicators. That's only a single line, but you could pack several in a single indicator, depending of the granularity or re-usability you may want for those calculations.
-
Updated it to this:
''' //PRC_Reversal point indicator | indicator //09.04.2017 //Nicolas @ www.prorealcode.com //Sharing ProRealTime knowledge // https://www.prorealcode.com/topic/need-help-coding-the-following-reversal-indicator/ // https://www.prorealcode.com/prorealtime-indicators/reversal-point-indicator/ // LONG //Red/Green candle (reversal) //Bullish candle close above the open of the previous red candle //Space to the left (the low of the last 3 candles lower than the low of the last 50 candles) //default stochastic (8,3,3) was in the oversold area within the last 3 candles // SHORT //Green/Red candle (reversal) //Bearish candle close below the close of the previous green candle //Space to the left (the high of the last 3 candles higher than the high of the last 50 candles) //default stochastic (8,3,3) was in the overbought area within the last 3 candles. ''' from __future__ import (absolute_import, division, print_function, unicode_literals) # Import the backtrader platform import backtrader as bt import backtrader.indicators as btind class ReversalPoint(bt.Indicator): lines = ('revpoint',) plotinfo = dict( # Add extra margins above and below the 1s and -1s plotymargin=0.15, # Plot a reference horizontal line at 1.0 and -1.0 plothlines=[1.0, -1.0], # Simplify the y scale to 1.0 and -1.0 plotyticks=[1.0, -1.0]) # Plot the line "overunder" (the only one) with dash style # ls stands for linestyle and is directly passed to matplotlib plotlines = dict(revpoint=dict(ls='--')) params = dict( sto_period=8, sto_period_dfast=3, sto_oversold=20, sto_overbought=80, lperiod=50, speriod=3, ) def __init__(self): self.sto = btind.Stochastic(period=self.p.sto_period, period_dfast=self.p.sto_period_dfast) self.addminperiod(self.p.lperiod + self.p.speriod) self.c1 = bt.And(self.data.close(-1) < self.data.open(-1), self.data.close(0) > self.data.open(0)) self.c2 = self.data.close(0) > self.data.open(-1) self.c5 = bt.And(self.data.close(-1) > self.data.open(-1), self.data.close(0) < self.data.open(0)) self.c6 = self.data.close(0) < self.data.open(-1) def next(self): sList = self.sto.get(size=self.p.speriod) sLow = min(self.data.low.get(size=self.p.speriod)) lLow1 = min(self.data.low.get(ago=-1, size=self.p.lperiod)) lLow2 = min(self.data.low.get(ago=-2, size=self.p.lperiod)) lLow3 = min(self.data.low.get(ago=-3, size=self.p.lperiod)) sHigh = max(self.data.high.get(size=self.p.speriod)) lHigh1 = max(self.data.high.get(ago=-1, size=self.p.lperiod)) lHigh2 = max(self.data.high.get(ago=-2, size=self.p.lperiod)) lHigh3 = max(self.data.high.get(ago=-3, size=self.p.lperiod)) c1 = self.c1 c2 = self.c2 c3 = sLow < lLow1 or sLow < lLow2 or sLow < lLow3 c4 = len(list(filter(lambda x: (x < self.p.sto_oversold), sList))) > 0 tLong = c1 and c2 and c3 and c4 c5 = self.c5 c6 = self.c6 c7 = sHigh > lHigh1 or sHigh > lHigh2 or sHigh > lHigh3 c8 = len(list(filter(lambda x: (x > self.p.sto_overbought), sList))) > 0 tShort = c5 and c6 and c7 and c8 if tLong: self.revpoint[0] = 1.0 elif tShort: self.revpoint[0] = -1.0 else: self.revpoint[0] = 0.0
i also changed this calculation:
c8 = False for i in self.sto.get(size=3): if i > 80: c8 = True break
to this
c8 = len(list(filter(lambda x: (x > self.p.sto_overbought), sList))) > 0