Skip to content

Commit 8d585b2

Browse files
authored
Merge pull request #4 from RektPunk/feat/clean-it-up
Code Cleanup and Formatting for Improved Readability
2 parents 62b1a51 + d709637 commit 8d585b2

12 files changed

+775
-751
lines changed

agent_state.py

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
from typing import Annotated, Sequence, TypedDict, List
2-
from langgraph.graph import MessagesState
1+
from typing import Annotated, List, TypedDict
2+
33
from langchain_core.messages import BaseMessage
44

55

66
class IndicatorAgentState(TypedDict):
77
"""State type for the Indicator Agent including messages, input data, and analysis result."""
8-
kline_data: Annotated[dict, "OHLCV dictionary used for computing technical indicators"]
8+
9+
kline_data: Annotated[
10+
dict, "OHLCV dictionary used for computing technical indicators"
11+
]
912
time_frame: Annotated[str, "time period for k line data provided"]
1013
stock_name: Annotated[dict, "stock name for prompt"]
11-
14+
1215
# Indicator Agent Tools output values (explicitly added per indicator)
1316
rsi: Annotated[List[float], "Relative Strength Index values"]
1417
macd: Annotated[List[float], "MACD line values"]
@@ -18,23 +21,47 @@ class IndicatorAgentState(TypedDict):
1821
stoch_d: Annotated[List[float], "Stochastic Oscillator %D values"]
1922
roc: Annotated[List[float], "Rate of Change values"]
2023
willr: Annotated[List[float], "Williams %R values"]
21-
indicator_report: Annotated[str, "Final indicator agent summary report to be used by downstream agents"]
22-
24+
indicator_report: Annotated[
25+
str, "Final indicator agent summary report to be used by downstream agents"
26+
]
2327

2428
# Pattern Agent
25-
pattern_image: Annotated[str, "Base64-encoded K-line chart for pattern recognition agent use"]
26-
pattern_image_filename: Annotated[str, "Local file path to saved K-line chart image"]
27-
pattern_image_description: Annotated[str, "Brief description of the generated K-line image"]
28-
pattern_report: Annotated[str, "Final pattern agent summary report to be used by downstream agents"]
29+
pattern_image: Annotated[
30+
str, "Base64-encoded K-line chart for pattern recognition agent use"
31+
]
32+
pattern_image_filename: Annotated[
33+
str, "Local file path to saved K-line chart image"
34+
]
35+
pattern_image_description: Annotated[
36+
str, "Brief description of the generated K-line image"
37+
]
38+
pattern_report: Annotated[
39+
str, "Final pattern agent summary report to be used by downstream agents"
40+
]
2941

3042
# Trend Agent
31-
trend_image: Annotated[str, "Base64-encoded trend-annotated candlestick (K-line) chart for trend recognition agent use"]
32-
trend_image_filename: Annotated[str, "Local file path to saved trendline-enhanced K-line chart image"]
33-
trend_image_description: Annotated[str, "Brief description of the chart, including presence of support/resistance lines and visual characteristics"]
34-
trend_report: Annotated[str, "Final trend analysis summary, describing structure, directional bias, and technical observations for downstream agents"]
43+
trend_image: Annotated[
44+
str,
45+
"Base64-encoded trend-annotated candlestick (K-line) chart for trend recognition agent use",
46+
]
47+
trend_image_filename: Annotated[
48+
str, "Local file path to saved trendline-enhanced K-line chart image"
49+
]
50+
trend_image_description: Annotated[
51+
str,
52+
"Brief description of the chart, including presence of support/resistance lines and visual characteristics",
53+
]
54+
trend_report: Annotated[
55+
str,
56+
"Final trend analysis summary, describing structure, directional bias, and technical observations for downstream agents",
57+
]
3558

