<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nyria</title>
    <description>The latest articles on DEV Community by Nyria (@nyriaautomation).</description>
    <link>https://dev.to/nyriaautomation</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4196492%2Fd15af64b-39cb-4f28-8bd7-51bd8fbb1d50.png</url>
      <title>DEV Community: Nyria</title>
      <link>https://dev.to/nyriaautomation</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nyriaautomation"/>
    <language>en</language>
    <item>
      <title>How to connect Coinbase to TradingView</title>
      <dc:creator>Nyria</dc:creator>
      <pubDate>Sun, 15 Nov 2026 14:05:00 +0000</pubDate>
      <link>https://dev.to/nyriaautomation/how-to-connect-coinbase-to-tradingview-459g</link>
      <guid>https://dev.to/nyriaautomation/how-to-connect-coinbase-to-tradingview-459g</guid>
      <description>&lt;p&gt;There are two ways to connect Coinbase to TradingView. TradingView lists &lt;a href="https://www.tradingview.com/broker/Coinbase_Advanced/" rel="noopener noreferrer"&gt;Coinbase Advanced&lt;/a&gt; as a broker, so you can place orders from a chart by hand. To have an alert place the order, give the alert a webhook URL. A program you host receives the alert and calls Coinbase's Advanced Trade API.&lt;/p&gt;

&lt;p&gt;That program takes about 120 lines of Python on Coinbase's &lt;a href="https://github.com/coinbase/coinbase-advanced-py" rel="noopener noreferrer"&gt;official SDK&lt;/a&gt;. The one here trades spot pairs with market orders, and previews them until you switch orders on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Can TradingView automate Coinbase orders?
&lt;/h2&gt;

&lt;p&gt;Not by itself. TradingView's page on &lt;a href="https://www.tradingview.com/support/solutions/43000481026-how-to-autotrade-using-pine-script-strategies/" rel="noopener noreferrer"&gt;autotrading with strategies&lt;/a&gt; says: "Automated strategy trading with a brokerage account is not available on TradingView yet." The broker connection covers the orders you click.&lt;/p&gt;

&lt;p&gt;An alert can call your server instead. The &lt;a href="https://www.tradingview.com/support/solutions/43000529348-how-to-configure-webhook-alerts/" rel="noopener noreferrer"&gt;webhook doc&lt;/a&gt; says TradingView sends "a POST request as soon as the alert is triggered, with the alert message in the body of the request." Only ports 80 and 443 are accepted, and a request is cancelled after three seconds. The &lt;a href="https://www.tradingview.com/pricing/" rel="noopener noreferrer"&gt;pricing page&lt;/a&gt; puts webhooks on paid plans, and TradingView allows them only with 2-factor authentication enabled.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which fields does the alert carry?
&lt;/h2&gt;

&lt;p&gt;Create the alert on a strategy with this message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"pair"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"{{ticker}}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"{{strategy.order.action}}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"position"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"{{strategy.market_position}}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"order"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"{{strategy.order.id}}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"bar"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"{{time}}"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In TradingView's &lt;a href="https://www.tradingview.com/support/solutions/43000531021-how-to-use-a-variable-value-in-alert/" rel="noopener noreferrer"&gt;list of placeholders&lt;/a&gt;, &lt;code&gt;{{strategy.order.action}}&lt;/code&gt; is &lt;code&gt;buy&lt;/code&gt; or &lt;code&gt;sell&lt;/code&gt; for the executed order and &lt;code&gt;{{strategy.market_position}}&lt;/code&gt; is the strategy's current position, one of &lt;code&gt;long&lt;/code&gt;, &lt;code&gt;flat&lt;/code&gt; or &lt;code&gt;short&lt;/code&gt;. &lt;code&gt;{{strategy.order.id}}&lt;/code&gt; is the first argument of the Pine call that created the order. &lt;code&gt;{{time}}&lt;/code&gt; is the bar's time in UTC.&lt;/p&gt;

&lt;p&gt;The receiver joins the five values into an id and handles each id once.&lt;/p&gt;

&lt;p&gt;The webhook doc warns against "login credentials or passwords in the webhook body", so the secret goes in the URL. The order size stays on your server.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;strategy&lt;/code&gt; placeholders "can only be used in strategy alerts", so an indicator alert needs &lt;code&gt;action&lt;/code&gt;, &lt;code&gt;position&lt;/code&gt; and &lt;code&gt;order&lt;/code&gt; typed in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which API key does it need?
&lt;/h2&gt;

&lt;p&gt;Advanced Trade takes keys from the Coinbase Developer Platform (CDP). Coinbase's &lt;a href="https://docs.cdp.coinbase.com/coinbase-app/authentication-authorization/api-key-authentication" rel="noopener noreferrer"&gt;API key guide&lt;/a&gt; starts at Secret API Keys, under the API Keys tab. Click Create API key, then expand API restrictions and Advanced Settings. The guide calls the IP allowlist "recommended but not required".&lt;/p&gt;

&lt;p&gt;Select View and Trade, and leave Transfer off. Coinbase's &lt;a href="https://docs.cdp.coinbase.com/coinbase-app/authentication-authorization/authorization" rel="noopener noreferrer"&gt;permission list&lt;/a&gt; says Transfer lets an application "send and receive funds, on and off platform". The receiver reads the key's permissions at start and exits if Transfer is on.&lt;/p&gt;

&lt;p&gt;Each key is tied to one portfolio. A sell alert sells that portfolio's whole available balance of the coin, so choose a portfolio holding only what the receiver may trade.&lt;/p&gt;

&lt;p&gt;Set the signature algorithm to ECDSA. The guide says Coinbase App SDKs don't support Ed25519 keys, though the Python SDK's &lt;a href="https://github.com/coinbase/coinbase-advanced-py/blob/master/CHANGELOG.md" rel="noopener noreferrer"&gt;changelog&lt;/a&gt; lists them as added in version 1.8.3.&lt;/p&gt;

&lt;p&gt;Key files "are no longer automatically downloaded", so click Download API key and copy the JSON file to your server.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does the code do?
&lt;/h2&gt;

&lt;p&gt;Install three packages with &lt;code&gt;pip install coinbase-advanced-py flask waitress&lt;/code&gt;. The receiver is one file, &lt;code&gt;app.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;hmac&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;logging&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;queue&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sqlite3&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;threading&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;uuid&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;contextlib&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;closing&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Decimal&lt;/span&gt;

&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;coinbase&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rest&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;RESTClient&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;flask&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;abort&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;

&lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;basicConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;INFO&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getLogger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"tv-coinbase"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="no"&gt;TOKEN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"WEBHOOK_TOKEN"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;BUY_QUOTE_SIZE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"BUY_QUOTE_SIZE"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;PLACE_ORDERS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"PLACE_ORDERS"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"yes"&lt;/span&gt;
&lt;span class="no"&gt;DB_PATH&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ALERT_DB"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"alerts.sqlite3"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;PRODUCTS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"BTCUSD"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"BTC-USD"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"ETHUSD"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"ETH-USD"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="no"&gt;FIELDS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"pair"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"action"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"position"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"order"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"bar"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RESTClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key_file&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"COINBASE_KEY_FILE"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;grants&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_api_key_permissions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;grants&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"can_transfer"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;SystemExit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"this API key can transfer funds; create one without Transfer"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="no"&gt;PLACE_ORDERS&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;grants&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"can_trade"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;SystemExit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"PLACE_ORDERS=yes needs an API key with Trade"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"portfolio %s, placing orders: %s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;grants&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"portfolio_uuid"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;PLACE_ORDERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;closing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sqlite3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;DB_PATH&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"CREATE TABLE IF NOT EXISTS alerts (id TEXT PRIMARY KEY)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;pending&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Queue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;is_new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alert_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;closing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sqlite3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;DB_PATH&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"INSERT INTO alerts (id) VALUES (?)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alert_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;sqlite3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IntegrityError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;


&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/alert/&amp;lt;token&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;receive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;hmac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compare_digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;TOKEN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;abort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;silent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;force&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;abort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;values&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="no"&gt;FIELDS&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nb"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;abort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;pair&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;position&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;pair&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="no"&gt;PRODUCTS&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"buy"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"sell"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;abort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;alert_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nf"&gt;is_new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alert_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"result"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"duplicate"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;pending&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alert_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;PRODUCTS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pair&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"result"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"queued"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;floor_to&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;step&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;step&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;step&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;available&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;currency&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;cursor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_accounts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;250&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;currency&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;currency&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;available_balance&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"value"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;has_next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;cursor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cursor&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alert_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"buy"&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;position&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s2"&gt;"long"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%s ignored: a buy that does not open a long"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;alert_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;product&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_public_product&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"buy"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;floor_to&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;BUY_QUOTE_SIZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;quote_increment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;minimum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;quote_min_size&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"quote_size"&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;floor_to&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;available&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;base_currency_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;base_increment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;minimum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;base_min_size&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"base_size"&lt;/span&gt;
    &lt;span class="n"&gt;sizing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{size:f}"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;minimum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;warning&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%s skipped: %s is under the product minimum"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;alert_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sizing&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;side&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;upper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="no"&gt;PLACE_ORDERS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;errs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preview_market_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;side&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;sizing&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"errs"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%s preview %s %s: errs=%s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;alert_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sizing&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;client_order_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;uuid5&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;NAMESPACE_URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;alert_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;market_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client_order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;side&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;sizing&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"success"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%s %s %s: order %s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;alert_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sizing&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"success_response"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"order_id"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%s rejected: %s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;alert_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"error_response"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;drain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;job&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;iter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pending&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;job&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%s raised"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;job&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;threading&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;drain&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;daemon&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The URL's token goes through &lt;a href="https://docs.python.org/3/library/hmac.html#hmac.compare_digest" rel="noopener noreferrer"&gt;&lt;code&gt;hmac.compare_digest&lt;/code&gt;&lt;/a&gt;, a comparison Python's docs say avoids "content-based short circuiting behaviour". A mismatch gets a 404.&lt;/p&gt;

&lt;p&gt;TradingView labels a JSON message &lt;code&gt;application/json&lt;/code&gt; and anything else &lt;code&gt;text/plain&lt;/code&gt;, so &lt;code&gt;force=True&lt;/code&gt; parses the body whatever the label. A body that isn't JSON, a missing field, an unlisted pair or an unknown action gets a 400.&lt;/p&gt;

&lt;p&gt;The handler does one SQLite insert and one &lt;code&gt;queue.put&lt;/code&gt;. A single worker thread makes every Coinbase call, outside TradingView's three-second limit.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;id&lt;/code&gt; column is a primary key, so a repeated id raises &lt;code&gt;sqlite3.IntegrityError&lt;/code&gt; and the handler answers &lt;code&gt;duplicate&lt;/code&gt;. The id is saved before the order goes out, so an alert that fails at Coinbase isn't sent again.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;PRODUCTS&lt;/code&gt; maps TradingView tickers to Coinbase product ids and doubles as the allowlist. TradingView shows Coinbase's bitcoin pair as &lt;a href="https://www.tradingview.com/symbols/BTCUSD/?exchange=COINBASE" rel="noopener noreferrer"&gt;&lt;code&gt;COINBASE:BTCUSD&lt;/code&gt;&lt;/a&gt;, and its example of &lt;code&gt;{{ticker}}&lt;/code&gt; is &lt;code&gt;BTCUSD&lt;/code&gt;. Coinbase calls it &lt;code&gt;BTC-USD&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How are orders sized?
&lt;/h2&gt;

&lt;p&gt;Coinbase's &lt;a href="https://docs.cdp.coinbase.com/coinbase-app/advanced-trade-apis/guides/orders" rel="noopener noreferrer"&gt;order guide&lt;/a&gt; sets the rule for market orders: "To buy a product, provide a &lt;code&gt;quote_size&lt;/code&gt; or &lt;code&gt;base_size&lt;/code&gt;; to sell, provide a &lt;code&gt;base_size&lt;/code&gt;." In &lt;code&gt;BTC-USD&lt;/code&gt;, bitcoin is the base and dollars are the quote.&lt;/p&gt;

&lt;p&gt;A buy spends &lt;code&gt;BUY_QUOTE_SIZE&lt;/code&gt; of the quote currency. A sell uses the whole available base balance, so a strategy's partial exit becomes a full exit at Coinbase. A &lt;code&gt;buy&lt;/code&gt; that leaves the strategy &lt;code&gt;flat&lt;/code&gt; only covered a short, and the receiver skips it.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://docs.cdp.coinbase.com/api-reference/advanced-trade-api/rest-api/public/get-public-product" rel="noopener noreferrer"&gt;product endpoint&lt;/a&gt; describes &lt;code&gt;base_increment&lt;/code&gt; as the "Minimum amount base value can be increased or decreased at once". On 13 Nov 2026, &lt;code&gt;BTC-USD&lt;/code&gt; had a &lt;code&gt;base_increment&lt;/code&gt; of &lt;code&gt;0.00000001&lt;/code&gt; and a &lt;code&gt;quote_min_size&lt;/code&gt; of &lt;code&gt;1&lt;/code&gt;. The receiver rounds sizes down to the increment and skips an order under the product's minimum.&lt;/p&gt;

&lt;p&gt;Every order carries a &lt;code&gt;client_order_id&lt;/code&gt;. The &lt;a href="https://docs.cdp.coinbase.com/api-reference/advanced-trade-api/rest-api/orders/create-order" rel="noopener noreferrer"&gt;Create Order reference&lt;/a&gt; says that if it isn't unique, "the order will not be created and the order corresponding with that ID will be returned instead." The receiver derives it from the alert id with &lt;code&gt;uuid5&lt;/code&gt;, so a replayed alert carries the same one even if the SQLite file is lost.&lt;/p&gt;

&lt;p&gt;A refused order arrives with &lt;code&gt;success&lt;/code&gt; false and the reason in &lt;code&gt;error_response&lt;/code&gt;, which the worker logs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Can I test without real orders?
&lt;/h2&gt;

&lt;p&gt;Yes, in preview mode. Coinbase's &lt;a href="https://docs.cdp.coinbase.com/coinbase-app/advanced-trade-apis/sandbox" rel="noopener noreferrer"&gt;sandbox&lt;/a&gt; doc says "All responses are static and pre-defined", so it can't check a size against your balance.&lt;/p&gt;

&lt;p&gt;Until &lt;code&gt;PLACE_ORDERS=yes&lt;/code&gt; is set, the receiver sends each order to &lt;a href="https://docs.cdp.coinbase.com/api-reference/advanced-trade-api/rest-api/orders/preview-orders" rel="noopener noreferrer"&gt;Preview Order&lt;/a&gt; and places nothing. Coinbase's &lt;a href="https://docs.cdp.coinbase.com/coinbase-app/advanced-trade-apis/rest-scopes" rel="noopener noreferrer"&gt;permission table&lt;/a&gt; lists that call under &lt;code&gt;view&lt;/code&gt;, so a View-only key is enough at first. The receiver logs the preview's &lt;code&gt;errs&lt;/code&gt;, the "List of potential failure reasons were this order to be submitted".&lt;/p&gt;

