码迷,mamicode.com
首页 > 移动开发 > 详细

锤子线趋势策略

时间:2018-10-21 16:53:55      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:ack   down   min   def   otv   出现   卖出   and   open   

之前一段时间研究了下数字货币的量化交易,决定自己动手写一个最简单的,这个策略非常简单,就是出现上升趋势锤子线的时候卖出,下降趋势锤子线的时候进行买入,没有仓位管理和止盈止损,每次全仓操作。

代码说明:使用的平台是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

 

锤子线趋势策略

标签:ack   down   min   def   otv   出现   卖出   and   open   

原文地址:https://www.cnblogs.com/jarviswhj/p/9825415.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!