海龟模型 改成WH8 [文华财经]
-
咨询内容:
请教老师,可否帮忙改成WH8格式?示范中的和这个不一样。
import talib as taimport numpy as npfrom EsSeries import NumericSeriesimport EsTalib
g_params['axPositionNum' = 99 g_params['topPoint' = 30 g_params['inPoint' = 50 g_params['loatStopStart' = 40 g_params['loatStopPoint' = 10
p1= 7 p = 3 p2 = 50 RiskRatio = 10 ATRLength = 20 *p1 boLength = 20 *p1 fsLength = 55 *p1 teLength = 10 *p1 LPTF = True PreEP = 0 AvgTR = 1
SendOrderThisBar = False PreBreakoutFailure = False
myEntryPrice = 0myExitPrice = 0DonchianHi = 0DonchianLo = 0fsDonchianHi = 0fsDonchianLo = 0ExitHighestPrice = 0ExitLowestPrice = 0
ContractId = 'HFE|F|RB|2010'br/>def initialize(context): global ContractId SetBarInterval(ContractId, '' 1, 5000) SetActual()
def handle_data(context): global ContractId
bars = HisBarsInfo() barLen = len(bars) if barLen < 10: return
def condict(): a = (0.090000 < Time(ContractId, Enum_Period_Min(), 1) < 0.240000) return a
def initialize(context): SetOrderWay(2)
def TrueRange(barsinfo:list): if len(barsinfo) <= 0: return None, None, None
ths = [] tls = [] trs = [] hs = [] ls = []
for i, p in enumerate(barsinfo): high = barsinfo[i]["HighPrice"] low = barsinfo[i]["LowPrice"] hs.append(high) ls.append(low)
if i == 0: th = high tl = low else: pclose = barsinfo[i-1]["LastPrice"] th = high if high >= pclose else pclose tl = low if low <= pclose else pclose
tr = th - tl ths.append(th) tls.append(tl) trs.append(tr)
return np.array(hs), np.array(ls), np.array(trs)
def XAverage(prices:np.array, length): fac = 2.0/(length+1) xa = 1
if len(prices) < length: return xa
for i, p in enumerate(prices): if i == 0: xa = p else: xa = xa + fac * (p - xa) return xa
def handle_data(context): global RiskRatio, ATRLength, boLength global teLength, fsLength, LPTF global PreEP, AvgTR, SendOrderThisBar global PreBreakoutFailure, ContractId global myEntryPrice, myExitPrice global ExitHighestPrice, ExitLowestPrice global DonchianHi, DonchianLo global fsDonchianHi, fsDonchianLo
if BarStatus() == 0: PreEP = np.nan PreBreakoutFailure = False
MinPoint = PriceTick()
barsinfo = HisBarsInfo()
bslen = len(barsinfo)
if bslen < fsLength: return
op = barsinfo[-1]["OpeningPrice"]
highs, lows, trs = TrueRange(barsinfo) high = highs[-1] low = lows[-1] phigh = high if len(highs) <= 1 else highs[-2] plow = low if len(lows) <= 1 else lows[-2]
N = AvgTR AvgTR = XAverage(trs, ATRLength)
TotalEquity = Available() + Margin() TurtleUnits = (TotalEquity*RiskRatio/100)/(N*ContractUnit(ContractId)) TurtleUnits = int(TurtleUnits) maxUnites = int(Available()/(high*ContractUnit(ContractId))) TurtleUnits = min(maxUnites , TurtleUnits) LogInfo("avl:%f, mar:%f, conu:%f, tu:%f, N:%f, mp:%f, cidx:%d, bslen:%d\n" \ %(Available(), Margin(), ContractUnit(ContractId), TurtleUnits, N, MinPoint, CurrentBar(), bslen))
DonchianHi = ta.MAX(highs[:-1], timeperiod=boLength)[-1] DonchianLo = ta.MIN(lows[:-1], timeperiod=boLength)[-1] fsDonchianHi = ta.MAX(highs[:-1], timeperiod=fsLength)[-1] fsDonchianLo = ta.MIN(lows[:-1], timeperiod=fsLength)[-1] ExitHighestPrice = ta.MAX(highs[:-1], timeperiod=teLength)[-1] ExitLowestPrice = ta.MIN(lows[:-1], timeperiod=teLength)[-1]
LogInfo("mpos:%d, bpos:%d, spos:%d\n" %(MarketPosition(), BuyPosition(), SellPosition())) LogInfo("low:%f,high:%f,dh:%f,dl:%f,fdh:%f,fdl:%f,ehp:%f,elp:%f,pep:%f,n:%f\n" %(low, high, DonchianHi, \ DonchianLo, fsDonchianHi, fsDonchianLo, ExitHighestPrice, ExitLowestPrice,PreEP,N))
if MarketPosition() == 0 and ((not LPTF) or PreBreakoutFailure) and condict(): if Close()[-1] > DonchianHi + p*PriceTick() and Open()[-1]-Open()[-2] < 200*PriceTick() and Close()[-1] > Open()[-1] and TurtleUnits >= 1 and Close()[-1] - Open()[-1] < p2*PriceTick() and Close()[-1] > max(Close()[-2] , Open()[-2]) : myEntryPrice = min(high, DonchianHi + MinPoint) myEntryPrice = max(op, myEntryPrice) PreEP = myEntryPrice Buy(TurtleUnits, Close()[-1]+1*PriceTick()) SendOrderThisBar = True PreBreakoutFailure = False
if Close()[-1] < DonchianLo - p*PriceTick() and Open()[-2]-Open()[-1]<200*PriceTick() and Close()[-1] < Open()[-1] and TurtleUnits >= 1 and Open()[-1] - Close()[-1] < p2*PriceTick() and Close()[-1] < min(Close()[-2] , Open()[-2]) : # myEntryPrice = max(low, DonchianLo - MinPoint) # myEntryPrice = min(op, myEntryPrice) PreEP = myEntryPrice SellShort(TurtleUnits, Close()[-1]-1*PriceTick()) SendOrderThisBar = True PreBreakoutFailure = False
# if MarketPosition() == 0 and condict(): if Close()[-1] > fsDonchianHi + p*PriceTick() and Open()[-1]-Open()[-2]<100*PriceTick() and Close()[-1] > Open()[-1] and TurtleUnits >= 1 and Close()[-1] - Open()[-1] < p2*PriceTick() and Close()[-1] > max(Close()[-2] , Open()[-2]) : # myEntryPrice = min(high, fsDonchianHi + MinPoint) # myEntryPrice = max(op, myEntryPrice) PreEP = myEntryPrice Buy(TurtleUnits, Close()[-1]) SendOrderThisBar = True PreBreakoutFailure = False
if Close()[-1] < fsDonchianLo - p*PriceTick() and Open()[-2]-Open()[-1]<100*PriceTick() and Close()[-1] < Open()[-1] and TurtleUnits >= 1 and Open()[-1] - Close()[-1] < p2*PriceTick() and Close()[-1] < min(Close()[-2] , Open()[-2]): # myEntryPrice = max(low, fsDonchianLo - MinPoint) # myEntryPrice = min(op, myEntryPrice) PreEP = myEntryPrice SellShort(TurtleUnits, Close()[-1]) SendOrderThisBar = True PreBreakoutFailure = False
# if MarketPosition() == 1 and condict(): if Close()[-1] < ExitLowestPrice : myExitPrice = max(low, ExitLowestPrice - MinPoint) myExitPrice = min(op, myExitPrice) # 全平多仓 bpos = BuyPosition() if bpos > 0: Sell(bpos, Close()[-1]-1*PriceTick() ) else: if PreEP != np.nan and TurtleUnits >= 1 and BuyPosition() <= 400 and Close()[-1] - Open()[-1] < p2*PriceTick() and Close()[-1] > max(Close()[-2] , Open()[-2]) : if Close()[-1] >= PreEP + 0.5*N and Close()[-1] > Open()[-1] : myEntryPrice = high PreEP = myEntryPrice Buy(TurtleUnits, Close()[-1]) SendOrderThisBar = True if PreEP != np.nan and BuyPosition() >= 400 and Close()[-1] - Open()[-1] < p2*PriceTick() and Close()[-1] > max(Close()[-2] , Open()[-2]): if Close()[-1] >= PreEP + 0.5*N and Close()[-1] > Open()[-1] : myEntryPrice = high PreEP = myEntryPrice Buy(int(TurtleUnits/3), Close()[-1]) SendOrderThisBar = True
# if (Close()[-1] <= (PreEP - 2*N) - p*PriceTick() or Close()[-1] < Open()[-1]) and SendOrderThisBar == False: myExitPrice = PreEP - 2*N # bpos = BuyPosition() if bpos > 0: Sell(bpos, Close()[-1]-1*PriceTick() ) PreBreakoutFailure = True # elif MarketPosition() == -1 and condict(): # # if Close()[-1] > ExitHighestPrice : myExitPrice = min(high, ExitHighestPrice + MinPoint) # myExitPrice = max(op, myExitPrice) # spos = SellPosition() if spos > 0: BuyToCover(spos, Close()[-1] +1*PriceTick()) else: if PreEP != np.nan and TurtleUnits >= 1 and SellPosition() <= 1500 and Open()[-1] - Close()[-1] < p2*PriceTick() and Close()[-1] < min(Low()[-2] , Low()[-3] , Low()[-4], Low()[-5], Low()[-6], Low()[-7] , Low()[-8], Low()[-9], Low()[-10] ): if Close()[-1] <= PreEP - 0.5*N and Close()[-1] < Open()[-1]: myEntryPrice = low PreEP = myEntryPrice SellShort(TurtleUnits, Close()[-1]) SendOrderThisBar = True if PreEP != np.nan and SellPosition() > 1500 and Open()[-1] - Close()[-1] < p2*PriceTick() and Close()[-1] < min(Low()[-2] , Low()[-3] , Low()[-4], Low()[-5], Low()[-6], Low()[-7] , Low()[-8], Low()[-9], Low()[-10]): if Close()[-1] <= PreEP - 0.5*N and Close()[-1] < Open()[-1]: myEntryPrice = low PreEP = myEntryPrice SellShort(int(TurtleUnits/2), Close()[-1]) SendOrderThisBar = True
# if (Close()[-1] >= (PreEP + 2*N) + p*PriceTick()) and SendOrderThisBar == False: myExitPrice = PreEP + 2*N # spos = SellPosition() if spos > 0: BuyToCover(spos, Close()[-1]+1*PriceTick() ) PreBreakoutFailure = True
# if SellPosition() >= 1800 and condict(): BuyToCover(SellPosition(), Close()[-1]+1*PriceTick()) if SellPosition() >= 1800 and condict(): Sell(SellPosition(), Close()[-1]-1*PriceTick()) # 止损 SetStopPoint(g_params['topPoint') # 止赢 SetWinPoint(g_params['inPoint') # 浮动止损 SetFloatStopPoint(g_params['loatStopStart', g_params['loatStopPoint')
来源:程序化99
-
文华技术人员:
指标源码与麦语言相差太大,无法改写的
或者,您详细描述下指标的取值定义思路,我们根据思路直接帮您编写实现
有思路,想编写各种指标公式,交易模型,选股公式,还原公式的朋友
可联系技术人员 QQ: 262069696 或微信号:cxh99cxh99 进行 有偿收费 编写!
(注:由于人数限制,QQ或微信请选择方便的一个联系我们就行,加好友时请简单备注下您的需求,否则无法通过。谢谢您!)
相关文章
-
没有相关内容