&lt;p&gt;Send an alert by hand:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -X POST &lt;span class="s2"&gt;"https://alerts.example.com/alert/$WEBHOOK_TOKEN"&lt;/span&gt; \
  -d &lt;span class="s1"&gt;'{"pair":"BTCUSD","action":"buy","position":"long","order":"test","bar":"1"}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Flask answers &lt;code&gt;{"result":"queued"}&lt;/code&gt;, and the preview appears in the log. The same body sent twice gets &lt;code&gt;{"result":"duplicate"}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When the log looks right, restart with &lt;code&gt;PLACE_ORDERS=yes&lt;/code&gt; and a key that has View and Trade.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I keep it running?
&lt;/h2&gt;

&lt;p&gt;Four environment variables configure it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;WEBHOOK_TOKEN&lt;/code&gt;, the secret part of the URL, such as the output of Python's &lt;code&gt;secrets.token_hex(32)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;COINBASE_KEY_FILE&lt;/code&gt;, the path of the key file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;BUY_QUOTE_SIZE&lt;/code&gt;, what each buy spends, in the quote currency.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PLACE_ORDERS&lt;/code&gt;, unset until the previews look right.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A systemd unit keeps the process up:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="k"&gt;[Service]&lt;/span&gt;
&lt;span class="na"&gt;User&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;alerts&lt;/span&gt;
&lt;span class="na"&gt;WorkingDirectory&lt;/span&gt;&lt;span class="o"&gt;=/&lt;/span&gt;&lt;span class="n"&gt;opt&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;alerts&lt;/span&gt;
&lt;span class="na"&gt;EnvironmentFile&lt;/span&gt;&lt;span class="o"&gt;=/&lt;/span&gt;&lt;span class="n"&gt;etc&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;alerts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;
&lt;span class="na"&gt;ExecStart&lt;/span&gt;&lt;span class="o"&gt;=/&lt;/span&gt;&lt;span class="n"&gt;opt&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;alerts&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;venv&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;bin&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;waitress&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;serve&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;host&lt;/span&gt; &lt;span class="mf"&gt;127.0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt; &lt;span class="mi"&gt;8000&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;
&lt;span class="na"&gt;Restart&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;on&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;failure&lt;/span&gt;

&lt;span class="k"&gt;[Install]&lt;/span&gt;
&lt;span class="na"&gt;WantedBy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;multi&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html" rel="noopener noreferrer"&gt;&lt;code&gt;EnvironmentFile=&lt;/code&gt;&lt;/a&gt; "reads the environment variables from a text file", and &lt;a href="https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html" rel="noopener noreferrer"&gt;&lt;code&gt;Restart=on-failure&lt;/code&gt;&lt;/a&gt; restarts the process after a non-zero exit.&lt;/p&gt;

&lt;p&gt;Flask's &lt;a href="https://flask.palletsprojects.com/en/stable/deploying/waitress/" rel="noopener noreferrer"&gt;Waitress page&lt;/a&gt; says Waitress shouldn't run as root, which rules out ports 80 and 443: "a reverse proxy such as nginx or Apache httpd should be used in front". Point the proxy at &lt;code&gt;127.0.0.1:8000&lt;/code&gt;. Give TradingView &lt;code&gt;https://alerts.example.com/alert/&lt;/code&gt; plus your token as the webhook URL.&lt;/p&gt;

&lt;h2&gt;
  
  
  What do the errors mean?
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Log text&lt;/th&gt;
&lt;th&gt;Why&lt;/th&gt;
&lt;th&gt;What to do&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;403 Client Error: Missing Required Scopes. Please verify your API keys include the necessary permissions.&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The key lacks a permission the call needs&lt;/td&gt;
&lt;td&gt;Orders need Trade. Previews and balances need View&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Product BTC-USDX not supported&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A product id in &lt;code&gt;PRODUCTS&lt;/code&gt; doesn't exist&lt;/td&gt;
&lt;td&gt;Use Coinbase's form, &lt;code&gt;BTC-USD&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;INSUFFICIENT_FUND&lt;/code&gt;, or &lt;code&gt;PREVIEW_INSUFFICIENT_FUND&lt;/code&gt; in a preview&lt;/td&gt;
&lt;td&gt;The balance doesn't cover the order&lt;/td&gt;
&lt;td&gt;Fund the portfolio or lower &lt;code&gt;BUY_QUOTE_SIZE&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Cannot specify both api_key and key_file in constructor&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;COINBASE_API_KEY&lt;/code&gt; or &lt;code&gt;COINBASE_API_SECRET&lt;/code&gt; is set in the environment&lt;/td&gt;
&lt;td&gt;Unset both&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The SDK's &lt;a href="https://github.com/coinbase/coinbase-advanced-py/tree/master/coinbase" rel="noopener noreferrer"&gt;source&lt;/a&gt; holds the first and last texts. The product text is Coinbase's answer to a made-up id on 13 Nov 2026. The sandbox doc names both fund reasons.&lt;/p&gt;

&lt;p&gt;AI tools wrote this article. Its facts were checked against the official docs linked above, and its code was run against a stubbed exchange.&lt;/p&gt;

&lt;p&gt;Trading involves risk of loss.&lt;/p&gt;

</description>
      <category>tradingview</category>
      <category>coinbase</category>
      <category>python</category>
      <category>webhooks</category>
    </item>
    <item>
      <title>How to connect TradingView to Hyperliquid</title>
      <dc:creator>Nyria</dc:creator>
      <pubDate>Sat, 14 Nov 2026 14:05:00 +0000</pubDate>
      <link>https://dev.to/nyriaautomation/how-to-connect-tradingview-to-hyperliquid-5090</link>
      <guid>https://dev.to/nyriaautomation/how-to-connect-tradingview-to-hyperliquid-5090</guid>
      <description>&lt;p&gt;To connect TradingView to Hyperliquid, give a TradingView alert a webhook URL. TradingView POSTs the alert's message to that URL. The program behind it turns the message into a perpetual order, signed by an API wallet your Hyperliquid account has approved.&lt;/p&gt;

&lt;p&gt;This article builds that program in about 100 lines of Python on Hyperliquid's &lt;a href="https://github.com/hyperliquid-dex/hyperliquid-python-sdk" rel="noopener noreferrer"&gt;official SDK&lt;/a&gt;. It trades crypto perpetuals with market orders and starts on testnet.&lt;/p&gt;

&lt;h2&gt;
  
  
  What limits does TradingView set?
&lt;/h2&gt;

&lt;p&gt;Each alert is one POST with the message as its body. TradingView's &lt;a href="https://www.tradingview.com/support/solutions/43000529348-how-to-configure-webhook-alerts/" rel="noopener noreferrer"&gt;webhook doc&lt;/a&gt; says only ports 80 and 443 are accepted, and a request is cancelled if your server takes longer than three seconds. IPv6 isn't supported. Webhook alerts need 2-factor authentication and, on the &lt;a href="https://www.tradingview.com/pricing/" rel="noopener noreferrer"&gt;pricing page&lt;/a&gt;, a paid plan.&lt;/p&gt;

&lt;h2&gt;
  
  
  What goes in the message?
&lt;/h2&gt;

&lt;p&gt;Use this as the message of a strategy alert:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"{{ticker}} {{strategy.order.id}} {{time}}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"ticker"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"{{ticker}}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"position"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"{{strategy.market_position}}"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;TradingView's &lt;a href="https://www.tradingview.com/support/solutions/43000531021-how-to-use-a-variable-value-in-alert/" rel="noopener noreferrer"&gt;placeholder list&lt;/a&gt; defines &lt;code&gt;{{strategy.market_position}}&lt;/code&gt; as the strategy's current position, &lt;code&gt;long&lt;/code&gt;, &lt;code&gt;flat&lt;/code&gt; or &lt;code&gt;short&lt;/code&gt;. The receiver treats that word as the target for the Hyperliquid position. A reversal becomes two orders.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{{strategy.order.id}}&lt;/code&gt; is the id you gave the order in Pine, and &lt;code&gt;{{time}}&lt;/code&gt; is the UTC time of the alert's bar. The receiver handles each &lt;code&gt;id&lt;/code&gt; once, so a replayed alert can't reopen a position you have closed since.&lt;/p&gt;

&lt;p&gt;A target can't express an add or a partial exit. Both leave &lt;code&gt;market_position&lt;/code&gt; where it was, and the receiver ignores a target it already holds.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;strategy&lt;/code&gt; placeholders resolve only in strategy alerts. For an indicator, create one alert per target and type the word yourself, in &lt;code&gt;position&lt;/code&gt; and in place of &lt;code&gt;{{strategy.order.id}}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The message carries no size. An environment variable sets it, so someone who learns the URL can't pick the amount.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an API wallet?
&lt;/h2&gt;

&lt;p&gt;Every Hyperliquid order carries a signature from a private key. An API wallet is a separate key that signs for your account. Hyperliquid's &lt;a href="https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/nonces-and-api-wallets" rel="noopener noreferrer"&gt;docs&lt;/a&gt; say: "A master account can approve API wallets to sign on behalf of the master account or any of the sub-accounts."&lt;/p&gt;

&lt;p&gt;The SDK's README says to "Generate and authorize a new API private key on &lt;a href="https://app.hyperliquid.xyz/API" rel="noopener noreferrer"&gt;https://app.hyperliquid.xyz/API&lt;/a&gt;". Its &lt;a href="https://github.com/hyperliquid-dex/hyperliquid-python-sdk/blob/master/examples/basic_agent.py" rel="noopener noreferrer"&gt;agent example&lt;/a&gt; adds: "The agent does not have permission to transfer or withdraw funds."&lt;/p&gt;

&lt;p&gt;The receiver needs that private key and your main account's address. Hyperliquid's docs say API wallets "are only used to sign", and that querying account data with the agent wallet "leads to an empty result". The receiver refuses to start on the API wallet's own address.&lt;/p&gt;

&lt;p&gt;An API wallet can expire, and registering a new unnamed one deregisters the old.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does the receiver look like?
&lt;/h2&gt;

&lt;p&gt;Run &lt;code&gt;pip install hyperliquid-python-sdk flask waitress&lt;/code&gt; and save this as &lt;code&gt;app.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;hmac&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;logging&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;queue&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sqlite3&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;threading&lt;/span&gt;

&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;eth_account&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Account&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;flask&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;abort&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;hyperliquid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exchange&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Exchange&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;hyperliquid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;utils&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;constants&lt;/span&gt;

&lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;basicConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;INFO&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getLogger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"tv-hyperliquid"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="no"&gt;SECRET&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"WEBHOOK_SECRET"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;ADDRESS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"HL_ACCOUNT_ADDRESS"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="no"&gt;USD_PER_ENTRY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"USD_PER_ENTRY"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;COINS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"BTCUSDT"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"BTC"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"ETHUSDT"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"ETH"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="no"&gt;API_URL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;constants&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;TESTNET_API_URL&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"HL_TESTNET"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"1"&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;constants&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;MAINNET_API_URL&lt;/span&gt;

&lt;span class="n"&gt;wallet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"HL_API_WALLET_KEY"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;wallet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;ADDRESS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"HL_ACCOUNT_ADDRESS must be the main account"&lt;/span&gt;
&lt;span class="n"&gt;exchange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Exchange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wallet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;API_URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;account_address&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;ADDRESS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;info&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;exchange&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt;
&lt;span class="no"&gt;SZ_DECIMALS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;asset&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;asset&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"szDecimals"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;asset&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;meta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"universe"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="kt"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;COINS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="kt"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;SZ_DECIMALS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"unknown coin in COINS"&lt;/span&gt;

&lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sqlite3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hook.db"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;check_same_thread&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;isolation_level&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"CREATE TABLE IF NOT EXISTS handled (id TEXT PRIMARY KEY)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;store_lock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;threading&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Lock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;jobs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Queue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/hook/&amp;lt;token&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;hook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;hmac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compare_digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SECRET&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;abort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;alert&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;silent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;force&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;abort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;alert_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;coin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;COINS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ticker"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"position"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;alert_id&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;coin&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"long"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"short"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"flat"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;abort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;store_lock&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"INSERT OR IGNORE INTO handled VALUES (?)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alert_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rowcount&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alert_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"queued"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;position&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;user_state&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ADDRESS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"assetPositions"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"position"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"coin"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"position"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"szi"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;filled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;what&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s2"&gt;"ok"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%s failed: %s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;what&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;
    &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"response"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"statuses"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="s2"&gt;"filled"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"filled"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"totalSz"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%s: %s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;what&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"error"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;trade&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alert_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;szi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;position&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;have&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"long"&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;szi&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s2"&gt;"short"&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;szi&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s2"&gt;"flat"&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;have&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%s ignored: %s is already %s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;alert_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;have&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;have&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s2"&gt;"flat"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;closed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;filled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"close "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exchange&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;market_close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# reduce-only&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%s closed %s %s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;alert_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;closed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;position&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%s: %s is still open, no new entry"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;alert_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"flat"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all_mids&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;USD_PER_ENTRY&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SZ_DECIMALS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;warning&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%s skipped: order value under $10"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;alert_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;got&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;filled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"open "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exchange&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;market_open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"long"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%s %s %s: filled %s of %s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;alert_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;got&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;job&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;trade&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;job&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"alert %s failed"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;job&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;threading&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;daemon&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The token check is &lt;a href="https://docs.python.org/3/library/hmac.html#hmac.compare_digest" rel="noopener noreferrer"&gt;&lt;code&gt;hmac.compare_digest&lt;/code&gt;&lt;/a&gt;, which the Python docs describe as "designed to prevent timing analysis". Any other token gets a 404.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;force=True&lt;/code&gt; parses the body as JSON even when it arrives as &lt;code&gt;text/plain&lt;/code&gt;. A body that won't parse gets a 400.&lt;/p&gt;

&lt;p&gt;The handler saves the id and queues the job. One worker thread makes every call to Hyperliquid, so the reply to TradingView doesn't wait on the exchange.&lt;/p&gt;

&lt;p&gt;A repeated id gets &lt;code&gt;{"queued": false}&lt;/code&gt;. The id is saved before any order goes out, so a crash in between loses that alert and a restart can't send it twice.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{{ticker}}&lt;/code&gt; is the ticker of the alert's symbol, &lt;code&gt;BTCUSD&lt;/code&gt; in TradingView's example. Hyperliquid names a perpetual by coin, the &lt;code&gt;name&lt;/code&gt; in its &lt;a href="https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals" rel="noopener noreferrer"&gt;meta response&lt;/a&gt;, such as &lt;code&gt;BTC&lt;/code&gt; or the less obvious &lt;code&gt;kPEPE&lt;/code&gt;. The ticker depends on the chart, so list yours in &lt;code&gt;COINS&lt;/code&gt;. An unlisted one gets a 400.&lt;/p&gt;

