Loading ...

菲阿里四价

Author: 比特币大空头, Date: 2021-03-10 10:10:51
Tags:

摘要

在期货市场,价格呈现一切。几乎所有的技术分析,如均线、布林线、MACD、KDJ等等,这些都是以价格为基础,通过特定的方法计算。包括基本面分析也是如此,通过分析近期和远期价差、期货和现货升贴水、上下游库存等等数据,计算当前价格是否合理,并预估未来的价格。既然如此,为什么不直接研究价格呢?今天我们讲的菲阿里四价策略就是完全根据价格来做出卖决定。

点击阅读更多内容


import time  										# 导入库,用于转换时间格式

mp = 0  												# 虚拟持仓
long_stop = 0                               # 多头止损
long_stop_1 = 0                             # 多头止盈
short_stop = 0                              # 空头止损
short_stop_1 = 0                            # 空头止盈

def allclose(records):
    closes = []
    for record in records:
        closes.append(record['Close'])
    return closes

def trade_time(hour, minute):
    minute = str(minute)
    if len(minute) == 1:
        minute = "0" + minute
    return int(str(hour) + minute)

def onTick():
    bar_arr = _C(exchange.GetRecords, PERIOD_D1)	# 获取日线数组
    if len(bar_arr) < 2:  							# 如果小于2根K线
        return  										# 返回继续等待数据
    yh = bar_arr[-2]['High']  						# 昨日最高价
    yl = bar_arr[-2]['Low']  	                    # 昨日最低价
    today_open = bar_arr[-1]['Open']  				# 当日开盘价
    closes = allclose(bar_arr)
    bar_arr = _C(exchange.GetRecords)  				# 获取当前设置周期K线数组
    
    
    #current = bar_arr[-1]['Time']  					# 获取当前K线时间戳
    current = time.time()
    local = time.localtime(current)  		# 处理时间戳
    hour = int(time.strftime("%H", local))  		# 格式化时间戳,并获取小时
    minute = int(time.strftime("%M", local))  		# 格式化时间戳,并获取分钟
    
    price = bar_arr[-1]['Close']  					# 获取最新价格

    global mp
    global long_stop
    global short_stop 
    global long_stop_1
    global short_stop_1 
    
    # 下单交易
    trading = trade_time(hour, minute)

    if mp > 0:  									# 如果当前持有多单
# 如果当前价格小于多头止损线,或者超过规定的交易时间
        if trading >= 2358 or price < today_open:  
            position = exchange.GetPosition()
            if len(position) > 0:
                Log("Amount:", position[0]["Amount"], "FrozenAmount:", position[0]["FrozenAmount"], "Price:", 
                    position[0]["Price"], "Profit:", position[0]["Profit"], "Type:", position[0]["Type"], 
                    "ContractType:", position[0]["ContractType"], "Price:", price, "long_stop:", long_stop)
            
            exchange.SetDirection("closebuy")  	    # 设置交易方向和类型
            exchange.Sell(-1, 0.3)  			    # 平多单
            exchange.SetDirection("sell")  		    # 设置交易方向和类型
            exchange.Sell(-1, 0.3)  	        # 开空单
            mp = -1  
            return
    if mp < 0:  									# 如果当前持有空单
# 如果当前价格大于空头止损线,或者超过规定的交易时间
        if trading >= 2358 or price > today_open:  
            position = exchange.GetPosition()
            if len(position) > 0:
                Log("Amount:", position[0]["Amount"], "FrozenAmount:", position[0]["FrozenAmount"], "Price:", 
                    position[0]["Price"], "Profit:", position[0]["Profit"], "Type:", position[0]["Type"], 
                    "ContractType:", position[0]["ContractType"], "Price:", price, "Short_stop:", short_stop)
            exchange.SetDirection("closesell")  	# 设置交易方向和类型
            exchange.Buy(-1, 0.3)  				    # 平空单
            exchange.SetDirection("buy")  		    # 设置交易方向和类型
            exchange.Buy(-1, 0.3)  				    # 开多单
            mp = 1  								# 重置虚拟持仓
            return
# 如果当前无持仓,并且在规定的交易时间内
    if mp == 0:  		
        if price > today_open * 1.01:  							# 如果当前价格大于昨天最高价
            exchange.SetDirection("buy")  		    # 设置交易方向和类型
            exchange.Buy(-1, 0.3)  				    # 开多单
            mp = 1  								# 重置虚拟持仓
            position = exchange.GetPosition()
            if len(position) > 0:
                Log("Amount:", position[0]["Amount"], "FrozenAmount:", position[0]["FrozenAmount"], "Price:", 
                    position[0]["Price"], "Profit:", position[0]["Profit"], "Type:", position[0]["Type"], 
                    "ContractType:", position[0]["ContractType"])
                long_stop_1 = position[0]["Price"] * 1.2
            long_stop = yh
            Log("Price:", price, "yh:", yh, "yl:", yl)
        elif price < today_open * 0.99:  						    # 如果价格小于昨天最低价
            exchange.SetDirection("sell")  		    # 设置交易方向和类型
            exchange.Sell(-1, 0.3)  	        # 开空单
            mp = -1  								# 重置虚拟持仓
            position = exchange.GetPosition()
            if len(position) > 0:
                Log("Amount:", position[0]["Amount"], "FrozenAmount:", position[0]["FrozenAmount"], "Price:", 
                    position[0]["Price"], "Profit:", position[0]["Profit"], "Type:", position[0]["Type"], 
                    "ContractType:", position[0]["ContractType"])
                short_stop_1 = position[0]["Price"] * 0.8
            short_stop = yl
            Log("Price:", price, "yh:", yh, "yl:", yl)


    positions = exchange.GetPosition();
     
def main():
    initAccount = _C(exchange.GetAccount)
    Log(initAccount)
    exchange.SetContractType("swap")         # 举例设置为OKEX期货当周合约
    exchange.SetMarginLevel(5);              # 设置杠杆为5倍
    
    while True:   								# 无限循环
        onTick()  								# 执行策略主函数
        Sleep(1000 * 60)  								# 休眠1秒

More