Skip to main content
Glama
zkyko
by zkyko
enhanced_extract_trade.cpython-313.pyc15.4 kB
� ��jh'�� �SrSSKrSSKrSSKrSSKrSSKrSSKJr SSKJrJ r J r J r SSK J r SSKJrJr SSKJr SSKrSSKJr \ "5 \R,R/\R,R/\R,R1\555r\R,R7\S S 5r\R,R7\S 5r\R,R7\S 5r\"\R>"S 5\R>"SS5S9r "SS\5r!S\"S\ \"\ 44Sjr#S\"S\"S\"4Sjr$S\"S\"S\ S\!4Sjr%S$S\!S\"S\ \"4Sjjr&S$S\"S\"S\ 4Sjjr'S$S \"S\"S\ 4S!jjr(S"r)\*S#:Xa\)"5 gg)%a^ enhanced_extract_trade.py AI-powered trade extractor with single-image and batch support, OCR confidence metrics, DeepSeek integration, and multi-format logging. Usage examples (CLI): python enhanced_extract_trade.py trade.png python enhanced_extract_trade.py screenshots/ --batch python enhanced_extract_trade.py trade.png --json-only �N)�datetime)�Optional�List�Dict�Tuple)� load_dotenv)� BaseModel�field_validator)�Image)�OpenAI�logsztrade_log.jsonl�output� summaries�DEEPSEEK_API_KEY�DEEPSEEK_API_BASEzhttps://api.deepseek.com)�api_key�base_urlc�*�\rSrSr%\\S'Sr\\\S'Sr\\\S'Sr \\ \S'Sr \\ \S'Sr \\\S'Sr \\\S 'Sr\\ \S 'Sr\\\S 'Sr\\\S 'Sr\\\S '\\S'Sr\\\S'\"S SS9\S55rSrg)� TradeData�&�trade_idN�ticker� timeframe� entry_price� exit_price� direction�pnl� pnl_amount� date_time�reason_or_annotations� image_source� logged_at�ocr_confidence�before)�modec�$�UbUS:Xag[U[[45(a [U5$[U[5(a;[R "SSUR SS55nU(a [U5$g![a gf=f)z,Parse PnL amount from various string formatsN�z [^\d\.\-\+]�,)� isinstance�int�float�str�re�sub�replace� ValueError)�cls�v�cleaneds �<C:\Users\Timmy\Documents\MCP\tools\enhanced_extract_trade.py�parse_pnl_amount�TradeData.parse_pnl_amount5s��� �9��R��� �a�#�u�� &� &���8�O� �a�� � ��f�f�^�R����3��1C�D�G�� � ��>�)���"� �� �s�6 B� B�B�)�__name__� __module__� __qualname__�__firstlineno__r,�__annotations__rrrrr+rrrrrr r!r#r � classmethodr5�__static_attributes__r7�r4rr&s����M� �F�H�S�M� �#�I�x��}�#�#'�K��%��'�"&�J����&�#�I�x��}�#��C��#���"&�J����&�#�I�x��}�#�+/��8�C�=�/�"&�L�(�3�-�&��N�$(�N�H�S�M�(��\��1����2�r?r� image_path�returnc ��[RRU5(d[SU35e[R "U5n[ R"U5n[ R"U[ RRS9n/nUSH(n[U5nUS:�aURU5 M(M* U(a[U5[!U5- OSnUU[!USVs/sHo�R#5(dMUPM sn5UR$S.4$![[4a M�f=fs snf)zOCR text + confidence infozImage not found: )� output_type�confrg�text)� confidence� total_words� image_size)�os�path�exists�FileNotFoundErrorr �open� pytesseract�image_to_string� image_to_data�Output�DICTr*�appendr0� TypeError�sum�len�strip�size) r@�imgrE�data�confs�c�conf_val�avg_conf�ws r4�extract_text_from_imager`Ks�� �7�7�>�>�*� %� %��"3�J�<� @�A�A� �*�*�Z� �C� � &� &�s� +�D� � $� $�S�k�6H�6H�6M�6M� N�D� �E� �&�\�� ��1�v�H��!�|�� � �X�&���+0�s�5�z�C��J�&�S�H� ���t�F�|�A�|�!�w�w�y�A�|�A�B��h�h�� ��� �I�&� � � ��Bs�"D$�-D; � D; �$D8�7D8�raw_textc���S[RRU5SUS3n[RR R SSUS./SSS 9nURS RR$) z8Call DeepSeek (OpenAI-compatible) to structure the tradea` You are an expert trading analyst. Given OCR text from a trading screenshot, output ONLY valid JSON with the following keys: ticker, timeframe, entry_price, exit_price, direction, pnl, pnl_amount, date_time, reason_or_annotations IMPORTANT: For pnl_amount, extract only the numeric value (e.g., if you see "+38.07 USD", output 38.07) OCR text from z: """a""" Example output: { "ticker": "SOLUSD", "timeframe": "5m", "entry_price": 150.25, "exit_price": 151.50, "direction": "long", "pnl": "+38.07 USD", "pnl_amount": 38.07, "date_time": "2025-07-06 14:20:58", "reason_or_annotations": "Quick scalp trade" } z deepseek-chat�user)�role�contentg�������?i�)�model�messages� temperature� max_tokensr) rIrJ�basename�client�chat� completions�create�choices�messagere)rar@�prompt�rsps r4�analyze_trade_with_airsis�����w�w��� �+�,�-��j���F�. �+�+� !� !� (� (��!�f�5�6��� )� �C� �;�;�q�>� !� !� )� )�)r?�ai_json�ocr_infoc��UR5nURS5(aUSSR5nO)URS5(aUSSR5n[R"U5n[ SU35 [ [[R"55SS URS 5URS 5URS 5URS5URS5URS5URS5URS5URS5[RRU5[R"5R!5URSS5SS3S9 $![R a n[ SU35 SS U30nSnAGN<SnAff=f)Nz```json������z```�u🔍 Parsed AI data: u❌ JSON parse error: �errorzJSON parse error: �rrrrrrrrr rFr�.1f�%) rrrrrrrrrr r!r"r#)rW� startswith�json�loads�print�JSONDecodeErrorrr,�uuid�uuid4�getrIrJrjr�now� isoformat)rtr@rur3rZ�es r4�create_trade_recordr��s����m�m�o�G����)�$�$��!�B�-�%�%�'�� � � �E� "� "��!�B�-�%�%�'��3��z�z�'�"�� �%�d�V�,�-� ��T�Z�Z�\�"�2�A�&��x�x��!��(�(�;�'��H�H�]�+��8�8�L�)��(�(�;�'� �H�H�U�O��8�8�L�)��(�(�;�'�"�h�h�'>�?��W�W�%�%�j�1��,�,�.�*�*�,�"�,�,�|�Q�7��<�A�>� ��� � � �3� �&�q�c�*�+��-�a�S�1�2���3�s�%$F�G �*G�G �trader%c�4�/n[R"[RR[5SS9 [ [SSS9nUR UR5S-5 SSS5 UR[5 US;a�[R"[SS9 [RR[SURS [R"5S S 35n[ US SS9n[R"UR!5US ["S9 SSS5 URU5 US;Ga�[R"5R%S5n[RR[&SUS 35n[R"[&SS9 [RR)U5(a+[ USSS9n[R*"U5nSSS5 O)U/SS[R"5R-5S.nWSRUR!55 [/US5US'[1SUS55US'[R"5R-5US'[ US SS9n[R"XsS ["S9 SSS5 URU5 U$!,(df  GNp=f!,(df  GN�=f!,(df  N�=f!,(df  NV=f)NT)�exist_ok�azutf-8)�encoding� >�bothr�trade_�_z %Y%m%d_%H%M%Sz.jsonr_�)�indent�default>r��jsonlz%Y-%m-%d�daily_summary_�rr)�date�trades� total_trades� total_pnl� created_atr�r�c3�T# �UHoRS5=(d Sv� M g7f)rrN)r�)�.0�ts r4� <genexpr>�"save_trade_data.<locals>.<genexpr>�s!���"W�EV��5�5��#6�#;�!�#;�EV�s�&(r�� updated_at)rI�makedirsrJ�dirname�TRADE_LOG_PATHrM�write�model_dump_jsonrS� OUTPUT_DIR�joinrrr�r�dump� model_dumpr,�strftime� SUMMARIES_DIRrK�loadr�rVrU)r�r%�saved�f�fp�day� summary_path�summarys r4�save_trade_datar��sM���E��K�K�������/�$�?� �n�c�G� 4�� ����%�%�'�$�.�/� 5� �L�L�� � ��� � � �J��.� �W�W�\�\� �&���� 0��(�,�,�.��1O�u�U� ���"�c�G� ,�� �I�I�e�&�&�(�!�A�s� C�-� � � �R�� � � ��l�l�n�%�%�j�1���w�w�|�|�M�^�C�5��3N�O� � � � �M�D�1� �7�7�>�>�,� '� '��l�C�'�:�a��)�)�A�,��;�:��� !��&�l�l�n�6�6�8� �G� ��� � ��!1�!1�!3�4�"%�g�h�&7�"8����"�"W�W�X�EV�"W�W�� �� (� � �� 8� 8� :�� �� �,��g� 6�!� �I�I�g��C� 8�7� � � �\�"� �L�M 5� 4��-� ,��;�:��7� 6�s0�#K�;*K&�K8�L � K#�& K5�8 L� L� save_modec� �[SU35 [U5up#[SUSSS35 [S5 [X 5n[S5 [S5 [X@U5n[S 5 [ XQ5n[S 5 UR [ RRU5URURURUSUS .$![an[S [U535 eSnAff=f) Nu"🔍 Starting OCR extraction for: u 📝 OCR completed, confidence: rFr|r}u🤖 Calling AI analysis...u🤖 AI response receivedu📊 Creating trade record...u💾 Saving trade data...u✅ Processing complete)r�imagerrrrF� saved_filesu#❌ Error in process_single_image: )r�r`rsr�r�rrIrJrjrrr� Exceptionr,)r@r�rarurtr��filesr�s r4�process_single_imager��s���� �2�:�,�?�@�4�Z�@��� �0��,�1G��0L�A�N�O� �+�-�'��=�� �)�+� �-�/�#�G��B�� �)�+���1�� �'�)�����W�W�%�%�j�1��l�l�����*�*�"�<�0� � � �� �� �3�C��F�8�<�=� ���s�CC� D�%C=�=D�folderc��[RRU5(dSSU30$1Skn[R"U5Vs/sHPo3R 5R [ U55(dM1[RRX5PMR nnU(dSS0$[U5SS/S.nUH0n[Xa5nUSRU5 US==S - ss'M2 U$s snf![aUnUSR[RRU5[U5S .5 US ==S - ss'SnAM�SnAff=f) NrzzFolder not found: >�.bmp�.gif�.jpg�.png�.jpeg�.tiffzNo image files foundr)�total�ok�fail�detailsr�r��)r�rzr�)rIrJ�isdir�listdir�lower�endswith�tupler�rVr�rSr�rjr,) r�r��extsr��images�resultsrY�resr�s r4�process_multiple_imagesr��s,�� �7�7�=�=�� � ��-�f�X�6�7�7� =�D�/1�z�z�&�/A� e�/A�!�W�W�Y�EW�EW�X]�^b�Xc�Ed�%�b�g�g�l�l�6�%�/A�F� e� ��/�0�0��F� �1�a�B�G�G��� !�&�s�6�C� �I� � %� %�c� *� �D�M�Q� �M� � �N��f��� !� �I� � %� %����0@�0@��0E�PS�TU�PV�&W� X� �F�O�q� �O�O�� !�s%�0C4�<#C4�,C9�9 E�A E�Ec�<�[[R5S:a![S5 [R"S5 [RSnS[R;=(d [ R RU5nSnS[R;aSnS[R;aS nU(a6[S U35 [[R"[X5SS 95 g[[R"[X5SS 95 g) Nr�z]Usage: python enhanced_extract_trade.py <image>|<folder> [--batch] [--json-only|--jsonl-only]r�z--batchr�z --json-onlyrz --jsonl-onlyr�u🗂 Batch processing folder: )r�) rV�sys�argvr��exitrIrJr�r�dumpsr�r�)�target�batchr%s r4�_clir� s��� �3�8�8�}�q�� �m�n� ���� � �X�X�a�[�F� ���� !� :�R�W�W�]�]�6�%:�E� �D����� ��$�����!�'�4� � �.�v�h�7�8� �d�j�j�0��>�q�I�J� �d�j�j�-�f�;�A�F�Gr?�__main__)r�)+�__doc__rIr�rr�r-r�typingrrrr�dotenvr�pydanticr r �PILr rN�openair rJr��abspath�__file__�BASE_DIRr�r�r�r��getenvrkrr,r`rsr�r�r�r�r�r8r7r?r4�<module>r�s��� �����.�.��/���� � � �7�7�?�?�2�7�7�?�?�2�7�7�?�?�8�+D�E� F�������h��0A�B�� �W�W�\�\�(�H� -� ���� � �X�{�3� � � �I�I�(� )� �Y�Y�*�,F� G� �� "� �"�J����c�4�i�0@��<*�C�*�S�*�S�*�D���#����)��@+�9�+�C�+�T�#�Y�+�\�S��S��d��<�C��C��T��*H�" �z���F�r?

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/zkyko/MCP'

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