&lt;p&gt;Sizes follow the asset's &lt;code&gt;szDecimals&lt;/code&gt;. Hyperliquid's &lt;a href="https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/tick-and-lot-size" rel="noopener noreferrer"&gt;tick and lot size page&lt;/a&gt; says that with &lt;code&gt;szDecimals = 3&lt;/code&gt;, &lt;code&gt;1.001&lt;/code&gt; is a valid size but &lt;code&gt;1.0001&lt;/code&gt; is not. The receiver divides &lt;code&gt;USD_PER_ENTRY&lt;/code&gt; by the mid price and rounds to that many decimals, 5 for BTC and 4 for ETH on 13 Nov 2026.&lt;/p&gt;

&lt;p&gt;Before each trade the receiver reads &lt;code&gt;szi&lt;/code&gt;, Hyperliquid's signed position size, so it also sees positions you opened by hand. A &lt;code&gt;flat&lt;/code&gt; alert closes those too.&lt;/p&gt;

&lt;h2&gt;
  
  
  How is a market order placed?
&lt;/h2&gt;

&lt;p&gt;Hyperliquid's &lt;a href="https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint" rel="noopener noreferrer"&gt;exchange endpoint&lt;/a&gt; takes limit and trigger orders. The SDK's &lt;a href="https://github.com/hyperliquid-dex/hyperliquid-python-sdk/blob/master/hyperliquid/exchange.py" rel="noopener noreferrer"&gt;&lt;code&gt;market_open&lt;/code&gt;&lt;/a&gt; sends a limit order with time in force &lt;code&gt;Ioc&lt;/code&gt;, priced &lt;code&gt;slippage&lt;/code&gt; away from the mid (&lt;code&gt;0.05&lt;/code&gt; by default). The endpoint doc says IOC "will have the unfilled part canceled instead of resting". An entry can fill in part or not at all, and nothing stays on the book.&lt;/p&gt;

&lt;p&gt;Exits go through &lt;code&gt;market_close&lt;/code&gt;. It sends the opposite side for the full position with &lt;code&gt;reduce_only=True&lt;/code&gt;, which the &lt;a href="https://hyperliquid.gitbook.io/hyperliquid-docs/trading/order-types" rel="noopener noreferrer"&gt;order types page&lt;/a&gt; defines as "An order that reduces a current position as opposed to opening a new position in the opposite direction".&lt;/p&gt;

&lt;p&gt;On a reversal the receiver closes first and reads the position again. If anything is left, it logs that and stops.&lt;/p&gt;

&lt;p&gt;A rejected order doesn't raise an exception. The response is still HTTP 200 with the error text inside &lt;code&gt;statuses&lt;/code&gt;, and &lt;code&gt;filled()&lt;/code&gt; logs it.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I run it?
&lt;/h2&gt;

&lt;p&gt;Set five environment variables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;WEBHOOK_SECRET&lt;/code&gt;, a long random string. &lt;code&gt;python -c "import secrets; print(secrets.token_urlsafe(32))"&lt;/code&gt; prints one.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;HL_ACCOUNT_ADDRESS&lt;/code&gt;, the main account's address.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;HL_API_WALLET_KEY&lt;/code&gt;, the API wallet's private key.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;USD_PER_ENTRY&lt;/code&gt;, the value of each entry in USD.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;HL_TESTNET&lt;/code&gt;, set to &lt;code&gt;1&lt;/code&gt; for testnet.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Start it and put HTTPS in front:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;waitress-serve --listen=127.0.0.1:8000 app:app
caddy reverse-proxy --from alerts.example.com --to :8000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Flask's &lt;a href="https://flask.palletsprojects.com/en/stable/deploying/waitress/" rel="noopener noreferrer"&gt;Waitress page&lt;/a&gt; describes "a single process with multiple thread workers", which fits a queue held in memory. &lt;a href="https://caddyserver.com/docs/quick-starts/reverse-proxy" rel="noopener noreferrer"&gt;Caddy&lt;/a&gt; defaults to port 443 for HTTPS and tries to get a certificate once DNS points at the machine with ports 80 and 443 open.&lt;/p&gt;

&lt;p&gt;Keep it running as a &lt;a href="https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html" rel="noopener noreferrer"&gt;systemd service&lt;/a&gt; with &lt;code&gt;Restart=on-failure&lt;/code&gt;. Its &lt;a href="https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html" rel="noopener noreferrer"&gt;&lt;code&gt;EnvironmentFile=&lt;/code&gt;&lt;/a&gt; setting reads the private key from a separate file.&lt;/p&gt;

&lt;p&gt;The webhook URL for the alert is &lt;code&gt;https://alerts.example.com/hook/&lt;/code&gt; plus your secret.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does Hyperliquid have a testnet?
&lt;/h2&gt;

&lt;p&gt;Yes. The &lt;a href="https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api" rel="noopener noreferrer"&gt;API docs&lt;/a&gt; say you can make the same requests against &lt;code&gt;https://api.hyperliquid-testnet.xyz&lt;/code&gt;, and &lt;code&gt;HL_TESTNET=1&lt;/code&gt; points the SDK there. The &lt;a href="https://hyperliquid.gitbook.io/hyperliquid-docs/onboarding/testnet-faucet" rel="noopener noreferrer"&gt;testnet faucet&lt;/a&gt; page says an address that has deposited on mainnet may receive 1,000 mock USDC.&lt;/p&gt;

&lt;p&gt;The action that approves an API wallet, &lt;code&gt;approveAgent&lt;/code&gt;, names its chain, &lt;code&gt;Mainnet&lt;/code&gt; or &lt;code&gt;Testnet&lt;/code&gt;. An API wallet approved on mainnet is not approved on testnet, so approve one on each network you use. The SDK's agent example approves an API wallet on testnet in code.&lt;/p&gt;

&lt;p&gt;Then send one alert yourself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -X POST &lt;span class="s2"&gt;"https://alerts.example.com/hook/$WEBHOOK_SECRET"&lt;/span&gt; \
  -H &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; \
  -d &lt;span class="s1"&gt;'{"id":"manual-1","ticker":"BTCUSDT","position":"long"}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The reply is &lt;code&gt;{"queued":true}&lt;/code&gt; and the worker logs the fill. To close the position, send &lt;code&gt;"position":"flat"&lt;/code&gt; under a new id.&lt;/p&gt;

&lt;p&gt;On mainnet, start with a &lt;code&gt;USD_PER_ENTRY&lt;/code&gt; comfortably above the $10 minimum order value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why was my order rejected?
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Error text&lt;/th&gt;
&lt;th&gt;Cause&lt;/th&gt;
&lt;th&gt;Fix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Order must have minimum value of $10.&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Size times price is under $10&lt;/td&gt;
&lt;td&gt;Raise &lt;code&gt;USD_PER_ENTRY&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Insufficient margin to place order.&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The account can't margin that order&lt;/td&gt;
&lt;td&gt;Deposit, or lower &lt;code&gt;USD_PER_ENTRY&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Order could not immediately match against any resting orders.&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Nothing on the book sat inside the IOC limit&lt;/td&gt;
&lt;td&gt;Wait for the next alert, or pass a larger &lt;code&gt;slippage&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Reduce only order would increase position.&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The position changed before the exit landed&lt;/td&gt;
&lt;td&gt;The next alert reads the position again&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;L1 error: User or API Wallet 0x0123... does not exist.&lt;/code&gt; or &lt;code&gt;Must deposit before performing actions. User: 0x123...&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;The signature recovered to an address Hyperliquid doesn't know&lt;/td&gt;
&lt;td&gt;Check the key is the approved API wallet's and hasn't expired&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;float_to_wire causes rounding&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The SDK raises &lt;code&gt;ValueError&lt;/code&gt; for a number with more than 8 decimals&lt;/td&gt;
&lt;td&gt;Round the size to &lt;code&gt;szDecimals&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The exchange strings are from Hyperliquid's &lt;a href="https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/error-responses" rel="noopener noreferrer"&gt;error responses&lt;/a&gt; and &lt;a href="https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/signing" rel="noopener noreferrer"&gt;signing&lt;/a&gt; pages.&lt;/p&gt;

&lt;p&gt;AI tools wrote this article. Its facts were checked against the official docs linked above, and its code was run against a stubbed exchange.&lt;/p&gt;

&lt;p&gt;Trading involves risk of loss.&lt;/p&gt;

</description>
      <category>tradingview</category>
      <category>hyperliquid</category>
      <category>python</category>
      <category>webhooks</category>
    </item>
    <item>
      <title>How to send TradingView alerts to Binance</title>
      <dc:creator>Nyria</dc:creator>
      <pubDate>Fri, 13 Nov 2026 17:19:46 +0000</pubDate>
      <link>https://dev.to/nyriaautomation/how-to-send-tradingview-alerts-to-binance-amc</link>
      <guid>https://dev.to/nyriaautomation/how-to-send-tradingview-alerts-to-binance-amc</guid>
      <description>&lt;p&gt;A TradingView alert reaches Binance through a webhook. TradingView POSTs the alert message to a URL you choose, and the program behind that URL places the order through Binance's API with a key from your account.&lt;/p&gt;

&lt;p&gt;Binance has its own TradingView webhook feature, but its &lt;a href="https://www.binance.com/en/support/faq/how-to-set-up-signal-trading-with-tradingview-3f57291b56474f5e900cc4b754f61ff3" rel="noopener noreferrer"&gt;help page&lt;/a&gt; says: "Currently, only USDⓈ-M Futures users can access this feature." For spot, something else has to receive the webhook.&lt;/p&gt;

&lt;p&gt;The receiver below is about 80 lines of Flask and &lt;a href="https://docs.ccxt.com/" rel="noopener noreferrer"&gt;ccxt&lt;/a&gt;. It places spot market orders.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does TradingView send?
&lt;/h2&gt;

&lt;p&gt;One HTTP POST per alert, with the alert message as the body. Valid JSON arrives with an &lt;code&gt;application/json&lt;/code&gt; content type and anything else as &lt;code&gt;text/plain&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;TradingView's &lt;a href="https://www.tradingview.com/support/solutions/43000529348-how-to-configure-webhook-alerts/" rel="noopener noreferrer"&gt;webhook doc&lt;/a&gt; sets four limits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only ports 80 and 443 are accepted.&lt;/li&gt;
&lt;li&gt;A request is cancelled when your server takes longer than three seconds.&lt;/li&gt;
&lt;li&gt;IPv6 isn't supported, so the hostname needs an IPv4 address.&lt;/li&gt;
&lt;li&gt;Webhooks need two-factor sign-in, and the &lt;a href="https://www.tradingview.com/pricing/" rel="noopener noreferrer"&gt;pricing page&lt;/a&gt; lists webhook notifications on paid plans only.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The same doc says to keep credentials out of the body. The shared secret below sits in the URL path instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  What should the alert say?
&lt;/h2&gt;

&lt;p&gt;For a strategy alert, paste this as the message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"{{ticker}}-{{strategy.order.action}}-{{time}}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"symbol"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"{{ticker}}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"side"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"{{strategy.order.action}}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"position"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"{{strategy.market_position}}"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In TradingView's &lt;a href="https://www.tradingview.com/support/solutions/43000531021-how-to-use-a-variable-value-in-alert/" rel="noopener noreferrer"&gt;placeholder list&lt;/a&gt;, &lt;code&gt;{{strategy.order.action}}&lt;/code&gt; is &lt;code&gt;buy&lt;/code&gt; or &lt;code&gt;sell&lt;/code&gt;, &lt;code&gt;{{strategy.market_position}}&lt;/code&gt; is &lt;code&gt;long&lt;/code&gt;, &lt;code&gt;flat&lt;/code&gt; or &lt;code&gt;short&lt;/code&gt;, and &lt;code&gt;{{time}}&lt;/code&gt; is the bar's time in UTC.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;id&lt;/code&gt; is the duplicate guard. One ticker, side and bar produce one id, and the receiver acts on each id once. That costs you a second same-side fill inside a single bar.&lt;/p&gt;

&lt;p&gt;A strategy alert runs on a &lt;a href="https://www.tradingview.com/support/solutions/43000481368-strategy-alerts/" rel="noopener noreferrer"&gt;server-side copy&lt;/a&gt; of your strategy, so after you change the strategy's settings you delete the alert and create a new one. Skip the delete and both copies fire.&lt;/p&gt;

&lt;p&gt;Strategy placeholders only work in strategy alerts. For an indicator, create one alert per side and type the values in. The entry alert gets &lt;code&gt;buy&lt;/code&gt; in place of &lt;code&gt;{{strategy.order.action}}&lt;/code&gt; and &lt;code&gt;long&lt;/code&gt; as the position. The exit alert gets &lt;code&gt;sell&lt;/code&gt; and &lt;code&gt;flat&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Set an indicator alert's &lt;a href="https://www.tradingview.com/support/solutions/43000690908-why-is-the-once-per-bar-option-missing/" rel="noopener noreferrer"&gt;Trigger&lt;/a&gt; to Once per bar close. A strategy alert has no such option, because it fires on every order.&lt;/p&gt;

&lt;p&gt;Size stays out of the message. The receiver sizes every entry from its own config, so a leaked URL can't send a bigger number.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which Binance API key works?
&lt;/h2&gt;

&lt;p&gt;Create a system-generated key under API Management, following Binance's &lt;a href="https://www.binance.com/en/support/faq/how-to-create-api-keys-on-binance-360002502072" rel="noopener noreferrer"&gt;API key guide&lt;/a&gt;. Three settings matter:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reading and trading on.&lt;/li&gt;
&lt;li&gt;Withdrawals off. The receiver makes no withdrawals.&lt;/li&gt;
&lt;li&gt;Access restricted to your server's IPv4 address.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A new key can't trade yet. Binance's &lt;a href="https://developers.binance.com/docs/binance-spot-api-docs/rest-api/request-security" rel="noopener noreferrer"&gt;API doc&lt;/a&gt; says: "By default, an API key cannot TRADE. You need to enable trading in API Management first."&lt;/p&gt;

&lt;p&gt;Binance enforces the IP restriction by default. Its guide says: "Unrestricted-IP-Access HMAC API Key(s) won't have any permission other than reading." So with a system-generated key, the receiver needs a server with a fixed public IP.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I receive the webhook?
&lt;/h2&gt;

&lt;p&gt;Run &lt;code&gt;pip install flask ccxt gunicorn&lt;/code&gt; and save this as &lt;code&gt;app.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;hmac&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;logging&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;queue&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sqlite3&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;threading&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ccxt&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;flask&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;abort&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;

&lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;basicConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;INFO&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getLogger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"tv-binance"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="no"&gt;SECRET&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"WEBHOOK_SECRET"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;BUDGET&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"QUOTE_PER_ENTRY"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"25"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;SYMBOLS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"BTCUSDT"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"BTC/USDT"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"ETHUSDT"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"ETH/USDT"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;exchange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ccxt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;binance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"apiKey"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"BINANCE_API_KEY"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"secret"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"BINANCE_API_SECRET"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"options"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"fetchMarkets"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"types"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"spot"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"BINANCE_TESTNET"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;exchange&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_sandbox_mode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# has to come before any other call&lt;/span&gt;
&lt;span class="n"&gt;exchange&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load_markets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sqlite3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"alerts.db"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;check_same_thread&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;isolation_level&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"CREATE TABLE IF NOT EXISTS seen (id TEXT PRIMARY KEY)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"CREATE TABLE IF NOT EXISTS held (symbol TEXT PRIMARY KEY, amount REAL)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;db_lock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;threading&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Lock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;jobs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Queue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/tv/&amp;lt;token&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;webhook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;hmac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compare_digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SECRET&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;abort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;alert&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;silent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;force&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"symbol"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="no"&gt;SYMBOLS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;abort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;db_lock&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"INSERT OR IGNORE INTO seen VALUES (?)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rowcount&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"queued"&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s2"&gt;"duplicate"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;place&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;symbol&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SYMBOLS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"symbol"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;market&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;exchange&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;market&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;side&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"side"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;db_lock&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SELECT amount FROM held WHERE symbol = ?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;held&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;
    &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;exchange&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetch_ticker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"last"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;side&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"buy"&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"position"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"long"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;BUDGET&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;
    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;side&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"sell"&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;held&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;held&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exchange&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetch_balance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;market&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"base"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"free"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ignored %s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exchange&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;amount_to_precision&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;market&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"limits"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"cost"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"min"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;warning&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"skipped %s: under the minimum notional"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;exchange&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"market"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;side&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;filled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"filled"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;
    &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;held&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;filled&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;side&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"buy"&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;held&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;filled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;db_lock&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"INSERT OR REPLACE INTO held VALUES (?, ?)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%s %s %s, filled %s, order %s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;side&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;filled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;alert&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;place&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"no order for %s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;threading&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;daemon&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The secret check uses &lt;a href="https://docs.python.org/3/library/hmac.html#hmac.compare_digest" rel="noopener noreferrer"&gt;hmac.compare_digest&lt;/a&gt;, which compares without leaking timing. A wrong token gets a 404.&lt;/p&gt;

&lt;p&gt;Flask's &lt;a href="https://flask.palletsprojects.com/en/stable/api/#flask.Request.get_json" rel="noopener noreferrer"&gt;get_json&lt;/a&gt; with &lt;code&gt;force=True&lt;/code&gt; parses the body as JSON whatever content type it arrives with. With &lt;code&gt;silent=True&lt;/code&gt;, a body that isn't JSON comes back as &lt;code&gt;None&lt;/code&gt; and the handler answers 400.&lt;/p&gt;

&lt;p&gt;The handler records the id, queues the alert and returns 200. One worker thread talks to Binance, so the three-second limit only covers a database write.&lt;/p&gt;

&lt;p&gt;The id goes into SQLite before the order goes out, and a repeat gets &lt;code&gt;duplicate&lt;/code&gt; back. If the process dies in between, that alert is dropped. The design accepts a missed order to rule out a doubled one. Binance's &lt;code&gt;newClientOrderId&lt;/code&gt; won't cover this, because the &lt;a href="https://developers.binance.com/docs/binance-spot-api-docs/rest-api/trading-endpoints" rel="noopener noreferrer"&gt;trading doc&lt;/a&gt; lets an id be reused once the earlier order has filled.&lt;/p&gt;

&lt;p&gt;On a Binance BTC/USDT chart, &lt;code&gt;{{ticker}}&lt;/code&gt; is &lt;code&gt;BTCUSDT&lt;/code&gt;, and ccxt wants &lt;code&gt;BTC/USDT&lt;/code&gt;. The &lt;code&gt;SYMBOLS&lt;/code&gt; map translates, and a ticker missing from it gets a 400.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;fetchMarkets&lt;/code&gt; option keeps &lt;code&gt;load_markets()&lt;/code&gt; to spot, so startup doesn't call Binance's futures API.&lt;/p&gt;

&lt;p&gt;Binance's &lt;a href="https://developers.binance.com/docs/binance-spot-api-docs/filters" rel="noopener noreferrer"&gt;LOT_SIZE filter&lt;/a&gt; only accepts quantities in increments of the symbol's &lt;code&gt;stepSize&lt;/code&gt;, so &lt;code&gt;amount_to_precision&lt;/code&gt; cuts the amount down to that step. On 13 Nov 2026, BTCUSDT's step was 0.00001.&lt;/p&gt;

&lt;p&gt;The NOTIONAL filter rejects an order when price times quantity is under &lt;code&gt;minNotional&lt;/code&gt;, 5 USDT for BTCUSDT on the same day. ccxt exposes it as &lt;code&gt;limits.cost.min&lt;/code&gt;. The receiver checks against the last price and Binance uses an average, so treat it as a pre-check.&lt;/p&gt;

&lt;p&gt;Sells cover only what the receiver bought, capped at the free balance. Unless you pay fees in BNB, Binance takes its &lt;a href="https://developers.binance.com/docs/binance-spot-api-docs/faqs/commission_faq" rel="noopener noreferrer"&gt;commission&lt;/a&gt; out of the coin you received, so the free balance ends up below the filled amount.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;sell&lt;/code&gt; with nothing held is ignored, and so is a &lt;code&gt;buy&lt;/code&gt; that only covers a short.&lt;/p&gt;

&lt;p&gt;Export &lt;code&gt;WEBHOOK_SECRET&lt;/code&gt;, &lt;code&gt;BINANCE_API_KEY&lt;/code&gt; and &lt;code&gt;BINANCE_API_SECRET&lt;/code&gt; before you start the receiver. Generate the secret with &lt;code&gt;python -c "import secrets; print(secrets.token_urlsafe(32))"&lt;/code&gt;. &lt;code&gt;QUOTE_PER_ENTRY&lt;/code&gt; is the USDT spent on each entry and defaults to 25.&lt;/p&gt;

&lt;p&gt;Start it under &lt;a href="https://gunicorn.org/reference/settings/" rel="noopener noreferrer"&gt;gunicorn&lt;/a&gt; with one worker process, because the queue lives in memory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gunicorn --workers 1 --threads 4 --bind 127.0.0.1:8000 app:app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To keep it up, run that command as a &lt;a href="https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html" rel="noopener noreferrer"&gt;systemd service&lt;/a&gt; with &lt;code&gt;Restart=on-failure&lt;/code&gt;, which restarts it after a crash. An &lt;a href="https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html" rel="noopener noreferrer"&gt;EnvironmentFile=&lt;/a&gt; line loads the same variables from a file.&lt;/p&gt;

&lt;p&gt;Put &lt;a href="https://caddyserver.com/docs/quick-starts/reverse-proxy" rel="noopener noreferrer"&gt;Caddy&lt;/a&gt; in front for HTTPS on 443 with this Caddyfile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;alerts.example.com
reverse_proxy 127.0.0.1:8000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Caddy &lt;a href="https://caddyserver.com/docs/automatic-https" rel="noopener noreferrer"&gt;fetches and renews the certificate&lt;/a&gt; once the hostname points at the server and ports 80 and 443 are open. The webhook URL for TradingView is &lt;code&gt;https://alerts.example.com/tv/&lt;/code&gt; followed by your secret.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I test it?
&lt;/h2&gt;

&lt;p&gt;Start on Binance's &lt;a href="https://developers.binance.com/docs/binance-spot-api-docs/testnet/rest-api" rel="noopener noreferrer"&gt;Spot Test Network&lt;/a&gt;, which issues its own API keys. Set &lt;code&gt;BINANCE_TESTNET=1&lt;/code&gt; to point ccxt's sandbox mode at it, then send one alert yourself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -X POST &lt;span class="s2"&gt;"https://alerts.example.com/tv/$WEBHOOK_SECRET"&lt;/span&gt; \
  -H &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; \
  -d &lt;span class="s1"&gt;'{"id":"t1","symbol":"BTCUSDT","side":"buy","position":"long"}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first call returns &lt;code&gt;{"status":"queued"}&lt;/code&gt; and a repeat returns &lt;code&gt;{"status":"duplicate"}&lt;/code&gt;. The worker logs the order and its filled amount.&lt;/p&gt;

&lt;p&gt;Then go live with the smallest &lt;code&gt;QUOTE_PER_ENTRY&lt;/code&gt; that clears the minimum notional.&lt;/p&gt;

&lt;h2&gt;
  
  
  What breaks orders?
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What you see&lt;/th&gt;
&lt;th&gt;Cause&lt;/th&gt;
&lt;th&gt;Fix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Nothing reaches the server&lt;/td&gt;
&lt;td&gt;Port other than 80 or 443, IPv6-only hostname, free plan, or two-factor sign-in off&lt;/td&gt;
&lt;td&gt;Serve on 443 over IPv4. Check Webhook status in the alert log&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;TradingView cancels the request&lt;/td&gt;
&lt;td&gt;Handler took longer than three seconds&lt;/td&gt;
&lt;td&gt;Queue the alert and return&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Receiver answers 400&lt;/td&gt;
&lt;td&gt;Message isn't valid JSON, has no &lt;code&gt;id&lt;/code&gt;, or the ticker isn't in &lt;code&gt;SYMBOLS&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Fix the message or add the ticker&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Alerts stop arriving&lt;/td&gt;
&lt;td&gt;A standard alert &lt;a href="https://www.tradingview.com/support/solutions/43000520149-introduction-to-tradingview-alerts/" rel="noopener noreferrer"&gt;lasts two months at most&lt;/a&gt;, open-ended on Premium and Ultimate. A strategy alert stops after more than 15 triggers in 3 minutes&lt;/td&gt;
&lt;td&gt;Create it again&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-2015 Invalid API-key, IP, or permissions for action.&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Server IP missing from the key, or trading not enabled&lt;/td&gt;
&lt;td&gt;Fix the key&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-1013 Filter failure: NOTIONAL&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Order value under &lt;code&gt;minNotional&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Raise &lt;code&gt;QUOTE_PER_ENTRY&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-1013 Filter failure: LOT_SIZE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Quantity off the &lt;code&gt;stepSize&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Use &lt;code&gt;amount_to_precision&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-2010 Account has insufficient balance for requested action.&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Too little quote currency, or a sell above the free balance&lt;/td&gt;
&lt;td&gt;Fund the account&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-1021 Timestamp for this request is outside of the recvWindow.&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Server clock has drifted&lt;/td&gt;
&lt;td&gt;Sync it with NTP&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HTTP 451 with &lt;code&gt;Service unavailable from a restricted location according to 'b. Eligibility' in https://www.binance.com/en/terms.&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Binance's terms restrict the location the request came from. A US IP address got this on 13 Nov 2026&lt;/td&gt;
&lt;td&gt;Check eligibility in those terms. ccxt lists Binance US separately, as &lt;code&gt;binanceus&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Binance's &lt;a href="https://developers.binance.com/docs/binance-spot-api-docs/errors" rel="noopener noreferrer"&gt;error list&lt;/a&gt; defines the numbered codes. ccxt raises them as exceptions and the worker logs each one.&lt;/p&gt;

&lt;p&gt;AI tools wrote this article. Its facts were checked against the official docs linked above, and its code was run against a stubbed exchange.&lt;/p&gt;

&lt;p&gt;Trading involves risk of loss.&lt;/p&gt;

</description>
      <category>tradingview</category>
      <category>binance</category>
      <category>python</category>
      <category>webhooks</category>
    </item>
    <item>
      <title>How to pick an options contract from a TradingView alert</title>
      <dc:creator>Nyria</dc:creator>
      <pubDate>Fri, 23 Oct 2026 09:41:00 +0000</pubDate>
      <link>https://dev.to/nyriaautomation/tradingview-alert-to-a-real-options-order-picking-the-strike-when-the-alert-doesnt-say-1a54</link>
      <guid>https://dev.to/nyriaautomation/tradingview-alert-to-a-real-options-order-picking-the-strike-when-the-alert-doesnt-say-1a54</guid>
      <description>&lt;p&gt;A TradingView alert can say which ticker moved and in which direction. It has no built-in placeholder for a strike or an expiration, so unless the message text names the contract, it can't say which option to trade. The program that receives the alert then has to choose the contract from rules written in advance. Those rules cover the expiration, the strike, the liquidity of the quote and the cases where no order should go out.&lt;/p&gt;

&lt;p&gt;Throughout, a bullish alert buys a call and a bearish alert buys a put.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does a TradingView alert carry?
&lt;/h2&gt;

&lt;p&gt;An alert message is text with &lt;a href="https://www.tradingview.com/support/solutions/43000531021-how-to-use-a-variable-value-in-alert/" rel="noopener noreferrer"&gt;placeholders&lt;/a&gt; that TradingView fills when the alert fires. &lt;code&gt;{{ticker}}&lt;/code&gt; gives the symbol's ticker, with AAPL and BTCUSD as the page's examples. &lt;code&gt;{{close}}&lt;/code&gt; and &lt;code&gt;{{time}}&lt;/code&gt; give the closing price and the time of the bar that triggered the alert. On a strategy, &lt;code&gt;{{strategy.order.action}}&lt;/code&gt; returns &lt;code&gt;buy&lt;/code&gt; or &lt;code&gt;sell&lt;/code&gt;, and &lt;code&gt;{{strategy.market_position}}&lt;/code&gt; returns &lt;code&gt;long&lt;/code&gt;, &lt;code&gt;flat&lt;/code&gt; or &lt;code&gt;short&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That list has no placeholder for a strike, an expiration or a delta. A script can still send one, because the message is free text and &lt;code&gt;{{plot_0}}&lt;/code&gt; to &lt;code&gt;{{plot_19}}&lt;/code&gt; carry the values an indicator outputs. TradingView's part ends at delivery, which its &lt;a href="https://www.tradingview.com/support/solutions/43000529348-how-to-configure-webhook-alerts/" rel="noopener noreferrer"&gt;webhook guide&lt;/a&gt; describes as a POST request to your URL with the message as its body. Unless the message names the contract, the receiver decides it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What identifies one option contract?
&lt;/h2&gt;

&lt;p&gt;Four facts do. The Options Industry Council's &lt;a href="https://www.optionseducation.org/optionsoverview/what-is-an-option" rel="noopener noreferrer"&gt;introduction&lt;/a&gt; describes a contract as fixing a strike price and carrying an expiration date. The underlying and the choice of call or put complete the set.&lt;/p&gt;

