Skip to content

Commit dabad6d

Browse files
authored
CNT-794-807: Algo samples (spotware#33)
1 parent 2bdd6f1 commit dabad6d

File tree

11 files changed

+313
-0
lines changed

11 files changed

+313
-0
lines changed

Robots/.samples.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@
8383
{
8484
"name": "Directional Movement System Sample"
8585
},
86+
{
87+
"name": "Discord Message Example"
88+
},
8689
{
8790
"name": "Donchian Channel Sample"
8891
},
@@ -167,6 +170,9 @@
167170
{
168171
"name": "Partial Close Sample"
169172
},
173+
{
174+
"name": "Patterns Strategy Sample"
175+
},
170176
{
171177
"name": "Pending Order Cancelation Sample"
172178
},
@@ -233,6 +239,9 @@
233239
{
234240
"name": "Relative Strength Index Sample"
235241
},
242+
{
243+
"name": "RSI Reversal Strategy Sample"
244+
},
236245
{
237246
"name": "Running Mode Sample"
238247
},
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}") = "Discord Message Example", "Discord Message Example\Discord Message Example.csproj", "{4fff20d4-4ec9-4c51-ae35-32c172c2cb2b}"
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+
{4fff20d4-4ec9-4c51-ae35-32c172c2cb2b}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{4fff20d4-4ec9-4c51-ae35-32c172c2cb2b}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{4fff20d4-4ec9-4c51-ae35-32c172c2cb2b}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{4fff20d4-4ec9-4c51-ae35-32c172c2cb2b}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 example cBot send messages to a Discord channel.
8+
//
9+
// For a detailed tutorial on creating this cBot, watch the video at: https://youtu.be/NhEeySAKZUo
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+
using Discord.WebSocket;
19+
using Discord;
20+
21+
namespace cAlgo.Robots
22+
{
23+
[Robot(AccessRights = AccessRights.None, AddIndicators = true)]
24+
public class DiscordMessageExample : Robot
25+
{
26+
[Parameter("Discord Bot Token")]
27+
public string BotToken { get; set; }
28+
29+
[Parameter("Discord Channel ID")]
30+
public string ChannelID { get; set; }
31+
32+
DiscordSocketClient _discordSocketClient;
33+
IMessageChannel _channel;
34+
35+
protected override void OnStart()
36+
{
37+
_discordSocketClient = new DiscordSocketClient();
38+
_discordSocketClient.LoginAsync(TokenType.Bot, BotToken);
39+
_discordSocketClient.StartAsync();
40+
41+
var channelID = Convert.ToUInt64(ChannelID);
42+
_channel = _discordSocketClient.GetChannelAsync(channelID).Result as IMessageChannel;
43+
_channel.SendMessageAsync("Example cBot Started");
44+
}
45+
46+
protected override void OnTick()
47+
{
48+
// Handle price updates here
49+
}
50+
51+
protected override void OnStop()
52+
{
53+
// Handle cBot stop here
54+
}
55+
}
56+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.NET.Sdk">
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="cTrader.Automate" Version="*" />
8+
</ItemGroup>
9+
<ItemGroup>
10+
<Reference Include="Discord.Net.Commands, Version=3.16.0.0, Culture=neutral, PublicKeyToken=null">
11+
<HintPath>..\..\..\..\..\..\..\..\Discord DLL Files\Discord.Net.Commands.dll</HintPath>
12+
</Reference>
13+
<Reference Include="Discord.Net.Core, Version=3.16.0.0, Culture=neutral, PublicKeyToken=null">
14+
<HintPath>..\..\..\..\..\..\..\..\Discord DLL Files\Discord.Net.Core.dll</HintPath>
15+
</Reference>
16+
<Reference Include="Discord.Net.Interactions, Version=3.16.0.0, Culture=neutral, PublicKeyToken=null">
17+
<HintPath>..\..\..\..\..\..\..\..\Discord DLL Files\Discord.Net.Interactions.dll</HintPath>
18+
</Reference>
19+
<Reference Include="Discord.Net.Rest, Version=3.16.0.0, Culture=neutral, PublicKeyToken=null">
20+
<HintPath>..\..\..\..\..\..\..\..\Discord DLL Files\Discord.Net.Rest.dll</HintPath>
21+
</Reference>
22+
<Reference Include="Discord.Net.Webhook, Version=3.16.0.0, Culture=neutral, PublicKeyToken=null">
23+
<HintPath>..\..\..\..\..\..\..\..\Discord DLL Files\Discord.Net.Webhook.dll</HintPath>
24+
</Reference>
25+
<Reference Include="Discord.Net.WebSocket, Version=3.16.0.0, Culture=neutral, PublicKeyToken=null">
26+
<HintPath>..\..\..\..\..\..\..\..\Discord DLL Files\Discord.Net.WebSocket.dll</HintPath>
27+
</Reference>
28+
</ItemGroup>
29+
</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}") = "Patterns Strategy Sample", "Patterns Strategy Sample\Patterns Strategy Sample.csproj", "{b757f66c-537d-4605-b428-bef6881d45a0}"
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+
{b757f66c-537d-4605-b428-bef6881d45a0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{b757f66c-537d-4605-b428-bef6881d45a0}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{b757f66c-537d-4605-b428-bef6881d45a0}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{b757f66c-537d-4605-b428-bef6881d45a0}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 example cBot trades the hammer pattern for long entries and the hanging man pattern for short entries.
8+
//
9+
// For a detailed tutorial on creating this cBot, watch the video at: https://youtu.be/mEoIvP11Z1U
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 PatternsStrategySample : Robot
23+
{
24+
[Parameter(DefaultValue = 1000)]
25+
public double Volume { get; set; }
26+
27+
[Parameter(DefaultValue = 10)]
28+
public double StopLoss { get; set; }
29+
30+
[Parameter(DefaultValue = 10)]
31+
public double TakeProfit { get; set; }
32+
33+
protected override void OnStart()
34+
{
35+
36+
}
37+
38+
protected override void OnBarClosed()
39+
{
40+
if (Bars.Last(0).Close == Bars.Last(0).High &&
41+
(Bars.Last(0).Close - Bars.Last(0).Open) < (Bars.Last(0).Close - Bars.Last(0).Low) * 0.2)
42+
{
43+
ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, InstanceId, StopLoss, TakeProfit);
44+
}
45+
46+
if (Bars.Last(0).Close == Bars.Last(0).Low &&
47+
(Bars.Last(0).Open - Bars.Last(0).Close) < (Bars.Last(0).High - Bars.Last(0).Close) * 0.2)
48+
{
49+
ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, InstanceId, StopLoss, TakeProfit);
50+
}
51+
52+
}
53+
54+
protected override void OnStop()
55+
{
56+
57+
}
58+
}
59+
}
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}") = "RSI Reversal Strategy Sample", "RSI Reversal Strategy Sample\RSI Reversal Strategy Sample.csproj", "{e72d7681-531b-4abe-8c9a-e2a74e52eb7a}"
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+
{e72d7681-531b-4abe-8c9a-e2a74e52eb7a}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{e72d7681-531b-4abe-8c9a-e2a74e52eb7a}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{e72d7681-531b-4abe-8c9a-e2a74e52eb7a}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{e72d7681-531b-4abe-8c9a-e2a74e52eb7a}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 example cBot implements a strategy based on the Relative Strength Index (RSI) indicator reversal.
8+
//
9+
// For a detailed tutorial on creating this cBot, watch the video at: https://youtu.be/mEoIvP11Z1U
10+
//
11+
// -------------------------------------------------------------------------------------------------
12+
13+
using System;
14+
using System.Linq;
15+
using cAlgo.API;
16+
using cAlgo.API.Collections;
17+
using cAlgo.API.Indicators;
18+
using cAlgo.API.Internals;
19+
20+
namespace cAlgo.Robots
21+
{
22+
[Robot(AccessRights = AccessRights.None, AddIndicators = true)]
23+
public class RSIReversalStrategySample : Robot
24+
{
25+
[Parameter(DefaultValue = 30)]
26+
public int BuyLevel { get; set; }
27+
28+
[Parameter(DefaultValue = 70)]
29+
public int SellLevel { get; set; }
30+
31+
private RelativeStrengthIndex _rsi;
32+
33+
protected override void OnStart()
34+
{
35+
_rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 14);
36+
}
37+
38+
protected override void OnBarClosed()
39+
{
40+
if (_rsi.Result.LastValue < BuyLevel)
41+
{
42+
if (Positions.Count == 0)
43+
ExecuteMarketOrder(TradeType.Buy, SymbolName, 1000);
44+
foreach (var position in Positions.Where(p => p.TradeType == TradeType.Sell))
45+
{
46+
position.Close();
47+
}
48+
49+
}
50+
51+
else if (_rsi.Result.LastValue > SellLevel)
52+
{
53+
if (Positions.Count == 0)
54+
ExecuteMarketOrder(TradeType.Sell, SymbolName, 1000);
55+
foreach (var position in Positions.Where(p => p.TradeType == TradeType.Buy))
56+
{
57+
position.Close();
58+
}
59+
}
60+
}
61+
62+
protected override void OnStop()
63+
{
64+
65+
}
66+
}
67+
}
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)