Use a Timer to set up a loop that keeps cBot operation stopped for the set time.
Use a Timer to set up a loop that keeps cBot operation stopped for the set time.
30 Jul 2023, 09:46
PanagiotisCharalampous said:
Hi tradermatrix,
It depends on what are you trying to achieve. In principle, you can set your timer to a fast interval e.g. one second, and then implement time checks on custom intervals. See an example below
using System;using System.Linq;using cAlgo.API;using cAlgo.API.Indicators;using cAlgo.API.Internals;using cAlgo.Indicators;namespace cAlgo.Robots{ [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class NewcBot : Robot { [Parameter(DefaultValue = 30)] public int Interval1 { get; set; } [Parameter(DefaultValue = 50)] public int Interval2 { get; set; } DateTime _interval1; DateTime _interval2; protected override void OnStart() { _interval1 = Server.Time; _interval2 = Server.Time; Timer.TimerTick += Timer_TimerTick; Timer.Start(1); } private void Timer_TimerTick() { if (_interval1.AddSeconds(Interval1) < Server.Time) { Print("Interval 1 Triggered"); _interval1 = Server.Time; } if (_interval2.AddSeconds(Interval2) < Server.Time) { Print("Interval 2 Triggered"); _interval2 = Server.Time; } } protected override void OnTick() { // Put your core logic here } protected override void OnStop() { // Put your deinitialization logic here } }}
Best Regards,
Panagiotis
This is the post I'm referring to:
https://ctrader.com/forum/cbot-support/24285
Good evening, I have a request similar to this user's post:
I programmed a cBot that works on a single instance of cTrader simultaneously on all 28 Forex pairs.
The cBot is able to recognize if there are "correlated" pairs to avoid having to open multiple operations, for example: if a EURUSD operation is already open, it will no longer open operations on all pairs that contain EUR or USD.
This logic works well but only if there is already an open trade.
When there is no operation open, it happens that, if the opening conditions occur simultaneously, the various instances of the cBot open the first operation and very often the presence of pairs that should not have been opened is seen.
I tried inserting a timer inside the code that sets the seconds of delay depending on the pair it's running on taking the value from a list but unfortunately I can't get the result I want because the timer doesn't make the requested pause but it loops in just one tick.
This must be done "OnBar" every time.
I may have some suggestions or correction of the code that I report in the attachment.
This is a part of code:
…..…..
…..…..
var Loop = 0;
string AUDCAD = "AUDCAD";
string AUDCHF = "AUDCHF";
string AUDJPY = "AUDJPY";
string AUDNZD = "AUDNZD";
string AUDUSD = "AUDUSD";
string CADCHF = "CADCHF";
string CADJPY = "CADJPY";
string CHFJPY = "CHFJPY";
string EURAUD = "EURAUD";
string EURCAD = "EURCAD";
string EURCHF = "EURCHF";
string EURGBP = "EURGBP";
string EURJPY = "EURJPY";
string EURNZD = "EURNZD";
string EURUSD = "EURUSD";
string GBPAUD = "GBPAUD";
string GBPCAD = "GBPCAD";
string GBPCHF = "GBPCHF";
string GBPJPY = "GBPJPY";
string GBPNZD = "GBPNZD";
string GBPUSD = "GBPUSD";
string NZDCAD = "NZDCAD";
string NZDCHF = "NZDCHF";
string NZDJPY = "NZDJPY";
string NZDUSD = "NZDUSD";
string USDCAD = "USDCAD";
string USDCHF = "USDCHF";
string USDJPY = "USDJPY";
if (Symbol.Name.Contains(AUDCAD))
{
Loop = 7;
}
if (Symbol.Name.Contains(AUDCHF))
{
Loop = 11;
}
if (Symbol.Name.Contains(AUDJPY))
{
Loop = 13;
}
if (Symbol.Name.Contains(AUDNZD))
{
Loop = 17;
}
if (Symbol.Name.Contains(AUDUSD))
{
Loop = 19;
}
if (Symbol.Name.Contains(CADCHF))
{
Loop = 23;
}
if (Symbol.Name.Contains(CADJPY))
{
Loop = 29;
}
if (Symbol.Name.Contains(CHFJPY))
{
Loop = 31;
}
if (Symbol.Name.Contains(EURAUD))
{
Loop = 37;
}
if (Symbol.Name.Contains(EURCAD))
{
Loop = 41;
}
if (Symbol.Name.Contains(EURCHF))
{
Loop = 43;
}
if (Symbol.Name.Contains(EURGBP))
{
Loop = 47;
}
if (Symbol.Name.Contains(EURJPY))
{
Loop = 53;
}
if (Symbol.Name.Contains(EURNZD))
{
Loop = 59;
}
if (Symbol.Name.Contains(EURUSD))
{
Loop = 61;
}
if (Symbol.Name.Contains(GBPAUD))
{
Loop = 67;
}
if (Symbol.Name.Contains(GBPCAD))
{
Loop = 71;
}
if (Symbol.Name.Contains(GBPCHF))
{
Loop = 73;
}
if (Symbol.Name.Contains(GBPJPY))
{
Loop = 79;
}
if (Symbol.Name.Contains(GBPNZD))
{
Loop = 83;
}
if (Symbol.Name.Contains(GBPUSD))
{
Loop = 89;
}
if (Symbol.Name.Contains(NZDCAD))
{
Loop = 97;
}
if (Symbol.Name.Contains(NZDCHF))
{
Loop = 101;
}
if (Symbol.Name.Contains(NZDJPY))
{
Loop = 103;
}
if (Symbol.Name.Contains(NZDUSD))
{
Loop = 107;
}
if (Symbol.Name.Contains(USDCAD))
{
Loop = 109;
}
if (Symbol.Name.Contains(USDCHF))
{
Loop = 113;
}
if (Symbol.Name.Contains(USDJPY))
{
Loop = 127;
}
var Account_Positions = Positions.Count;
Timer.Start(Loop);
for (int i = 0; i < Loop; i++)
{
// This is just to test if the loop works, after testing it should be disabled
Print("Loop n.: " + Loop + " >> " + i + "");
}
Timer.Stop();
…….
…..
Once the cBot has "entered" the Loop, it should remain still for the set time but instead immediately exits and executes the rest of the code.
Surely I made a mistake in setting this part of the code but despite trying to write it in another way, I still can't find the solution!