55 浏览
0

以下是sdk文档中的示例,但无法运行,首先get_quote不是一个可等待对象,似乎无法使用await。请问有详细的异步案例吗,或者对这个案例的疑问答复

<span class="c1"># 协程示例,为每个合约创建 task</span>
<span class="kn">from</span> <span class="nn">tqsdk</span> <span class="kn">import</span> <span class="n">TqApi</span><span class="p">,</span> <span class="n">TqAuth</span>

<span class="k">async</span> <span class="k">def</span> <span class="nf">demo</span><span class="p">(</span><span class="n">SYMBOL</span><span class="p">):</span>
    <span class="n">quote</span> <span class="o">=</span> <span class="k">await</span> <span class="n">api</span><span class="o">.</span><span class="n">get_quote</span><span class="p">(</span><span class="n">SYMBOL</span><span class="p">)</span>  <span class="c1"># 支持 await <span class="highlighted">异步</span>,这里会订阅合约,等到收到合约行情才返回</span>
    <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"quote: </span><span class="si">{</span><span class="n">SYMBOL</span><span class="si">}</span><span class="s2">"</span><span class="p">,</span> <span class="n">quote</span><span class="o">.</span><span class="n">datetime</span><span class="p">,</span> <span class="n">quote</span><span class="o">.</span><span class="n">last_price</span><span class="p">)</span>  <span class="c1"># 这一行就会打印出合约的最新行情</span>
    <span class="k">async</span> <span class="k">with</span> <span class="n">api</span><span class="o">.</span><span class="n">register_update_notify</span><span class="p">()</span> <span class="k">as</span> <span class="n">update_chan</span><span class="p">:</span>
        <span class="k">async</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="n">update_chan</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">api</span><span class="o">.</span><span class="n">is_changing</span><span class="p">(</span><span class="n">quote</span><span class="p">):</span>
                <span class="nb">print</span><span class="p">(</span><span class="n">SYMBOL</span><span class="p">,</span> <span class="n">quote</span><span class="o">.</span><span class="n">datetime</span><span class="p">,</span> <span class="n">quote</span><span class="o">.</span><span class="n">last_price</span><span class="p">)</span>
            <span class="c1"># ... 策略代码 ...</span>

<span class="n">api</span> <span class="o">=</span> <span class="n">TqApi</span><span class="p">(</span><span class="n">auth</span><span class="o">=</span><span class="n">TqAuth</span><span class="p">(</span><span class="s2">"快期账户"</span><span class="p">,</span> <span class="s2">"账户密码"</span><span class="p">))</span>

<span class="n">symbol_list</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"SHFE.rb2107"</span><span class="p">,</span> <span class="s2">"DCE.m2109"</span><span class="p">]</span>  <span class="c1"># 设置合约代码</span>
<span class="k">for</span> <span class="n">symbol</span> <span class="ow">in</span> <span class="n">symbol_list</span><span class="p">:</span>
    <span class="n">api</span><span class="o">.</span><span class="n">create_task</span><span class="p">(</span><span class="n">demo</span><span class="p">(</span><span class="n">symbol</span><span class="p">))</span>  <span class="c1"># 为每个合约创建<span class="highlighted">异步</span>任务</span>

<span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
    <span class="n">api</span><span class="o">.</span><span class="n">wait_update</span><span class="p">()</span>
chaos 已回答的问题 2天 前
0

示例的合约已经下市了所以不会推送行情。可以试试用cursor解答

chaos 发表新评论 2天 前

欢迎加入官方群聊611806823一起讨论,也可以试试首页右边小助手答疑~

您正在查看1个答案中的1个,单击此处查看所有答案。