Loading ...

k线判断理论

Author: jcinedc, Date: 2021-08-17 19:06:49
Tags:

理论时间是国际时区,回测胜率在百分之八十左右,开仓数量可以自己调整


'''backtest
start: 2021-08-10 00:00:00
end: 2021-08-18 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
args: [["siven",false]]
'''

def main():
    import time 
    import json
    import datetime
    time_hh = []
    time_mm = []
    buy_tick = []
    buy_list = []
    buy_over = []
    buy_come = []
    line = []
    line_list = []
    sell_list = []
    sell_tick = []
    pan = []
    hangqing = []
    a = 0
    b = 0
    c = "None"
    d = "None"
    exchange.SetContractType("swap")
    exchange.SetMarginLevel(lever)
    ticker = _C(exchange.GetTicker)
    account = _C(exchange.GetAccount)
    position = exchange.GetPosition()
    timestamp = time.time()
    localtime = time.localtime(timestamp)
    # 将localtime转换成想要的时间格式
    daytime = time.strftime("%Y-%m-%d %H:%M:%S", localtime)
    d1 = datetime.datetime.strptime(daytime, '%Y-%m-%d %H:%M:%S')
    x = '初始化时间(国际时区):'
    y = daytime
    if chart:
        LogProfit(0,'&')
    Log("策略开始!")
            
    while True:
        start =time.clock()
        exchange.SetContractType("swap")
        exchange.SetMarginLevel(lever)
        ticker = _C(exchange.GetTicker)
        account = _C(exchange.GetAccount)
        position = exchange.GetPosition()
        
        r = _C(exchange.GetRecords, PERIOD_M1)
        _CDelay(2000)
        for i in r.Time:
            time_local = time.localtime(i/1000)
            ot = time.strftime("%M", time_local)
            time_mm.append(int(ot))
            if len(time_mm) == 100:
                del time_mm[0]
            
        r = _C(exchange.GetRecords, PERIOD_H1)
        _CDelay(2000)
        for i in r.Time:
            # 转换成localtime
            time_local = time.localtime(i/1000)
            # 转换成新的时间格式(精确到秒)
            dt = time.strftime("%I", time_local)
            time_hh.append(int(dt))
            if len(time_hh) == 100:
                del time_hh[0]
                
        
        if timetishi:                
            if time_hh[-1] == 11 and time_mm[-1] == 50 and len(pan) == 0:
                Log('还有十分钟收盘!')
                pan.append(1000)
                Sleep(1000*60*2)
                pan.clear()
            elif time_hh[-1] == 11 and time_mm[-1] == 54 and len(pan) == 0:
                Log('还有五分钟收盘!')
                pan.append(2000)
                Sleep(1000*60*2)
                pan.clear()
        
        if chart:
            if time_mm[-1] % 1 == 0 and len(position) > 0:
                LogProfit(position[0]["Profit"],'&')
                a = position[0]["Profit"]
                b = position[0]["Amount"]
                c = position[0]["Type"]
                if c == 1:
                    d = "做空#FF0000"
                elif c == 0:
                    d = "做多#00FF00"
                LogProfitReset(2000)
            elif time_mm[-1] % 1 == 0 and len(position) == 0:
                a = 0
                b = 0
                d = "None"
            
                
            timestamp1 = time.time()
            localtime1 = time.localtime(timestamp1)
            # 将localtime转换成想要的时间格式
            daytime1 = time.strftime("%Y-%m-%d %H:%M:%S", localtime1)
            d1 = datetime.datetime.strptime(daytime1, '%Y-%m-%d %H:%M:%S')
            e = '当前时间:'
            f = daytime1
                
            
            table = {
                "type": "table",
                "title": "持仓信息",
                "cols": ["币种","方向","当前价格","未实现盈亏","数量","循环延时"], 
                "rows": []
            } 
        
        
        if siven:
            if time_hh[-1] == 12 and time_mm[-1] == 0 and len(buy_list) == 0 and r[-2]["Open"]-r[-2]["Close"] > 0 and r[-2]["Open"]-r[-2]["Close"] <= 500:
                exchange.SetDirection("buy")
                exchange.Buy(_N(ticker['Last'],2), quantity,"震荡收阴开多")
                sell_list.append(ticker['Last'])
                line.append(ticker['Last'])
                buy_list.append(10000)
            elif len(buy_list) == 1:
                if ticker['Last'] > line[0]+300:
                    exchange.SetDirection("closebuy")
                    exchange.Sell(_N(ticker['Last'],2),quantity,"止盈",'#238E23')
                    a = _N((ticker['Last']-sell_list[0])*quantity,2)
                    Log("收益",a)
                    sell_list.clear()
                    line.clear()
                    buy_list.clear()
                elif ticker['Last'] < line[0]-300:
                    exchange.SetDirection("closebuy")
                    exchange.Sell(_N(ticker['Last'],2),quantity,"止损",'#BC1717')
                    a = _N((ticker['Last']-sell_list[0])*quantity,2)
                    Log("收益",a)
                    sell_list.clear()
                    line.clear()
                    buy_list.clear()
                
            if time_hh[-1] == 12 and time_mm[-1] == 0 and len(buy_over) == 0 and r[-2]["Open"] - r[-2]["Close"] < 0 and r[-2]["Open"] - r[-2]["Close"] >= -500:
                exchange.SetDirection("sell")
                exchange.Sell(_N(ticker['Last'],2),quantity,"震荡收阳开空")
                sell_list.append(ticker['Last'])
                line.append(ticker['Last'])
                buy_over.append(10000)
            elif len(buy_over) == 1:
                if ticker['Last'] < line[0]-300:
                    exchange.SetDirection("closesell")
                    exchange.Buy(_N(ticker['Last'],2),quantity,"止盈",'#238E23')
                    a = _N((sell_list[0]-ticker['Last'])*quantity,2)
                    Log("收益",a)
                    sell_list.clear()
                    line.clear()
                    buy_over.clear()
                elif ticker['Last'] > line[0]+300:
                    exchange.SetDirection("closesell")
                    exchange.Buy(_N(ticker['Last'],2),quantity,"止损",'#BC1717')
                    a = _N((sell_list[0]-ticker['Last'])*quantity,2)
                    Log("收益",a)
                    sell_list.clear()
                    line.clear()
                    buy_over.clear()

            if time_hh[-1] == 12 and time_mm[-1] == 0 and len(buy_list) == 0 and r[-2]["Open"] - r[-2]["Close"] > 500:
                Log("准备砸盘开多")
                line.append(ticker['Last'])
                if ticker['Last'] < line[0]-200:
                    exchange.SetDirection("buy")
                    exchange.Buy(_N(ticker['Last'],2), quantity,"砸盘开多")
                    sell_list.append(ticker['Last'])
                    line.clear()
                    line.append(ticker['Last'])
                    buy_list.append(10000)
            elif len(buy_list) == 1:
                if ticker['Last'] > line[0]+300:
                    exchange.SetDirection("closebuy")
                    exchange.Sell(_N(ticker['Last'],2),quantity,"止盈",'#238E23')
                    a = _N((ticker['Last']-sell_list[0])*quantity,2)
                    Log("收益",a)
                    sell_list.clear()
                    line.clear()
                    buy_list.clear()
                elif ticker['Last'] < line[0]-300:
                    exchange.SetDirection("closebuy")
                    exchange.Sell(_N(ticker['Last'],2),quantity,"止损",'#BC1717')
                    a = _N((ticker['Last']-sell_list[0])*quantity,2)
                    Log("收益",a)
                    sell_list.clear()
                    line.clear()
                    buy_list.clear()

            if time_hh[-1] == 12 and time_mm[-1] == 0 and len(buy_list) == 0 and r[-2]["Open"] - r[-2]["Close"] < -500:
                line.append(ticker['Last'])
                Log("准备拉盘开空")
                if ticker['Last'] > line[0]+200 and len(buy_list) == 0:
                    exchange.SetDirection("sell")
                    exchange.Sell(_N(ticker['Last'],2),quantity,"拉盘开空")
                    sell_list.append(ticker['Last'])
                    line.clear()
                    line.append(ticker['Last'])
                    buy_list.append(10000)
            elif len(buy_list) == 1:
                if ticker['Last'] < line[0]-300:
                    exchange.SetDirection("closesell")
                    exchange.Buy(_N(ticker['Last'],2), quantity,"止盈",'#238E23')
                    a = _N((sell_list[0]-ticker['Last'])*quantity,2)
                    Log("收益",a)
                    sell_list.clear()
                    line.clear()
                    buy_list.clear()
                elif ticker['Last'] > line[0]+300:
                    exchange.SetDirection("closesell")
                    exchange.Buy(_N(ticker['Last'],2), quantity,"止损",'#BC1717')
                    a = _N((sell_list[0]-ticker['Last'])*quantity,2)
                    Log("收益",a)
                    sell_list.clear()
                    LogProfit(account["Balance"])
                    line.clear()
                    buy_list.clear()
        
        if fangxiang:
            if time_hh[-1] == 1 and time_mm[-1] == 2 and len(buy_tick) == 0 and r[-3]["Open"] - r[-3]["Close"] < -100:
                if r[-2]["Close"] >= r[-3]["Open"]+(r[-3]["Close"]-r[-3]["Open"])*0.5:
                    line_list.append(ticker['Last'])
                    exchange.SetDirection("buy")
                    exchange.Buy(_N(ticker['Last'],2),trend,"方向看多")
                    sell_tick.append(ticker['Last'])
                    buy_tick.append(1)
                else:
                    Log("没有触发方向单")
            elif len(buy_tick) == 1:
                if ticker['Last'] > line_list[0]+700 and len(position) > 0:
                    exchange.SetDirection("closebuy")
                    exchange.Sell(_N(ticker['Last'],2),trend,"方向止盈",'#238E23')
                    a = _N((ticker['Last']-sell_tick[0])*quantity,2)
                    Log("收益",a)
                    sell_tick.clear()
                    buy_tick.clear()
                    line_list.clear()
                elif ticker['Last'] < line_list[0]-700 and len(position) > 0:
                    exchange.SetDirection("closebuy")
                    exchange.Sell(_N(ticker['Last'],2),trend,"方向止损",'#BC1717')
                    a = _N((ticker['Last']-sell_tick[0])*quantity,2)
                    Log("收益",a)
                    sell_tick.clear()
                    buy_tick.clear()
                    line_list.clear()
                elif time_hh[-1] == 4 and time_mm[-1] == 0:
                    if ticker['Last'] > line_list[0] and len(position) > 0:
                        exchange.SetDirection("closebuy")
                        exchange.Sell(_N(ticker['Last'],2),trend,"到点止盈",'#238E23')
                        a = _N((ticker['Last']-sell_tick[0])*quantity,2)
                        Log("收益",a)
                        sell_tick.clear()
                        buy_tick.clear()
                        line_list.clear()
                    elif ticker['Last'] < line_list[0] and len(position) > 0:
                        exchange.SetDirection("closebuy")
                        exchange.Sell(_N(ticker['Last'],2),trend,"到点止损",'#BC1717')
                        a = _N((ticker['Last']-sell_tick[0])*quantity,2)
                        Log("收益",a)
                        sell_tick.clear()
                        buy_tick.clear()
                        line_list.clear()
                    
            if time_hh[-1] == 1 and time_mm[-1] == 2 and len(buy_come) == 0 and r[-3]["Open"] - r[-3]["Close"] > 100:
                if r[-2]["Close"] <= r[-3]["Open"]-(r[-3]["Open"]-r[-3]["Close"])*0.5:
                    line_list.append(ticker['Last'])
                    exchange.SetDirection("sell")
                    exchange.Sell(_N(ticker['Last'],2),trend,"方向看空")
                    sell_tick.append(ticker['Last'])
                    buy_come.append(1)
                else:
                    Log("没有触发方向单")
            elif len(buy_come) == 1:
                if ticker['Last'] < line_list[0]-700 and len(position) > 0:
                    exchange.SetDirection("closesell")
                    exchange.Buy(_N(ticker['Last'],2),trend,"方向止盈",'#238E23')
                    a = _N((sell_tick[0]-ticker['Last'])*quantity,2)
                    Log("收益",a)
                    sell_tick.clear()
                    buy_come.clear()
                    line_list.clear()
                elif ticker['Last'] > line_list[0]+700 and len(position) > 0:
                    exchange.SetDirection("closesell")
                    exchange.Buy(_N(ticker['Last'],2),trend,"方向止损",'#BC1717')
                    a = _N((sell_tick[0]-ticker['Last'])*quantity,2)
                    Log("收益",a)
                    sell_tick.clear()
                    buy_come.clear()
                    line_list.clear()
                elif time_hh[-1] == 4 and time_mm[-1] == 0:
                    if ticker['Last'] < line_list[0] and len(position) > 0:
                        exchange.SetDirection("closesell")
                        exchange.Buy(_N(ticker['Last'],2),trend,"到点止盈",'#238E23')
                        a = _N((sell_tick[0]-ticker['Last'])*quantity,2)
                        Log("收益",a)
                        sell_tick.clear()
                        buy_come.clear()
                        line_list.clear()
                    elif ticker['Last'] > line_list[0] and len(position) > 0:
                        exchange.SetDirection("closesell")
                        exchange.Buy(_N(ticker['Last'],2),trend,"到点止损",'#BC1717')
                        a = _N((sell_tick[0]-ticker['Last'])*quantity,2)
                        Log("收益",a)
                        sell_tick.clear()
                        buy_come.clear()
                        line_list.clear()
            elif len(buy_come) == 1 and len(position) == 0:
                Sleep(1000*10)
                if len(buy_come) == 1 and len(position) == 0:
                    sell_tick.clear()
                    buy_come.clear()
                    line_list.clear()
                    Log("已手动平仓,本次策略结束")
            
            end = time.clock()
            table["rows"].append([
                exchange.GetCurrency(),
                d,
                ticker['Last'],
                a,
                b,
                '%s ms' % int((end-start)*1000)
            ])
            LogStatus("" + x,y + "\n" + 
                "" + e,f + "\n" +
                "`" + json.dumps(table) + "`")
        Sleep(1000)
                    
                    

More

hatepop 实测胜率如何啊

tony233 哥们,你chart,siven还有那个fangxiang这几个啥意思啊?为啥你没定义函数也能用啊

bobly 老哥的思路是怎么样的啊

tony233 啊,还可以这样用啊,学到了

jcinedc 策略参数啊