&lt;p&gt;Brokers pack all four into one symbol. tastytrade's &lt;a href="https://developer.tastytrade.com/docs/concepts/instruments-and-symbology" rel="noopener noreferrer"&gt;symbology page&lt;/a&gt; lays out the OCC format as a root of "up to 6 alphanumeric characters, right-padded with spaces", a six-digit expiration, &lt;code&gt;C&lt;/code&gt; or &lt;code&gt;P&lt;/code&gt;, and eight digits holding the "strike multiplied by 1000". It warns, "Note the padding spaces between root and expiration are significant." Alpaca's &lt;a href="https://docs.alpaca.markets/us/docs/options-trading" rel="noopener noreferrer"&gt;options page&lt;/a&gt; writes its example without padding, as &lt;code&gt;AAPL240119C00100000&lt;/code&gt;. Reading the symbol from your broker's own chain avoids building it by hand.&lt;/p&gt;

&lt;p&gt;OIC's &lt;a href="https://www.optionseducation.org/optionsoverview/options-basics" rel="noopener noreferrer"&gt;basics page&lt;/a&gt; puts the usual contract size at 100 shares, so a premium quoted at 2.10 costs 210 dollars per contract.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you choose the expiration?
&lt;/h2&gt;

&lt;p&gt;Write the rule as a window in days and take the nearest listed expiry inside it. A same-day strategy sets the window to today. A swing strategy might ask for the first expiry at least two weeks out.&lt;/p&gt;

&lt;p&gt;The list of real expirations has to come from the broker. OIC notes that standard monthly options usually expire on the third Friday, and that "Many products now offer short-term options with weekly expirations, so investors should know the exact contract terms, including expiration dates, for all contracts they trade." A date computed from the calendar can miss a holiday, or assume weeklies on a ticker that has none.&lt;/p&gt;

&lt;p&gt;Two API details bite here. Tradier's &lt;a href="https://docs.tradier.com/brokerage-api/market-data/get-options-chains" rel="noopener noreferrer"&gt;chain reference&lt;/a&gt; says "The expiration parameter must be a date the underlying symbol actually has options listed for." Pass an unlisted date and there is no error, because "it returns an empty response." Alpaca's contracts route has a short default horizon, listed on its options page as "expiration_date_lte: Next weekend", so a swing rule must send its own date range.&lt;/p&gt;

&lt;p&gt;The window matters because of time decay. OIC's &lt;a href="https://www.optionseducation.org/advancedconcepts/theta" rel="noopener noreferrer"&gt;page on Theta&lt;/a&gt; says "options with the least remaining time until expiration will tend to decay the most."&lt;/p&gt;

&lt;h2&gt;
  
  
  What if an earnings date falls inside the window?
&lt;/h2&gt;

&lt;p&gt;"Nearest listed expiry" can land on a contract whose life includes a scheduled event, such as an earnings report. That contract is priced for the event. OIC's &lt;a href="https://www.optionseducation.org/advancedconcepts/volatility-the-greeks" rel="noopener noreferrer"&gt;volatility page&lt;/a&gt; defines implied volatility as "a measure of how much the marketplace expects the asset price to move based on the price of the option." A premium that holds an expected event move can't be compared with the same strike in a quiet week.&lt;/p&gt;

&lt;p&gt;The alert carries no event calendar, so the rule needs one from another source. Then make the choice explicit. Either refuse the order when an event sits between today and the expiry, or place it and mark the log line so the event premium is visible. Picking such a contract silently is the outcome to avoid.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you choose the strike?
&lt;/h2&gt;

&lt;p&gt;Two rules are common. One takes a fixed distance from the underlying price. The other targets a delta. OIC's &lt;a href="https://www.optionseducation.org/advancedconcepts/delta" rel="noopener noreferrer"&gt;Delta page&lt;/a&gt; says "Delta is a theoretical estimate of how much an option's premium may change given a $1 move in the underlying." It gives the range too, "Call Deltas range from 0.00 to 1.00 while put Delta ranges from 0.00 to -1.00."&lt;/p&gt;

&lt;p&gt;A delta rule picks the listed strike closest to the target and then measures the miss. Asking for 0.30 and accepting 0.62 because it was nearest defeats the rule, so set a band and treat anything outside it as a refusal.&lt;/p&gt;

&lt;p&gt;Delta comes from the broker's chain. Alpaca's &lt;a href="https://docs.alpaca.markets/us/reference/optionchain" rel="noopener noreferrer"&gt;option chain route&lt;/a&gt; "provides the latest trade, latest quote, and greeks for each contract symbol of the underlying symbol." Tradier returns greeks when the request sets &lt;code&gt;greeks=true&lt;/code&gt;, along with an &lt;code&gt;updated_at&lt;/code&gt; field described as "The timestamp indicating when the greeks and implied volatility values were last calculated." Compare that timestamp with the clock before trusting the number.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you check liquidity before sending?
&lt;/h2&gt;

&lt;p&gt;Read the quote for the chosen contract and test it. Alpaca's chain returns &lt;code&gt;bp&lt;/code&gt; and &lt;code&gt;ap&lt;/code&gt;, which the reference calls "The best bid and ask information for a given option." Tradier's chain lists &lt;code&gt;bid&lt;/code&gt;, &lt;code&gt;ask&lt;/code&gt;, &lt;code&gt;volume&lt;/code&gt; and &lt;code&gt;open_interest&lt;/code&gt; for each contract.&lt;/p&gt;

&lt;p&gt;Run three tests. The bid must be above zero. The gap between bid and ask must stay under a limit you set, in cents or as a fraction of the mid price. Open interest must clear a floor.&lt;/p&gt;

&lt;p&gt;Check which feed the quote came from. Alpaca's reference says "&lt;code&gt;indicative&lt;/code&gt; is a free indicative feed where trades are delayed and quotes are modified." A spread test on modified quotes tells you little.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which order type and size should the rule use?
&lt;/h2&gt;

&lt;p&gt;Use a limit order priced from the quote you just checked. Alpaca's &lt;a href="https://docs.alpaca.markets/us/docs/orders-at-alpaca" rel="noopener noreferrer"&gt;orders page&lt;/a&gt; says of market orders that "your fill price may slip depending on the available liquidity at each price level". On a wide option quote that slip can be large.&lt;/p&gt;

&lt;p&gt;Size in whole contracts. Alpaca's option validations include "Ensuring &lt;code&gt;qty&lt;/code&gt; is a whole number" and "&lt;code&gt;time_in_force&lt;/code&gt; must be &lt;code&gt;day&lt;/code&gt; or &lt;code&gt;gtc&lt;/code&gt;". Divide the budget by the limit price times 100 and round down. A result of zero is a refusal.&lt;/p&gt;

&lt;p&gt;The side stays the same for both directions under that mapping. Tradier's &lt;a href="https://docs.tradier.com/brokerage-api/trading/place-option-order" rel="noopener noreferrer"&gt;option order reference&lt;/a&gt; lists &lt;code&gt;side&lt;/code&gt; as "One of: buy_to_open, buy_to_close, sell_to_open, sell_to_close". A bearish alert opens a put with &lt;code&gt;buy_to_open&lt;/code&gt;, the same side a bullish alert uses for a call.&lt;/p&gt;

&lt;h2&gt;
  
  
  When should the rule refuse to trade?
&lt;/h2&gt;

&lt;p&gt;A rule that always finds a contract will sooner or later find a bad one. Write each refusal to the log beside the alert that caused it.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Refusal&lt;/th&gt;
&lt;th&gt;Condition&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;No expiry in the window&lt;/td&gt;
&lt;td&gt;The broker lists no expiration inside it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Strike too far from target&lt;/td&gt;
&lt;td&gt;The nearest delta falls outside the band&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stale greeks&lt;/td&gt;
&lt;td&gt;The greeks timestamp is older than your limit&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Thin or one-sided quote&lt;/td&gt;
&lt;td&gt;Zero bid, wide spread or low open interest&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Event before expiry&lt;/td&gt;
&lt;td&gt;An earnings date or similar sits inside the contract's life&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Market closed&lt;/td&gt;
&lt;td&gt;The contract's own session isn't open&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Budget below one contract&lt;/td&gt;
&lt;td&gt;The limit price times 100 exceeds the budget&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Account not approved&lt;/td&gt;
&lt;td&gt;On Alpaca, buying a call or put needs options level 2&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Don't hard-code one closing time for the market-closed check. Cboe's &lt;a href="https://www.cboe.com/about/hours/us-options/" rel="noopener noreferrer"&gt;hours page&lt;/a&gt; runs regular hours for equity options on its C1 exchange from 9:30 a.m. until 4:00 p.m. ET. ETF options that trade until 4:15 p.m. stay inside that regular session, and the core session on its BZX, C2 and EDGX exchanges also ends at 4:15 p.m. C1 lists Global Trading Hours for select equity options from 7:30 a.m. to 9:25 a.m. as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens at the exit and at expiration?
&lt;/h2&gt;

&lt;p&gt;An exit alert built from the same placeholders names the ticker again and no contract. Store the contract symbol chosen at entry and close that exact symbol, with &lt;code&gt;sell_to_close&lt;/code&gt; on Tradier.&lt;/p&gt;

&lt;p&gt;Don't leave a long option to expire by accident. Alpaca's options page says "By default, Alpaca will automatically exercise in-the-money (ITM) contracts at expiry." When the account can't afford the exercise, "Alpaca will sell-out the position within 1 hour before expiry." Add a rule that closes any open contract before its last session if no exit alert has arrived.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you test the rules?
&lt;/h2&gt;

&lt;p&gt;Run them on a paper account and read every refusal. Options trading is on by default in Alpaca's paper environment, per its options page. Its &lt;a href="https://docs.alpaca.markets/us/docs/paper-trading" rel="noopener noreferrer"&gt;paper trading page&lt;/a&gt; warns that a paper order can fill at a size far beyond the liquidity that is really there. Paper fills on thin contracts therefore prove less than the log of what the rule declined.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Symptom&lt;/th&gt;
&lt;th&gt;Likely cause&lt;/th&gt;
&lt;th&gt;Where it is documented&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Chain comes back empty&lt;/td&gt;
&lt;td&gt;The expiration date isn't listed for that ticker&lt;/td&gt;
&lt;td&gt;Tradier chain reference&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Only this week's contracts appear&lt;/td&gt;
&lt;td&gt;Default &lt;code&gt;expiration_date_lte&lt;/code&gt; on the contracts route&lt;/td&gt;
&lt;td&gt;Alpaca options page&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Contract lookup fails&lt;/td&gt;
&lt;td&gt;Padding in the OCC root differs by broker&lt;/td&gt;
&lt;td&gt;tastytrade symbology page&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Order rejected&lt;/td&gt;
&lt;td&gt;Fractional &lt;code&gt;qty&lt;/code&gt;, or a &lt;code&gt;time_in_force&lt;/code&gt; other than &lt;code&gt;day&lt;/code&gt; or &lt;code&gt;gtc&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Alpaca options page&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;AI tools wrote this article. Its facts were checked against the official docs linked above.&lt;/p&gt;

&lt;p&gt;Trading involves risk of loss.&lt;/p&gt;

</description>
      <category>tradingview</category>
      <category>options</category>
      <category>trading</category>
      <category>automation</category>
    </item>
    <item>
      <title>How to auto-trade Discord trading calls within Discord's rules</title>
      <dc:creator>Nyria</dc:creator>
      <pubDate>Fri, 23 Oct 2026 04:51:47 +0000</pubDate>
      <link>https://dev.to/nyriaautomation/how-to-auto-trade-the-calls-from-a-discord-trading-server-without-a-bot-token-12ig</link>
      <guid>https://dev.to/nyriaautomation/how-to-auto-trade-the-calls-from-a-discord-trading-server-without-a-bot-token-12ig</guid>
      <description>&lt;p&gt;Calls posted in a Discord server can be traded automatically in two ways that Discord permits. A bot application can read the channel once someone with permission in that server adds it. Or the person who makes the calls can send each one straight to a program that places orders and post it to Discord through a webhook. Automating your own user account to read a server is forbidden, and it can cost you the account.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does Discord allow?
&lt;/h2&gt;

&lt;p&gt;Discord has an account type made for automation. Its support article on &lt;a href="https://support.discord.com/hc/en-us/articles/115002192352-Automated-User-Accounts-Self-Bots" rel="noopener noreferrer"&gt;automated user accounts&lt;/a&gt; says "Discord's API provides a separate type of user account dedicated to automation, called a bot account." It goes on, "Bot accounts can be created through the applications page, and are authenticated using a token (rather than a username and password)."&lt;/p&gt;

&lt;p&gt;A bot enters a server by being added to it. The &lt;a href="https://docs.discord.com/developers/topics/oauth2" rel="noopener noreferrer"&gt;OAuth2 docs&lt;/a&gt; say "Bots are added to guilds through the OAuth2 API, and cannot accept normal invites." Whoever opens the install link is "prompted to add the bot to a guild in which they have proper permissions." The decision belongs to the people who run the server.&lt;/p&gt;

&lt;p&gt;Webhooks are the second permitted tool, and they work in the other direction. The &lt;a href="https://docs.discord.com/developers/resources/webhook" rel="noopener noreferrer"&gt;webhook docs&lt;/a&gt; call them "a low-effort way to post messages to channels in Discord." A webhook writes into a channel and reads nothing from it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why are self-bots and user tokens banned?
&lt;/h2&gt;

&lt;p&gt;A self-bot is a normal account driven by a script. The support article says automating normal user accounts "outside of the OAuth2/bot API is forbidden, and can result in an account termination if found."&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://discord.com/guidelines" rel="noopener noreferrer"&gt;Community Guidelines&lt;/a&gt; repeat it as rule 14, "Do not use self-bots or user-bots. Each account must be associated with a human, not a bot." That page also states, "These Guidelines are incorporated into our Terms of Service". The &lt;a href="https://discord.com/terms" rel="noopener noreferrer"&gt;Terms&lt;/a&gt; add their own ban on "scraping our services without our written consent, including by using any robot, spider, crawler, scraper, or other automatic device, process, or software".&lt;/p&gt;

&lt;p&gt;A tool that asks you to paste your Discord token wants to read a server as you. Discord's &lt;a href="https://docs.discord.com/developers/policies-and-agreements/developer-policy" rel="noopener noreferrer"&gt;Developer Policy&lt;/a&gt; closes that door from the developer's side. Its rule 4 reads "Under no circumstances may you or your Application request or attempt to obtain login credentials from Discord users. This includes information such as passwords or account access or login tokens."&lt;/p&gt;

