Skip to content

CNT-794-807: Algo samples #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Robots/.samples.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
{
"name": "Directional Movement System Sample"
},
{
"name": "Discord Message Example"
},
{
"name": "Donchian Channel Sample"
},
Expand Down Expand Up @@ -167,6 +170,9 @@
{
"name": "Partial Close Sample"
},
{
"name": "Patterns Strategy Sample"
},
{
"name": "Pending Order Cancelation Sample"
},
Expand Down Expand Up @@ -233,6 +239,9 @@
{
"name": "Relative Strength Index Sample"
},
{
"name": "RSI Reversal Strategy Sample"
},
{
"name": "Running Mode Sample"
},
Expand Down
22 changes: 22 additions & 0 deletions Robots/Discord Message Example/Discord Message Example.sln
Original file line number Diff line number Diff line change
@@ -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}") = "Discord Message Example", "Discord Message Example\Discord Message Example.csproj", "{4fff20d4-4ec9-4c51-ae35-32c172c2cb2b}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4fff20d4-4ec9-4c51-ae35-32c172c2cb2b}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4fff20d4-4ec9-4c51-ae35-32c172c2cb2b}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4fff20d4-4ec9-4c51-ae35-32c172c2cb2b}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4fff20d4-4ec9-4c51-ae35-32c172c2cb2b}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="cTrader.Automate" Version="*" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// -------------------------------------------------------------------------------------------------
//
// 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 example cBot send messages to a Discord channel.
//
// For a detailed tutorial on creating this cBot, watch the video at: https://youtu.be/NhEeySAKZUo
//
// -------------------------------------------------------------------------------------------------

using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using Discord.WebSocket;
using Discord;

namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None, AddIndicators = true)]
public class DiscordMessageExample : Robot
{
[Parameter("Discord Bot Token")]
public string BotToken { get; set; }

[Parameter("Discord Channel ID")]
public string ChannelID { get; set; }

DiscordSocketClient _discordSocketClient;
IMessageChannel _channel;

protected override void OnStart()
{
_discordSocketClient = new DiscordSocketClient();
_discordSocketClient.LoginAsync(TokenType.Bot, BotToken);
_discordSocketClient.StartAsync();

var channelID = Convert.ToUInt64(ChannelID);
_channel = _discordSocketClient.GetChannelAsync(channelID).Result as IMessageChannel;
_channel.SendMessageAsync("Example cBot Started");
}

protected override void OnTick()
{
// Handle price updates here
}

protected override void OnStop()
{
// Handle cBot stop here
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="cTrader.Automate" Version="*" />
</ItemGroup>
<ItemGroup>
<Reference Include="Discord.Net.Commands, Version=3.16.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\..\..\..\..\..\..\Discord DLL Files\Discord.Net.Commands.dll</HintPath>
</Reference>
<Reference Include="Discord.Net.Core, Version=3.16.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\..\..\..\..\..\..\Discord DLL Files\Discord.Net.Core.dll</HintPath>
</Reference>
<Reference Include="Discord.Net.Interactions, Version=3.16.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\..\..\..\..\..\..\Discord DLL Files\Discord.Net.Interactions.dll</HintPath>
</Reference>
<Reference Include="Discord.Net.Rest, Version=3.16.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\..\..\..\..\..\..\Discord DLL Files\Discord.Net.Rest.dll</HintPath>
</Reference>
<Reference Include="Discord.Net.Webhook, Version=3.16.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\..\..\..\..\..\..\Discord DLL Files\Discord.Net.Webhook.dll</HintPath>
</Reference>
<Reference Include="Discord.Net.WebSocket, Version=3.16.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\..\..\..\..\..\..\Discord DLL Files\Discord.Net.WebSocket.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
22 changes: 22 additions & 0 deletions Robots/Patterns Strategy Sample/Patterns Strategy Sample.sln
Original file line number Diff line number Diff line change
@@ -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}") = "Patterns Strategy Sample", "Patterns Strategy Sample\Patterns Strategy Sample.csproj", "{b757f66c-537d-4605-b428-bef6881d45a0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{b757f66c-537d-4605-b428-bef6881d45a0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{b757f66c-537d-4605-b428-bef6881d45a0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{b757f66c-537d-4605-b428-bef6881d45a0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{b757f66c-537d-4605-b428-bef6881d45a0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// -------------------------------------------------------------------------------------------------
//
// 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 example cBot trades the hammer pattern for long entries and the hanging man pattern for short entries.
//
// For a detailed tutorial on creating this cBot, watch the video at: https://youtu.be/mEoIvP11Z1U
//
// -------------------------------------------------------------------------------------------------

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 PatternsStrategySample : Robot
{
[Parameter(DefaultValue = 1000)]
public double Volume { get; set; }

[Parameter(DefaultValue = 10)]
public double StopLoss { get; set; }

[Parameter(DefaultValue = 10)]
public double TakeProfit { get; set; }

protected override void OnStart()
{

}

protected override void OnBarClosed()
{
if (Bars.Last(0).Close == Bars.Last(0).High &&
(Bars.Last(0).Close - Bars.Last(0).Open) < (Bars.Last(0).Close - Bars.Last(0).Low) * 0.2)
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, InstanceId, StopLoss, TakeProfit);
}

if (Bars.Last(0).Close == Bars.Last(0).Low &&
(Bars.Last(0).Open - Bars.Last(0).Close) < (Bars.Last(0).High - Bars.Last(0).Close) * 0.2)
{
ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, InstanceId, StopLoss, TakeProfit);
}

}

protected override void OnStop()
{

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="cTrader.Automate" Version="*" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -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}") = "RSI Reversal Strategy Sample", "RSI Reversal Strategy Sample\RSI Reversal Strategy Sample.csproj", "{e72d7681-531b-4abe-8c9a-e2a74e52eb7a}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{e72d7681-531b-4abe-8c9a-e2a74e52eb7a}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{e72d7681-531b-4abe-8c9a-e2a74e52eb7a}.Debug|Any CPU.Build.0 = Debug|Any CPU
{e72d7681-531b-4abe-8c9a-e2a74e52eb7a}.Release|Any CPU.ActiveCfg = Release|Any CPU
{e72d7681-531b-4abe-8c9a-e2a74e52eb7a}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// -------------------------------------------------------------------------------------------------
//
// 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 example cBot implements a strategy based on the Relative Strength Index (RSI) indicator reversal.
//
// For a detailed tutorial on creating this cBot, watch the video at: https://youtu.be/mEoIvP11Z1U
//
// -------------------------------------------------------------------------------------------------

using System;
using System.Linq;
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 RSIReversalStrategySample : Robot
{
[Parameter(DefaultValue = 30)]
public int BuyLevel { get; set; }

[Parameter(DefaultValue = 70)]
public int SellLevel { get; set; }

private RelativeStrengthIndex _rsi;

protected override void OnStart()
{
_rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 14);
}

protected override void OnBarClosed()
{
if (_rsi.Result.LastValue < BuyLevel)
{
if (Positions.Count == 0)
ExecuteMarketOrder(TradeType.Buy, SymbolName, 1000);
foreach (var position in Positions.Where(p => p.TradeType == TradeType.Sell))
{
position.Close();
}

}

else if (_rsi.Result.LastValue > SellLevel)
{
if (Positions.Count == 0)
ExecuteMarketOrder(TradeType.Sell, SymbolName, 1000);
foreach (var position in Positions.Where(p => p.TradeType == TradeType.Buy))
{
position.Close();
}
}
}

protected override void OnStop()
{

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="cTrader.Automate" Version="*" />
</ItemGroup>
</Project>