可以帮助你的圣杯策略:Ma8
温馨提示:本文最后更新于2023年4月1日 21:01,若内容或图片失效,请在下方留言或联系博主。
1# 屁话
偶然发现的一款策略,能神奇的不改参数适应市场
并且2018年到现在不改参数都是盈利的
比所谓的拟合策略强不知道多少倍,哈哈
后来这个策略作为一个因子伴随了我几个月的时间用于实盘
总比一直放在收藏夹吃灰,还不如分享给你们让我赚点流量
仅供分享思路,真要实盘赚钱,你要做的还很多
仅供分享思路,真要实盘赚钱,你要做的还很多
2# 原理
开仓:连续8根收盘价大于均线,最后一根收盘价小于均线
平仓:持仓3根k线,或突破最高点,或ATR,或固定止盈止损
3# 回测
4# 源码
原版:
https://www.tradingview.com/script/gl17fzdf-8-Day-Extended-Runs/
自己修改的:
import talib
import pandas as pd
from main.zhibiao import *
seting = {
"备注": "禁用",
"name": "Ma8",
"symbol": "BTCUSDT_1h_Buy",
"kTime": "1h",
"ma_len": 29,
"ma_type": "lsma",
"ma_src": "o+h+l+c",
"pianli": 14,
"pingcang": 4,
"tupo": 34,
"atr_len": 43,
"atr_mult": 2.871,
"atr_type": "rma",
"buy": 1,
"buyZhiying": 96.216,
"buyZhisun": 12.06,
"sell": 0,
"sellZhiying": 0,
"sellZhisun": 0,
"kai": 100
}
"""
MA连续偏离8次入场,十根K线后出场
"""
def Ma8(r, df, seting):
c = len(df)
df['ma_src'] = GetSrc(r, seting['ma_src'])
df['ma'] = GetMa(df['ma_src'], seting['ma_len'], seting['ma_type'])
df['atr'] = ATR(r, seting['atr_len'], seting['atr_type'])
df['zhisun'] = 0.0
if seting['buy']:
df['h'] = df['High'].rolling(seting['tupo']).max()
df['l2'] = df['Low'].rolling(seting['pianli']).min()
if seting['sell']:
df['l'] = df['Low'].rolling(seting['tupo']).min()
df['h2'] = df['High'].rolling(seting['pianli']).max()
df['da'] = 0
df['xiao'] = 0
df['buy'] = 0
# df['pingtime'] = 0
for i in range(c):
if i > seting['ma_len']:
if df['Close'][i] > df['ma'][i] and (df['Close'][i-1] < df['ma'][i-1] or df['da'][i-1]):
df['da'].values[i] = df['da'][i-1] + 1
if df['Close'][i] < df['ma'][i] and (df['Close'][i-1] > df['ma'][i-1] or df['xiao'][i-1]):
df['xiao'].values[i] = df['xiao'][i-1] + 1
preSide = 0
preZhisun = 0
preZhisunBili = 0
preZhiYingBili = 0
buy = 0
sell = 0
for i in range(c):
if i > seting['ma_len'] and i+1 < c:
if seting['buy']:
#连续多少根 大于 均线之上
if df['Close'][i] < df['ma'][i] and df['da'][i-1] > seting['pianli']: #and df['Close'][i] > df['l2'][i]
df['side'].values[i] = 'BUY'
if preSide != 'BUY':
preSide = 'BUY'
preZhisun = df['Open'][i+1] - seting['atr_mult'] * df['atr'][i]
preZhisunBili = GetRoeJia(-seting['buyZhisun'], {'liang': 1, 'jiage': df['Open'][i+1]})
preZhiYingBili = GetRoeJia(seting['buyZhiying'], {'liang': 1, 'jiage': df['Open'][i+1]})
# if not buy:
# df['pingtime'].values[i] = df['Time'][i] + (df['Time'][1] - df['Time'][0]) * (seting['pingcang'] + 2)
# df['pingtime'].values[i] = GetTime(df['pingtime'].values[i], '%Y-%m-%d %H:%M')
buy += 1
elif buy:
buy += 1
if buy > seting['pingcang']+1 or (seting['tupo'] and df['Close'][i] >= df['h'][i-1]):
df['close'].values[i] = 'BUY'
buy = 0
if preSide == "BUY":
preSide = 0
df['buy'].values[i] = buy
if seting['sell']:
if df['Close'][i] > df['ma'][i] and df['xiao'][i-1] > seting['pianli']: # and df['Close'][i] < df['h2'][i]
df['side'].values[i] = 'SELL'
if preSide != 'SELL':
preSide = 'SELL'
preZhisun = df['Open'][i+1] + seting['atr_mult'] * df['atr'][i]
preZhisunBili = GetRoeJia(-seting['sellZhisun'], {'liang': -1, 'jiage': df['Open'][i+1]})
preZhiYingBili = GetRoeJia(seting['sellZhiying'], {'liang': -1, 'jiage': df['Open'][i+1]})
# if not sell:
# df['pingtime'].values[i] = df['Time'][i] + (df['Time'][1] - df['Time'][0]) * (seting['pingcang'] + 2)
# df['pingtime'].values[i] = GetTime(df['pingtime'].values[i], '%Y-%m-%d %H:%M')
sell += 1
elif sell:
sell += 1
if sell > seting['pingcang']+1 or (seting['tupo'] and df['Close'][i] <= df['l'][i-1]):
df['close'].values[i] = 'SELL'
sell = 0
if preSide == "SELL":
preSide = 0
df['zhisun'].values[i] = preZhisun
if (preSide == 'BUY' and df['Low'][i] <= preZhisun) or (preSide == 'SELL' and df['High'][i] >= preZhisun) or \
(preSide == 'BUY' and df['Low'][i] <= preZhisunBili) or (preSide == 'SELL' and df['High'][i] >= preZhisunBili) or \
(preSide == 'BUY' and df['High'][i] >= preZhiYingBili) or (preSide == 'SELL' and df['Low'][i] <= preZhiYingBili):
preSide = 0
buy = 0
sell = 0
return df