克隆策略

    {"description":"实验创建于2017/8/26","graph":{"edges":[{"to_node_id":"-831:input_1","from_node_id":"-819:data_1"},{"to_node_id":"-843:input_1","from_node_id":"-831:data_1"},{"to_node_id":"-867:input_1","from_node_id":"-843:data_1"},{"to_node_id":"-879:input_1","from_node_id":"-843:data_1"},{"to_node_id":"-891:input_1","from_node_id":"-843:data_1"},{"to_node_id":"-903:input_1","from_node_id":"-843:data_1"}],"nodes":[{"node_id":"-819","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"def bigquant_run(input_1, input_2, input_3):\n import requests\n response = requests.post(\"https://www.f2pool.com/coins\", data={\"sort_by\": \"output24h\", \"sort_type\": \"desc\"})\n data = response.json()\n ds = DataSource.write_pickle(data)\n return Outputs(data_1=ds)\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-819"},{"name":"input_2","node_id":"-819"},{"name":"input_3","node_id":"-819"}],"output_ports":[{"name":"data_1","node_id":"-819"},{"name":"data_2","node_id":"-819"},{"name":"data_3","node_id":"-819"}],"cacheable":true,"seq_num":1,"comment":"coins","comment_collapsed":false},{"node_id":"-831","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"def bigquant_run(input_1, input_2, input_3):\n # date 没有实际使用,更新这个值缓存可以更新\n def fetch_coin_data(currency_code, date=None):\n import requests\n response = requests.post(\n \"https://www.f2pool.com/coins-chart\",\n data={\"currency_code\": currency_code, \"history_days\": \"30d\", \"interval\": \"60m\"}\n )\n try:\n data = response.json()\n df = pd.DataFrame(data[\"data\"][\"chart_data\"])\n df[\"timestamp\"] = pd.to_datetime(df['timestamp'], unit=\"ms\")\n ds = DataSource.write_df(df)\n except Exception as e:\n T.log.exception(e)\n ds = None\n return Outputs(data=ds)\n\n coins = input_1.read()\n i = 0\n for coin in coins[\"data\"][\"top100\"]:\n i += 1\n T.log.info(f'fetch {i}/{len(coins[\"data\"][\"top100\"])} {coin[\"code\"]} ..')\n coin[\"data_ds\"] = M.cached.v3(\n run=fetch_coin_data,\n params={\"currency_code\": coin[\"code\"], \"date\": \"2021-08-17\"}\n ).data\n\n data = DataSource.write_pickle(coins)\n\n return Outputs(data_1=data)\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-831"},{"name":"input_2","node_id":"-831"},{"name":"input_3","node_id":"-831"}],"output_ports":[{"name":"data_1","node_id":"-831"},{"name":"data_2","node_id":"-831"},{"name":"data_3","node_id":"-831"}],"cacheable":true,"seq_num":2,"comment":"获取算力和价格数据","comment_collapsed":false},{"node_id":"-843","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"def bigquant_run(input_1, input_2, input_3):\n data = input_1.read()\n if 'all_df' in data[\"data\"]:\n del data[\"data\"][\"all_df\"]\n N_START = 0\n N_END = 10\n T.log.warn(f\"处理数据范围 [{N_START}, {N_END})\")\n for i, coin in enumerate(data[\"data\"][\"top100\"][N_START:N_END]):\n T.log.info(f'process data {i+1}/{len(data[\"data\"][\"top100\"])} {coin[\"code\"]}')\n if \"data_ds\" not in coin:\n continue\n df = coin[\"data_ds\"].read()\n df.set_index(\"timestamp\", inplace=True)\n df[\"price_0\"] = df[\"price\"] / df[\"price\"].iloc[0]\n df[\"hashrate_0\"] = df[\"hashrate\"] / df[\"hashrate\"].iloc[0]\n df[\"price_per_hashrate\"] = df[\"price_0\"] / df[\"hashrate_0\"]\n\n # 处理异常数据\n if df[\"hashrate_0\"].max() > 10:\n T.log.warn(f'{coin[\"code\"]}, 异常数据, hashrate_0.max() is {df[\"hashrate_0\"].max()}')\n continue\n\n df.columns = coin[\"code\"] + \"_\" + df.columns\n # coin[\"data_df\"] = df\n if \"all_df\" not in data[\"data\"]:\n data[\"data\"][\"all_df\"] = df\n else:\n data[\"data\"][\"all_df\"][df.columns] = df\n return Outputs(data_1=data)\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-843"},{"name":"input_2","node_id":"-843"},{"name":"input_3","node_id":"-843"}],"output_ports":[{"name":"data_1","node_id":"-843"},{"name":"data_2","node_id":"-843"},{"name":"data_3","node_id":"-843"}],"cacheable":false,"seq_num":3,"comment":"数据预处理","comment_collapsed":false},{"node_id":"-867","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"def bigquant_run(input_1, input_2, input_3):\n plot_options = {\n \"chart\": {\n \"height\": 800,\n },\n \"plotOptions\": {\n \"series\": {\n # \"compare\": 'percent',\n # \"showInNavigator\": True\n }\n }\n }\n T.plot(\n input_1[\"data\"][\"all_df\"],\n title=\"算力\",\n y=[c for c in data[\"data\"][\"all_df\"].columns if c.endswith(\"hashrate_0\")], # [\"price_0\", \"hashrate_0\", \"price_per_hashrate\"],\n options=plot_options\n )\n\n return Outputs()\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-867"},{"name":"input_2","node_id":"-867"},{"name":"input_3","node_id":"-867"}],"output_ports":[{"name":"data_1","node_id":"-867"},{"name":"data_2","node_id":"-867"},{"name":"data_3","node_id":"-867"}],"cacheable":false,"seq_num":5,"comment":"算力趋势","comment_collapsed":false},{"node_id":"-879","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"def bigquant_run(input_1, input_2, input_3):\n plot_options = {\n \"chart\": {\n \"height\": 800,\n },\n \"plotOptions\": {\n \"series\": {\n # \"compare\": 'percent',\n # \"showInNavigator\": True\n }\n }\n }\n T.plot(\n input_1[\"data\"][\"all_df\"],\n title=\"价格\",\n y=[c for c in data[\"data\"][\"all_df\"].columns if c.endswith(\"price_0\")], # [\"price_0\", \"hashrate_0\", \"price_per_hashrate\"],\n options=plot_options\n )\n\n return Outputs()\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-879"},{"name":"input_2","node_id":"-879"},{"name":"input_3","node_id":"-879"}],"output_ports":[{"name":"data_1","node_id":"-879"},{"name":"data_2","node_id":"-879"},{"name":"data_3","node_id":"-879"}],"cacheable":false,"seq_num":4,"comment":"价格趋势","comment_collapsed":false},{"node_id":"-891","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"def bigquant_run(input_1, input_2, input_3):\n plot_options = {\n \"chart\": {\n \"height\": 800,\n },\n \"plotOptions\": {\n \"series\": {\n # \"compare\": 'percent',\n # \"showInNavigator\": True\n }\n }\n }\n T.plot(\n input_1[\"data\"][\"all_df\"],\n title=\"价格/算力\",\n y=[c for c in data[\"data\"][\"all_df\"].columns if c.endswith(\"price_per_hashrate\")], # [\"price_0\", \"hashrate_0\", \"price_per_hashrate\"],\n options=plot_options\n )\n\n return Outputs()\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-891"},{"name":"input_2","node_id":"-891"},{"name":"input_3","node_id":"-891"}],"output_ports":[{"name":"data_1","node_id":"-891"},{"name":"data_2","node_id":"-891"},{"name":"data_3","node_id":"-891"}],"cacheable":false,"seq_num":6,"comment":"价格/算力趋势","comment_collapsed":false},{"node_id":"-903","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"def bigquant_run(input_1, input_2, input_3):\n plot_options = {\n \"chart\": {\n \"height\": 800,\n },\n \"plotOptions\": {\n \"series\": {\n # \"compare\": 'percent',\n # \"showInNavigator\": True\n }\n }\n }\n T.plot(\n input_1[\"data\"][\"all_df\"],\n title=\"价格 vs 算力\",\n y=[c for c in data[\"data\"][\"all_df\"].columns if c.endswith(\"_0\")], # [\"price_0\", \"hashrate_0\", \"price_per_hashrate\"],\n options=plot_options\n )\n\n return Outputs()\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-903"},{"name":"input_2","node_id":"-903"},{"name":"input_3","node_id":"-903"}],"output_ports":[{"name":"data_1","node_id":"-903"},{"name":"data_2","node_id":"-903"},{"name":"data_3","node_id":"-903"}],"cacheable":false,"seq_num":7,"comment":"价格趋势","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-819' Position='282.9435729980469,133.19387817382812,200,200'/><node_position Node='-831' Position='328.4577941894531,251.87100219726562,200,200'/><node_position Node='-843' Position='354.33740234375,375.15191650390625,200,200'/><node_position Node='-867' Position='163.17971801757812,527.8353576660156,200,200'/><node_position Node='-879' Position='474.4544677734375,526.5917053222656,200,200'/><node_position Node='-891' Position='791.468505859375,526.9677429199219,200,200'/><node_position Node='-903' Position='540.3040771484375,679.6363830566406,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [130]:
    # 本代码由可视化策略环境自动生成 2021年8月17日 12:04
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    def m1_run_bigquant_run(input_1, input_2, input_3):
        import requests
        response = requests.post("https://www.f2pool.com/coins", data={"sort_by": "output24h", "sort_type": "desc"})
        data = response.json()
        ds = DataSource.write_pickle(data)
        return Outputs(data_1=ds)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m1_post_run_bigquant_run(outputs):
        return outputs
    
    def m2_run_bigquant_run(input_1, input_2, input_3):
        # date 没有实际使用,更新这个值缓存可以更新
        def fetch_coin_data(currency_code, date=None):
            import requests
            response = requests.post(
                "https://www.f2pool.com/coins-chart",
                data={"currency_code": currency_code, "history_days": "30d", "interval": "60m"}
            )
            try:
                data = response.json()
                df = pd.DataFrame(data["data"]["chart_data"])
                df["timestamp"] = pd.to_datetime(df['timestamp'], unit="ms")
                ds = DataSource.write_df(df)
            except Exception as e:
                T.log.exception(e)
                ds = None
            return Outputs(data=ds)
    
        coins = input_1.read()
        i = 0
        for coin in coins["data"]["top100"]:
            i += 1
            T.log.info(f'fetch {i}/{len(coins["data"]["top100"])} {coin["code"]} ..')
            coin["data_ds"] = M.cached.v3(
                run=fetch_coin_data,
                params={"currency_code": coin["code"], "date": "2021-08-17"}
            ).data
    
        data = DataSource.write_pickle(coins)
    
        return Outputs(data_1=data)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m2_post_run_bigquant_run(outputs):
        return outputs
    
    def m3_run_bigquant_run(input_1, input_2, input_3):
        data = input_1.read()
        if 'all_df' in data["data"]:
            del data["data"]["all_df"]
        N_START = 0
        N_END = 10
        T.log.warn(f"处理数据范围 [{N_START}, {N_END})")
        for i, coin in enumerate(data["data"]["top100"][N_START:N_END]):
            T.log.info(f'process data {i+1}/{len(data["data"]["top100"])} {coin["code"]}')
            if "data_ds" not in coin:
                continue
            df = coin["data_ds"].read()
            df.set_index("timestamp", inplace=True)
            df["price_0"] = df["price"] / df["price"].iloc[0]
            df["hashrate_0"] = df["hashrate"] / df["hashrate"].iloc[0]
            df["price_per_hashrate"] = df["price_0"] / df["hashrate_0"]
    
            # 处理异常数据
            if df["hashrate_0"].max() > 10:
                T.log.warn(f'{coin["code"]}, 异常数据, hashrate_0.max() is {df["hashrate_0"].max()}')
                continue
    
            df.columns = coin["code"] + "_" + df.columns
            # coin["data_df"] = df
            if "all_df" not in data["data"]:
                data["data"]["all_df"] = df
            else:
                data["data"]["all_df"][df.columns] = df
        return Outputs(data_1=data)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m3_post_run_bigquant_run(outputs):
        return outputs
    
    def m5_run_bigquant_run(input_1, input_2, input_3):
        plot_options = {
            "chart": {
                "height": 800,
            },
            "plotOptions": {
                "series": {
                    # "compare": 'percent',
                    # "showInNavigator": True
                }
            }
        }
        T.plot(
            input_1["data"]["all_df"],
            title="算力",
            y=[c for c in data["data"]["all_df"].columns if c.endswith("hashrate_0")], # ["price_0", "hashrate_0", "price_per_hashrate"],
            options=plot_options
        )
    
        return Outputs()
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m5_post_run_bigquant_run(outputs):
        return outputs
    
    def m4_run_bigquant_run(input_1, input_2, input_3):
        plot_options = {
            "chart": {
                "height": 800,
            },
            "plotOptions": {
                "series": {
                    # "compare": 'percent',
                    # "showInNavigator": True
                }
            }
        }
        T.plot(
            input_1["data"]["all_df"],
            title="价格",
            y=[c for c in data["data"]["all_df"].columns if c.endswith("price_0")], # ["price_0", "hashrate_0", "price_per_hashrate"],
            options=plot_options
        )
    
        return Outputs()
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m4_post_run_bigquant_run(outputs):
        return outputs
    
    def m6_run_bigquant_run(input_1, input_2, input_3):
        plot_options = {
            "chart": {
                "height": 800,
            },
            "plotOptions": {
                "series": {
                    # "compare": 'percent',
                    # "showInNavigator": True
                }
            }
        }
        T.plot(
            input_1["data"]["all_df"],
            title="价格/算力",
            y=[c for c in data["data"]["all_df"].columns if c.endswith("price_per_hashrate")], # ["price_0", "hashrate_0", "price_per_hashrate"],
            options=plot_options
        )
    
        return Outputs()
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m6_post_run_bigquant_run(outputs):
        return outputs
    
    def m7_run_bigquant_run(input_1, input_2, input_3):
        plot_options = {
            "chart": {
                "height": 800,
            },
            "plotOptions": {
                "series": {
                    # "compare": 'percent',
                    # "showInNavigator": True
                }
            }
        }
        T.plot(
            input_1["data"]["all_df"],
            title="价格 vs 算力",
            y=[c for c in data["data"]["all_df"].columns if c.endswith("_0")], # ["price_0", "hashrate_0", "price_per_hashrate"],
            options=plot_options
        )
    
        return Outputs()
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m7_post_run_bigquant_run(outputs):
        return outputs
    
    
    m1 = M.cached.v3(
        run=m1_run_bigquant_run,
        post_run=m1_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m2 = M.cached.v3(
        input_1=m1.data_1,
        run=m2_run_bigquant_run,
        post_run=m2_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m3 = M.cached.v3(
        input_1=m2.data_1,
        run=m3_run_bigquant_run,
        post_run=m3_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports='',
        m_cached=False
    )
    
    m5 = M.cached.v3(
        input_1=m3.data_1,
        run=m5_run_bigquant_run,
        post_run=m5_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports='',
        m_cached=False
    )
    
    m4 = M.cached.v3(
        input_1=m3.data_1,
        run=m4_run_bigquant_run,
        post_run=m4_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports='',
        m_cached=False
    )
    
    m6 = M.cached.v3(
        input_1=m3.data_1,
        run=m6_run_bigquant_run,
        post_run=m6_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports='',
        m_cached=False
    )
    
    m7 = M.cached.v3(
        input_1=m3.data_1,
        run=m7_run_bigquant_run,
        post_run=m7_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports='',
        m_cached=False
    )