1+ // -------------------------------------------------------------------------------------------------
2+ //
3+ // This code is a cTrader Algo API example.
4+ //
5+ // This code is intended to be used as a sample and does not guarantee any particular outcome or
6+ // profit of any kind. Use it at your own risk.
7+ //
8+ // This sample adds a new block into the ASP. The block displays the percentage difference between
9+ // the current price of a symbol its price a month ago; the symbol is taken from the currently active
10+ // chart frame. This is achieved by handling the ChartManager.ActiveFrameChanged event.
11+ //
12+ // -------------------------------------------------------------------------------------------------
13+
14+ using System ;
15+ using cAlgo . API ;
16+ using cAlgo . API . Collections ;
17+ using cAlgo . API . Indicators ;
18+ using cAlgo . API . Internals ;
19+
20+ namespace cAlgo . Plugins
21+ {
22+ [ Plugin ( AccessRights = AccessRights . None ) ]
23+ public class ActiveFrameChangedSample : Plugin
24+ {
25+
26+ // Declaring the necessary UI elements
27+ private Grid _grid ;
28+ private TextBlock _percentageTextBlock ;
29+ private Frame _activeFrame ;
30+
31+ protected override void OnStart ( )
32+ {
33+ // Initialising the grid and the TextBlock
34+ // displaying the percentage difference
35+ _grid = new Grid ( 1 , 1 ) ;
36+ _percentageTextBlock = new TextBlock
37+ {
38+ HorizontalAlignment = HorizontalAlignment . Center ,
39+ VerticalAlignment = VerticalAlignment . Center ,
40+ Text = "Monthly change: " ,
41+ } ;
42+
43+ _grid . AddChild ( _percentageTextBlock , 0 , 0 ) ;
44+
45+ // Initialising a new block inside the ASP
46+ // and adding the grid as a child
47+ var block = Asp . SymbolTab . AddBlock ( "Monthly Change Plugin" ) ;
48+
49+ block . Child = _grid ;
50+
51+ // Attaching a custom handler to the
52+ // ActiveFrameChanged event
53+ ChartManager . ActiveFrameChanged += ChartManager_ActiveFrameChanged ;
54+
55+ }
56+
57+ private void ChartManager_ActiveFrameChanged ( ActiveFrameChangedEventArgs obj )
58+ {
59+ if ( obj . NewFrame is ChartFrame )
60+ {
61+ // Casting the Frame into a ChartFrame
62+ var newChartFrame = obj . NewFrame as ChartFrame ;
63+
64+ // Attaining market data for the symbol for which
65+ // the currently active ChartFrame is opened
66+ var dailySeries = MarketData . GetBars ( TimeFrame . Daily , newChartFrame . Symbol . Name ) ;
67+
68+ // Calculating the monthly change and displaying it
69+ // inside the TextBlock
70+ double monthlyChange = ( newChartFrame . Symbol . Bid - dailySeries . ClosePrices [ dailySeries . ClosePrices . Count - 30 ] ) / 100 ;
71+ _percentageTextBlock . Text = $ "Monthly change: { monthlyChange } ";
72+ }
73+ }
74+
75+
76+ }
77+ }
0 commit comments