Skip to main content
Glama
finance_server.cpython-311.pyc17 kB
� ~5�g�7� ���UddlmZddlZddlZddlmZddlmZddlm Z m Z edgd����Z d Z eGd �d ����Z Gd �d ��ZiZe ee fed<e �d��defd���Ze ���d dededede ee ffd���Ze ���d!dedede ee ffd���Ze ���dede ee ffd���Ze ���dedefd���Ze ���dedefd���Ze ���dedefd���ZdS)"�)�FastMCPN)� dataclass)�datetime)�Dict�Any�AlphaVantageTrader)�requests�pandas�tabulate)� dependencies�60SDH22B4PJGUSTEc�B�eZdZUeed<eed<ejed<eed<dS)� MarketData�symbol�interval�data� last_updatedN)�__name__� __module__� __qualname__�str�__annotations__�pd� DataFramer���MC:\Users\seewi\Akram-dev\Upskill\MCP-weather-server\finance\finance_server.pyrrs>������� �K�K�K��M�M�M� �,���������rrc �B�eZdZed dedededejfd���ZdS) �AlphaVantageAPI�1min�compactrr� outputsize�returnc�Z�d|�d|�d|�dt��}tj|��}|���}d|vrt d|d�����d|vrt d|d����d |�d �}||vrt d |�d |�����||}t j�|d ���}t j |j ��|_ |� ��}d�|j D��|_ |j D]} t j || ��|| <� |S)z)Fetch intraday data from AlphaVantage APIzGhttps://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=z &interval=z &outputsize=z&apikey=z Error Messagez API Error: �Notez API Note: z Time Series (�)zNo time series data found for z with interval �index)�orientc�D�g|]}|�d��d��S)�. �)�split)�.0�cols r� <listcomp>z5AlphaVantageAPI.get_intraday_data.<locals>.<listcomp>1s'��?�?�?�S�c�i�i��o�o�a�(�?�?�?r)�API_KEYr �get�json� ValueError�printrr� from_dict� to_datetimer'� sort_index�columns� to_numeric) rrr"�url�responser�time_series_key� time_series�dfr.s r�get_intraday_dataz!AlphaVantageAPI.get_intraday_datas���^�X^�^�^�jr�^�^�AK�^�^�U\�^�^���<��$�$���}�}���� �d� "� "��B�4��+@�B�B�C�C� C� �T�>�>� �-�t�F�|�-�-� .� .� .�6�(�5�5�5�� �$� &� &��_�f�_�_�U]�_�_�`�`� `��?�+� ��\� #� #�K�� #� @� @���>�"�(�+�+��� �]�]�_�_��@�?�B�J�?�?�?�� ��:� -� -�C��m�B�s�G�,�,�B�s�G�G�� rN)r r!)rrr� staticmethodrrrr?rrrrrsW���������#����3��_a�_k�����\���rr�market_data_cachez config://appr#c��dS)zStatic configuration datazApp configuration hererrrr� get_configrC;s �� $� #r��2r� short_period� long_periodc"�d�|�d�}|tvrIt�|dd���}t|d|t j�����t|<t|j}|d�|������|d|��<|d�|������|d|��<|j d }|d}|d|��}|d|��} || krd } n || krd } nd } |j d d�} d} d} tdt| ����D]�}| j |dz }| j |}|d|��|d|��kr|d|��|d|��krd} d} n7|d|��|d|��kr|d|��|d|��krd} d} n��d|d|d|��|d|��| d| d| d| r| nddd|�d|d�d|�d |d�d|�d | d�d!| �d"| rd#| znd$�d%| r| dkrd&n| d krd'n| r| dkrd(n | d krd)nd*��iS)+aB Calculate short and long moving averages for a symbol Args: symbol: The ticker symbol to analyze short_period: Short moving average period in minutes long_period: Long moving average period in minutes Returns: Dictionary with moving average data and analysis �_1minr �full�r"�rrrr�close��window�SMA�����z BULLISH (Short MA above Long MA)z BEARISH (Short MA below Long MA)zNEUTRAL (MAs are equal)�����NF�r+TzGOLDEN CROSS (Bullish)zDEATH CROSS (Bearish)r� current_price�signal�crossover_detected�crossover_type�None�analysiszMoving Average Analysis for z: Current Price: $�.2f� z-period SMA: $� Signal: � Recent Crossover: �Yes - �No� Recommendation: � STRONG BUY�BUY� STRONG SELL�SELL�HOLD) rArr?rr�nowr�rolling�mean�iloc�range�len)rrFrG� cache_keyr>r�latestrT�short_ma�long_marU�last_5� crossoverrW�i�prev�currs r�calculate_moving_averagesruBs���� � � �I��)�)�)� � .� .�v�v�&� .� Q� Q��'1����!���� ( �( �( ��)�$� �Y� '� ,�D�"&�g��!6�!6�l�!6�!K�!K�!P�!P�!R�!R�D� �|� � �� $�W� � 5� 5�[� 5� I� I� N� N� P� P�D� �{� � ���Y�r�]�F��7�O�M��*�L�*�*�+�H��(�;�(�(�)�G��'���3��� �G� � �3���*���Y�r�s�s�^�F��I��N� �1�c�&�k�k� "� "�����{�1�Q�3����{�1�~�� �$�l�$�$� %��.A�K�.A�.A�)B� B� B�t�L`�R^�L`�L`�Ga�dh�i|�oz�i|�i|�d}�G}�G}��I�5�N� �E� �$�l�$�$� %��.A�K�.A�.A�)B� B� B�t�L`�R^�L`�L`�Ga�dh�i|�oz�i|�i|�d}�G}�G}��I�4�N� �E�� �&����l���h��k���W��&��i��I�A�.�.�6�� �V� � �� � � � � � �%� � � � � � �$� � � � � � � 1:�C�8�n�,�,�t� � �� �.�4L�"L�"L�L�L� �9� 9� 9�E�E�� �>�5L�#L�#L�M�M��:�:�:�F�F� � � � �r��periodc��|�d�}|tvrIt�|dd���}t|d|t j�����t|<t|j���}|d���}|���}|���}d||dk<d||dk<t|��}|� |���� ��}|� |���� ��} || z } d d d | zz z } | j d } | d krd } n | dkrd} nd} ||| | d|�d|�d| d�d| �d| d krdn | dkrdnd�� d�S)z� Calculate Relative Strength Index (RSI) for a symbol Args: symbol: The ticker symbol to analyze period: RSI calculation period in minutes Returns: Dictionary with RSI data and analysis rIr rJrKrLrMrrN�dr+rQ�z$OVERSOLD (Potential buy opportunity)�Fz'OVERBOUGHT (Potential sell opportunity)�NEUTRALzRSI Analysis for z: z -period RSI: rZr\r`rbrdre)rrw�rsirUrY) rArr?rrrfr�copy�diff�absrgrhri)rrwrlr>r�delta�gain�loss�avg_gain�avg_loss�rsr}� latest_rsirUs r� calculate_rsir��s���� � � �I��)�)�)� � .� .�v�v�&� .� Q� Q��'1����!���� ( �( �( ��)�$� �Y� '� ,� 1� 1� 3� 3�D� ��M� � � � �E� �:�:�<�<�D� �:�:�<�<�D��D����N��D����N� �t�9�9�D��|�|�6�|�*�*�/�/�1�1�H��|�|�6�|�*�*�/�/�1�1�H� �H� �B� ���B��� �C���"��J��B���7��� �b���:����������&������ ���� ��� �"�_�_�E�E��2�o�o�F�F� ���  � �rc���t|��}t|��}|d}|d}|d}|d}|d}d}d|vr|dz }n d|vr|dz}|rd |vr|d z }n d |vr|d z}d |vr|d z }n d|vr|d z}|d krd} n|dkrd} n|dkrd} n |dkrd} nd} d} t|��dkrd} nt|��dkrd} d|�d| �d| �d|d�d|�d|rd |znd!�d"|d#�d$|d%�d&|�d'|rd(|�d)�nd*�d+|vr1d,|�d-��d����d.�nd*�d/| dkrd0n| dkrd1n| dkrd2n | dkrd3nd4�d5�} || | ||||d6| d7�S)8z� Provide a comprehensive trade recommendation based on multiple indicators Args: symbol: The ticker symbol to analyze Returns: Dictionary with trading recommendation and supporting data rUrVrWr}r�BULLISHr+�BEARISH�GOLDEN��DEATH�OVERSOLDg�?� OVERBOUGHTrarb�����rcrdre�MEDIUM��LOW�HIGHz# Trading Recommendation for z ## Summary Recommendation: z Risk Level: z Signal Strength: z.1fz1 / 4.5 ## Technical Indicators Moving Averages: r]r^r_z RSI (rwz): rZz - zl ## Reasoning This recommendation is based on a combination of Moving Average analysis and RSI indicators. zThe z' provides a strong directional signal. rSr|zThe RSI indicates the stock is � r*z ## Action Plan z^Consider immediate entry with a stop loss at the recent low. Target the next resistance level.zDLook for a good entry point on small dips. Set reasonable stop loss.zFConsider immediate exit or setting tight stop losses to protect gains.z@Start reducing position on strength or set trailing stop losses.z4Monitor the position but no immediate action needed.r[rT)r�recommendation� risk_level�signal_strength� ma_signal� rsi_signalrTrY)rur�r�r,�lower) r�ma_data�rsi_datar�� ma_crossover�ma_crossover_type� rsi_valuer�r�r�r�rYs r�trade_recommendationr��s+��(��/�/�G��V�$�$�H���!�I��/�0�L�� 0�1�����I��(�#�J��O��I����1���� �i� � ��1����!� �(� (� (� �q� �O�O� �)� )� )� �q� �O��Z����3���� �� #� #��3����!���%��� �1� � ���� �B� � �&��� �1� � �������J� �?���a���� � � �_� � �� !� !�� ����� ���� �� "� ������4@�I�8�/�/�/�T����x����'����0:���JV�]�E� �E�E�E�E�[]��"OX�_i�Ni�Ni�J�j�&6�&6�s�&;�&;�A�&>�&D�&D�&F�&F�J�J�J�J�oq�#��,iw�{G�iG�iG�d�d�N\�`e�Ne�Ne�J�J�P^�bo�Po�Po�L�L�JX�\b�Jb�Jb�F�F�:�5���H�>�(� �*�� � ��1�� � � rc��d|�d|�d�S)z; Analyze a ticker symbol for trading opportunities zSYou are a professional stock market analyst. I would like you to analyze the stock z� and provide trading insights. Start by examining the current market data and technical indicators. Here are the specific tasks: 1. First, check the current market data for aU 2. Calculate the moving averages using the calculate_moving_averages tool 3. Calculate the RSI using the calculate_rsi tool 4. Generate a comprehensive trade recommendation using the trade_recommendation tool 5. Based on all this information, provide your professional analysis, highlighting: - The current market position - Key technical indicators and what they suggest - Potential trading opportunities and risks - Your recommended action (buy, sell, or hold) with a brief explanation Please organize your response in a clear, structured format suitable for a professional trader.r�rs r�analyze_tickerr�Js4��  c�ci� c� c�.4�  c� c� c�cr�symbolsc��d�|�d��D��}d�d�|D����}d|�d�S)z� Compare multiple ticker symbols for the best trading opportunity Args: symbols: Comma-separated list of ticker symbols c�6�g|]}|�����Sr)�strip�r-�ss rr/z#compare_tickers.<locals>.<listcomp>hs ��9�9�9��1�7�7�9�9�9�9�9r�,r[c��g|]}d|����S)z- rr�s rr/z#compare_tickers.<locals>.<listcomp>is��>�>�>�Q��Q���>�>�>rz�You are a professional stock market analyst. I would like you to compare these stocks and identify the best trading opportunity: a� For each stock in the list, please: 1. Check the current market data using the appropriate resource 2. Generate a comprehensive trade recommendation using the trade_recommendation tool 3. Compare all stocks based on: - Current trend direction and strength - Technical indicator signals - Risk/reward profile - Trading recommendation strength After analyzing each stock, rank them from most promising to least promising trading opportunity. Explain your ranking criteria and why you believe the top-ranked stock represents the best current trading opportunity. Conclude with a specific recommendation on which stock to trade and what action to take (buy, sell, or hold).)r,�join)r�� symbol_list�symbol_sections r�compare_tickersr�`sd��:�9�g�m�m�C�&8�&8�9�9�9�K��Y�Y�>�>�+�>�>�>�?�?�N� q�� q� q� q�qrc��d|�d|�d|�d�S)zH Build a custom intraday trading strategy for a specific ticker z�You are an expert algorithmic trader specializing in intraday strategies. I want you to develop a custom intraday trading strategy for zM. Please follow these steps: 1. First, analyze the current market data for a  using the market-data resource 2. Calculate relevant technical indicators: - Moving averages (short and long periods) - RSI 3. Based on your analysis, design an intraday trading strategy that includes: - Specific entry conditions (technical setups that would trigger a buy/sell) - Exit conditions (both take-profit and stop-loss levels) - Position sizing recommendations - Optimal trading times during the day - Risk management rules Make your strategy specific to the current market conditions for z�, not just generic advice. Include exact indicator values and price levels where possible. Conclude with a summary of the strategy and how a trader should implement it for today's trading session.rr�s r�intraday_strategy_builderr�~sK��  m�X^� m� m�06�  m� m�CI� m� m� m�mr)rDrE)rv)�mcp.server.fastmcprr r r� dataclassesrr�typingrr�mcpr0rrrArr�resourcerC�tool�intrur�r��promptr�r�r�rrr�<module>r�s���&�&�&�&�&�&�&���������!�!�!�!�!�!����������������g�"�1S�1S�1S�T�T�T�� �� �������� ��� � � � � � � � �F,.��4��Z��(�-�-�-����n���$�C�$�$�$���$� �����V�V�c�V��V�PS�V�]a�be�gj�bj�]k�V�V�V� ��V�r�����C�C�#�C�s�C�D��c��N�C�C�C� ��C�J�����e��e��c�3�h��e�e�e� ��e�N�����c�3�c�3�c�c�c���c�*�����q�S�q�S�q�q�q���q�:�����m�c�m�c�m�m�m���m�m�mr

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/akramsheriff5/MCP-Server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server