Skip to content

Commit

Permalink
+ fixed NtimestrueOk
Browse files Browse the repository at this point in the history
  • Loading branch information
WISEPLAT committed Sep 13, 2022
1 parent e875d0a commit 11ec57b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
70 changes: 47 additions & 23 deletions Strategy2_ROC_KC_UpDownTrend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,42 @@
import random


class Ntimes1(bt.Indicator):
lines = ('ntimes1',)
class NtimestrueOk(bt.Indicator):
lines = ('ntimestrue',)
params = dict(period=10)
plotinfo = dict(plot=True, subplot=True, plotname='Ntimestrue', )

def __init__(self):
#alln = bt.indicators.AllN(self.data, period=self.p.period)
self.l.ntimes1 = self.data - 1
self.l.ntimestrue = bt.indicators.AllN(self.data1, period=self.p.period)


class Ntimestrue(bt.Indicator):
lines = ('ntimestrue',)
params = dict(period=10)
plotinfo = dict(plot=True, subplot=True, plotname='Ntimestrue', )

def __init__(self):
#self.l.ntimestrue = bt.indicators.AllN(self.data, period=self.p.period)
pass

def next(self):
print("hi", self.data[0], self.data1[0]) # -200 => 1

if self.data1[0]:
self.l.ntimestrue[0] = 1.0
else:
self.l.ntimestrue[0] = 0.0

#self.l.ntimestrue[0] = random.randint(0, 1)


class And3(bt.Indicator):
lines = ('and3',)
params = dict(data2=1, data3=1)

def __init__(self):
self.l.and3 = bt.And(bt.And(self.data, self.p.data2), self.p.data3)
self.l.and3 = bt.And(self.data, self.p.data2, self.p.data3)
#self.l.and3 = bt.And(bt.And(self.data, self.p.data2), self.p.data3)


class OverUnder(bt.Indicator):
Expand All @@ -31,15 +52,12 @@ def __init__(self):
self.l.overunder = self.data > self.p.data2 # данные над data2 == 1


class OverUnder2(bt.Indicator):
lines = ('overunder',)
class UnderOver(bt.Indicator):
lines = ('underover',)
params = dict(data2=20)

def __init__(self):
# self.l.overunder = bt.If(self.data > self.p.data2, 1.0, 0.0) # данные над data2 == 1
self.l.overunder = self.data > self.p.data2 # данные над data2 == 1
# self.l.overunder = bt.Cmp(self.data, self.p.data2) # данные над data2 == 1
# self.l.overunder = bt.Max(self.data, self.p.data2)
self.l.underover = self.data < self.p.data2 # данные под data2 == 1


class UpDownTrend(bt.Indicator):
Expand All @@ -49,7 +67,8 @@ class UpDownTrend(bt.Indicator):
def __init__(self):
y1 = self.data
y2 = self.data(-self.p.period)
self.l.trend = cond = bt.Cmp(y1, y2) # => 1 если y1 > y2
#self.l.trend = cond = bt.Cmp(y1, y2) # => 1 если y1 > y2 => 0 если y1 == y2 => -1 иначе
self.l.trend = cond = y1 > y2 # => 1 если y1 > y2


class KC(bt.Indicator):
Expand Down Expand Up @@ -166,17 +185,17 @@ def __init__(self):
self.roc = defaultdict(list)
self.kc = defaultdict(list)
self.trend = defaultdict(list)

self.roc_over_0 = defaultdict(list)
self.close_over_kc_top = defaultdict(list)
self.and3 = defaultdict(list)

self.enter_long = defaultdict(list)
self.ntimes1 = defaultdict(list)
self.close_long = defaultdict(list)

for i in range(len(self.datas)):
ticker = list(self.dnames.keys())[i] # key name is ticker name

# self.ema_all1[ticker] = bt.indicators.ExponentialMovingAverage(self.datas[i], period=8)
self.ema_all1[ticker] = bt.indicators.ExponentialMovingAverage(self.datas[i], period=8)
# self.ema_all2[ticker] = bt.indicators.ExponentialMovingAverage(self.datas[i], period=16)

# self.close_under_ema_all10[ticker] = OverUnder(self.ema_all1[ticker].lines.ema, data2=self.ema_all2[ticker].lines.ema)
Expand All @@ -187,16 +206,18 @@ def __init__(self):

self.trend[ticker] = UpDownTrend(self.kc[ticker].lines.top, period=20)

self.roc_over_0[ticker] = OverUnder2(self.roc[ticker].lines.roc100, data2=0.0)
self.roc_over_0[ticker] = OverUnder(self.roc[ticker].lines.roc100, data2=0.0)

self.close_over_kc_top[ticker] = OverUnder(self.datas[i].close, data2=self.kc[ticker].lines.top)