&lt;p&gt;So the tool is barred from asking, and you are barred from running the result. The quality of the calls changes none of it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What if you run the server?
&lt;/h2&gt;

&lt;p&gt;Then the permitted route is short. Create an application in the Developer Portal, add its bot to your server, and have it listen to the channel where calls are posted.&lt;/p&gt;

&lt;p&gt;Reading message text takes one extra step. The &lt;a href="https://docs.discord.com/developers/events/gateway" rel="noopener noreferrer"&gt;gateway docs&lt;/a&gt; class &lt;code&gt;MESSAGE_CONTENT&lt;/code&gt; as a privileged intent that "permits your app to receive message content data across the APIs." They add, "Before you can specify any of these privileged intents in your &lt;code&gt;IDENTIFY&lt;/code&gt; payload, you must enable the specific privileged intents you need in the Developer Portal."&lt;/p&gt;

&lt;p&gt;A small private bot needs no review, because "Apps with fewer than 10,000 users can access privileged intents by enabling them in the Developer Portal." One condition applies at any size, "You may only enable a privileged intent if your use case requires it to function and is consistent with our Developer Policy."&lt;/p&gt;

&lt;p&gt;That policy also limits what a bot does with the messages it reads. Discord's &lt;a href="https://support-dev.discord.com/hc/en-us/articles/8562894815383-Discord-Developer-Terms-of-Service" rel="noopener noreferrer"&gt;Developer Terms&lt;/a&gt; define API Data as "any data, information, or other content you obtain through the APIs". Rule 15 of the policy says "Do not use API Data for any purpose outside of what is necessary to provide your stated functionality." Rule 18 says "Do not sell, license, or otherwise commercialize API Data or any of Discord's services". Rule 21 bars training AI models on message content obtained through the APIs unless Discord gives express permission. A bot whose stated function is placing orders from the calls should use the messages for that purpose.&lt;/p&gt;

&lt;p&gt;A bot built for one server can stay closed to everyone else. Of the &lt;code&gt;Public Bot&lt;/code&gt; setting, the OAuth2 docs say "If unchecked, only you can add the bot to guilds."&lt;/p&gt;

&lt;p&gt;There is also a design that needs no reading at all. If the calls begin in your own tool, such as a script or a chart alert, send each one to the order program first and to Discord second. The webhook docs say "Incoming Webhooks can post messages to channels with a generated token", so the same event can post the call for members. The channel then displays what was traded, and no program depends on parsing it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What if you are only a member?
&lt;/h2&gt;

&lt;p&gt;A member without the needed permissions can't add a bot to someone else's server, and the member's own account can't be automated. Each remaining option runs through the owner.&lt;/p&gt;

&lt;p&gt;The first is to ask the owner to install an app for the purpose. The Terms place that step with administrators, saying "third parties can build apps for content moderation or interactive games that server administrators can add to their servers on Discord". Their examples are moderation and games, and trading isn't mentioned.&lt;/p&gt;

&lt;p&gt;The second is an announcement channel. Discord's &lt;a href="https://support.discord.com/hc/en-us/articles/360032008192-Announcement-Channel-FAQ" rel="noopener noreferrer"&gt;Announcement Channel FAQ&lt;/a&gt; says these channels "include a Follow button, allowing server members to receive updates in their own servers." It adds that a published message "appears as a regular message in all following servers". The &lt;a href="https://docs.discord.com/developers/resources/message" rel="noopener noreferrer"&gt;message reference&lt;/a&gt; gives such a post the &lt;code&gt;IS_CROSSPOST&lt;/code&gt; flag, described as "this message originated from a message in another channel (via Channel Following)". A bot in your own server should receive it there as an ordinary message, under the same intent. The FAQ sets one limit, "Announcement Channels are only available to servers with Community enabled."&lt;/p&gt;

&lt;p&gt;Discord's pages describe following as a feature and say nothing about trading on followed posts. The data rules above apply to a bot that reads them.&lt;/p&gt;

&lt;p&gt;Keep what arrives for your own orders. The Terms bar "selling, licensing, or otherwise commercializing content or data obtained from our services", and the server may have rules of its own about reuse.&lt;/p&gt;

&lt;p&gt;If the owner offers neither route, ask whether the alerts are delivered anywhere outside Discord. Failing that, the permitted answer is to place the trades by hand.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does a call need before a program can trade it?
&lt;/h2&gt;

&lt;p&gt;A call written for people is often ambiguous to a parser. "Looking at XYZ calls into the close" names no contract, no price and no size.&lt;/p&gt;

&lt;p&gt;A program needs every call in one fixed format. For an option, that means the ticker, call or put, strike, expiration and a limit price on a single line. Exits need their own messages naming the same contract, since a reply that says "out" depends on context a parser doesn't have.&lt;/p&gt;

&lt;p&gt;Format also decides whether the bot can see the call at all. In the gateway docs, "the &lt;code&gt;content&lt;/code&gt;, &lt;code&gt;embeds&lt;/code&gt;, &lt;code&gt;attachments&lt;/code&gt;, &lt;code&gt;components&lt;/code&gt;, and &lt;code&gt;poll&lt;/code&gt; fields in message objects all contain message content and therefore require the intent." A call posted as a screenshot arrives as an attachment with no text to parse.&lt;/p&gt;

&lt;p&gt;Decide in advance what an edited message means. A safe rule treats the first version as the call and flags any edit for a person to review.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where should the orders go first?
&lt;/h2&gt;

&lt;p&gt;Send them to a broker's paper account before a live one. On Alpaca's &lt;a href="https://docs.alpaca.markets/us/docs/paper-trading" rel="noopener noreferrer"&gt;paper trading page&lt;/a&gt;, orders follow the live flow but are filled by a simulator from real-time quotes and never reach an exchange. That is enough to prove the parsing and the order fields.&lt;/p&gt;

&lt;p&gt;Paper also exposes duplicates. tastytrade's &lt;a href="https://developer.tastytrade.com/docs/guides/idempotency-and-retries" rel="noopener noreferrer"&gt;idempotency guide&lt;/a&gt; warns that its API doesn't deduplicate retried order submissions. A call that reaches the program twice can become two positions unless the program remembers which message ids it has already handled.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which methods stay inside the rules?
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Does Discord's published text allow it?&lt;/th&gt;
&lt;th&gt;Source&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Bot application added by the server's admins&lt;/td&gt;
&lt;td&gt;Yes, within the Developer Policy's data rules&lt;/td&gt;
&lt;td&gt;OAuth2 docs, Developer Policy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Incoming webhook posting calls from your own tool&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Webhook docs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Following an announcement channel into your own server&lt;/td&gt;
&lt;td&gt;The feature is documented, and trading on followed posts isn't addressed&lt;/td&gt;
&lt;td&gt;Announcement Channel FAQ, message reference&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Script logged in as your user account&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Community Guidelines, rule 14&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tool that asks for your user token or password&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Developer Policy, rule 4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scraper reading the server without Discord's consent&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Terms of Service&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Discord's &lt;a href="https://discord.com/safety/platform-manipulation-policy-explainer" rel="noopener noreferrer"&gt;Platform Manipulation Policy Explainer&lt;/a&gt; also tells users not to modify the Discord client, and it names automating account actions. A patched client that forwards messages belongs with the No rows.&lt;/p&gt;

&lt;p&gt;AI tools wrote this article. Its facts were checked against the official docs linked above.&lt;/p&gt;

&lt;p&gt;Trading involves risk of loss.&lt;/p&gt;

</description>
      <category>discord</category>
      <category>trading</category>
      <category>automation</category>
      <category>bots</category>
    </item>
    <item>
      <title>How to evaluate an options alert service before you pay</title>
      <dc:creator>Nyria</dc:creator>
      <pubDate>Thu, 22 Oct 2026 20:41:20 +0000</pubDate>
      <link>https://dev.to/nyriaautomation/evaluate-an-options-alert-service-before-you-pay-2b98</link>
      <guid>https://dev.to/nyriaautomation/evaluate-an-options-alert-service-before-you-pay-2b98</guid>
      <description>&lt;p&gt;An options alert service can be graded before any money goes to it or into a live trade. Check first whether each alert is complete enough to place as an order. Then place every alert on a broker's paper account for a few weeks and keep your own log of times and fills. That log is evidence you produced yourself. The service's screenshots stay marketing until your record agrees with them.&lt;/p&gt;

&lt;p&gt;The method needs a sample of the service's alerts, from a trial, a free channel or a delayed feed. A service that offers none can't be graded this way.&lt;/p&gt;

&lt;h2&gt;
  
  
  What makes an options alert usable?
&lt;/h2&gt;

&lt;p&gt;An option position is one specific contract. The Options Industry Council's &lt;a href="https://www.optionseducation.org/optionsoverview/what-is-an-option" rel="noopener noreferrer"&gt;introduction&lt;/a&gt; says the contract "establishes a specific price, called the strike price, at which the contract may be exercised, or acted upon." It continues, "Contracts also have an expiration date." Its &lt;a href="https://www.optionseducation.org/optionsoverview/options-basics" rel="noopener noreferrer"&gt;basics page&lt;/a&gt; says "A call option gives its holder the right to buy 100 shares of the underlying security at the strike price, anytime before the option's expiration date." A put is the right to sell.&lt;/p&gt;

&lt;p&gt;A usable alert therefore states five things.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;th&gt;If it is missing&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Underlying&lt;/td&gt;
&lt;td&gt;XYZ&lt;/td&gt;
&lt;td&gt;Nothing can be placed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Call or put&lt;/td&gt;
&lt;td&gt;Call&lt;/td&gt;
&lt;td&gt;The direction is a guess&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Strike&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;Each subscriber picks a different contract&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Expiration&lt;/td&gt;
&lt;td&gt;The exact date&lt;/td&gt;
&lt;td&gt;Same problem, plus a different rate of time decay&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Entry price&lt;/td&gt;
&lt;td&gt;A limit, or a range&lt;/td&gt;
&lt;td&gt;Your fill can't be compared with the claim&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;An alert that reads "calls on XYZ here" leaves three of the five to you. Two subscribers will then buy different contracts, and neither result can be set against the outcome the service reports.&lt;/p&gt;

&lt;p&gt;Price deserves the same care. The introduction says "The premium isn't fixed and changes constantly." An alert with no entry price gives you nothing to hold your fill against.&lt;/p&gt;

&lt;p&gt;Exits count as alerts too. A service that announces entries and stays silent on closes has told you half of each trade.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why grade it on a broker's paper account?
&lt;/h2&gt;

&lt;p&gt;A paper account produces a neutral record. The order time, fill time and fill price come from a broker's system, so neither you nor the service wrote them. On Alpaca's &lt;a href="https://docs.alpaca.markets/us/docs/paper-trading" rel="noopener noreferrer"&gt;paper trading page&lt;/a&gt;, "the system simulates the order filling based on the real-time quotes." Its &lt;a href="https://docs.alpaca.markets/us/docs/options-trading" rel="noopener noreferrer"&gt;options page&lt;/a&gt; adds that "In the Paper environment, options trading capability will be enabled by default".&lt;/p&gt;

&lt;p&gt;Sandboxes are not equally suited to this job. Tradier's &lt;a href="https://docs.tradier.com/brokerage-api/overview/faq" rel="noopener noreferrer"&gt;FAQ&lt;/a&gt; says "We delay our market data by the industry standard 15 minutes for all sandbox data", so quotes seen there trail the live market. tastytrade's &lt;a href="https://developer.tastytrade.com/docs/sandbox" rel="noopener noreferrer"&gt;sandbox page&lt;/a&gt; says "The environment resets every 24 hours", which wipes a multi-week record unless you export it daily. The same page states, "This page makes no claim about how current the simulator's quotes are."&lt;/p&gt;

&lt;p&gt;Whichever broker you use, confirm two things in its docs before you start. The quotes behind the fills should be real-time, and the order history should survive for the length of the test.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does paper trading not tell you?
&lt;/h2&gt;

&lt;p&gt;Paper fills can flatter a thin contract. Alpaca lists what its simulator leaves out, including "Price slippage due to latency" and "Order queue position (for non-marketable limit orders)". Size goes unchecked as well. In Alpaca's words, "you can submit and receive a fill for an order that is much larger than the actual available liquidity."&lt;/p&gt;

&lt;p&gt;Record the quote next to every fill for that reason. Write down the bid and the ask at the moment you saw the alert. If the service's stated entry on a buy sits below the bid you recorded, a real order at that price may never have filled.&lt;/p&gt;

&lt;h2&gt;
  
  
  How should you log each alert?
&lt;/h2&gt;

&lt;p&gt;One row per alert, filled in when the alert arrives.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Column&lt;/th&gt;
&lt;th&gt;What to write&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Alert time&lt;/td&gt;
&lt;td&gt;The timestamp on the message&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Seen time&lt;/td&gt;
&lt;td&gt;When you could have acted on it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Contract&lt;/td&gt;
&lt;td&gt;Underlying, call or put, strike, expiration&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stated entry&lt;/td&gt;
&lt;td&gt;The price in the alert, or "none"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bid and ask&lt;/td&gt;
&lt;td&gt;The quote when you saw the alert&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Your fill&lt;/td&gt;
&lt;td&gt;Paper fill price and time&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Exit alert&lt;/td&gt;
&lt;td&gt;Time and price of the service's close, if one came&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Your exit&lt;/td&gt;
&lt;td&gt;Paper fill on the close&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Save a copy of the message itself. Alerts can be edited or deleted later, and your copy is the version you traded.&lt;/p&gt;

&lt;p&gt;The gap between alert time and seen time is part of what you are buying. It matters most on short-dated contracts, because OIC's &lt;a href="https://www.optionseducation.org/advancedconcepts/theta" rel="noopener noreferrer"&gt;page on Theta&lt;/a&gt; says the rate of time decay tends to rise as expiration approaches.&lt;/p&gt;

&lt;p&gt;Log every alert, including the ones you would have skipped. Leaving some out turns the test into a measure of your own judgment.&lt;/p&gt;

&lt;h2&gt;
  
  
  What should the log show after a few weeks?
&lt;/h2&gt;

&lt;p&gt;Four counts answer most of the question.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How many alerts carried all five fields.&lt;/li&gt;
&lt;li&gt;How many filled on paper at the stated entry or better.&lt;/li&gt;
&lt;li&gt;How many open trades received an exit alert, and how many were left to expire.&lt;/li&gt;
&lt;li&gt;The net result across every logged alert in dollars, with each premium multiplied by the 100 shares a standard contract covers. Losing trades stay in the sum.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then compare your rows with what the service published for the same period. Where both describe the same alert, the contract should match and the prices should fall inside the quotes you recorded. A small gap in entry price has an innocent explanation, because you saw the alert later than it was sent. A losing alert that appears in your log and is absent from the service's record is a reason to ask why.&lt;/p&gt;

