{"id":47,"date":"2025-08-31T11:05:06","date_gmt":"2025-08-31T03:05:06","guid":{"rendered":"http:\/\/www.028168.xyz\/?p=47"},"modified":"2025-08-31T11:05:06","modified_gmt":"2025-08-31T03:05:06","slug":"%e9%87%8f%e5%8c%96%e4%ba%a4%e6%98%93%e5%9f%ba%e7%a1%80%ef%bc%88%e4%ba%8c%ef%bc%89","status":"publish","type":"post","link":"http:\/\/www.028168.xyz\/index.php\/2025\/08\/31\/%e9%87%8f%e5%8c%96%e4%ba%a4%e6%98%93%e5%9f%ba%e7%a1%80%ef%bc%88%e4%ba%8c%ef%bc%89\/","title":{"rendered":"\u91cf\u5316\u4ea4\u6613\u57fa\u7840\uff08\u4e8c\uff09"},"content":{"rendered":"\n<p>\u8f6c\u81ea\u5fae\u4fe1\u516c\u4f17\u53f7 \u300a\u6570\u636e\u79d1\u5b66\u5b9e\u8df5\u300b<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# \u4ea4\u6613\u793a\u4f8b\nnumber_of_shares = 100\ntrade_id = 15432\ndays_to_expiry = 30\nprint(f&quot;Trade ID {trade_id}: Bought {number_of_shares} shares.&quot;)\nprint(f&quot;Type of 'number_of_shares': {type(number_of_shares)}&quot;)\n\n# \u91d1\u878d\u6307\u6807\u793a\u4f8b\nstock_price = 149.95\ninterest_rate = 0.0525  # 5.25% \u7684\u5229\u7387\ndaily_return = -0.015   # -1.5% \u7684\u65e5\u6536\u76ca\u7387\nprint(f&quot;Current stock price: ${stock_price}&quot;)\nprint(f&quot;Type of 'stock_price': {type(stock_price)}&quot;)\n\n#\u6d6e\u70b9\u7cbe\u5ea6\u95ee\u9898\nval = 0.35 + 0.1\nprint(val)  # \u8f93\u51fa: 0.44999999999999996\n\n\n#\u4f7f\u7528Decimal\u6a21\u5757\nfrom decimal import Decimal\n# \u4f7f\u7528 Decimal \u8fdb\u884c\u7cbe\u786e\u7684\u91d1\u878d\u8ba1\u7b97\nfee_1 = Decimal('0.35')\nfee_2 = Decimal('0.10')\ntotal_fee = fee_1 + fee_2\nprint(f&quot;Total fee with Decimal: {total_fee}&quot;)  # \u8f93\u51fa: 0.45\n\n# \u8d44\u4ea7\u6807\u8bc6\u7b26\u793a\u4f8b\nticker = 'aapl'\ncurrency_pair = 'EURUSD'\nsector = 'Information Technology'\n# \u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u6807\u51c6\u5316\u6570\u636e\nstandardized_ticker = ticker.upper()  # \u5e38\u89c1\u505a\u6cd5\nprint(f&quot;Standardized Ticker: {standardized_ticker}&quot;)\n# F-\u5b57\u7b26\u4e32\u53ef\u7528\u4e8e\u521b\u5efa\u683c\u5f0f\u5316\u62a5\u544a\ntrade_confirmation = f&quot;Executed trade for 100 shares of {standardized_ticker}&quot;\nprint(trade_confirmation)\n\n# \u4ea4\u6613\u7cfb\u7edf\u4e2d\u7684\u6807\u5fd7\u793a\u4f8b\nis_market_open = True\nhas_sufficient_capital = True\nis_trade_profitable = (151.25 - stock_price) &gt; 0  # \u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4f1a\u8ba1\u7b97\u4e3a\u5e03\u5c14\u503c\n# \u5e03\u5c14\u503c\u63a7\u5236\u51b3\u7b56\nif is_market_open and has_sufficient_capital:\n    print(&quot;System is ready to place a trade.&quot;)\nelse:\n    print(&quot;System is offline or capital is insufficient.&quot;)\nprint(f&quot;Is the trade profitable? {is_trade_profitable}&quot;)\n\n\n#**\u5217\u8868 (list)**\uff1a\u53ef\u53d8\u3001\u6709\u5e8f\u7684\u5143\u7d20\u5e8f\u5217\uff0c\u7528\u4e8e\u5b58\u50a8\u5386\u53f2\u4ef7\u683c\u65f6\u95f4\u5e8f\u5217\u6216\u4ea4\u6613\u8bb0\u5f55\u96c6\u5408\uff1a\n# \u4e00\u5468\u7684\u80a1\u7968\u6536\u76d8\u4ef7\u793a\u4f8b\naapl_prices_list = &#x5B;150.10, 151.20, 150.85, 152.50, 152.30]\naapl_prices_list.append(153.10)  # \u5217\u8868\u662f\u53ef\u53d8\u7684\uff0c\u53ef\u4ee5\u6dfb\u52a0\u5143\u7d20\nprint(f&quot;Price on the first day: {aapl_prices_list&#x5B;0]}&quot;)\nprint(f&quot;Updated price list: {aapl_prices_list}&quot;)\n\n#**\u5143\u7ec4 (tuple)**\uff1a\u4e0d\u53ef\u53d8\u3001\u6709\u5e8f\u7684\u5e8f\u5217\u3002\u5728\u91d1\u878d\u4e2d\uff0c\u8bb8\u591a\u8bb0\u5f55\u4e00\u65e6\u521b\u5efa\u5c31\u5e94\u6c38\u4e45\u4fdd\u5b58\uff1a\n# \u5df2\u5b8c\u6210\u7684\u4ea4\u6613\u8bb0\u5f55\u793a\u4f8b\ntrade_record = ('AAPL', 152.50, 100, '2023-10-27T10:00:00Z')\n# \u5c1d\u8bd5\u4fee\u6539\u5b83\u4f1a\u5f15\u53d1\u9519\u8bef\uff0c\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\n# trade_record&#x5B;1] = 152.60  # \u8fd9\u4f1a\u5f15\u8d77 TypeError\nprint(f&quot;Immutable Trade Record: {trade_record}&quot;)\n\n# \u7b80\u5355\u80a1\u7968\u6295\u8d44\u7ec4\u5408\u793a\u4f8b\nportfolio = {'AAPL': 100, 'GOOG': 50, 'MSFT': 75}\n# \u8bbf\u95ee\u6301\u4ed3\nprint(f&quot;Shares of GOOG held: {portfolio&#x5B;'GOOG']}&quot;)\n# \u6dfb\u52a0\u65b0\u6301\u4ed3\nportfolio&#x5B;'AMZN'] = 25\nprint(f&quot;Updated Portfolio: {portfolio}&quot;)\n# \u66f4\u65b0\u73b0\u6709\u6301\u4ed3\nportfolio&#x5B;'AAPL'] += 50  # \u53c8\u4e70\u4e86 50 \u80a1\nprint(f&quot;Final shares of AAPL: {portfolio&#x5B;'AAPL']}&quot;)\n\n# \u7ba1\u7406\u89c2\u5bdf\u6e05\u5355\u793a\u4f8b\nwatchlist_1 = {'AAPL', 'MSFT', 'GOOG', 'AMZN'}\nwatchlist_2 = {'AAPL', 'NVDA', 'TSLA', 'MSFT'}\n# \u67e5\u627e\u4e24\u4e2a\u89c2\u5bdf\u6e05\u5355\u4e2d\u7684\u5171\u540c\u80a1\u7968\uff08\u4ea4\u96c6\uff09\ncommon_stocks = watchlist_1.intersection(watchlist_2)\nprint(f&quot;Common Stocks: {common_stocks}&quot;)\n# \u67e5\u627e\u4e24\u4e2a\u89c2\u5bdf\u6e05\u5355\u4e2d\u7684\u6240\u6709\u552f\u4e00\u80a1\u7968\uff08\u5e76\u96c6\uff09\nall_stocks = watchlist_1.union(watchlist_2)\nprint(f&quot;All Unique Stocks: {all_stocks}&quot;)\n\n# \u7b80\u5316\u7684\u4ef7\u683c\u6570\u636e\u793a\u4f8b\nprices = &#x5B;5, 6, 7, 7, 6, 9, 8, 9, 9, 7, 6, 6]\n# \u672c\u4f8b\u4e2d\u4f7f\u7528\u7b80\u5316\u7684&quot;\u5f53\u524d&quot;\u79fb\u52a8\u5e73\u5747\u7ebf\n# \u5b9e\u9645\u573a\u666f\u4e2d\uff0c\u8fd9\u4e9b\u4f1a\u57fa\u4e8e\u5b8c\u6574\u5386\u53f2\u8ba1\u7b97\nshort_term_sma = sum(prices&#x5B;-3:]) \/ 3  # 3 \u5929 SMA\nlong_term_sma = sum(prices&#x5B;-7:]) \/ 7   # 7 \u5929 SMA\nprint(f&quot;Short-term SMA: {short_term_sma:.2f}&quot;)\nprint(f&quot;Long-term SMA: {long_term_sma:.2f}&quot;)\n# \u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u5b9e\u73b0\u4ea4\u6613\u903b\u8f91\nif short_term_sma &gt; long_term_sma:\n    print(&quot;Signal: BUY&quot;)\n    print(&quot;Reason: Short-term momentum is positive.&quot;)\nelif short_term_sma &lt; long_term_sma:\n    print(&quot;Signal: SELL&quot;)\n    print(&quot;Reason: Short-term momentum is negative.&quot;)\nelse:\n    print(&quot;Signal: HOLD&quot;)\n    print(&quot;Reason: No clear trend divergence.&quot;)\n    \ntickers = &#x5B;'AAPL', 'MSFT', 'GOOG', 'AMZN']\nprint(&quot;Processing trades for the following tickers:&quot;)\nfor ticker in tickers:\n    # \u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8fd9\u91cc\u4f1a\u8c03\u7528\u6570\u636e\u83b7\u53d6\u51fd\u6570\n    print(f&quot; -&gt; Fetching latest price for {ticker}...&quot;)\n    # \u6a21\u62df\u4e00\u4e9b\u5904\u7406\u8fc7\u7a0b\n    print(f&quot; -&gt; Analyzing {ticker} for trading opportunities.&quot;)\n    \nportfolio = {'AAPL': 150, 'NVDA': 50, 'TSLA': 75}\n# \u5047\u8bbe\u6211\u4eec\u5df2\u5c06\u6700\u65b0\u4ef7\u683c\u83b7\u53d6\u5230\u53e6\u4e00\u4e2a\u5b57\u5178\u4e2d\ncurrent_prices = {'AAPL': 171.50, 'NVDA': 460.18, 'TSLA': 256.49}\ntotal_portfolio_value = 0.0\nprint(&quot;\\nCalculating portfolio value:&quot;)\nfor ticker, shares in portfolio.items():\n    price = current_prices&#x5B;ticker]\n    position_value = shares * price\n    total_portfolio_value += position_value\n    print(f&quot; Position: {ticker}, Shares: {shares}, Value: ${position_value:,.2f}&quot;)\nprint(f&quot;\\nTotal Portfolio Market Value: ${total_portfolio_value:,.2f}&quot;)\n\nimport random\n# \u7b80\u5316\u7684\u98ce\u9669\u503c\u793a\u4f8b\n# \u5b9e\u9645\u4e2d\uff0c\u8fd9\u4f1a\u662f\u590d\u6742\u8ba1\u7b97\uff0c\u5982\u6295\u8d44\u7ec4\u5408\u6807\u51c6\u5dee\nasset_risks = {'Bonds': 0.05, 'BlueChipStock': 0.15, 'TechStock': 0.25, 'Crypto': 0.40}\nasset_list = list(asset_risks.keys())\nmax_portfolio_risk = 0.50\ncurrent_portfolio_risk = 0.0\nportfolio_composition = &#x5B;]\nprint(f&quot;\\nBuilding portfolio with max risk target of {max_portfolio_risk:.2f}:&quot;)\n# \u53ea\u8981\u98ce\u9669\u4f4e\u4e8e\u6700\u5927\u503c\u5c31\u7ee7\u7eed\u6dfb\u52a0\u8d44\u4ea7\nwhile current_portfolio_risk &lt; max_portfolio_risk:\n    # \u968f\u673a\u9009\u62e9\u8981\u6dfb\u52a0\u7684\u8d44\u4ea7\n    asset_to_add = random.choice(asset_list)\n    asset_risk_value = asset_risks&#x5B;asset_to_add]\n    # \u68c0\u67e5\u6dfb\u52a0\u6b64\u8d44\u4ea7\u662f\u5426\u4f1a\u8d85\u8fc7\u6700\u5927\u98ce\u9669\n    if current_portfolio_risk + asset_risk_value &gt; max_portfolio_risk:\n        print(f&quot;Cannot add {asset_to_add} (risk {asset_risk_value:.2f}), would exceed max risk.&quot;)\n        break  # break \u8bed\u53e5\u7acb\u5373\u9000\u51fa\u5faa\u73af\n    # \u6dfb\u52a0\u8d44\u4ea7\u5e76\u66f4\u65b0\u98ce\u9669\n    portfolio_composition.append(asset_to_add)\n    current_portfolio_risk += asset_risk_value\n    print(f&quot; Added {asset_to_add}. Current portfolio risk: {current_portfolio_risk:.2f}&quot;)\nprint(f&quot;\\nFinal Portfolio Composition: {portfolio_composition}&quot;)\nprint(f&quot;Final Portfolio Risk: {current_portfolio_risk:.2f}&quot;)\n\n\ndef function_name(parameter1, parameter2):\n    &quot;&quot;&quot;\n    \u8fd9\u662f\u6587\u6863\u5b57\u7b26\u4e32\u3002\u5b83\u89e3\u91ca\u51fd\u6570\u7684\u529f\u80fd\u3002\n    \u4e13\u4e1a\u4ee3\u7801\u5fc5\u987b\u6709\u826f\u597d\u7684\u6587\u6863\u5b57\u7b26\u4e32\u3002\n    &quot;&quot;&quot;\n    # \u6267\u884c\u64cd\u4f5c\u7684\u4ee3\u7801\u5757\n    result = parameter1 + parameter2\n    return result\n    \nimport math\ndef calculate_annualized_volatility(prices_list):\n    &quot;&quot;&quot;\n    \u4ece\u65e5\u4ef7\u683c\u5217\u8868\u8ba1\u7b97\u80a1\u7968\u7684\u5e74\u5316\u6ce2\u52a8\u7387\u3002\n    \n    \u53c2\u6570:\n    prices_list (list): \u8868\u793a\u65e5\u4ef7\u683c\u7684\u6d6e\u70b9\u6570\u6216\u6574\u6570\u5217\u8868\n    \n    \u8fd4\u56de:\n    float: \u5e74\u5316\u6ce2\u52a8\u7387\n    &quot;&quot;&quot;\n    # \u6b65\u9aa4 1: \u8ba1\u7b97\u65e5\u6536\u76ca\u7387\n    daily_returns = &#x5B;]\n    for i in range(1, len(prices_list)):\n        # (\u4eca\u65e5\u4ef7\u683c \/ \u6628\u65e5\u4ef7\u683c) - 1\n        daily_return = (prices_list&#x5B;i] \/ prices_list&#x5B;i-1]) - 1\n        daily_returns.append(daily_return)\n    \n    if not daily_returns:\n        return 0.0\n    \n    # \u6b65\u9aa4 2: \u8ba1\u7b97\u6536\u76ca\u7387\u7684\u6807\u51c6\u5dee\n    # \u9996\u5148\uff0c\u8ba1\u7b97\u5e73\u5747\u6536\u76ca\u7387\n    mean_return = sum(daily_returns) \/ len(daily_returns)\n    # \u7136\u540e\uff0c\u8ba1\u7b97\u65b9\u5dee\n    variance = sum(&#x5B;(r - mean_return) ** 2 for r in daily_returns]) \/ len(daily_returns)\n    std_dev = math.sqrt(variance)\n    \n    # \u6b65\u9aa4 3: \u5c06\u6ce2\u52a8\u7387\u5e74\u5316\n    trading_days = 252\n    annualized_volatility = std_dev * math.sqrt(trading_days)\n    \n    return annualized_volatility\n\n# --- \u4f7f\u7528\u51fd\u6570 ---\naapl_prices = &#x5B;150.1, 151.2, 150.8, 152.5, 152.3, 153.1, 155.0, 154.5]\nvolatility = calculate_annualized_volatility(aapl_prices)\nprint(f&quot;Calculated Annualized Volatility: {volatility:.2%}&quot;)\n\n# \u5bfc\u5165\u6574\u4e2a\u6a21\u5757\nimport math\nprint(math.sqrt(25))  # \u4f7f\u7528 module_name.function_name \u8c03\u7528\u51fd\u6570\n\n# \u4ece\u6a21\u5757\u5bfc\u5165\u7279\u5b9a\u51fd\u6570\nfrom math import sqrt\nprint(sqrt(25))  # \u76f4\u63a5\u6309\u540d\u79f0\u8c03\u7528\u51fd\u6570\n\n# \u91cf\u5316\u793e\u533a\u901a\u7528\u7ea6\u5b9a\u662f\u4f7f\u7528\u522b\u540d\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\n\n###\n#\u521b\u5efa\u6a21\u5757\u6587\u4ef6\uff1a\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u521b\u5efa\u540d\u4e3a financial_metrics.py \u7684\u65b0\u6587\u4ef6\u3002\n\n#\u5411\u6a21\u5757\u6dfb\u52a0\u51fd\u6570\uff1a\n\n###\n\nimport math\n\ndef calculate_annualized_volatility(prices_list):\n    &quot;&quot;&quot;\n    \u4ece\u65e5\u4ef7\u683c\u5217\u8868\u8ba1\u7b97\u80a1\u7968\u7684\u5e74\u5316\u6ce2\u52a8\u7387\u3002\n    ... (\u4e0e\u524d\u9762\u76f8\u540c\u7684\u6587\u6863\u5b57\u7b26\u4e32)...\n    &quot;&quot;&quot;\n    #... (\u4e0e\u524d\u9762\u76f8\u540c\u7684\u51fd\u6570\u4ee3\u7801)...\n    return annualized_volatility\n\ndef calculate_simple_return(start_price, end_price):\n    &quot;&quot;&quot;\u8ba1\u7b97\u4e00\u6bb5\u65f6\u95f4\u5185\u7684\u7b80\u5355\u6536\u76ca\u7387\u3002&quot;&quot;&quot;\n    return (end_price \/ start_price) - 1\n    \n#\u5bfc\u5165\u548c\u4f7f\u7528\u81ea\u5b9a\u4e49\u6a21\u5757\uff1a\n# \u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\nimport financial_metrics\n\n# \u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570\naapl_prices = &#x5B;150.1, 151.2, 150.8, 152.5, 152.3, 153.1, 155.0, 154.5]\nvol = financial_metrics.calculate_annualized_volatility(aapl_prices)\nret = financial_metrics.calculate_simple_return(aapl_prices&#x5B;0], aapl_prices&#x5B;-1])\nprint(f&quot;Using our custom module:&quot;)\nprint(f&quot; Annualized Volatility: {vol:.2%}&quot;)\nprint(f&quot; Total Return: {ret:.2%}&quot;)\n\n\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u81ea\u5fae\u4fe1\u516c\u4f17\u53f7 \u300a\u6570\u636e\u79d1\u5b66\u5b9e\u8df5\u300b<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-47","post","type-post","status-publish","format-aside","hentry","category-7","post_format-post-format-aside"],"_links":{"self":[{"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=47"}],"version-history":[{"count":1,"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/posts\/47\/revisions"}],"predecessor-version":[{"id":48,"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/posts\/47\/revisions\/48"}],"wp:attachment":[{"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}