Skip to content

Commit e92ef76

Browse files
Merge pull request spotware#14 from spotware/am/XT-16089
Add new plugin samples.
2 parents a95efe6 + 6df33b9 commit e92ef76

26 files changed

+1312
-128
lines changed

Plugins/.samples.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,52 @@
55
{
66
"name": "All placements"
77
},
8+
{
9+
"name": "ActiveFrameChanged Sample"
10+
},
11+
{
12+
"name": "AlgoRegistry Sample"
13+
},
14+
{
15+
"name": "BacktestingInPlugins Sample"
16+
},
17+
{
18+
"name": "ChartId Sample"
19+
},
20+
{
21+
"name": "ChartIndicators Sample"
22+
},
23+
{
24+
"name": "ChartRobots Sample"
25+
},
26+
{
27+
"name": "Commands Sample"
28+
},
29+
{
30+
"name": "CoordinatesConversion Sample"
31+
},
32+
{
33+
"name": "Custom Frame Sample"
34+
},
35+
{
36+
"name": "IndicatorTitles Sample"
37+
},
838
{
939
"name": "Interactive WebView"
1040
},
1141
{
1242
"name": "Order by Margin"
43+
},
44+
{
45+
"name": "PositionCurrentPrice Sample"
46+
},
47+
{
48+
"name": "SmoothMouseMove Sample"
49+
},
50+
{
51+
"name": "TradeWatch Tab Sample"
52+
},
53+
{
54+
"name": "WebSocket Sample"
1355
}
1456
]

Plugins/AlgoRegistry Sample/AlgoRegistry Sample.sln

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 16
44
VisualStudioVersion = 16.0.30011.22
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AlgoRegistry Sample", "AlgoRegistry Sample\AlgoRegistry Sample.csproj", "{253c1137-a441-477f-bbdd-8dffcad26446}"
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AlgoRegistry Sample", "AlgoRegistry Sample\AlgoRegistry Sample.csproj", "{43349484-218e-4bf3-a452-0e44bf636cfe}"
77
EndProject
88
Global
99
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1010
Debug|Any CPU = Debug|Any CPU
1111
Release|Any CPU = Release|Any CPU
1212
EndGlobalSection
1313
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14-
{253c1137-a441-477f-bbdd-8dffcad26446}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15-
{253c1137-a441-477f-bbdd-8dffcad26446}.Debug|Any CPU.Build.0 = Debug|Any CPU
16-
{253c1137-a441-477f-bbdd-8dffcad26446}.Release|Any CPU.ActiveCfg = Release|Any CPU
17-
{253c1137-a441-477f-bbdd-8dffcad26446}.Release|Any CPU.Build.0 = Release|Any CPU
14+
{43349484-218e-4bf3-a452-0e44bf636cfe}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{43349484-218e-4bf3-a452-0e44bf636cfe}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{43349484-218e-4bf3-a452-0e44bf636cfe}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{43349484-218e-4bf3-a452-0e44bf636cfe}.Release|Any CPU.Build.0 = Release|Any CPU
1818
EndGlobalSection
1919
GlobalSection(SolutionProperties) = preSolution
2020
HideSolutionNode = FALSE

Plugins/AlgoRegistry Sample/AlgoRegistry Sample/AlgoRegistry Sample.cs

Lines changed: 12 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -5,99 +5,31 @@
55
// This code is intended to be used as a sample and does not guarantee any particular outcome or
66
// profit of any kind. Use it at your own risk.
77
//
8-
// This sample adds a new block into the ASP. The block displays statistics about the number of
9-
// different types of algorithms installed on the user's machine. Information in the block is
10-
// updated every second to make sure that it is always accurate.
8+
// This sample adds a trade watch tab, and uses AlgoRegistry API to show stats about installed algo types.
119
//
1210
// -------------------------------------------------------------------------------------------------
1311

14-
15-
using System;
16-
using System.Collections.Generic;
17-
using System.Linq;
18-
using System.Text;
1912
using cAlgo.API;
20-
using cAlgo.API.Collections;
21-
using cAlgo.API.Indicators;
22-
using cAlgo.API.Internals;
2313