3659
# Final analysis and messaging context
3760
analysis_results: Annotated[str, "Computed result of the analysis or decision"]
38-
messages: Annotated[List[BaseMessage], "List of chat messages used in LLM prompt construction"]
61+
messages: Annotated[
62+
List[BaseMessage], "List of chat messages used in LLM prompt construction"
63+
]
3964
decision_prompt: Annotated[str, "decision prompt for reflection"]
40-
final_trade_decision: Annotated[str, "Final BUY or SELL decision made after analyzing indicators"]
65+
final_trade_decision: Annotated[
66+
str, "Final BUY or SELL decision made after analyzing indicators"
67+
]

color_style.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import mplfinance as mpf
22

33
font = {
4-
'font.family': 'sans-serif',
5-
'font.sans-serif': ['Helvetica Neue', 'Arial', 'DejaVu Sans'],
6-
'font.weight': 'normal',
7-
'font.size': 15
4+
"font.family": "sans-serif",
5+
"font.sans-serif": ["Helvetica Neue", "Arial", "DejaVu Sans"],
6+
"font.weight": "normal",
7+
"font.size": 15,
88
}
99

1010
my_color_style = mpf.make_mpf_style(
1111
marketcolors=mpf.make_marketcolors(
12-
down="#A02128", # color for bullish candles
13-
up="#006340", # color for bearish candles
14-
edge='none', # use candle fill color for edge
15-
wick='black', # color of the wicks
16-
volume='in' # default volume coloring
12+
down="#A02128", # color for bullish candles
13+
up="#006340", # color for bearish candles
14+
edge="none", # use candle fill color for edge
15+
wick="black", # color of the wicks
16+
volume="in", # default volume coloring
1717
),
18-
gridstyle='-',
19-
facecolor='white', # background color
20-
rc= font,
18+
gridstyle="-",
19+
facecolor="white", # background color
20+
rc=font,
2121
)

decision_agent.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ def create_final_trade_decider(llm):
99
Create a trade decision agent node. The agent uses LLM to synthesize indicator, pattern, and trend reports
1010
and outputs a final trade decision (LONG or SHORT) with justification and risk-reward ratio.
1111
"""
12+
1213
def trade_decision_node(state) -> dict:
1314
indicator_report = state["indicator_report"]
14-
pattern_report = state['pattern_report']
15-
trend_report = state['trend_report']
16-
time_frame = state['time_frame']
17-
stock_name = state['stock_name']
15+
pattern_report = state["pattern_report"]
16+
trend_report = state["trend_report"]
17+
time_frame = state["time_frame"]
18+
stock_name = state["stock_name"]
1819

1920
# --- System prompt for LLM ---
2021
prompt = f"""You are a high-frequency quantitative trading (HFT) analyst operating on the current {time_frame} K-line chart for {stock_name}. Your task is to issue an **immediate execution order**: **LONG** or **SHORT**. ⚠️ HOLD is prohibited due to HFT constraints.

default_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
"agent_llm_temperature": 0.1,
55
"graph_llm_temperature": 0.1,
66
"api_key": "",
7-
}
7+
}

graph_setup.py

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,15 @@
1-
from typing import Dict, Any
1+
from typing import Dict
2+
23
from langchain_openai import ChatOpenAI
3-
from langgraph.graph import END, StateGraph, START
4+
from langgraph.graph import END, START, StateGraph
45
from langgraph.prebuilt import ToolNode
5-
from graph_util import TechnicalTools
6-
import default_config
7-
import os
8-
from langchain.chat_models import init_chat_model
9-
from langchain_core.messages import AnyMessage
10-
from langchain_core.runnables import RunnableConfig
11-
from langgraph.prebuilt.chat_agent_executor import AgentState
12-
from langgraph.prebuilt import create_react_agent
13-
from langgraph.graph import END, StateGraph, START
14-
15-
from typing import TypedDict, List
16-
import random
17-
from IPython.display import Image, display
18-
import pandas as pd
19-
from graph_util import *
20-
# from langchain_community.chat_models import ChatOpenAI
21-
from langchain_openai import ChatOpenAI
22-
from langchain_core.prompts import ChatPromptTemplate
236

