@@ -204,13 +204,16 @@ class StrategyContext(object):
204204 :ivar name: 策略名
205205 :ivar blotter: 订单管理
206206 :ivar exchange: 价格撮合器
207+ :ivar marks: 绘图标志集合
207208 """
208209 def __init__ (self , name , settings = {}):
209210 self .events_pool = EventsPool ()
210211 # @TODO merge blotter and exchange
211212 self .blotter = SimpleBlotter (name , self .events_pool , settings )
212213 self .exchange = Exchange (name , self .events_pool , strict = True )
213214 self .name = name
215+ # 0: line_marks, 1: text_marks
216+ self .marks = [{}, {}]
214217 self ._entry_orders = []
215218 self ._exit_orders = []
216219 self ._datetime = None
@@ -246,6 +249,35 @@ def process_trading_events(self, at_baropen):
246249 self .events_pool .put (OnceEvent ())
247250 self ._process_trading_events (at_baropen , append )
248251
252+ def plot_line (self , name , x , y , styles , lw = 1 , ms = 10 ):
253+ """ 绘制曲线
254+
255+ Args:
256+ name (str): 标志名称
257+ x (datetime): 时间坐标
258+ y (float): y坐标
259+ styles (str): 控制颜色,线的风格,点的风格
260+ lw (int): 线宽
261+ ms (int): 点的大小
262+ """
263+ mark = self .marks [0 ].setdefault (name , [])
264+ mark .append ((x , y , styles , lw , ms ))
265+
266+ def plot_text (self , name , x , y , text , color = 'black' , size = 10 , rotation = 0 ):
267+ """ 绘制文本
268+
269+ Args:
270+ name (str): 标志名称
271+ x (float): x坐标
272+ y (float): y坐标
273+ text (str): 文本内容
274+ color (str): 颜色
275+ size (int): 字体大小
276+ rotation (float): 旋转角度
277+ """
278+ mark = self .marks [1 ].setdefault (name , [])
279+ mark .append ((x , y , text , color , size , rotation ))
280+
249281 def _process_trading_events (self , at_baropen , append ):
250282 """"""
251283 while True :
@@ -257,9 +289,9 @@ def _process_trading_events(self, at_baropen, append):
257289 except IndexError :
258290 break
259291 else :
260- #if event.type == 'MARKET':
261- ## strategy.calculate_signals(event)
262- #port.update_timeindex(event)
292+ # if event.type == 'MARKET':
293+ # strategy.calculate_signals(event)
294+ # port.update_timeindex(event)
263295 if event .type == Event .SIGNAL :
264296 assert (not at_baropen )
265297 self .blotter .update_signal (event )
@@ -414,9 +446,9 @@ def time_aligned(self):
414446 return (self ._cur_data_context .datetime [0 ] <= self .ctx_datetime and
415447 self ._cur_data_context .last_date <= self .ctx_datetime )
416448 ## 第一根是必须运行
417- #return (self._cur_data_context.datetime[0] <= self.ctx_dt_series and
418- #self._cur_data_context.ctx_dt_series <= self.ctx_dt_series) or \
419- #self._cur_data_context.curbar == 0
449+ # return (self._cur_data_context.datetime[0] <= self.ctx_dt_series and
450+ # self._cur_data_context.ctx_dt_series <= self.ctx_dt_series) or \
451+ # self._cur_data_context.curbar == 0
420452
421453 def switch_to_strategy (self , i , j , trading = False ):
422454 self ._trading = trading
@@ -498,7 +530,7 @@ def symbol(self):
498530
499531 @property
500532 def curbar (self ):
501- """ 当前是第几根k线, 从0开始 """
533+ """ 当前是第几根k线, 从1开始 """
502534 if self .on_bar :
503535 return self .step + 1
504536 else :
@@ -717,6 +749,12 @@ def profit(self, contract=None):
717749 #return
718750 pass
719751
752+ def plot_line (self , name , x , y , styles , lw = 1 , ms = 10 ):
753+ self ._cur_strategy_context .plot_line (name , x - 1 , float (y ), styles , lw , ms )
754+
755+ def plot_text (self , name , x , y , text , color = 'black' , size = 10 , rotation = 0 ):
756+ self ._cur_strategy_context .plot_text (name , x - 1 , float (y ), text , color , size , rotation )
757+
720758 def day_profit (self , contract = None ):
721759 """ 当前持仓的浮动盈亏 """
722760 #if not self._trading:
0 commit comments