2017-01-11 7 views
0

こんにちは私はフォーラムを初めて使いました。これは初めての投稿です。私はExcelでvbaの新人ですが、ThinkorSwimでthinkscriptを書いています。範囲棒グラフの作成方法

誰もが範囲の株価チャートに精通していれば、それは何が起こっているのですか。

私は折れ線グラフのコードを見つけましたが、それを使用していますが、これはいつでも価格がどこにあるかに基づいています。私はこの線グラフを変更して、範囲の上または下にあるときに値をプロットして、芯がない燭台のチャートに似ているようにしたい。データがその範囲に入ると、その範囲で新しい高値または低値が作成されるたびに更新されます。範囲を事前に設定する必要があります(例:50ティック)。範囲を超えたら、次の範囲にプロットされたデータを元に戻して、処理を繰り返します。時間と日付は無視し、価格行動に基づいてプロットするだけです。

誰にもアイデアはありますか?

Option Explicit 

'Update the values between the quotes here: 
Private Const sChartWSName = "Chart" 
Private Const sSourceWSName = "Sheet1" 
Private Const sTableName = "tblValues" 
Public RunTime As Double 

Private Sub Chart_Setup() 
'Create the structure needed to preserve and chart data 
    Dim wsChart As Worksheet 
    Dim lstObject As ListObject 
    Dim cht As Chart 
    Dim shp As Button 
    'Create sheet if necessary 
    Set wsChart = Worksheets.Add 
    wsChart.Name = sChartWSName 
    'Set up listobject to hold data 
    With wsChart 
     .Range("A1").Value = "Time" 
     .Range("B1").Value = "Value" 
     Set lstObject = .ListObjects.Add(_ 
         SourceType:=xlSrcRange, _ 
         Source:=.Range("A1:B1"), _ 
         xllistobjecthasheaders:=xlYes) 
     lstObject.Name = sTableName 
     .Range("A2").NumberFormat = "h:mm:ss AM/PM (mmm-d)" 
     .Columns("A:A").ColumnWidth = 25 
     .Select 
    End With 
    'Create the chart 
    With ActiveSheet 
     .Shapes.AddChart.Select 
     Set cht = ActiveChart 
     With cht 
      .ChartType = xlLine 
      .SetSourceData Source:=Range(sTableName) 
      .PlotBy = xlColumns 
      .Legend.Delete 
      .Axes(xlCategory).CategoryType = xlCategoryScale 
      With .SeriesCollection(1).Format.Range 
       .Visible = msoTrue 
       .Weight = 1.25 
      End With 
     End With 
    End With 
    'Add buttons to start/stop the routine 
    Set shp = ActiveSheet.Buttons.Add(242.25, 0, 83.75, 33.75) 
    With shp 
     .OnAction = "Chart_Initialize" 
     .Characters.Text = "Restart Plotting" 
    End With 
    Set shp = ActiveSheet.Buttons.Add(326.25, 0, 83.75, 33.75) 
    With shp 
     .OnAction = "Chart_Stop" 
     .Characters.Text = "Stop Plotting" 
    End With 
End Sub 

Public Sub Chart_Initialize() 
'Initialize the routine 
Dim wsTarget As Worksheet 
Dim lstObject As ListObject 

'Make sure worksheet exists 
On Error Resume Next 
Set wsTarget = Worksheets(sChartWSName) 
If Err.Number <> 0 Then 
    Call Chart_Setup 
    Set wsTarget = Worksheets(sChartWSName) 
End If 
On Error GoTo 0 

'Check if chart data exists 
With Worksheets(sChartWSName) 
    Set lstObject = .ListObjects(sTableName) 
    If lstObject.ListRows.Count > 0 Then 
     Select Case MsgBox("You already have data. Do you want to clear it and start fresh?", vbYesNoCancel, "Clear out old data?") 

      Case Is = vbYes 
       'User wants to clear the data 
       lstObject.DataBodyRange.Delete 

      Case Is = vbCancel 
       'User cancelled so exit routine 
       Exit Sub 

      Case Is = vbNo 
       'User just wants to append to existing table 
     End Select 
    End If 

    'Begin appending 
    Call Chart_AppendData 
End With 
End Sub 

Private Sub Chart_AppendData() 
'Append data to the chart table 
Dim lstObject As ListObject 
Dim lRow As Long 

With Worksheets(sChartWSName) 
    Set lstObject = .ListObjects(sTableName) 
    If lstObject.ListRows.Count = 0 Then 
     lRow = .Range("A1").End(xlDown).Row 
    End If 
    If lRow = 0 Then 
     lRow = .Range("A" & .Rows.Count).End(xlUp).Offset(1, 0).Row 
    End If 
    If lRow > 2 Then 
     If .Range("B" & lRow - 1).Value = Worksheets(sSourceWSName).Range("C10").Value Then 
      'Data is a match, so do nothing 
     Else 
      'Data needs appending 
      .Range("A" & lRow).Value = CDate(Now) 
      .Range("B" & lRow).Value = Worksheets(sSourceWSName).Range("C10").Value 
     End If 
    Else 
      'Data needs appending 
      .Range("A" & lRow).Value = CDate(Now) 
      .Range("B" & lRow).Value = Worksheets(sSourceWSName).Range("C10").Value 
    End If 
End With 

RunTime = Now + TimeValue("00:00:01") 
Application.OnTime RunTime, "Chart_AppendData" 
End Sub 

Public Sub Chart_Stop() 
'Stop capturing data 
On Error Resume Next 
Application.OnTime EarliestTime:=RunTime, Procedure:="Chart_AppendData", Schedule:=False 
End Sub 
+0

チャートの範囲を変更するとき、私にはうまくいく方法の1つは、コンテンツをフィルタリングすることです。グラフの範囲を設定すると、A)シートをフィルタリングし、B)パラメータに基づいて範囲を選択し、C)グラフで出力します。フィルタリング/非表示にすると、広告申込情報がグラフに表示されなくなります。 – Cyril

+0

例を表示できますか? –

+0

私が得たものをあなたに見せるためにチャートをアップロードしようとしていますが、それを行う方法がわかりません –

答えて

0

データやフィルタのあなたのシートを取る...例は次のようになります。

Columns("A:C").Sort key1:=Range("C2"), _ 
    order1:=xlAscending, header:=xlYes 

ソート情報:https://msdn.microsoft.com/en-us/library/office/ff840646.aspx

次に、あなたの希望の範囲を選択するように定義することができます。

Dim High1 as integer 
Dim Low1 as integer 

High1 = Match(Max(B:B),B:B) 'This isn't tested, just an idea 
Low1 = Match(Max(B:B)+50,B:B) 'Again, not tested 

及びそれらの定義されたパラメータを使用して:

.Range(Cells(High1,1),Cells(Low1,2).Select 

これはアイデアを与えるべきである(評価する必要性を修正するためのどこパラメータ)カラムAは、x軸であり、Bはy軸であると仮定するとHigh1/Low1では、最大値が発生する行をどのように定義するかを作業できます。

次に、使用するデータ範囲を選択して、必要なグラフのCreateObjectを作成します。

+0

ます。Option Explicit ます。Private Sub Workbook_BeforeClose(ブール値としてキャンセル) コールChart_Stop End Subの –

+0

をリフレッシュ 「STOPワークブック。それはあまりにも多くの文字を言う。 msgであなたに送ることができる方法はありますか? –

+0

私は元のコードを上記の元の質問に入れました。あなたが送ったコードはどこに置くべきですか? –

関連する問題