2414
namespace cAlgo.Plugins
2515
{
2616
[Plugin(AccessRights = AccessRights.None)]
2717
public class AlgoRegistrySample : Plugin
2818
{
29-
// Declaring the TextBlock for displaying data about
30-
// custom indicators
31-
private TextBlock _customIndicatorsBlock = new TextBlock
32-
{
33-
FontSize = 12,
34-
FontWeight = FontWeight.Bold,
35-
TextAlignment = TextAlignment.Left,
36-
Padding = new Thickness(5, 5, 5, 5),
37-
};
38-
39-
// Declaring the TextBlock for displaying data about
40-
// built-in indicators
41-
private TextBlock _indicatorsBlock = new TextBlock
42-
{
43-
FontSize = 12,
44-
FontWeight = FontWeight.Bold,
45-
TextAlignment = TextAlignment.Left,
46-
Padding = new Thickness(5, 5, 5, 5),
47-
};
48-
49-
// Declaring the TextBlock for displaying data about
50-
// cBots
51-
private TextBlock _robotsBlock = new TextBlock
52-
{
53-
FontSize = 12,
54-
FontWeight = FontWeight.Bold,
55-
TextAlignment = TextAlignment.Left,
56-
Padding = new Thickness(5, 5, 5, 5),
57-
};
58-
59-
// Declaring the TextBlock for displaying data about
60-
// plugins
61-
private TextBlock _pluginsBlock = new TextBlock
62-
{
63-
FontSize = 12,
64-
FontWeight = FontWeight.Bold,
65-
TextAlignment = TextAlignment.Left,
66-
Padding = new Thickness(5, 5, 5, 5),
67-
};
68-
69-
// Declaring the Grid to store custom controls
70-
private Grid _grid = new Grid(4, 1);
71-
7219
protected override void OnStart()
7320
{
74-
// Adding a new block into the ASP and adding
75-
// the Grid with all TextBlocks as a child
76-
var aspBlock = Asp.SymbolTab.AddBlock("Algo Registry");
77-
aspBlock.IsExpanded = true;
78-
aspBlock.Height = 200;
79-
80-
_grid.AddChild(_robotsBlock, 0, 0);
81-
_grid.AddChild(_indicatorsBlock, 1, 0);
82-
_grid.AddChild(_customIndicatorsBlock, 2, 0);
83-
_grid.AddChild(_pluginsBlock, 3, 0);
21+
var tradeWatchTab = TradeWatch.AddTab("Algo Registry");
8422

85-
aspBlock.Child = _grid;
23+
var panel = new StackPanel
24+
{
25+
Orientation = Orientation.Horizontal,
26+
HorizontalAlignment = HorizontalAlignment.Center,
27+
};
28+
29+
panel.AddChild(new AlgoStatsControl(AlgoRegistry) {Margin = 10, VerticalAlignment = VerticalAlignment.Top});
30+
panel.AddChild(new AlgoTypeInfoControl(AlgoRegistry) {Margin = 10, VerticalAlignment = VerticalAlignment.Top});
8631

87-
// Starting the Timer to refresh information
88-
// in all TextBlocks
89-
Timer.Start(TimeSpan.FromSeconds(1));
32+
tradeWatchTab.Child = panel;
9033
}
91-
92-
protected override void OnTimer()
93-
{
94-
// Using the AlgoRegistry to attain information about the
95-
// different AlgoKinds installed on the user's machine
96-
_robotsBlock.Text = $"cBots: {AlgoRegistry.GetCount(AlgoKind.Robot)}";
97-
_customIndicatorsBlock.Text = $"Custom indicators: {AlgoRegistry.GetCount(AlgoKind.CustomIndicator)}";
98-
_indicatorsBlock.Text = $"Indicators: {AlgoRegistry.GetCount(AlgoKind.StandardIndicator)}";
99-
_pluginsBlock.Text = $"Plugins: {AlgoRegistry.GetCount(AlgoKind.Plugin)}";
100-
}
101-
102-
}
34+
}
10335
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System.Linq;
2+
using cAlgo.API;
3+
4+
namespace cAlgo.Plugins;
5+
6+
public class AlgoStatsControl: CustomControl
7+
{
8+
private const string FontFamily = "Calibri";
9+
10+
private readonly AlgoRegistry _algoRegistry;
11+
private readonly TextBlock _algosCountTextBlock;
12+
private readonly TextBlock _customIndicatorsCountTextBlock;
13+
private readonly TextBlock _standardIndicatorsCountTextBlock;
14+
private readonly TextBlock _botsCountTextBlock;
15+
private readonly TextBlock _pluginsCountTextBlock;
16+
17+
public AlgoStatsControl(AlgoRegistry algoRegistry)
18+
{
19+
_algoRegistry = algoRegistry;
20+
21+
var panel = new Grid(6, 2);
22+
23+
var titleTextBlock = GetTextBlock("Algo Stats");
24+
25+
titleTextBlock.HorizontalAlignment = HorizontalAlignment.Center;
26+
27+
panel.AddChild(titleTextBlock, 0, 0, 1, 2);
28+
29+
panel.AddChild(GetTextBlock("Algos #"), 1, 0);
30+
31+
_algosCountTextBlock = GetTextBlock();
32+
33+
panel.AddChild(_algosCountTextBlock, 1, 1);
34+
35+
panel.AddChild(GetTextBlock("Standard Indicators #"), 2, 0);
36+
37+
_standardIndicatorsCountTextBlock = GetTextBlock();
38+
39+
panel.AddChild(_standardIndicatorsCountTextBlock, 2, 1);
40+
41+
panel.AddChild(GetTextBlock("Custom Indicators #"), 3, 0);
42+
43+
_customIndicatorsCountTextBlock = GetTextBlock();
44+
45+
panel.AddChild(_customIndicatorsCountTextBlock, 3, 1);
46+
47+
panel.AddChild(GetTextBlock("cBots #"), 4, 0);
48+
49+
_botsCountTextBlock = GetTextBlock();
50+
51+
panel.AddChild(_botsCountTextBlock, 4, 1);
52+
53+
panel.AddChild(GetTextBlock("Plugins #"), 5, 0);
54+
55+
_pluginsCountTextBlock = GetTextBlock();
56+
57+
panel.AddChild(_pluginsCountTextBlock, 5, 1);
58+
59+
AddChild(panel);
60+
61+
Populate();
62+
63+
_algoRegistry.AlgoTypeInstalled += _ => Populate();
64+
_algoRegistry.AlgoTypeDeleted += _ => Populate();
65+
}
66+
67+
private void Populate()
68+
{
69+
_algosCountTextBlock.Text = _algoRegistry.Count.ToString();
70+
_botsCountTextBlock.Text = _algoRegistry.Count(type => type.AlgoKind == AlgoKind.Robot).ToString();
71+
_customIndicatorsCountTextBlock.Text = _algoRegistry.Count(type => type.AlgoKind == AlgoKind.CustomIndicator).ToString();
72+
_standardIndicatorsCountTextBlock.Text = _algoRegistry.Count(type => type.AlgoKind == AlgoKind.StandardIndicator).ToString();
73+
_pluginsCountTextBlock.Text = _algoRegistry.Count(type => type.AlgoKind == AlgoKind.Plugin).ToString();
74+
}
75+
76+
private TextBlock GetTextBlock(string text = null) => new()
77+
{
78+
Margin = 3,
79+
FontSize = 20,
80+
FontWeight = FontWeight.Bold,
81+
FontFamily = FontFamily,
82+
Text = text
83+
};
84+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using System.Linq;
2+
using cAlgo.API;
3+
4+
namespace cAlgo.Plugins;
5+
6+
public class AlgoTypeInfoControl: CustomControl
7+
{
8+
private const string FontFamily = "Calibri";
9+
10+
private readonly AlgoRegistry _algoRegistry;
11+
private readonly ComboBox _algoTypesComboBox;
12+
private readonly TextBlock _algoTypeKindTextBlock;
13+
private readonly TextBlock _algoTypeParametersTextBlock;
14+
private readonly TextBlock _algoTypeOutputsTextBlock;
15+
16+
public AlgoTypeInfoControl(AlgoRegistry algoRegistry)
17+
{
18+
_algoRegistry = algoRegistry;
19+
20+
var panel = new Grid(5, 2) {BackgroundColor = Color.Gray};
21+
22+
var titleTextBlock = GetTextBlock("Algo Type Info");
23+
24+
titleTextBlock.HorizontalAlignment = HorizontalAlignment.Center;
25+
26+
panel.AddChild(titleTextBlock, 0, 0, 1, 2);
27+
28+
panel.AddChild(GetTextBlock("Types"), 1, 0);
29+
30+
_algoTypesComboBox = new ComboBox
31+
{
32+
Margin = 3,
33+
FontSize = 20,
34+
FontWeight = FontWeight.Bold,
35+
FontFamily = FontFamily,
36+
Padding = 2
37+
};
38+
39+
panel.AddChild(_algoTypesComboBox, 1, 1);
40+
41+
panel.AddChild(GetTextBlock("Kind"), 2, 0);
42+
43+
_algoTypeKindTextBlock = GetTextBlock();
44+
45+
panel.AddChild(_algoTypeKindTextBlock, 2, 1);
46+
47+
panel.AddChild(GetTextBlock("Parameters"), 3, 0);
48+
49+
_algoTypeParametersTextBlock = GetTextBlock();
50+
51+
panel.AddChild(_algoTypeParametersTextBlock, 3, 1);
52+
53+
panel.AddChild(GetTextBlock("Outputs"), 4, 0);
54+
55+
_algoTypeOutputsTextBlock = GetTextBlock();
56+
57+
panel.AddChild(_algoTypeOutputsTextBlock, 4, 1);
58+
59+
AddChild(panel);
60+
61+
_algoTypesComboBox.SelectedItemChanged += _ => OnAlgoTypesComboBoxSelectedItemChanged();
62+
63+
PopulateTypes();
64+
65+
_algoRegistry.AlgoTypeInstalled += _ => PopulateTypes();
66+
_algoRegistry.AlgoTypeDeleted += _ => PopulateTypes();
67+
}
68+
69+
private void OnAlgoTypesComboBoxSelectedItemChanged()
70+
{
71+
if (_algoRegistry.Get(_algoTypesComboBox.SelectedItem) is not { } algoType)
72+
{
73+
_algoTypeKindTextBlock.Text = null;
74+
_algoTypeParametersTextBlock.Text = null;
75+
_algoTypeOutputsTextBlock.Text = null;
76+
77+
return;
78+
}
79+
80+
_algoTypeKindTextBlock.Text = algoType.AlgoKind.ToString();
81+
_algoTypeParametersTextBlock.Text = algoType switch
82+
{
83+
IndicatorType indicatorType => string.Join(", ", indicatorType.Parameters.Select(p => p.Name)),
84+
RobotType robotType => string.Join(", ", robotType.Parameters.Select(p => p.Name)),
85+
_ => null
86+
};
87+
_algoTypeOutputsTextBlock.Text = algoType switch
88+
{
89+
IndicatorType indicatorType => string.Join(", ", indicatorType.Outputs.Select(p => p.Name)),
90+
_ => null
91+
};
92+
}
93+
94+
private void PopulateTypes()
95+
{
96+
foreach (var algoType in _algoRegistry)
97+
{
98+
_algoTypesComboBox.AddItem(algoType.Name);
99+
}
100+
101+
_algoTypesComboBox.SelectedItem = _algoRegistry.FirstOrDefault()?.Name;
102+
103+
OnAlgoTypesComboBoxSelectedItemChanged();
104+
}
105+
106+
private TextBlock GetTextBlock(string text = null) => new()
107+
{
108+
Margin = 3,
109+
FontSize = 20,
110+
FontWeight = FontWeight.Bold,
111+
FontFamily = FontFamily,
112+
Text = text
113+
};
114+
}

0 commit comments

Comments
 (0)