self.enter_long[ticker] = And3(self.trend[ticker].lines.trend,
self.and3[ticker] = And3(self.trend[ticker].lines.trend,
data2=self.roc_over_0[ticker].lines.overunder,
data3=self.close_over_kc_top[ticker].lines.overunder)

self.ntimes1[ticker] = Ntimes1(self.roc_over_0[ticker].lines.overunder, period=20)
#self.ntimes1[ticker] = Ntimes1(self.datas[i].close, period=20)
self.enter_long[ticker] = NtimestrueOk(self.datas[i], self.and3[ticker].lines.and3, period=50)

#self.close_long[ticker] = UnderOver(self.datas[i].close, data2=self.kc[ticker].lines.bot)
self.close_long[ticker] = UnderOver(self.ema_all1[ticker].lines.ema, data2=self.kc[ticker].lines.mid)

# self.sma_all1[ticker] = bt.indicators.SMA(self.datas[i], period=64)
# self.sma_all2[ticker] = bt.indicators.SMA(self.datas[i], period=128)
Expand Down Expand Up @@ -271,9 +292,12 @@ def next(self):
_oc2 = (_open + _close) / 2
_volume = data.volume # ссылка на Объемы # print(volume[0])

#print(bool(self.trend[ticker]), bool(self.close_over_kc_top[ticker]), bool(self.roc_over_0[ticker]), bool(self.and3[ticker]))
print(bool(self.enter_long[ticker]))


print(bool(self.cond1[ticker]), bool(self.test1[ticker]), self.cond1[ticker] == self.test1[ticker])
if self.cond1[ticker] != self.test1[ticker]: print("ERROR***")
#print(bool(self.cond1[ticker]), bool(self.test1[ticker]), self.cond1[ticker] == self.test1[ticker])
#if self.cond1[ticker] != self.test1[ticker]: print("ERROR***")


# # https://www.lfd.uci.edu/~gohlke/pythonlibs/#ta-lib
Expand All @@ -291,7 +315,7 @@ def next(self):

# условие на покупку
if not self.orders[ticker]:
if self.crossover_DK[ticker] == -1 and self.cond1[ticker]: #
if self.enter_long[ticker]: #
lot = self.p.lots[ticker]
percent = 3 # сколько % от депозита использовать на сделку
depo = self.cerebro.broker.get_cash()
Expand Down Expand Up @@ -330,7 +354,7 @@ def next(self):
# self.first_buy[ticker] = True

# условие на продажу take-profit
if self.close_under_ema_all1[ticker] == -1: #
if self.close_long[ticker]: #
self.log(f"SELL CREATE [{ticker}] {self.data.close[0]:.2f}")
self.log_csv(ticker=ticker, signal='SELL', signal_price=_close, size=size)

Expand Down
6 changes: 6 additions & 0 deletions main2_ROC_KC_UpDownTrend.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
cerebro.broker.setcommission(commission=0.01)
#cerebro.broker.set_fundmode(True) # the default is 100

symbols = ('TQBR.AFKS', 'TQBR.AFLT', 'TQBR.ALRS', 'TQBR.CBOM', 'TQBR.CHMF', 'TQBR.DSKY', 'TQBR.ENPG', 'TQBR.FEES',
'TQBR.FIVE', 'TQBR.FIXP', 'TQBR.FLOT', 'TQBR.GAZP', 'TQBR.GMKN', 'TQBR.HHRU', 'TQBR.HYDR', 'TQBR.LKOH',
'TQBR.MAGN', 'TQBR.MGNT', 'TQBR.MOEX', 'TQBR.MTSS', 'TQBR.NLMK', 'TQBR.NVTK', 'TQBR.OZON', 'TQBR.PHOR',
'TQBR.PIKK', 'TQBR.PLZL', 'TQBR.POLY', 'TQBR.ROSN', 'TQBR.RTKM', 'TQBR.SBER', 'TQBR.SNGS', 'TQBR.TATN',
'TQBR.TCSG', 'TQBR.VKCO', 'TQBR.VTBR', 'TQBR.TATNP', 'TQBR.TRNFP', 'TQBR.SBERP', 'TQBR.SNGSP',
'TQBR.AKRN', 'TQBR.SIBN', 'TQBR.LSRG', 'TQBR.RASP', 'TQBR.BSPB', 'TQBR.MTLRP', 'TQBR.RTKMP',)
symbols = ('TQBR.SBER', 'TQBR.GAZP', 'TQBR.LKOH', 'TQBR.GMKN',) # Кортеж тикеров
symbols = ('TQBR.SBER', 'TQBR.LKOH', ) # Кортеж тикеров
symbols = ('TQBR.SBER', )
Expand Down

0 comments on commit 11ec57b

Please sign in to comment.