锤子线趋势策略
Posted jarviswhj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了锤子线趋势策略相关的知识,希望对你有一定的参考价值。
之前一段时间研究了下数字货币的量化交易,决定自己动手写一个最简单的,这个策略非常简单,就是出现上升趋势锤子线的时候卖出,下降趋势锤子线的时候进行买入,没有仓位管理和止盈止损,每次全仓操作。
代码说明:使用的平台是BotVs,KNum是参数,表示用来判断趋势的k线数量,代码中涉及的api可以在平台文档上找到说明。
‘‘‘backtest start: 2018-10-01 00:00:00 end: 2018-10-21 00:00:00 period: 1h exchanges: [{"eid":"OKCoin_EN","currency":"BTC_USD","stocks":0,"fee":[0,0]}] ‘‘‘ def main(): while True: #获取k线数据 records = exchange.GetRecords() account = exchange.GetAccount() recordsLen = len(records) if recordsLen <KNum+1: Log("k线总数小于最小k线数无法计算") Sleep(1000) continue processRecords=records[recordsLen-KNum-1:recordsLen-2] # 判断第一根线是阴线还是阳线 isUp = processRecords[0].Open < processRecords[0].Close isDown = processRecords[0].Open > processRecords[0].Close # 锤子线之前是否是一个连续的下跌/上涨趋势 isTrend = IsTrend(processRecords[0:len(processRecords)-2]) # 最后一根线是否是锤子线 isHammerLine = IsHammerLine(processRecords[len(processRecords)-2]) if isUp and isTrend and isHammerLine: # 市价买入 if account.Balance>0 and account.FrozenBalance==0: exchange.Buy(-1,account.Balance) Log("买入:",account.Balance,"BTC:",records[len(records)-1].Close) if isDown and isTrend and isHammerLine: # 市价卖出 if account.Stocks>0 and account.FrozenStocks==0: exchange.Sell(-1,account.Stocks) Log("卖出:",account.Stocks,"BTC:",records[len(records)-1].Close) Sleep(1000) def IsTrend(processRecords): fstRecord=processRecords[0] lastLow = fstRecord.Low if fstRecord.Open < fstRecord.Close: for record in processRecords[1:len(processRecords)-1]: if lastLow<=record.Low: return False elif fstRecord.Open > fstRecord.Close: for record in processRecords[1:len(processRecords)-1]: if lastLow>=record.Low: return False else: return False return True def IsHammerLine(record): high = max(record.Open,record.Close) low = min(record.Open,record.Close) return (high-low) *3<=low-record.Low
以上是关于锤子线趋势策略的主要内容,如果未能解决你的问题,请参考以下文章