&lt;p&gt;A few weeks is a small sample, and it says nothing about the weeks after it. It can show whether the alerts are usable and whether the published record matches your own. It can't show skill.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are the warning signs?
&lt;/h2&gt;

&lt;p&gt;The SEC's Office of Investor Education and Advocacy has an &lt;a href="https://www.investor.gov/introduction-investing/general-resources/news-alerts/alerts-bulletins/investor-alerts/social-media-and-investment-fraud-investor-alert" rel="noopener noreferrer"&gt;investor alert on social media&lt;/a&gt; that asks readers to "be skeptical and never make investment decisions based solely on information from social media platforms or apps." Two more lines from it apply directly to alert services. "Do not be swayed by testimonials or celebrity endorsements when making an investment decision." And, "Do not be pressured to act quickly."&lt;/p&gt;

&lt;p&gt;The signs are concrete. Results arrive as images with no contract or timestamp. Entries are posted at prices the market had already left. Closed trades disappear from the feed without an exit. A discount expires before a test like this one could finish.&lt;/p&gt;

&lt;h2&gt;
  
  
  What changes if you trade the alerts live?
&lt;/h2&gt;

&lt;p&gt;Whether to subscribe is your decision. A live account adds mechanics that paper hides.&lt;/p&gt;

&lt;p&gt;Live accounts need approval. Alpaca, for one, gates option trades by level, and buying a call or a put needs level 2 of the levels listed on its options page.&lt;/p&gt;

&lt;p&gt;Expiration needs a plan. Alpaca's default is to exercise a contract that expires in the money. When the account lacks the buying power for that, Alpaca sells the position in the last hour before expiry. An alert service that never calls an exit leaves that outcome to your broker's defaults.&lt;/p&gt;

&lt;p&gt;Results differ as well. Alpaca's paper page calls simulation a good approximation of real trading and says live performance can differ.&lt;/p&gt;

&lt;h2&gt;
  
  
  What should pass before you pay?
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Check&lt;/th&gt;
&lt;th&gt;Pass&lt;/th&gt;
&lt;th&gt;Fail&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Completeness&lt;/td&gt;
&lt;td&gt;Every alert names ticker, call or put, strike, expiration and entry&lt;/td&gt;
&lt;td&gt;Any of the five is left to the reader&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Entry price&lt;/td&gt;
&lt;td&gt;Your paper fills land at the stated entry or better&lt;/td&gt;
&lt;td&gt;The stated entry sits outside the quotes you recorded&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Exits&lt;/td&gt;
&lt;td&gt;Each closed trade has its own alert&lt;/td&gt;
&lt;td&gt;Trades vanish from the feed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Record&lt;/td&gt;
&lt;td&gt;The service's results include every alert you logged&lt;/td&gt;
&lt;td&gt;Losing alerts are missing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pressure&lt;/td&gt;
&lt;td&gt;You had time to finish the test&lt;/td&gt;
&lt;td&gt;A deadline to pay arrived first&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;AI tools wrote this article. Its facts were checked against the official docs linked above.&lt;/p&gt;

&lt;p&gt;Trading involves risk of loss.&lt;/p&gt;

</description>
      <category>options</category>
      <category>trading</category>
      <category>beginners</category>
      <category>investing</category>
    </item>
    <item>
      <title>How a no-code trading bot turns alerts into orders</title>
      <dc:creator>Nyria</dc:creator>
      <pubDate>Thu, 22 Oct 2026 20:18:15 +0000</pubDate>
      <link>https://dev.to/nyriaautomation/no-code-trading-bot-stock-and-option-alerts-in-orders-out-54ed</link>
      <guid>https://dev.to/nyriaautomation/no-code-trading-bot-stock-and-option-alerts-in-orders-out-54ed</guid>
      <description>&lt;p&gt;A no-code trading bot is a hosted program that sits between an alert and a broker account. It receives the alert as an HTTP request or a chat message, reads the ticker and direction out of it, applies rules you entered in a form, and calls the broker's order API with credentials you gave it. The bot makes no trading decision. The alert says what to trade, and your rules set the size and the order type.&lt;/p&gt;

&lt;p&gt;TradingView's &lt;a href="https://www.tradingview.com/support/solutions/43000481026-how-to-autotrade-using-pine-script-strategies/" rel="noopener noreferrer"&gt;help page on strategies&lt;/a&gt; says strategy trading is limited to backtesting and that automated trading through a brokerage account isn't offered there yet. The category exists because TradingView stops at the alert.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where does the alert come from?
&lt;/h2&gt;

&lt;p&gt;Most bots accept a webhook. TradingView's &lt;a href="https://www.tradingview.com/support/solutions/43000529348-how-to-configure-webhook-alerts/" rel="noopener noreferrer"&gt;webhook guide&lt;/a&gt; says it posts the alert's message to a URL you supply the moment the alert triggers. The bot gives you a URL, you paste it into the alert, and the text in the alert's message box becomes the request body. The same guide requires two-factor authentication on the TradingView account before webhook alerts work.&lt;/p&gt;

&lt;p&gt;Chat is the other common source. On Discord, automation runs through a bot account, which the &lt;a href="https://docs.discord.com/developers/topics/oauth2" rel="noopener noreferrer"&gt;OAuth2 docs&lt;/a&gt; say can't accept an ordinary invite and has to be added through Discord's OAuth2 flow. Someone with permission in the server has to add it. To read what is posted, the bot also needs the &lt;code&gt;MESSAGE_CONTENT&lt;/code&gt; intent covered in the &lt;a href="https://docs.discord.com/developers/events/gateway" rel="noopener noreferrer"&gt;gateway docs&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What has to be in the alert?
&lt;/h2&gt;

&lt;p&gt;A bot can act only on what the message carries. TradingView fills in &lt;a href="https://www.tradingview.com/support/solutions/43000531021-how-to-use-a-variable-value-in-alert/" rel="noopener noreferrer"&gt;placeholders&lt;/a&gt; when the alert fires. &lt;code&gt;{{ticker}}&lt;/code&gt; becomes the symbol's ticker, such as AAPL. On a strategy alert, &lt;code&gt;{{strategy.order.action}}&lt;/code&gt; returns &lt;code&gt;buy&lt;/code&gt; or &lt;code&gt;sell&lt;/code&gt; for the executed order.&lt;/p&gt;

&lt;p&gt;A typical message is one line of JSON, such as &lt;code&gt;{"ticker": "{{ticker}}", "action": "{{strategy.order.action}}", "price": {{close}}}&lt;/code&gt;. The webhook guide says valid JSON goes out with an &lt;code&gt;application/json&lt;/code&gt; content-type header and anything else as &lt;code&gt;text/plain&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Whatever the alert leaves out has to come from a rule stored in the bot. Quantity is the usual example, and the choice of account is another.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does the bot turn text into an order?
&lt;/h2&gt;

&lt;p&gt;It maps the parsed alert onto the parameters the broker's API requires, and those parameters are public. Tradier's &lt;a href="https://docs.tradier.com/brokerage-api/trading/place-equity-order" rel="noopener noreferrer"&gt;order reference&lt;/a&gt; takes &lt;code&gt;class&lt;/code&gt;, &lt;code&gt;symbol&lt;/code&gt;, &lt;code&gt;side&lt;/code&gt;, &lt;code&gt;quantity&lt;/code&gt;, &lt;code&gt;type&lt;/code&gt; and &lt;code&gt;duration&lt;/code&gt;, where &lt;code&gt;type&lt;/code&gt; is "One of: market, limit, stop, stop_limit". Alpaca's &lt;a href="https://docs.alpaca.markets/us/reference/postorder" rel="noopener noreferrer"&gt;create-order reference&lt;/a&gt; has a similar set under different names. A no-code setup form is a front end for this list.&lt;/p&gt;

&lt;p&gt;Setups differ most in the mapping, which is also where they break. An alert that says &lt;code&gt;buy&lt;/code&gt; while the account is already long can mean "add" or "do nothing". An alert that says &lt;code&gt;sell&lt;/code&gt; can mean "close" or "open a short", and Tradier's reference lists those as separate &lt;code&gt;side&lt;/code&gt; values, "One of: buy, sell, sell_short, buy_to_cover". Find out how a bot resolves each case before it holds a position for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is different for an option alert?
&lt;/h2&gt;

&lt;p&gt;A share order needs a ticker. An option order needs one contract out of the many listed on that ticker. Each contract fixes a strike price and has an expiration date, as the Options Industry Council's &lt;a href="https://www.optionseducation.org/optionsoverview/what-is-an-option" rel="noopener noreferrer"&gt;introduction&lt;/a&gt; sets out. Brokers fold all of that into one symbol. Alpaca's &lt;a href="https://docs.alpaca.markets/us/docs/options-trading" rel="noopener noreferrer"&gt;options page&lt;/a&gt; shows &lt;code&gt;AAPL240119C00100000&lt;/code&gt; for the "AAPL Jan 19 2024 100 Call".&lt;/p&gt;

&lt;p&gt;An option bot therefore works in one of two ways. Either the alert names the exact contract, or the alert gives a direction and the bot picks the contract from rules for expiry and strike. TradingView's placeholder list has no strike, expiration or delta variable, so chart alerts fall into the second group unless the message itself supplies the contract.&lt;/p&gt;

&lt;p&gt;Option orders also carry tighter limits. Alpaca's page accepts only &lt;code&gt;day&lt;/code&gt; or &lt;code&gt;gtc&lt;/code&gt; as the &lt;code&gt;time_in_force&lt;/code&gt; of an option order, and its validations include "&lt;code&gt;extended_hours&lt;/code&gt; must be &lt;code&gt;false&lt;/code&gt; or not populated". The account needs approval too. On Alpaca, buying a call or a put sits at level 2 of the trading levels in that document.&lt;/p&gt;

&lt;p&gt;Expiry adds a duty that a share position doesn't have. Alpaca's options page says its default is to exercise in-the-money contracts automatically at expiry. A bot that opens option positions needs an exit rule that fires before then.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens when an alert fires twice or a request fails?
&lt;/h2&gt;

&lt;p&gt;A duplicate alert or a retried request can place a second order, and brokers don't always prevent it. tastytrade's &lt;a href="https://developer.tastytrade.com/docs/guides/idempotency-and-retries" rel="noopener noreferrer"&gt;idempotency guide&lt;/a&gt; says "Order placement is not idempotent." It adds that repeating your own order id on a second request doesn't prevent that order. The duplicate check has to live in the bot.&lt;/p&gt;

&lt;p&gt;Timing matters on the inbound side as well. TradingView drops a webhook request if the receiving server hasn't finished with it inside three seconds, its guide says. A bot has to accept the alert fast and place the order afterwards, and you need a log that shows both steps.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens outside market hours?
&lt;/h2&gt;

&lt;p&gt;Alerts fire whenever the chart condition is met, including after the close. On Alpaca, a &lt;code&gt;day&lt;/code&gt; order that arrives after the close is held and sent on the next trading day, per its &lt;a href="https://docs.alpaca.markets/us/docs/orders-at-alpaca" rel="noopener noreferrer"&gt;orders page&lt;/a&gt;. A bot that forwards a late alert this way trades at the next session, so the fill can differ from the price in the alert. A market order fills at whatever the market offers then, while a limit order fills at its limit or better, or not at all.&lt;/p&gt;

&lt;p&gt;Options are stricter because the extended-hours flag must be off. Cboe's &lt;a href="https://www.cboe.com/about/hours/us-options/" rel="noopener noreferrer"&gt;hours page&lt;/a&gt; ends regular trading in equity options on its C1 exchange at 4:00 p.m. Eastern Time, and its footnote keeps some ETF options open to 4:15 p.m.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you test one without real money?
&lt;/h2&gt;

&lt;p&gt;Point the bot at a broker's paper account. Alpaca's &lt;a href="https://docs.alpaca.markets/us/docs/paper-trading" rel="noopener noreferrer"&gt;paper trading page&lt;/a&gt; describes paper trading as a real-time simulation that runs like live trading except that no order goes to an exchange. Options are switched on for paper accounts by default, per its options page. Tradier's &lt;a href="https://docs.tradier.com/brokerage-api/overview/faq" rel="noopener noreferrer"&gt;FAQ&lt;/a&gt; puts sandbox market data on a 15-minute delay. tastytrade's &lt;a href="https://developer.tastytrade.com/docs/sandbox" rel="noopener noreferrer"&gt;sandbox page&lt;/a&gt; warns that "The environment resets every 24 hours".&lt;/p&gt;

&lt;p&gt;Read the results as a test of wiring. Alpaca's simulator doesn't check order size against the quantity quoted in the market, so a thin option contract can fill there and sit unfilled in a live account.&lt;/p&gt;

&lt;h2&gt;
  
  
  What access does the bot have to your account?
&lt;/h2&gt;

&lt;p&gt;The bot places orders with your credentials, so it can do whatever those credentials allow. Keep them out of the alert itself. TradingView's guide warns against putting passwords or other login details in a webhook's body.&lt;/p&gt;

&lt;p&gt;Treat the webhook URL as a secret too, because a bot that accepts any post to that address will act on it. Start with paper keys. Alpaca issues a paper account its own API key, separate from the live one. Before going live, find the place at your broker where that access is revoked.&lt;/p&gt;

&lt;h2&gt;
  
  
  What should you check before going live?
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Question to answer&lt;/th&gt;
&lt;th&gt;Why it matters&lt;/th&gt;
&lt;th&gt;Source&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Does the bot answer inside three seconds?&lt;/td&gt;
&lt;td&gt;TradingView cancels slower webhooks&lt;/td&gt;
&lt;td&gt;TradingView webhook guide&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;What does a repeated alert do?&lt;/td&gt;
&lt;td&gt;Not every broker rejects a duplicate order&lt;/td&gt;
&lt;td&gt;tastytrade idempotency guide&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;How is &lt;code&gt;sell&lt;/code&gt; resolved?&lt;/td&gt;
&lt;td&gt;Closing and shorting are different &lt;code&gt;side&lt;/code&gt; values&lt;/td&gt;
&lt;td&gt;Tradier order reference&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Which contract does an option alert buy?&lt;/td&gt;
&lt;td&gt;Alert placeholders carry no strike or expiry&lt;/td&gt;
&lt;td&gt;TradingView placeholder list&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;What happens to an alert after 4:00 p.m. ET?&lt;/td&gt;
&lt;td&gt;A &lt;code&gt;day&lt;/code&gt; order queues for the next session&lt;/td&gt;
&lt;td&gt;Alpaca orders page&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;What closes an option before expiry?&lt;/td&gt;
&lt;td&gt;In-the-money contracts are exercised by default&lt;/td&gt;
&lt;td&gt;Alpaca options page&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A bot that answers all six works end to end on a paper account. Whether the alerts are worth following is a separate question that no bot settles.&lt;/p&gt;

