diff --git a/Indicators/.samples.json b/Indicators/.samples.json index c65cb17..4550c97 100644 --- a/Indicators/.samples.json +++ b/Indicators/.samples.json @@ -428,6 +428,9 @@ { "name": "ToggleButtonEventArgs Sample" }, + { + "name": "Trading Panel Sample" + }, { "name": "TradingFromIndicators Sample" }, diff --git a/Indicators/Trading Panel Sample/Trading Panel Sample.sln b/Indicators/Trading Panel Sample/Trading Panel Sample.sln new file mode 100644 index 0000000..5dcbb1f --- /dev/null +++ b/Indicators/Trading Panel Sample/Trading Panel Sample.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30011.22 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Trading Panel Sample", "Trading Panel Sample\Trading Panel Sample.csproj", "{b7a7a3f2-374a-41e2-97d3-a27390202a75}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {b7a7a3f2-374a-41e2-97d3-a27390202a75}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {b7a7a3f2-374a-41e2-97d3-a27390202a75}.Debug|Any CPU.Build.0 = Debug|Any CPU + {b7a7a3f2-374a-41e2-97d3-a27390202a75}.Release|Any CPU.ActiveCfg = Release|Any CPU + {b7a7a3f2-374a-41e2-97d3-a27390202a75}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Indicators/Trading Panel Sample/Trading Panel Sample/Trading Panel Sample.cs b/Indicators/Trading Panel Sample/Trading Panel Sample/Trading Panel Sample.cs new file mode 100644 index 0000000..cdb1171 --- /dev/null +++ b/Indicators/Trading Panel Sample/Trading Panel Sample/Trading Panel Sample.cs @@ -0,0 +1,61 @@ +// ------------------------------------------------------------------------------------------------- +// +// This code is a cTrader Algo API example. +// +// 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. +// +// This sample indicator displays a simple trading panel. +// +// For a detailed tutorial on creating this indicator, see this video: https://www.youtube.com/watch?v=IJu7zxl5DA0 +// +// ------------------------------------------------------------------------------------------------- + +using System; +using cAlgo.API; +using cAlgo.API.Collections; +using cAlgo.API.Indicators; +using cAlgo.API.Internals; + +namespace cAlgo +{ + [Indicator(AccessRights = AccessRights.None, IsOverlay = true)] + public class TradingPanel : Indicator + { + protected override void Initialize() + { + var tradeButtonBuy = new Button + { + Text = "Buy", + ForegroundColor = Color.White, + BackgroundColor = Color.Green, + Height = 25, + Width = 75, + Margin = 2 + }; + + tradeButtonBuy.Click += args => ExecuteMarketOrderAsync(TradeType.Buy, SymbolName, 1000); + + var tradeButtonSell = new Button + { + Text = "Sell", + ForegroundColor = Color.White, + BackgroundColor = Color.Red, + Height = 25, + Width = 75, + Margin = 2 + }; + tradeButtonSell.Click += args => ExecuteMarketOrderAsync(TradeType.Sell, SymbolName, 1000); + + var grid = new Grid(1, 2); + grid.AddChild(tradeButtonBuy, 0,0); + grid.AddChild(tradeButtonSell, 0, 1); + Chart.AddControl(grid); + } + + public override void Calculate(int index) + { + // Calculate value at specified index + // Result[index] = + } + } +} \ No newline at end of file diff --git a/Indicators/Trading Panel Sample/Trading Panel Sample/Trading Panel Sample.csproj b/Indicators/Trading Panel Sample/Trading Panel Sample/Trading Panel Sample.csproj new file mode 100644 index 0000000..51ac844 --- /dev/null +++ b/Indicators/Trading Panel Sample/Trading Panel Sample/Trading Panel Sample.csproj @@ -0,0 +1,9 @@ + + + net6.0 + + + + + + diff --git a/Robots/.samples.json b/Robots/.samples.json index 740ec5d..af224c8 100644 --- a/Robots/.samples.json +++ b/Robots/.samples.json @@ -38,6 +38,12 @@ { "name": "Bollinger Bands Sample" }, + { + "name": "cBots Adds Indicator Sample" + }, + { + "name": "cBots Starts cBot Sample" + }, { "name": "Center Of Gravity Sample" }, @@ -299,6 +305,9 @@ { "name": "Volume ROC Sample" }, + { + "name": "Web Sockets Sample" + }, { "name": "WebSocket Sample" }, diff --git a/Robots/Web Sockets Sample/Web Sockets Sample.sln b/Robots/Web Sockets Sample/Web Sockets Sample.sln new file mode 100644 index 0000000..94dc2d2 --- /dev/null +++ b/Robots/Web Sockets Sample/Web Sockets Sample.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30011.22 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Web Sockets Sample", "Web Sockets Sample\Web Sockets Sample.csproj", "{cd5f4d41-2fe3-40da-b9b2-389ea0689fc4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {cd5f4d41-2fe3-40da-b9b2-389ea0689fc4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {cd5f4d41-2fe3-40da-b9b2-389ea0689fc4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {cd5f4d41-2fe3-40da-b9b2-389ea0689fc4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {cd5f4d41-2fe3-40da-b9b2-389ea0689fc4}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Robots/Web Sockets Sample/Web Sockets Sample/Web Sockets Sample.cs b/Robots/Web Sockets Sample/Web Sockets Sample/Web Sockets Sample.cs new file mode 100644 index 0000000..f86c35f --- /dev/null +++ b/Robots/Web Sockets Sample/Web Sockets Sample/Web Sockets Sample.cs @@ -0,0 +1,53 @@ +// ------------------------------------------------------------------------------------------------- +// +// This code is a cTrader Algo API example. +// +// 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. +// +// This sample cBot subscribes to a symbol price feed and streams the prices. +// +// For a detailed tutorial on creating this cBot, see this video: https://www.youtube.com/watch?v=y5ARwEEXSLI +// +// ------------------------------------------------------------------------------------------------- + +using System; +using cAlgo.API; +using cAlgo.API.Collections; +using cAlgo.API.Indicators; +using cAlgo.API.Internals; + +namespace cAlgo.Robots +{ + [Robot(AccessRights = AccessRights.None, AddIndicators = true)] + public class WebSocketsExample : Robot + { + private WebSocketClient _webSocketClient = new WebSocketClient(); + private readonly Uri _targetUri = new Uri("wss://marketdata.tradermade.com/feedadv"); + + protected override void OnStart() + { + _webSocketClient.Connect(_targetUri); + + _webSocketClient.TextReceived += _webSocketClient_TextReceived; + + var data = "{\"userKey\":\"PasteStreamingKeyHere\", \"symbol\":\"EURUSD\"}"; + + _webSocketClient.Send(data); + } + + private void _webSocketClient_TextReceived(WebSocketClientTextReceivedEventArgs obj) + { + Print(obj.Text.Replace("{", "").Replace("}", "").ToString()); + } + + protected override void OnTick() + { + // Handle price updates here + } + + protected override void OnStop() + { + _webSocketClient.Close(WebSocketClientCloseStatus.NormalClosure); + } + } +} \ No newline at end of file diff --git a/Robots/Web Sockets Sample/Web Sockets Sample/Web Sockets Sample.csproj b/Robots/Web Sockets Sample/Web Sockets Sample/Web Sockets Sample.csproj new file mode 100644 index 0000000..51ac844 --- /dev/null +++ b/Robots/Web Sockets Sample/Web Sockets Sample/Web Sockets Sample.csproj @@ -0,0 +1,9 @@ + + + net6.0 + + + + + + diff --git a/Robots/cBot Adds Indicator Sample/cBot Adds Indicator Sample.sln b/Robots/cBot Adds Indicator Sample/cBot Adds Indicator Sample.sln new file mode 100644 index 0000000..dc26e63 --- /dev/null +++ b/Robots/cBot Adds Indicator Sample/cBot Adds Indicator Sample.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30011.22 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "cBot Adds Indicator Sample", "cBot Adds Indicator Sample\cBot Adds Indicator Sample.csproj", "{1751be77-fe78-4e12-b558-7fb418ed6b44}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1751be77-fe78-4e12-b558-7fb418ed6b44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1751be77-fe78-4e12-b558-7fb418ed6b44}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1751be77-fe78-4e12-b558-7fb418ed6b44}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1751be77-fe78-4e12-b558-7fb418ed6b44}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Robots/cBot Adds Indicator Sample/cBot Adds Indicator Sample/cBot Adds Indicator Sample.cs b/Robots/cBot Adds Indicator Sample/cBot Adds Indicator Sample/cBot Adds Indicator Sample.cs new file mode 100644 index 0000000..92265c5 --- /dev/null +++ b/Robots/cBot Adds Indicator Sample/cBot Adds Indicator Sample/cBot Adds Indicator Sample.cs @@ -0,0 +1,93 @@ +// ------------------------------------------------------------------------------------------------- +// +// This code is a cTrader Algo API example. +// +// 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. +// +// This sample cBot adds two moving averages for trading to a chart. +// +// For a detailed tutorial on creating this cBot, see this video: https://www.youtube.com/watch?v=DUzdEt30OSE +// +// ------------------------------------------------------------------------------------------------- + + +using cAlgo.API; +using cAlgo.API.Indicators; + +namespace cAlgo +{ + [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None, AddIndicators = true)] + public class SampleTrendcBot : Robot + { + [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)] + public double Quantity { get; set; } + + [Parameter("MA Type", Group = "Moving Average")] + public MovingAverageType MAType { get; set; } + + [Parameter("Source", Group = "Moving Average")] + public DataSeries SourceSeries { get; set; } + + [Parameter("Slow Periods", Group = "Moving Average", DefaultValue = 10)] + public int SlowPeriods { get; set; } + + [Parameter("Fast Periods", Group = "Moving Average", DefaultValue = 5)] + public int FastPeriods { get; set; } + + private MovingAverage slowMa; + private MovingAverage fastMa; + private const string label = "Sample Trend cBot"; + + ChartIndicator _indicator1; + ChartIndicator _indicator2; + + protected override void OnStart() + { + fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType); + slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType); + + _indicator1 = Chart.Indicators.Add("Simple Moving Average", SourceSeries, FastPeriods, MAType); + _indicator2 = Chart.Indicators.Add("Simple Moving Average", SourceSeries, SlowPeriods, MAType); + + _indicator1.Lines[0].Color = Color.Red; + _indicator1.Lines[0].Thickness = 3; + } + + protected override void OnBarClosed() + { + Chart.Indicators.Remove(_indicator1); + Chart.Indicators.Remove(_indicator2); + } + + protected override void OnTick() + { + var longPosition = Positions.Find(label, SymbolName, TradeType.Buy); + var shortPosition = Positions.Find(label, SymbolName, TradeType.Sell); + + var currentSlowMa = slowMa.Result.Last(0); + var currentFastMa = fastMa.Result.Last(0); + var previousSlowMa = slowMa.Result.Last(1); + var previousFastMa = fastMa.Result.Last(1); + + if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa && longPosition == null) + { + if (shortPosition != null) + ClosePosition(shortPosition); + + ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInUnits, label); + } + else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && shortPosition == null) + { + if (longPosition != null) + ClosePosition(longPosition); + + ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, label); + } + } + + private double VolumeInUnits + { + get { return Symbol.QuantityToVolumeInUnits(Quantity); } + } + } +} \ No newline at end of file diff --git a/Robots/cBot Adds Indicator Sample/cBot Adds Indicator Sample/cBot Adds Indicator Sample.csproj b/Robots/cBot Adds Indicator Sample/cBot Adds Indicator Sample/cBot Adds Indicator Sample.csproj new file mode 100644 index 0000000..51ac844 --- /dev/null +++ b/Robots/cBot Adds Indicator Sample/cBot Adds Indicator Sample/cBot Adds Indicator Sample.csproj @@ -0,0 +1,9 @@ + + + net6.0 + + + + + + diff --git a/Robots/cBot Starts cBot Sample/cBot Starts cBot Sample.sln b/Robots/cBot Starts cBot Sample/cBot Starts cBot Sample.sln new file mode 100644 index 0000000..400d7b2 --- /dev/null +++ b/Robots/cBot Starts cBot Sample/cBot Starts cBot Sample.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30011.22 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "cBot Starts cBot Sample", "cBot Starts cBot Sample\cBot Starts cBot Sample.csproj", "{1751be77-fe78-4e12-b558-7fb418ed6b44}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1751be77-fe78-4e12-b558-7fb418ed6b44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1751be77-fe78-4e12-b558-7fb418ed6b44}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1751be77-fe78-4e12-b558-7fb418ed6b44}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1751be77-fe78-4e12-b558-7fb418ed6b44}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Robots/cBot Starts cBot Sample/cBot Starts cBot Sample/cBot Starts cBot Sample.cs b/Robots/cBot Starts cBot Sample/cBot Starts cBot Sample/cBot Starts cBot Sample.cs new file mode 100644 index 0000000..1cbd613 --- /dev/null +++ b/Robots/cBot Starts cBot Sample/cBot Starts cBot Sample/cBot Starts cBot Sample.cs @@ -0,0 +1,63 @@ +// ------------------------------------------------------------------------------------------------- +// +// This code is a cTrader Algo API example. +// +// 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. +// +// This sample cBot adds and starts two other cBots based on a certain logic. +// +// For a detailed tutorial on creating this cBot, see this video: https://www.youtube.com/watch?v=DUzdEt30OSE +// +// ------------------------------------------------------------------------------------------------- + + +using System; +using cAlgo.API; +using cAlgo.API.Collections; +using cAlgo.API.Indicators; +using cAlgo.API.Internals; + +namespace cAlgo.Robots +{ + [Robot(AccessRights = AccessRights.None, AddIndicators = true)] + public class AddcBots : Robot + { + + ChartRobot _robot1; + ChartRobot _robot2; + + protected override void OnStart() + { + _robot1 = Chart.Robots.Add("Sample Trend cBot", 0.01, MovingAverageType.Simple, Bars.ClosePrices, 10, 5); + _robot2 = Chart.Robots.Add("Sample Trend cBot", 0.01, MovingAverageType.Simple, Bars.ClosePrices, 12, 7); + + Chart.Robots.RobotStarted += ChartRobots_RobotStarted; + } + + private void ChartRobots_RobotStarted(ChartRobotStartedEventArgs obj) + { + Print ("Robot Started"); + + } + + protected override void OnBarClosed() + { + if (_robot1.State == RobotState.Stopped) + { + _robot1.Start(); + _robot2.Stop(); + } + + else if (_robot1.State == RobotState.Running) + { + _robot2.Start(); + _robot1.Stop(); + } + } + + protected override void OnStop() + { + // Handle cBot stop here + } + } +} \ No newline at end of file diff --git a/Robots/cBot Starts cBot Sample/cBot Starts cBot Sample/cBot Starts cBot Sample.csproj b/Robots/cBot Starts cBot Sample/cBot Starts cBot Sample/cBot Starts cBot Sample.csproj new file mode 100644 index 0000000..51ac844 --- /dev/null +++ b/Robots/cBot Starts cBot Sample/cBot Starts cBot Sample/cBot Starts cBot Sample.csproj @@ -0,0 +1,9 @@ + + + net6.0 + + + + + +