Login Page - Create Account

Support Board


Date/Time: Wed, 08 Oct 2025 08:16:12 +0000



[Programming Help] - Advice for ACSIL bot guards/checks

View Count: 124

[2025-10-06 07:42:43]
bozo_j - Posts: 27
Hi guys,

I am new to writing Sierra bots.

Pretty soon it became obvious that extremely important part is writing all kinds of guards (checks) so that bot does not trade when it is not supposed to, unrelated to the actual trading signals, as well as checking various "technical" things related to how Sierra works before starting to trade.

For example, it should only trade on new candles, and not on the historic ones, it should not trade live if that setting is not enabled and so on. Can you please assist with ideas on what a production-grade bot should check as it relates to technical/non-trading-signal items, which just make sense and should be a part of any well behaved trading bot? Maybe some special considerations for Replaying/Testing etc.


Here's what I have so far:



//----------------------------------------------------------
// SAFETY GUARDS
//----------------------------------------------------------
if (sc.HideStudy)
return;
if (sc.IsFullRecalculation)
return;
if (sc.ChartIsDownloadingHistoricalData(sc.ChartNumber))
return;

//----------------------------------------------------------
// SIMULATION vs LIVE CHECK
//----------------------------------------------------------
bool allowLive = AllowLiveTradingInput.GetYesNo();
bool isSimulation = sc.GlobalTradeSimulationIsOn;

if (!isSimulation && !allowLive)
{
sc.AddMessageToLog("TRADING BLOCKED: Live account detected with [Allow Live Trading] = No", 1);
return;
}

// Little fuzzy on this one - the idea is that it does not trade on old candles, but not sure it is implemented correctly:

SCDateTime& StudyStartTime = sc.GetPersistentSCDateTime(BOT_START_TIME_KEY);

if (StudyStartTime == 0)
{
StudyStartTime = sc.BaseDateTimeIn[sc.ArraySize - 1];
return; // Do not trade during the initialization bar
}

// --- Skip bars before study was started ---
SCDateTime CurrentBarTime = sc.BaseDateTimeIn[sc.Index];
if (CurrentBarTime < StudyStartTime)
return;


Thanks in advance for any assistance!
Date Time Of Last Edit: 2025-10-06 07:57:52
[2025-10-06 19:42:28]
ForgivingComputers.com - Posts: 1138
In general, it is easier to use Index numbers than time.

So if you want to reset everything on the first bar:
if (sc.Index == 0)
{
// reset variables here
}

If you only send an order when it is the last bar, you can do:
if (sc.Index < sc.ArraySize - 1)
{
// Not the last bar
return;
}
// Enter Trades here

If you want to wait for the last bar to close before sending an order:
if (sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_NOT_CLOSED)
{
  return;
}
// Last Bar has closed, ready to trade

[2025-10-06 19:53:06]
rajeshh - Posts: 37
Take a look at the included study scsf_TradingSystemBasedOnAlertCondition in TradingSystemBasedOnAlertCondition.cpp. Some of the things there are checks on Start trade time, and end trade time etc.

There is a bunch of settings in ACSIL which can restrict how flexible your automated trading can be vs not - like open positions in both directions, multiple trades in same direction or not. Take a look at that.

imho, the most important check is whether you have a reasonable stop in the market based on your approach or not. The rest really depends on your strategy - scalp or swing and signals, position size, how often your signals trigger, and your average risk/reward.
[2025-10-06 21:40:11]
ForgivingComputers.com - Posts: 1138
As far as guards go, you want to avoid Trade Service errors. You need to check for those conditions and prevent them. For example, if you cancel an order by order ID, it will give an error if you try to do it again.
[2025-10-07 07:01:47]
bozo_j - Posts: 27
@ForgivingComputers.com

Thanks, both your replies are very useful.

"you want to avoid Trade Service errors" -> Do you mean to check that Trade Service is actually connected before placing/modifying/cancelling an order? If so can you pls point me to the function to check, as I tried to find it but could not find a direct/simple way of doing it.

As for "Order Error Constants" returned by sc.BuyOrder and the co., good pointer, thank you!
[2025-10-07 08:42:20]
bozo_j - Posts: 27
@rajeshh

Pointing TradingSystemBasedOnAlertCondition.cpp was helpful, thank you as well!
[2025-10-07 14:32:18]
ForgivingComputers.com - Posts: 1138
Do you mean to check that Trade Service is actually connected before placing/modifying/cancelling an order?

No. I meant don't create conditions that will cause an error with the trade service. Your code should avoid doing things that make no sense to the broker.

Examples of trade service errors:

1. Flattening when already flat
2. Submitting an order when the max position quantity is met
3. Canceling an order that doesn't exist
4. Placing an order with a quantity of 0
etc.

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account