using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using ClickAlgo.NewsManager.Client;
/*-------------------------------------------------------------------------------------------------
*
* Created by ClickAlgo (c) 2024
*
* It reads signals from the ClickAlgo News Manager Robot to pause trading and closing of open positions.
* When news release is over the trading restarts automatically.
*
* THIS IS JUST A TEMPLATE FOR YOU TO INCORPORATE INTO YOUR OWN AUTOMATED STRATEGIES.
*
* IF USED INCORRECTLY IT COULD CAUSE LOSS OF MONEY, READ THE SUPPORTING DOCUMENTATION BEFORE USE.
* PLEASE CONDUCT COMPLETE TESTS ON A DEMO ACCOUNT BEFORE USING ON A LIVE ACCOUNT.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
---------------------------------------------------------------------------------------------------*/
namespace cAlgo.Robots {
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class ClickAlgoNewsManagerExample : Robot {
private NewsSignal _newsSignal;
protected override void OnStart() {
// configure news release manager client
_newsSignal = new NewsSignal(this, this.SymbolName, true);
}
protected override void OnTick() {
// if there is a news release event in progress, exit method, pause robot
if (_newsSignal.IsNewsRelease) {
return;
}
}
namespace ClickAlgo.NewsManager.Client {
public class NewsSignal {
#region public properties
public bool IsNewsRelease {
get;
set;
}
#endregion
#region private fields
private string _symbolCode = string.Empty;
private Robot _robot;
private bool _closePositions = false;
#endregion
#region News events
public NewsSignal(Robot robot, string SymbolCode, bool closePositions = false) {
IsNewsRelease = false;
_symbolCode = SymbolCode;
_robot = robot;
_closePositions = closePositions;
_robot.Timer.Start(TimeSpan.FromSeconds(60));
// setup timer to invoke method each minute to check for news releases
_robot.Timer.TimerTick += Timer_TimerTick;
}
private void Timer_TimerTick() {
if (CheckNewsRelease()) {
IsNewsRelease = true;
} else {
IsNewsRelease = false;
}
try {
// if user wants to close positions before a major news release, do it here.
if (_closePositions) {
if (IsNewsRelease) {
// get all open positions and close for this symbol only
foreach (var position in _robot.Positions.Where(x => x.SymbolName == _symbolCode)) {
_robot.ClosePosition(position);
_robot.Print("Position closed to avoid a high impact news event.");
}
}
}
} catch {
}
}
private bool CheckNewsRelease() {
try {
// split forex symbol pairs
var symbol1 = _symbolCode.Substring(0, 3) + "-NRM";
var symbol2 = _symbolCode.Substring(3, 3) + "-NRM";
// look at local storage for true news events.
var result1 = _robot.LocalStorage.GetString(symbol1, LocalStorageScope.Device);
var result2 = _robot.LocalStorage.GetString(symbol2, LocalStorageScope.Device);
_robot.Print("result1 : "+result1);
_robot.Print("result2 : "+result2); // always false
// does either currency has a news event.
if (result1 == "true" || result2 == "true") {
return true;
}
} catch (Exception e) {
_robot.Print("Timer_TimerTick: " + e.Message.ToString());
}
return false;
}
#endregion
}
}
_robot.Print("result2 : "+result2); // always false
the problem is when i display the resut of USD Currency that is impacted by news at that hour i have always false instead of true at that hour and minute always see false on the Register console and the positions doesn't close but i have the pop up displayed
Now i found that JSON IS UPDATED CORRECTLY TO TRUE VALUE BUT THE PROBLEM IS IN READING THAT VALUE FROM API HE DOESN't read it he doesn't refresh on the program until i stop the bot and i rerun it again