Skip to content

Commit 7668e8d

Browse files
authored
CNT-708-717-721 Algo samples (spotware#31)
1 parent 45c6679 commit 7668e8d

File tree

14 files changed

+406
-0
lines changed

14 files changed

+406
-0
lines changed

Indicators/.samples.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,9 @@
428428
{
429429
"name": "ToggleButtonEventArgs Sample"
430430
},
431+
{
432+
"name": "Trading Panel Sample"
433+
},
431434
{
432435
"name": "TradingFromIndicators Sample"
433436
},
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}") = "Trading Panel Sample", "Trading Panel Sample\Trading Panel Sample.csproj", "{b7a7a3f2-374a-41e2-97d3-a27390202a75}"
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+
{b7a7a3f2-374a-41e2-97d3-a27390202a75}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{b7a7a3f2-374a-41e2-97d3-a27390202a75}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{b7a7a3f2-374a-41e2-97d3-a27390202a75}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{b7a7a3f2-374a-41e2-97d3-a27390202a75}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// -------------------------------------------------------------------------------------------------
2+
//
3+
// This code is a cTrader Algo API example.
4+
//
5+
// The code is provided as a sample only and does not guarantee any particular outcome or profit of any kind. Use it at your own risk.
6+
//
7+
// This sample indicator displays a simple trading panel.
8+
//
9+
// For a detailed tutorial on creating this indicator, see this video: https://www.youtube.com/watch?v=IJu7zxl5DA0
10+
//
11+
// -------------------------------------------------------------------------------------------------
12+
13+
using System;
14+
using cAlgo.API;
15+
using cAlgo.API.Collections;
16+
using cAlgo.API.Indicators;
17+
using cAlgo.API.Internals;
18+
19+
namespace cAlgo
20+
{
21+
[Indicator(AccessRights = AccessRights.None, IsOverlay = true)]
22+
public class TradingPanel : Indicator
23+
{
24+
protected override void Initialize()
25+
{
26+
var tradeButtonBuy = new Button
27+
{
28+
Text = "Buy",
29+
ForegroundColor = Color.White,
30+
BackgroundColor = Color.Green,
31+
Height = 25,
32+
Width = 75,
33+
Margin = 2
34+
};
35+
36+
tradeButtonBuy.Click += args => ExecuteMarketOrderAsync(TradeType.Buy, SymbolName, 1000);
37+
38+
var tradeButtonSell = new Button
39+
{
40+
Text = "Sell",
41+
ForegroundColor = Color.White,
42+
BackgroundColor = Color.Red,
43+
Height = 25,
44+
Width = 75,
45+
Margin = 2
46+
};
47+
tradeButtonSell.Click += args => ExecuteMarketOrderAsync(TradeType.Sell, SymbolName, 1000);
48+
49+
var grid = new Grid(1, 2);
50+
grid.AddChild(tradeButtonBuy, 0,0);
51+
grid.AddChild(tradeButtonSell, 0, 1);
52+
Chart.AddControl(grid);
53+
}
54+
55+
public override void Calculate(int index)
56+
{
57+
// Calculate value at specified index
58+
// Result[index] =
59+
}
60+
}
61+
}
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>