&lt;p&gt;AI tools wrote this article. Its facts were checked against the official docs linked above.&lt;/p&gt;

&lt;p&gt;Trading involves risk of loss.&lt;/p&gt;

</description>
      <category>nocode</category>
      <category>trading</category>
      <category>automation</category>
      <category>tradingview</category>
    </item>
    <item>
      <title>How to send orders from a Python scanner to your broker</title>
      <dc:creator>Nyria</dc:creator>
      <pubDate>Thu, 22 Oct 2026 19:47:43 +0000</pubDate>
      <link>https://dev.to/nyriaautomation/send-alerts-from-your-python-script-to-your-broker-1f8n</link>
      <guid>https://dev.to/nyriaautomation/send-alerts-from-your-python-script-to-your-broker-1f8n</guid>
      <description>&lt;p&gt;A Python scanner can reach a broker in two ways. The script can call the broker's order API itself, or it can post each alert to a small web service you run, which then places the order. Both routes need an id that ties each alert to one order and a status check before any retry. Run the whole path on a paper account first.&lt;/p&gt;

&lt;p&gt;The sender below is 55 lines of Python on the &lt;a href="https://requests.readthedocs.io/en/latest/user/quickstart/" rel="noopener noreferrer"&gt;Requests&lt;/a&gt; library. It targets Alpaca's paper API as an illustration, because its order and lookup routes are documented in public.&lt;/p&gt;

&lt;h2&gt;
  
  
  Should the scanner call the broker or use a relay?
&lt;/h2&gt;

&lt;p&gt;Calling the API directly means the scanner holds the broker keys and sends the order from the process that found the setup. You run one program and read one log. That suits a single script on a machine you control.&lt;/p&gt;

&lt;p&gt;A relay is a small HTTP service you host. The scanner posts a short JSON alert to it, and the relay keeps the keys and talks to the broker. The split pays off when several scanners feed one account, or when the scanner runs somewhere you would not store keys. It also lets a charting tool share the same entry point, since a TradingView &lt;a href="https://www.tradingview.com/support/solutions/43000529348-how-to-configure-webhook-alerts/" rel="noopener noreferrer"&gt;webhook alert&lt;/a&gt; arrives as an HTTP POST with the alert's message as its body.&lt;/p&gt;

&lt;p&gt;A relay costs you a public endpoint to secure and one more hop that can fail. It also has a deadline when TradingView is the sender. The same guide says TradingView cancels a webhook request when the receiving server needs more than three seconds. A relay should acknowledge the alert before it starts talking to the broker.&lt;/p&gt;

&lt;p&gt;The order logic is identical in both designs.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does the broker need in an order request?
&lt;/h2&gt;

&lt;p&gt;Alpaca's &lt;a href="https://docs.alpaca.markets/us/reference/postorder" rel="noopener noreferrer"&gt;create-order reference&lt;/a&gt; takes a JSON body on &lt;code&gt;POST /v2/orders&lt;/code&gt;. Its example for buying shares carries &lt;code&gt;symbol&lt;/code&gt;, &lt;code&gt;qty&lt;/code&gt;, &lt;code&gt;side&lt;/code&gt;, &lt;code&gt;type&lt;/code&gt;, &lt;code&gt;time_in_force&lt;/code&gt; and &lt;code&gt;limit_price&lt;/code&gt;. The &lt;a href="https://docs.alpaca.markets/us/docs/authentication" rel="noopener noreferrer"&gt;authentication page&lt;/a&gt; says to "Use the &lt;code&gt;APCA-API-KEY-ID&lt;/code&gt; and &lt;code&gt;APCA-API-SECRET-KEY&lt;/code&gt; headers to send your key ID and secret key."&lt;/p&gt;

&lt;p&gt;A scanner should add &lt;code&gt;client_order_id&lt;/code&gt;, which the reference describes as "A unique identifier for the order. Automatically generated if not sent. (&amp;lt;= 128 characters)". The &lt;a href="https://docs.alpaca.markets/us/reference/getorderbyclientorderid" rel="noopener noreferrer"&gt;lookup reference&lt;/a&gt; adds that "Once an order is placed, it can be queried using the client-side order ID to check the status."&lt;/p&gt;

&lt;p&gt;An alert already has an identity made of its symbol, its direction and the bar that triggered it. Hash those three into the &lt;code&gt;client_order_id&lt;/code&gt; and one alert always yields one id, however many times the script sees it.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you stop one alert becoming two orders?
&lt;/h2&gt;

&lt;p&gt;Duplicates have two causes. A scanner can find the same setup on consecutive passes. A request can also fail in a way that hides whether the broker received it, since a timeout after the order was accepted looks identical to a request that never arrived.&lt;/p&gt;

&lt;p&gt;Broker docs are direct about the second cause. tastytrade's &lt;a href="https://developer.tastytrade.com/docs/guides/idempotency-and-retries" rel="noopener noreferrer"&gt;idempotency guide&lt;/a&gt; says "tastytrade does not provide a generic &lt;code&gt;Idempotency-Key&lt;/code&gt; header, so safe retries are a client-side discipline." For an uncertain response its instruction is "do not blindly resubmit."&lt;/p&gt;

&lt;p&gt;Alpaca's reference doesn't state what a repeated &lt;code&gt;client_order_id&lt;/code&gt; returns, so the sender doesn't rely on the broker to reject it. It looks the id up before every send, and again after any uncertain response.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;hashlib&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;

&lt;span class="no"&gt;BASE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"BROKER_BASE_URL"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"https://paper-api.alpaca.markets"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;HEADERS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"APCA-API-KEY-ID"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"BROKER_KEY_ID"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"APCA-API-SECRET-KEY"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"BROKER_SECRET_KEY"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="no"&gt;TIMEOUT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.05&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;order_key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;side&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bar_time&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{symbol}|{side}|{bar_time}"&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"scan-"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;hashlib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hexdigest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;find_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{BASE}/v2/orders:by_client_order_id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"client_order_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;HEADERS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;TIMEOUT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;raise_for_status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;send_once&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;side&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qty&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;limit_price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bar_time&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;order_key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;side&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bar_time&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;existing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;find_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;existing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;existing&lt;/span&gt;
    &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"symbol"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"qty"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;qty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"side"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;side&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"limit"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"limit_price"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;limit_price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"time_in_force"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"day"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"client_order_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{BASE}/v2/orders"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;HEADERS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;TIMEOUT&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;RequestException&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;find_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;find_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;raise_for_status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;order_key&lt;/code&gt; builds the id from the alert. &lt;code&gt;find_order&lt;/code&gt; asks the broker whether that id exists. &lt;code&gt;send_once&lt;/code&gt; returns the existing order when there is one and posts when there isn't. After a network error or a &lt;code&gt;5xx&lt;/code&gt; it looks again instead of posting again. A result of &lt;code&gt;None&lt;/code&gt; means the broker has no record yet, and the next scanner pass repeats the same steps under the same id.&lt;/p&gt;

&lt;p&gt;Confirm one detail on your own paper account. The lookup reference documents only a &lt;code&gt;200&lt;/code&gt; response, and the code assumes an unknown id answers &lt;code&gt;404&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Every call carries a timeout, because the Requests docs warn that "If no timeout is specified explicitly, requests do not time out."&lt;/p&gt;

&lt;h2&gt;
  
  
  Which failed requests are safe to retry?
&lt;/h2&gt;

&lt;p&gt;tastytrade's guide sorts responses into four groups, and the grouping works as a default for any order API.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Response&lt;/th&gt;
&lt;th&gt;Retry?&lt;/th&gt;
&lt;th&gt;What the sender does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;2xx&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Returns the order&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;4xx&lt;/code&gt; except &lt;code&gt;429&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;No, fix the request&lt;/td&gt;
&lt;td&gt;Raises &lt;code&gt;HTTPError&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;429&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Yes, after backoff&lt;/td&gt;
&lt;td&gt;Raises, and the caller waits before the next pass&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;5xx&lt;/code&gt; or a network error&lt;/td&gt;
&lt;td&gt;Yes, after a status check&lt;/td&gt;
&lt;td&gt;Looks the id up and never posts twice in one call&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Alpaca lists two rejections on order creation, &lt;code&gt;403&lt;/code&gt; "Buying power or shares is not sufficient." and &lt;code&gt;422&lt;/code&gt; "Input parameters are not recognized." A retry changes neither, so &lt;code&gt;send_once&lt;/code&gt; raises on both and leaves the decision to the caller.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does a paper account prove?
&lt;/h2&gt;

&lt;p&gt;A paper account proves the plumbing. Alpaca's &lt;a href="https://docs.alpaca.markets/us/docs/paper-trading" rel="noopener noreferrer"&gt;paper trading page&lt;/a&gt; says "Paper trading works the same way as live trading end to end - except the order is not routed a live exchange." It adds that "Your paper trading account will have a different API key from your live account" and that "The API spec is the same between the paper trading and live accounts." Going live later changes the keys and the base URL, and the code stays as it is.&lt;/p&gt;

&lt;p&gt;Fill quality is a separate question. The same page says paper trading "is not a substitute for real trading and performance may differ", and its list of what the simulator leaves out includes "Price slippage due to latency" and "Market impact of your orders". Size isn't modeled either, since "Your order quantity is not checked against the NBBO quantities."&lt;/p&gt;

&lt;p&gt;Sandboxes vary by broker. Tradier's &lt;a href="https://docs.tradier.com/brokerage-api/overview/endpoints" rel="noopener noreferrer"&gt;sandbox&lt;/a&gt; is "a paper trading account to test your integration with our API, including working with delayed market data and paper trades." Tradier can also validate an order without placing it. With &lt;code&gt;preview=true&lt;/code&gt; on the &lt;a href="https://docs.tradier.com/brokerage-api/trading/preview-order" rel="noopener noreferrer"&gt;preview route&lt;/a&gt;, "all order validation rules are run, including buying power checks." tastytrade's &lt;a href="https://developer.tastytrade.com/docs/sandbox" rel="noopener noreferrer"&gt;sandbox page&lt;/a&gt; says orders there "never reach a real market" and that "The environment resets every 24 hours".&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens to an order sent outside market hours?
&lt;/h2&gt;

&lt;p&gt;Scanners that work on daily bars usually finish after the close. Alpaca's &lt;a href="https://docs.alpaca.markets/us/docs/orders-at-alpaca" rel="noopener noreferrer"&gt;orders page&lt;/a&gt; says of a &lt;code&gt;day&lt;/code&gt; order, "By default, the order is only valid during Regular Trading Hours (9:30am - 4:00pm ET)." It continues, "If submitted after the close, it is queued and submitted the following trading day." The order then waits overnight with a limit price set before the close.&lt;/p&gt;

&lt;p&gt;An order sent outside the session often shows the status &lt;code&gt;accepted&lt;/code&gt;, which the page defines as "The order has been received by Alpaca, but hasn't yet been routed to the execution venue." Log that status as pending and keep polling until the broker reports a fill or a cancel.&lt;/p&gt;

&lt;p&gt;For trading outside the regular session, that page says "Only limit orders with &lt;code&gt;time_in_force&lt;/code&gt; set to &lt;code&gt;day&lt;/code&gt; or &lt;code&gt;gtc&lt;/code&gt; orders are accepted as extended hours eligible."&lt;/p&gt;

&lt;p&gt;A script can read the session state before it sends. Alpaca's &lt;a href="https://docs.alpaca.markets/us/reference/legacyclock" rel="noopener noreferrer"&gt;clock route&lt;/a&gt; "serves the current market timestamp, whether or not the market is currently open, as well as the times of the next market open and close." Tradier's &lt;a href="https://docs.tradier.com/brokerage-api/market-data/get-market-clock" rel="noopener noreferrer"&gt;market clock&lt;/a&gt; returns a &lt;code&gt;state&lt;/code&gt; of &lt;code&gt;premarket&lt;/code&gt;, &lt;code&gt;open&lt;/code&gt;, &lt;code&gt;postmarket&lt;/code&gt; or &lt;code&gt;closed&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What changes with a different broker?
&lt;/h2&gt;

&lt;p&gt;The pattern carries over to other brokers, but field names and dedupe behavior differ.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Broker&lt;/th&gt;
&lt;th&gt;Field for your own id&lt;/th&gt;
&lt;th&gt;Does the broker dedupe on it?&lt;/th&gt;
&lt;th&gt;Check without placing&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Alpaca&lt;/td&gt;
&lt;td&gt;&lt;code&gt;client_order_id&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Not stated in the reference&lt;/td&gt;
&lt;td&gt;Paper account&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;tastytrade&lt;/td&gt;
&lt;td&gt;&lt;code&gt;external-identifier&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No, "a repeated id does not stop a second order from being placed"&lt;/td&gt;
&lt;td&gt;&lt;a href="https://developer.tastytrade.com/reference/orders/postAccountsAccountNumberOrdersDryRun" rel="noopener noreferrer"&gt;Dry-run route&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tradier&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;tag&lt;/code&gt;, a "User-defined tag for the order"&lt;/td&gt;
&lt;td&gt;Not stated in the reference&lt;/td&gt;
&lt;td&gt;&lt;code&gt;preview=true&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Where the docs are silent, assume the broker will accept the duplicate and keep the lookup in your own code.&lt;/p&gt;

&lt;h2&gt;
  
  
  What should you check before the first live order?
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Check&lt;/th&gt;
&lt;th&gt;How to run it on paper&lt;/th&gt;
&lt;th&gt;A pass looks like&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Same alert twice&lt;/td&gt;
&lt;td&gt;Call &lt;code&gt;send_once&lt;/code&gt; twice with one bar time&lt;/td&gt;
&lt;td&gt;One order at the broker&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lost reply&lt;/td&gt;
&lt;td&gt;Cut the network after the request leaves&lt;/td&gt;
&lt;td&gt;The lookup returns the order and no second post goes out&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Rejection&lt;/td&gt;
&lt;td&gt;Ask for more shares than the account can buy&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;403&lt;/code&gt; raised, nothing retried&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;After the close&lt;/td&gt;
&lt;td&gt;Send a &lt;code&gt;day&lt;/code&gt; order after 4:00pm ET&lt;/td&gt;
&lt;td&gt;The order is held for the next session and no fill is logged early&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Unknown id&lt;/td&gt;
&lt;td&gt;Look up an id you never sent&lt;/td&gt;
&lt;td&gt;The status your broker uses for "not found"&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Keep the paper keys in place until every row passes.&lt;/p&gt;

&lt;p&gt;AI tools wrote this article. Its facts were checked against the official docs linked above, and its code was run against a stubbed exchange.&lt;/p&gt;

&lt;p&gt;Trading involves risk of loss.&lt;/p&gt;

</description>
      <category>python</category>
      <category>trading</category>
      <category>api</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