24-
from langgraph.graph import StateGraph
25-
from langgraph.prebuilt import ToolNode
267
from agent_state import IndicatorAgentState
27-
28-
from indicator_agent import *
29-
from decision_agent import *
30-
from pattern_agent import *
31-
from trend_agent import *
8+
from decision_agent import create_final_trade_decider
9+
from graph_util import TechnicalTools
10+
from indicator_agent import create_indicator_agent
11+
from pattern_agent import create_pattern_agent
12+
from trend_agent import create_trend_agent
3213

3314

3415
class SetGraph:
@@ -43,30 +24,32 @@ def __init__(
4324
self.graph_llm = graph_llm
4425
self.toolkit = toolkit
4526
self.tool_nodes = tool_nodes
46-
27+
4728
def set_graph(self):
48-
# Create analyst nodes
29+
# Create analyst nodes
4930
agent_nodes = {}
5031
tool_nodes = {}
51-
all_agents = ['indicator', 'pattern', 'trend']
52-
32+
all_agents = ["indicator", "pattern", "trend"]
33+
5334
# create nodes for indicator agent
54-
agent_nodes['indicator'] = create_indicator_agent(self.graph_llm, self.toolkit)
55-
tool_nodes['indicator'] = self.tool_nodes['indicator']
35+
agent_nodes["indicator"] = create_indicator_agent(self.graph_llm, self.toolkit)
36+
tool_nodes["indicator"] = self.tool_nodes["indicator"]
5637

5738
# create nodes for pattern agent
58-
agent_nodes['pattern'] = create_pattern_agent(self.agent_llm, self.graph_llm, self.toolkit)
59-
tool_nodes['pattern'] = self.tool_nodes['pattern']
39+
agent_nodes["pattern"] = create_pattern_agent(
40+
self.agent_llm, self.graph_llm, self.toolkit
41+
)
42+
tool_nodes["pattern"] = self.tool_nodes["pattern"]
6043

6144
# create nodes for trend agent
62-
agent_nodes['trend'] = create_trend_agent(self.agent_llm, self.graph_llm, self.toolkit)
63-
tool_nodes['trend'] = self.tool_nodes['trend']
45+
agent_nodes["trend"] = create_trend_agent(
46+
self.agent_llm, self.graph_llm, self.toolkit
47+
)
48+
tool_nodes["trend"] = self.tool_nodes["trend"]
6449

6550
# create nodes for decision agent
66-
# decision_agent_node = create_final_trade_decider(self.agent_llm)
6751
decision_agent_node = create_final_trade_decider(self.graph_llm)
6852

69-
7053
# create graph
7154
graph = StateGraph(IndicatorAgentState)
7255

@@ -75,31 +58,24 @@ def set_graph(self):
7558
graph.add_node(f"{agent_type.capitalize()} Agent", cur_node)
7659
graph.add_node(f"{agent_type}_tools", tool_nodes[agent_type])
7760

78-
7961
# add rest of the nodes
8062
graph.add_node("Decision Maker", decision_agent_node)
8163

8264
# set start of graph
8365
graph.add_edge(START, "Indicator Agent")
8466

85-
8667
# add edges to graph
8768
for i, agent_type in enumerate(all_agents):
8869
current_agent = f"{agent_type.capitalize()} Agent"
89-
current_tools = f"{agent_type}_tools"
90-
9170

9271
if i == len(all_agents) - 1:
9372
graph.add_edge(current_agent, "Decision Maker")
9473
else:
95-
74+
9675
next_agent = f"{all_agents[i + 1].capitalize()} Agent"
9776
graph.add_edge(current_agent, next_agent)
98-
9977

100-
10178
# Decision Maker Process
10279
graph.add_edge("Decision Maker", END)
10380

104-
10581
return graph.compile()

0 commit comments

Comments
 (0)