Message on a Discord Channel everytime a trade is Opened or Closed and an Update every hour
Message on a Discord Channel everytime a trade is Opened or Closed and an Update every hour
28 Jul 2022, 17:22
Hello, I am sending update messages to a discord channel from the VPS where Ctrader is working.
The problem I found was related to the Net version, needed to use net 5 so that there is a Ctrader Bot code and a c sharp app to be compiled in Visual Studio with net 5 target.
Apparently the issue is with Newonsoft Json serialization, I took a shortcut, console app net5 external, the code is right below.
This is the C sharp console app NET 5 , you need to create a webhook in discord and get the token.
Has to be saved in the correct path and with the right name... "C:\\DiscordMessage\\ConsoleApp1.exe
set the timeframe to 5 min before running to get also the updates, if you set the timeframe to >5minutes you will only get Open and Closed updates.
// C sharp NET 5 console app to send a message on Discord webhook
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string token = "https://discord.com/api/webhooks/YOURTOKEN";
WebRequest wr = (HttpWebRequest)WebRequest.Create(token);
wr.ContentType = "application/json";
wr.Method = "POST";
using (var sw = new StreamWriter(wr.GetRequestStream()))
{
string json = JsonConvert.SerializeObject(new
{
username = "KeepCalm_Bot",
content= string.Join(" ", args) ,
});
sw.Write(json);
}
var response=(HttpWebResponse)wr.GetResponse();
}
}
}
Now you need a new Ctrader Bot that send the updates and position open and close messages using the console app.
// Position Open, Close and Update message webhook on discord by paolo panicali july 2022
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
public class Telegram : Robot
{
string token, text;
protected override void OnStart()
{
Positions.Opened += PositionsOnOpened;
Positions.Closed += PositionsOnClosed;
foreach (var position in Positions)
{
text = "Discord Bot Assistant started // OPEN POSITION : ////--- BOTNAME: " + position.Label + "--- Symbol: " + position.SymbolName + "---- ENTRY: " + position.EntryPrice + "---- SL: " + position.StopLoss + "---- TP: " + position.TakeProfit + "---- TIME UTC: " + position.EntryTime + "---- PROFIT: " + position.Pips + " pips";
string filename = "C:\\DiscordMessage\\ConsoleApp1.exe";
var proc = System.Diagnostics.Process.Start(filename, text);
proc.CloseMainWindow();
proc.Close();
System.Threading.Thread.Sleep(5000);
Print(text);
}
}
protected override void OnBar()
{
if (Bars.OpenTimes.LastValue.Minute == 5)
{
OpenPositionsUpdate();
}
}
// Send discord position update messsage
protected void OpenPositionsUpdate()
{
foreach (var position in Positions)
{
text = "UPDATE CURRENTLY OPEN POSITION : ////--- BOTNAME: " + position.Label + "--- Symbol: " + position.SymbolName + "---- ENTRY: " + position.EntryPrice + "---- SL: " + position.StopLoss + "---- TP: " + position.TakeProfit + "---- TIME UTC: " + position.EntryTime + "---- PROFIT: " + position.Pips + " pips";
string filename = "C:\\DiscordMessage\\ConsoleApp1.exe";
var proc = System.Diagnostics.Process.Start(filename, text);
proc.CloseMainWindow();
proc.Close();
System.Threading.Thread.Sleep(5000);
Print(text);
}
}
private void PositionsOnOpened(PositionOpenedEventArgs args)
{
text = "NEW TRADE OPENED : ////--- BOTNAME: " + args.Position.Label + "--- Symbol: " + args.Position.SymbolName + "---- ENTRY: " + args.Position.EntryPrice + "---- SL: " + args.Position.StopLoss + "---- TP: " + args.Position.TakeProfit + "---- TIME UTC: " + args.Position.EntryTime + "---- PROFIT: " + args.Position.Pips + " pips";
string filename = "C:\\DiscordMessage\\ConsoleApp1.exe";
var proc = System.Diagnostics.Process.Start(filename, text);
proc.CloseMainWindow();
proc.Close();
System.Threading.Thread.Sleep(5000);
Print(text);
}
private void PositionsOnClosed(PositionClosedEventArgs args)
{
var position = args.Position;
text = "CLOSED TRADE : ////--- BOTNAME: " + position.Label + "--- Symbol: " + position.SymbolName + "---- ENTRY: " + position.EntryPrice + "---- SL: " + position.StopLoss + "---- TP: " + position.TakeProfit + "---- TIME UTC: " + position.EntryTime + "---- PROFIT: " + position.Pips + " pips";
string filename = "C:\\DiscordMessage\\ConsoleApp1.exe";
var proc = System.Diagnostics.Process.Start(filename, text);
proc.CloseMainWindow();
proc.Close();
System.Threading.Thread.Sleep(5000);
Print(text);
}
}
}
Hope this will help. Bye.