• 在聚宽平台上编写海龟交易法则


    import jqdata
    import math
    import numpy as np
    import pandas as pd
    from collections import deque

    def initialize(context):
        
        set_option('use_real_price', True)
        set_order_cost(OrderCost(close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock')

        g.security = '000060.XSHE'
        set_benchmark(g.security)
        g.in_day = 20
        g.out_day = 10
        g.today_units = 0
        g.current_units = 0
        g.N=deque(maxlen=19)
        g.current_N = 0
        g.last_buy_price = 0
        
        price = attribute_history(g.security, g.N.maxlen*2+1, '1d', ('high', 'low', 'close'))
        
        for i in range(g.N.maxlen+1, g.N.maxlen*2+1):
            li = []
            for j in range(i-19,i+1):
                a = price['high'][j]-price['low'][j]
                b = abs(price['high'][j]-price['close'][j-1])
                c = abs(price['low'][j]-price['close'][j-1])
                li.append(max(a,b,c))
            current_N = np.array(li).mean()
            g.N.append(current_N)
            
        
    def before_trading_start(context):
        g.current_N = cal_N()
        g.today_units = 0

        
    def handle_data(context, data):
        dt = context.current_dt
        current_price = data[g.security].price #上一分钟价格
        value = context.portfolio.total_value
        cash = context.portfolio.available_cash
        
        unit = math.floor(value * 0.01 / g.current_N)
        
            
        if g.current_units == 0:
            buy(current_price, cash, unit)
        else:
            if stop_loss(current_price):
                return
            if sell(current_price):
                return
            addin(current_price, cash, unit)
        
    def cal_N():
        # if len(g.N) < g.N.maxlen:
        #     price = attribute_history(g.security, g.N.maxlen+2, '1d', ('high', 'low', 'close'))
        #     li = []
        #     for i in range(1, g.N.maxlen+2):
        #         a = price['high'][i]-price['low'][i]
        #         b = abs(price['high'][i]-price['close'][i-1])
        #         c = abs(price['low'][i]-price['close'][i-1])
        #         li.append(max(a,b,c))
        #     current_N = np.array(li).mean()
        # else:
        price = attribute_history(g.security, 2, '1d', ('high', 'low', 'close'))
        a = price['high'][1]-price['low'][1]
        b = abs(price['high'][1]-price['close'][0])
        c = abs(price['low'][1]-price['close'][0])
        current_N = (max(a,b,c) + np.array(g.N).sum())/(g.N.maxlen+1)
        g.N.append(current_N)
        return current_N
        
    def buy(current_price, cash, unit):
        price = attribute_history(g.security, g.in_day, '1d', ('close',))
        if current_price > max(price['close']):
            shares = cash / current_price
            if shares >= unit:
                print("buying %d" % unit)
                o = order(g.security, unit)
                g.last_buy_price = o.price
                g.current_units += 1
                g.today_units += 1
                return True
        return False
                

    def addin(current_price, cash, unit):
        if current_price >= g.last_buy_price + 0.5 * g.current_N:
            shares = cash / current_price
            if shares >= unit:
                print("adding %d" % unit)
                o = order(g.security, unit)
                g.last_buy_price = o.price
                g.current_units += 1
                g.today_units += 1
                return True
        return False
                
    def sell(current_price):
        price = attribute_history(g.security, g.out_day, '1d', ('close',))
        if current_price < min(price['close']):
            print("selling")
            order_target(g.security, 0)
            g.current_units = g.today_units
            return True
        return False
            
    def stop_loss(current_price):
        if current_price < g.last_buy_price - 2 * g.current_N:
            print("stop loss")
            order_target(g.security, 0)
            g.current_units = g.today_units
            return True
        return False

  • 相关阅读:
    小星星短视频app
    多元思维模型整理--查理芒格---每本书的思维模型都要添加到这里
    张一鸣 南开大学演讲
    增长黑客特训营--入门
    深度学习 TensorFlow
    推荐算法之---FM算法;
    idea更改代码格式化快捷键
    centos7启动MySQL报 Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
    springboot启动报无法加载主类解决方法
    centos7搭建 mongodb 主从复制
  • 原文地址:https://www.cnblogs.com/IQ-Python/p/7423878.html
Copyright © 2020-2023  润新知