Robots/.samples.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
{
3939
"name": "Bollinger Bands Sample"
4040
},
41+
{
42+
"name": "cBots Adds Indicator Sample"
43+
},
44+
{
45+
"name": "cBots Starts cBot Sample"
46+
},
4147
{
4248
"name": "Center Of Gravity Sample"
4349
},
@@ -299,6 +305,9 @@
299305
{
300306
"name": "Volume ROC Sample"
301307
},
308+
{
309+
"name": "Web Sockets Sample"
310+
},
302311
{
303312
"name": "WebSocket Sample"
304313
},
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}") = "Web Sockets Sample", "Web Sockets Sample\Web Sockets Sample.csproj", "{cd5f4d41-2fe3-40da-b9b2-389ea0689fc4}"
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+
{cd5f4d41-2fe3-40da-b9b2-389ea0689fc4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{cd5f4d41-2fe3-40da-b9b2-389ea0689fc4}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{cd5f4d41-2fe3-40da-b9b2-389ea0689fc4}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{cd5f4d41-2fe3-40da-b9b2-389ea0689fc4}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// -------------------------------------------------------------------------------------------------
2+
//
3+
// This code is a cTrader Algo API example.
4+
//
5+
// The code is provided as a sample only and does not guarantee any particular outcome or profit of any kind. Use it at your own risk.
6+
//
7+
// This sample cBot subscribes to a symbol price feed and streams the prices.
8+
//
9+
// For a detailed tutorial on creating this cBot, see this video: https://www.youtube.com/watch?v=y5ARwEEXSLI
10+
//
11+
// -------------------------------------------------------------------------------------------------
12+
13+
using System;
14+
using cAlgo.API;
15+
using cAlgo.API.Collections;
16+
using cAlgo.API.Indicators;
17+
using cAlgo.API.Internals;
18+
19+
namespace cAlgo.Robots
20+
{
21+
[Robot(AccessRights = AccessRights.None, AddIndicators = true)]
22+
public class WebSocketsExample : Robot
23+
{
24+
private WebSocketClient _webSocketClient = new WebSocketClient();
25+
private readonly Uri _targetUri = new Uri("wss://marketdata.tradermade.com/feedadv");
26+
27+
protected override void OnStart()
28+
{
29+
_webSocketClient.Connect(_targetUri);
30+
31+
_webSocketClient.TextReceived += _webSocketClient_TextReceived;
32+
33+
var data = "{\"userKey\":\"PasteStreamingKeyHere\", \"symbol\":\"EURUSD\"}";
34+
35+
_webSocketClient.Send(data);
36+
}
37+
38+
private void _webSocketClient_TextReceived(WebSocketClientTextReceivedEventArgs obj)
39+
{
40+
Print(obj.Text.Replace("{", "").Replace("}", "").ToString());
41+
}
42+
43+
protected override void OnTick()
44+
{
45+
// Handle price updates here
46+
}
47+
48+
protected override void OnStop()
49+
{
50+
_webSocketClient.Close(WebSocketClientCloseStatus.NormalClosure);
51+
}
52+
}
53+
}
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>
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}") = "cBot Adds Indicator Sample", "cBot Adds Indicator Sample\cBot Adds Indicator Sample.csproj", "{1751be77-fe78-4e12-b558-7fb418ed6b44}"
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+
{1751be77-fe78-4e12-b558-7fb418ed6b44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{1751be77-fe78-4e12-b558-7fb418ed6b44}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{1751be77-fe78-4e12-b558-7fb418ed6b44}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{1751be77-fe78-4e12-b558-7fb418ed6b44}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// -------------------------------------------------------------------------------------------------
2+
//
3+
// This code is a cTrader Algo API example.
4+
//
5+
// The code is provided as a sample only and does not guarantee any particular outcome or profit of any kind. Use it at your own risk.
6+
//
7+
// This sample cBot adds two moving averages for trading to a chart.
8+
//
9+
// For a detailed tutorial on creating this cBot, see this video: https://www.youtube.com/watch?v=DUzdEt30OSE
10+
//
11+
// -------------------------------------------------------------------------------------------------
12+
13+
14+
using cAlgo.API;
15+
using cAlgo.API.Indicators;
16+
17+
namespace cAlgo
18+
{
19+
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None, AddIndicators = true)]
20+
public class SampleTrendcBot : Robot
21+
{
22+
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
23+
public double Quantity { get; set; }
24+
25+
[Parameter("MA Type", Group = "Moving Average")]
26+
public MovingAverageType MAType { get; set; }
27+
28+
[Parameter("Source", Group = "Moving Average")]
29+
public DataSeries SourceSeries { get; set; }
30+
31+
[Parameter("Slow Periods", Group = "Moving Average", DefaultValue = 10)]
32+
public int SlowPeriods { get; set; }
33+
34+
[Parameter("Fast Periods", Group = "Moving Average", DefaultValue = 5)]
35+
public int FastPeriods { get; set; }
36+
37+
private MovingAverage slowMa;
38+
private MovingAverage fastMa;
39+
private const string label = "Sample Trend cBot";
40+
41+
ChartIndicator _indicator1;
42+
ChartIndicator _indicator2;
43+
44+
protected override void OnStart()
45+
{
46+
fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
47+
slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
48+
49+
_indicator1 = Chart.Indicators.Add("Simple Moving Average", SourceSeries, FastPeriods, MAType);
50+
_indicator2 = Chart.Indicators.Add("Simple Moving Average", SourceSeries, SlowPeriods, MAType);
51+
52+
_indicator1.Lines[0].Color = Color.Red;
53+
_indicator1.Lines[0].Thickness = 3;
54+
}
55+
56+
protected override void OnBarClosed()
57+
{
58+
Chart.Indicators.Remove(_indicator1);
59+
Chart.Indicators.Remove(_indicator2);
60+
}
61+
62+
protected override void OnTick()
63+
{
64+
var longPosition = Positions.Find(label, SymbolName, TradeType.Buy);
65+
var shortPosition = Positions.Find(label, SymbolName, TradeType.Sell);
66+
67+
var currentSlowMa = slowMa.Result.Last(0);
68+
var currentFastMa = fastMa.Result.Last(0);
69+
var previousSlowMa = slowMa.Result.Last(1);
70+
var previousFastMa = fastMa.Result.Last(1);
71+
72+
if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa && longPosition == null)
73+
{
74+
if (shortPosition != null)
75+
ClosePosition(shortPosition);
76+
77+
ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInUnits, label);
78+
}
79+
else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && shortPosition == null)
80+
{
81+
if (longPosition != null)
82+
ClosePosition(longPosition);
83+
84+
ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, label);
85+
}
86+
}
87+
88+
private double VolumeInUnits
89+
{
90+
get { return Symbol.QuantityToVolumeInUnits(Quantity); }
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)