@@ -42,21 +42,9 @@ def iter_rows(self):
4242 }
4343
4444 # 2D array of processed data frame into [["Open", "Close", "High", "Low", "Volume"], ...]
45+ # aggregates trades into candlesticks over 'cycles' instead of timestamp
46+ # last value in array is the trade price
4547 def _process_csv (self , cycles : int = 10 , cycle_range : tuple [int , int ] | None = None , source : str = "expected" ) -> list [list [float ]]:
46- """
47- agg trades into candlesticks over `cycles` rows.
48-
49- Uses the last value of prices in a row as the trade price; volume is
50- the sum of quantities for that row. Empty rows (no trades) advance the
51- cycle window but do not change OHLCV unless a trade occurs in the window.
52- Returns a list of [Open, Close, High, Low, Volume].
53-
54- Args:
55- cycles: Number of rows per candlestick window.
56- cycle_range: Optional (min, max) tuple to filter rows by cycle number.
57- source: Either "expected" (exp_prices/exp_qtities) or "actual" (acc_prices/acc_qtities).
58- """
59-
6048 candles : list [list [float ]] = []
6149
6250 open_price = None
@@ -112,11 +100,10 @@ def _process_csv(self, cycles: int = 10, cycle_range: tuple[int, int] | None = N
112100
113101 return candles
114102
115-
116-
117103 def _split_column (self , col : str ):
118104 if col not in self .df :
119105 return
106+
120107 def _to_float_list (value ):
121108 if pd .isna (value ):
122109 return []
@@ -128,8 +115,10 @@ def _to_float_list(value):
128115 except TypeError :
129116 return [float (value )]
130117
131- self .df [col ] = self .df [col ].apply (_to_float_list )
132-
118+ self .df [col ] = self .df [col ].apply (lambda x : _to_float_list (x ))
119+ # self.df[col] = self.df[col].apply(_to_float_list) is equivalent, e.g. x -> f -> f x
120+
121+ # plots the candlestick chart
133122 def plot_candles (self , data , start = "2026-01-01" , freq = "T" , title = "Stock Price" , out : Path | None = None ):
134123 df = pd .DataFrame (
135124 data ,
@@ -155,19 +144,12 @@ def plot_candles(self, data, start="2026-01-01", freq="T", title="Stock Price",
155144 )
156145
157146 def plot_all (self , out_dir : Path | None = None , cycle_range : tuple [int , int ] | None = None ):
158- """
159- Generate and save exchange candlestick plots for both expected and actual data.
160-
161- Args:
162- out_dir: Output directory. If None, uses default exchange graphs directory.
163- cycle_range: Optional (min, max) tuple to filter data by cycle range.
164- """
165147 if out_dir is None :
166148 out_dir = get_graph_dir ("exchange" )
167149
168150 base_name = self .csv_path .stem
169151
170- # Plot expected data
152+ # process csv into useable form
171153 candles_expected = self ._process_csv (cycle_range = cycle_range , source = "expected" )
172154 if candles_expected :
173155 self .plot_candles (
@@ -180,7 +162,7 @@ def plot_all(self, out_dir: Path | None = None, cycle_range: tuple[int, int] | N
180162 else :
181163 print ("No expected trades found" )
182164
183- # Plot actual data
165+ # plot actual data
184166 candles_actual = self ._process_csv (cycle_range = cycle_range , source = "actual" )
185167 if candles_actual :
186168 self .plot_candles (
0 commit comments