DAY 21
0
AI & Data

# 一、策略

## 為什麼要使用策略?

• 理性投資，避免因人性恐慌而導致損失。
• 績效驗證，可透過歷史資料進行驗證。

# 二、常用策略

## All in

``````class Allin(ConsStrategy):
# Class variable for parameters tuning
def init(self):
super().init()
self.conservative = False
self.tag = True

def next(self):
if self.tag:
self.tag = False
``````

## 「只買不賣」Continue Holding

``````class Allin(ConsStrategy):
# Class variable for parameters tuning
def init(self):
super().init()
self.conservative = False
self.tag = True

def next(self):
if self.tag:
self.tag = False
``````

## MA Crossover

``````# SMA
# -----------------------------------
class SmaCrossCons(ConsStrategy):
# Class variable for parameters tuning
fast_days = 5
slow_days = 15

def init(self):
super().init()

self.fast_line = self.I(ta.sma, self.data.Close.s, self.fast_days)
self.slow_line = self.I(ta.sma, self.data.Close.s, self.slow_days)
self.conservative = True

def next(self):
if crossover(self.fast_line, self.slow_line):

elif crossover(self.slow_line, self.fast_line):

if self.conservative == False:
self.sell()
``````

## MACD Crossover

``````# MacdCrossCons
# https://rich01.com/what-is-macd-indicator/
# -----------------------------------
class MacdCrossCons(ConsStrategy):
# Class variable for parameters tuning
fast_days = 12
slow_days = 26
sig_days = 9

def init(self):
super().init()

self.MACD = self.I(
ta.macd,
self.data.Close.s,
self.fast_days,
self.slow_days,
self.sig_days,
plot=False,
)

self.OSC = self.I(
lambda A: A[2],
self.MACD,
name=f"Hist({self.sig_days},{self.fast_days},{self.slow_days})",
plot=True,
)

self.conservative = True

def next(self):
if crossover(self.OSC, 0):

elif crossover(0, self.OSC):

if self.conservative == False:
self.sell()

``````

## KDJ Check

``````class KCheckCons(ConsStrategy):
# Class variable for parameters tuning
kd_days = 14
high_cape = 80
low_cape = 20

def init(self):
super().init()

self.KDJ = self.I(
ta.kdj,
high=self.data.High.s,
low=self.data.Low.s,
close=self.data.Close.s,
length=self.kd_days,
plot=False,
)

self.K = self.I(lambda A: A[0], self.KDJ, plot=True)
self.D = self.I(lambda A: A[1], self.KDJ, plot=True)

self.conservative = True

def next(self):
# print(self.K.shape)
if crossover(self.low_cape, self.K):

elif crossover(self.K, self.high_cape):

if self.conservative == False:
self.sell()
``````

## KD Crossover

``````class KDCrossCons(ConsStrategy):
# Class variable for parameters tuning
kd_days = 14

def init(self):
super().init()

self.KDJ = self.I(
ta.kdj,
high=self.data.High.s,
low=self.data.Low.s,
close=self.data.Close.s,
length=self.kd_days,
plot=False,
)

self.K = self.I(lambda A: A[0], self.KDJ, plot=True)
self.D = self.I(lambda A: A[1], self.KDJ, plot=True)
# self.D = self.I(np.take, self.KDJ, 1, plot=True)
# self.D = self.I(SMA, self.data.Close, self.kd_days, plot=False)

self.conservative = True

def next(self):
# print(self.K.shape)
if crossover(self.K, self.D):

elif crossover(self.D, self.K):

if self.conservative == False:
self.sell()
``````

## Bias Check

``````class BiasCheckCons(ConsStrategy):
# Class variable for parameters tuning
sma_days = 10
high_cape = 0.08
low_cape = -0.07

def init(self):
super().init()

self.Bias = self.I(
ta.bias,
close=self.data.Close.s,
length=self.sma_days,
plot=False,
)

self.conservative = True

def next(self):
# print(self.K.shape)
if crossover(self.Bias, self.high_cape):