是这样的,目前我在做一个实时股票行情的接口,实时行情通过一个行情接收器接收实时数据,然后保存在本地文件中,也就是说,实时行情是通过这个接收器实时更新文件的。
服务器端,我去定时读取这个实时文件,然后生成1分钟、5分钟、15分钟、周、月、年的k线,然后再生成技术指标。
目前股票大约4000多只,每只有一个k线+7个指标,k线和指标分别对应9个周期,发现实时处理延迟很厉害,并且需要先生成k线,才能生成指标,而且周期之间也有先后顺序,5分钟依赖一分钟,30分钟以来15分钟,周、月、年依赖日线。
目前的方案是,我先定义了8个线程,每个线程里面
Dim queueMACD As New ConcurrentQueue(Of WorkItemIndex) Dim queueKDJ As New ConcurrentQueue(Of WorkItemIndex) Dim queueRSI As New ConcurrentQueue(Of WorkItemIndex) Dim queueCCI As New ConcurrentQueue(Of WorkItemIndex) Dim queueWR As New ConcurrentQueue(Of WorkItemIndex) Dim queueBOLL As New ConcurrentQueue(Of WorkItemIndex) Dim queueBIAS As New ConcurrentQueue(Of WorkItemIndex)
Sub ThMakeMACDReal() Do If queueMACD.Count > 0 Then Dim workItem As WorkItemIndex Dim result = queueMACD.TryDequeue(workItem) If result = True Then Me.Invoke(New Action(Sub() lblMACD.Text = queueMACD.Count End Sub)) log.Debug("MACD队列剩余" & queueMACD.Count & "个") workItem.DelegateMakeIndexRt.Invoke(workItem.Args(0)) End If End If Thread.Sleep(1000) Loop Until bStopDoRealThread = True End Sub Sub ThMakeKDJReal() Do If queueKDJ.Count > 0 Then Dim workItem As WorkItemIndex Dim result = queueKDJ.TryDequeue(workItem) If result = True Then Me.Invoke(New Action(Sub() lblKDJ.Text = queueKDJ.Count End Sub)) log.Debug("KDJ队列剩余" & queueKDJ.Count & "个") workItem.DelegateMakeIndexRt.Invoke(workItem.Args(0)) End If End If Thread.Sleep(1000) Loop Until bStopDoRealThread = True End Sub Sub ThMakeRSIReal() Do If queueRSI.Count > 0 Then Dim workItem As WorkItemIndex Dim result = queueRSI.TryDequeue(workItem) If result = True Then Me.Invoke(New Action(Sub() lblRSI.Text = queueRSI.Count End Sub)) log.Debug("RSI队列剩余" & queueRSI.Count & "个") workItem.DelegateMakeIndexRt.Invoke(workItem.Args(0)) End If End If Thread.Sleep(1000) Loop Until bStopDoRealThread = True End Sub Sub ThMakeCCIReal() Do If queueCCI.Count > 0 Then Dim workItem As WorkItemIndex Dim result = queueCCI.TryDequeue(workItem) If result = True Then Me.Invoke(New Action(Sub() lblCCI.Text = queueCCI.Count End Sub)) log.Debug("CCI队列剩余" & queueCCI.Count & "个") workItem.DelegateMakeIndexRt.Invoke(workItem.Args(0)) End If End If Thread.Sleep(1000) Loop Until bStopDoRealThread = True End Sub Sub ThMakeWRReal() Do If queueWR.Count > 0 Then Dim workItem As WorkItemIndex Dim result = queueWR.TryDequeue(workItem) If result = True Then Me.Invoke(New Action(Sub() lblWR.Text = queueWR.Count End Sub)) log.Debug("WR队列剩余" & queueWR.Count & "个") workItem.DelegateMakeIndexRt.Invoke(workItem.Args(0)) End If End If Thread.Sleep(1000) Loop Until bStopDoRealThread = True End Sub Sub ThMakeBOLLReal() Do If queueBOLL.Count > 0 Then Dim workItem As WorkItemIndex Dim result = queueBOLL.TryDequeue(workItem) If result = True Then Me.Invoke(New Action(Sub() lblBOLL.Text = queueBOLL.Count End Sub)) log.Debug("BOLL队列剩余" & queueBOLL.Count & "个") workItem.DelegateMakeIndexRt.Invoke(workItem.Args(0)) End If End If Thread.Sleep(1000) Loop Until bStopDoRealThread = True End Sub Sub ThMakeBIASReal() Do If queueBIAS.Count > 0 Then Dim workItem As WorkItemIndex Dim result = queueBIAS.TryDequeue(workItem) If result = True Then Me.Invoke(New Action(Sub() lblBIAS.Text = queueBIAS.Count End Sub)) log.Debug("BIAS队列剩余" & queueBIAS.Count & "个") workItem.DelegateMakeIndexRt.Invoke(workItem.Args(0)) End If End If Thread.Sleep(1000) Loop Until bStopDoRealThread = True End Sub
'处理实时MACD指标 Dim workItemMACD = New WorkItemIndex(New delegateMakeIndex_RT(AddressOf makeMACD_RT), {gi}) queueMACD.Enqueue(workItemMACD) '处理实时KDJ指标 Dim workItemKDJ = New WorkItemIndex(New delegateMakeIndex_RT(AddressOf makeKDJ_RT), {gi}) queueKDJ.Enqueue(workItemKDJ) '处理实时RSI指标 Dim workItemRSI = New WorkItemIndex(New delegateMakeIndex_RT(AddressOf makeRSI_RT), {gi}) queueRSI.Enqueue(workItemRSI) '处理实时CCI指标 Dim workItemCCI = New WorkItemIndex(New delegateMakeIndex_RT(AddressOf makeCCI_RT), {gi}) queueCCI.Enqueue(workItemCCI) '处理实时WR指标 Dim workItemWR = New WorkItemIndex(New delegateMakeIndex_RT(AddressOf makeWR_RT), {gi}) queueWR.Enqueue(workItemWR) '处理实时BOLL指标 Dim workItemBOLL = New WorkItemIndex(New delegateMakeIndex_RT(AddressOf makeBOLL_RT), {gi}) queueBOLL.Enqueue(workItemBOLL) '处理实时BIAS指标 Dim workItemBIAS = New WorkItemIndex(New delegateMakeIndex_RT(AddressOf makeBIAS_RT), {gi}) queueBIAS.Enqueue(workItemBIAS)