Topics
Replies
Daniel D.
30 Jan 2018, 15:41
RE:
The method should be called private void PrepareForNewEvent()@Daniel D.
Daniel D.
30 Jan 2018, 15:32
Hi,
To test the "using static" directive , the following code shows 2 versions of the same example. For anyone who would prefer not to use the message box, then replace it with a Print(..) statement and limit the AccessRights, but, for those who want to use the message box, first a reference to the “System.Windows.Forms” assembly (see the Solution Explorer).
(Version 1) The first version will compile with cAlgo, but the source code requires the static method's and property's names to be qualified with their class name (Global).
(Version 2) The second version (further below) is more succinct and I think it's neater, and it is compliant with C# and .NET. But, it will not compile with cAlgo. If you try to compile it, note that cAlgo's compiler does not even recognise the “using static” directive. Maybe the “using static” directive needs to be added in the next release of cAlgo: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-static
Thanks,
Daniel
Version 1
using System; using cAlgo.API; //using static cAlgo.Global; namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)] public class MyFirst_cBot : Robot { protected override void OnStart() { // Put your initialization logic here } protected override void OnTick() { if (DateTime.Now.AddSeconds(50) > Global.DateTimeOfNewsEvent) { Global.DateTimeOfNewsEvent = DateTime.MaxValue; PrepareForNewEvent(); } } private void PrepareForNewEvent() { // Close positions. System.Windows.Forms.MessageBox.Show( "Posistions closed due to pending news event:\r\n\r\n\t" + Global.DateTimeOfNewsEvent.ToString("HH.mm.ss") + " — " + Global.NewsEventDescription , "NEWS EVENT"); } } public static class Global { private static object mutex = new object(); private static string newsEventDescription = "Gross Domestic Product, both (QoQ) and (YoY)"; public static string NewsEventDescription { set { lock (mutex) { newsEventDescription = value; } } get { lock (mutex) { return newsEventDescription; } } } private static DateTime dtNewsEvent = DateTime.Now.AddMinutes(1); public static DateTime DateTimeOfNewsEvent { set { lock (mutex) { dtNewsEvent = value; } } get { lock (mutex) { return dtNewsEvent; } } } } }
Version 2
using System; using cAlgo.API; using static cAlgo.Global; namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)] public class MyFirst_cBot : Robot { protected override void OnStart() { // Put your initialization logic here } protected override void OnTick() { if (DateTime.Now.AddSeconds(50) > DateTimeOfNewsEvent) { DateTimeOfNewsEvent = DateTime.MaxValue; PrepareForNewEvent(); } } private void PrepareForNewEvent() { // Close positions. System.Windows.Forms.MessageBox.Show( "Posistions closed due to pending news event:\r\n\r\n\t" + DateTimeOfNewsEvent.ToString("HH.mm.ss") + " — " + NewsEventDescription , "NEWS EVENT"); } } public static class Global { private static object mutex = new object(); private static string newsEventDescription = "Gross Domestic Product, both (QoQ) and (YoY)"; public static string NewsEventDescription { set { lock (mutex) { newsEventDescription = value; } } get { lock (mutex) { return newsEventDescription; } } } private static DateTime dtNewsEvent = DateTime.Now.AddMinutes(1); public static DateTime DateTimeOfNewsEvent { set { lock (mutex) { dtNewsEvent = value; } } get { lock (mutex) { return dtNewsEvent; } } } } }
@Daniel D.
Daniel D.
30 Jan 2018, 15:47
RE: RE:
This is the first time that I have posted to this website. How do you update or delete a post?@Daniel D.