Skip to content

Commit 63ded1c

Browse files
committed
ActiveFrameChanged Sample added
1 parent 7579ecc commit 63ded1c

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30011.22
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ActiveFrameChanged Sample", "ActiveFrameChanged Sample\ActiveFrameChanged Sample.csproj", "{84085667-1adc-4ccd-9135-8be1880e28ee}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{84085667-1adc-4ccd-9135-8be1880e28ee}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{84085667-1adc-4ccd-9135-8be1880e28ee}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{84085667-1adc-4ccd-9135-8be1880e28ee}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{84085667-1adc-4ccd-9135-8be1880e28ee}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net6.0</TargetFramework>
4+
</PropertyGroup>
5+
6+
<ItemGroup>
7+
<PackageReference Include="cTrader.Automate" Version="*" />
8+
</ItemGroup>
9+
</Project>

0 commit comments

Comments
 (0)