/* We use the maximum of the high pricesof the last ten bars as the Y coordinate. */varhorizontalLine=Chart.DrawHorizontalLine("line",Bars.HighPrices.Maximum(10),Color.Red);
추세선
추세선은 차트의 특정 지점에서 시작하여 다른 지점에서 끝납니다. 추세선은 다양한 모양이나 복잡한 패턴을 만드는 데 유용합니다.
추세선을 그리려면 DrawTrendLine() 메서드를 사용하세요.
1234567
/* We draw a line from the low price of the first visible bar to the high price of the last visible bar on the chart. */vartrendLine=Chart.DrawTrendLine("line",Chart.FirstVisibleBarIndex,Bars.LowPrices[Chart.FirstVisibleBarIndex],Chart.LastVisibleBarIndex,Bars.HighPrices[Chart.LastVisibleBarIndex],Color.Red);// Alternatively, consider the following.vartrendLine=Chart.DrawTrendLine("line",Bars.OpenTimes[Chart.FirstVisibleBarIndex],Bars.LowPrices[Chart.FirstVisibleBarIndex],Bars.OpenTimes[Chart.LastVisibleBarIndex],Bars.HighPrices[Chart.LastVisibleBarIndex],Color.Red);
직사각형
직사각형을 그리려면 DrawRectangle() 메서드를 사용하세요.
1234567
varrectangle=Chart.DrawRectangle("rectangle",Chart.FirstVisibleBarIndex+1,Bars.LowPrices[Chart.FirstVisibleBarIndex+1],Chart.LastVisibleBarIndex,Bars.HighPrices[Chart.LastVisibleBarIndex],Color.Red);/* We fill the rectangle with a transparent color.By using its current color, we only change the alphachannel. */rectangle.IsFilled=true;rectangle.Color=Color.FromArgb(80,rectangle.Color);
삼각형
삼각형을 그리려면 DrawTriangle() 메서드를 사용하세요.
1 2 3 4 5 6 7 8 910111213
varmiddleX=Chart.FirstVisibleBarIndex+(Chart.LastVisibleBarIndex-Chart.FirstVisibleBarIndex)/2;varmiddleY=Bars.LowPrices[Chart.FirstVisibleBarIndex]+(Bars.HighPrices[Chart.LastVisibleBarIndex]-Bars.LowPrices[Chart.FirstVisibleBarIndex])/2;vartriangle=Chart.DrawTriangle("triangle",Chart.FirstVisibleBarIndex,Bars.LowPrices[Chart.FirstVisibleBarIndex],middleX,middleY,Chart.LastVisibleBarIndex,Bars.LowPrices[Chart.FirstVisibleBarIndex],Color.Red);// We fill the triangle with a transparent color// by using it's current color, we change only the alpha channel/* We fill the triangle with a transparent color.By using its current color, we only change the alphachannel. */triangle.IsFilled=true;triangle.Color=Color.FromArgb(80,triangle.Color);
기타 차트 객체
간결함을 위해 위의 코드 스니펫에서는 여러 다른 차트 객체를 언급하지 않았습니다. 이러한 객체에는 다음이 포함되지만 이에 국한되지 않습니다:
타원
아이콘
앤드류스 피치포크
등거리 채널
피보나치 확장
피보나치 팬
피보나치 되돌림
이러한 모든 객체는 유사한 매개변수를 받는 Draw...() 메서드를 사용하여 그릴 수 있습니다.
위험-보상
ChartRiskReward 인터페이스는 차트에서 위험-보상 객체를 프로그래밍 방식으로 생성하고 관리할 수 있는 타입을 제공합니다.
usingcAlgo.API;usingSystem.Linq;namespaceNewIndicator{[Indicator(IsOverlay = true, AccessRights = AccessRights.None)]publicclassNewIndicator:Indicator{protectedoverridevoidInitialize(){foreach(varchartObjectinChart.Objects){/* If the object is a trend line we change its Y1/2 properties */if(chartObjectisChartTrendLinetrendLine){trendLine.Y1=Chart.BottomY;trendLine.Y2=Chart.TopY;}}/* Here, we filter all objects of the 'ChartRectangle' type. */varrectangles=fromchartObjectinChart.ObjectswherechartObjectisChartRectangleselectchartObjectasChartRectangle;/* We select only the chart objects with a name that begins with "MyObjects". */varmyObjects=fromchartObjectinChart.ObjectswherechartObject.Name.StartsWith("MyObjects",System.StringComparison.OrdinalIgnoreCase)selectchartObject;/* We select only interactive objects. If an object is interactive, it will not be removed when the indicator is removed or reloaded. */varinteractiveObjects=fromchartObjectinChart.ObjectswherechartObject.IsInteractiveselectchartObject;/* We remove all objects with a name that begins with "ToRemove". */varchartObjectsCopy=Chart.Objects.ToArray();foreach(varchartObjectinchartObjectsCopy){if(chartObject.Name.StartsWith("ToRemove",System.StringComparison.OrdinalIgnoreCase)){/* Chart 'RemoveObject' gets the object name as a parameter. */Chart.RemoveObject(chartObject.Name);}}}publicoverridevoidCalculate(intindex){}}}
이벤트
차트 객체에는 객체가 그려지거나 업데이트되거나 제거될 때 알 수 있는 여러 이벤트가 있습니다:
ObjectsAdded - 하나 이상의 차트 객체가 차트에 추가될 때 트리거됩니다.
ObjectsRemoved - 하나 이상의 차트 객체가 차트에서 제거될 때 트리거됩니다.
ObjectsUpdated - 하나 이상의 차트 객체가 업데이트될 때 트리거됩니다 (사용자나 활성 지표 또는 cBot에 의해 속성이 변경됨).
ObjectsSelectionChanged - 사용자가 하나 이상의 차트 객체를 선택할 때 트리거됩니다.
ObjectHoverChanged - 마우스 커서가 차트 객체 위에 올라갈 때 트리거됩니다.
usingcAlgo.API;namespaceNewIndicator{[Indicator(IsOverlay = true, AccessRights = AccessRights.None)]publicclassNewIndicator:Indicator{privateChartVerticalLine_verticalLine;protectedoverridevoidInitialize(){_verticalLine=Chart.DrawVerticalLine("line1",Chart.LastVisibleBarIndex,Color.Red);_verticalLine.IsInteractive=true;Chart.ObjectsRemoved+=Chart_ObjectsRemoved;Print(_verticalLine.IsAlive);}privatevoidChart_ObjectsRemoved(ChartObjectsRemovedEventArgsobj){Print(_verticalLine.IsAlive);/* If the object is removed, we should rid of its reference. Otherwise, it will remain in memory. */if(_verticalLine.IsAliveisfalse){_verticalLine=null;Print("Object reference removed");}}publicoverridevoidCalculate(intindex){}}}
이 지표의 인스턴스를 실행하고 빨간 선을 마우스 오른쪽 버튼으로 클릭한 후 제거하세요. 그러면 로그에 "Object reference removed" 메시지가 출력됩니다.
(죽은) 차트 객체에 대한 참조를 유지하면 메모리 누수가 발생합니다. 가비지 컬렉션 중에 객체는 여전히 살아있는 것으로 간주되어 컬렉션 프로세스를 견딜 것입니다.
이 문제를 방지하려면 ObjectsRemoved 이벤트와 IsAlive 속성을 함께 사용하세요.
바 인덱스 또는 시간
차트 객체를 코딩할 때 X축에 대해 차트 시간 또는 바 인덱스를 사용할 수 있습니다.
우리의 의견으로는 차트 시간이 더 나은 옵션입니다. 바 인덱스를 사용할 때는 미래 값을 계획할 수 없으며 (필요한 인덱스가 아직 존재하지 않기 때문에) 바 사이의 공간을 고려할 수 없습니다. 그러나 바 인덱스는 특히 비교적 간단한 차트 객체의 경우 사용하기 더 쉬울 수 있습니다.
모든 Chart.Draw 메서드는 바 인덱스와 차트 시간에 대해 다른 오버로드를 가지고 있습니다.