Login Page - Create Account

Support Board


Date/Time: Fri, 19 Jul 2024 19:25:50 +0000



[Programming Help] - Error with Sim Lock & Orders being executed

View Count: 294

[2024-06-09 13:26:49]
User194332 - Posts: 35
Hi

I am trying to implement some code on a SIM Lock feature and seem to be stuck on a few things. Please can you review the code and suggest some alternatives. In short, I'm trying to have a sim lock for x no of minutes when Realized P&L exceeds 250 and then get the code to reset the P&L to -500 ect..

The Code I have attached has a sim lock in place but I can see trades still executing during the locked state so wandering why that is.

Additionally I want the state to be unlocked @ Market Open 09:30:00 EST but not sure if I'm using the right function to get the current time.

Appreciate any help you can provide on this.

Regards
Avi
imageSIM Lock taking positions.PNG / V - Attached On 2024-06-09 13:25:51 UTC - Size: 123.52 KB - 36 views
attachmentAvi_Lockout.cpp - Attached On 2024-06-09 13:26:11 UTC - Size: 2.58 KB - 68 views
[2024-06-24 04:45:26]
User194332 - Posts: 35
Hi

Please can i follow up for an update on this.
[2024-06-24 13:57:03]
ForgivingComputers.com - Posts: 903
This doesn't compile as-is.

These should be part of the scsf (above or below SetDefaults):

int Hour = sc.BaseDateTimeIn[sc.Index].GetHour();
int Minute = sc.BaseDateTimeIn[sc.Index].GetMinute();
int Second = sc.BaseDateTimeIn[sc.Index].GetSecond();

LockEndTime = sc.CurrentSystemDateTime + 15 * MINUTES; // Set lock duration to 15 minutes

MINUTES is undefined.

Correct code:

LockEndTime = sc.CurrentSystemDateTime + SCDateTime::MINUTES(15); // Set lock duration to 15 minutes

Date Time Of Last Edit: 2024-06-24 14:08:59
[2024-06-29 03:02:34]
User194332 - Posts: 35
Thanks Brad - The issue i'm having is that the Lock is getting set but the orders in the book are not cancelling.


Once, the the Lock is now on and I cannot cancel the orders manually but it still executes. I want the position to Flatten & All Orders Open to be cancelled before the lock gets set.


i'm using the following but think it might be interfering with a position limit set in global settings. Is there a way round this?

sc.PlaySound("Alert1.wav");
sc.CancelAllOrders();
sc.FlattenPosition();
  
//Locktrading
sc.SetTradingLockState(1);


Additionally can I ask when testing this out in Replay mode, CurrentsystemDateTime Is the time based on Global settings time zone which I've got set to New York or Local time is UK. Assuming its NY but could be mistaken there.
[2024-06-29 08:12:58]
User431178 - Posts: 452

The issue i'm having is that the Lock is getting set but the orders in the book are not cancelling.

You need to cancel the existing orders yourself before locking trading.



i'm using the following but think it might be interfering with a position limit set in global settings. Is there a way round this?

sc.PlaySound("Alert1.wav");
sc.CancelAllOrders();
sc.FlattenPosition();

You want a check before you set the lock, to ensure that the positions / orders cancelled as expected.


Additionally can I ask when testing this out in Replay mode, CurrentsystemDateTime Is the time based on Global settings time zone which I've got set to New York or Local time is UK. Assuming its NY but could be mistaken there.

It is based on the timezone of the chart, so unless you have changed the chart separately, the golbal time zone setting.

Also, there is the function sc.GetCurrentDateTime(), which returns the system time or the replay time automatically, depending on whether replay is running.
[2024-07-01 03:27:39]
User194332 - Posts: 35
Apologies I might be mis understanding something here.

The code is checking if P&L exceeds a specific amount and sets a lock state but before the lock I want the orders and positions flattened.

Are you saying that this cannot be done directly in the code? What does sc.CancelAllOrders(); do ?

I'd like to avoid P&L watching at all if possible and just let the program take care of the cancelling.
[2024-07-01 07:58:59]
User431178 - Posts: 452

Are you saying that this cannot be done directly in the code? What does sc.CancelAllOrders(); do ?

Not at all, just that you should check the the position is flat and that any orders have been cancelled before setting the lock.
[2024-07-03 10:25:09]
User194332 - Posts: 35
i'm trying to find out why the CancelAllOrders(); is not cancelling the limit orders on the DOM. Where do I find out why this happens?

On the Open Order Window, I can see LastOrderActionSource = Simulated order accepted but I am expecting it to be cancelled. sometimes I see cannot cancel because trading lock is on.

Is this the correct field ?
[2024-07-03 10:41:01]
User431178 - Posts: 452
sometimes I see cannot cancel because trading lock is on.

To check the position before locking trading:


s_SCPositionData PositionData;
int Result = sc.GetTradePosition(PositionData);

if (PositionData.PositionQuantity == 0
&& PositionData.NonAttachedWorkingOrdersExist == 0)
sc.SetTradingLockState(1);
else
/* Do something else */


Automated Trading From an Advanced Custom Study: Getting Trade Position Data
Date Time Of Last Edit: 2024-07-03 10:41:29
[2024-07-03 17:12:46]
ForgivingComputers.com - Posts: 903
As has been stated, you cannot flatten or cancel after you lock trading.

Something else to consider is to avoid errors. You don't want to flatten if already flat, and you don't want to cancel orders if none exist, as these will cause errors with the trading service.

So, check that orders exist before canceling, and check that there is a position before flattening.

    s_SCPositionData PositionData;
    sc.GetTradePosition(PositionData);
    if (PositionData.PositionQuantity != 0)
    {
      sc.FlattenAndCancelAllOrders();
    }
    else if (PositionData.WorkingOrdersExist)
    {
      sc.CancelAllOrders();
    }

[2024-07-04 10:59:29]
User194332 - Posts: 35
i'm declaring the variables below in the input parameters but keep getting the error in compile that it is not declared in scope.


bool Lockon = sc.SetTradingLockState(1);
bool Lockoff = sc.SetTradingLockState(0);
LockEndTime = sc.CurrentSystemDateTime + SCDateTime::MINUTES(15); // Set lock duration to 15 minutes   


If (LockOn)
{
LockEndTime;
}

Is this correct to apply the lock time? or does it need to be

If (LockendTime)

{
LockOff;
}
[2024-07-04 15:32:16]
ForgivingComputers.com - Posts: 903
If you want to know if 15 minutes have passed, save the lock time, then keep checking if the current time is 15 minutes later.


SCDateTime& LockDateTime = sc.GetPersistentSCDateTime(1);
SCDateTime CurrentDateTime;

if (sc.IsReplayRunning())
  CurrentDateTime = sc.CurrentDateTimeForReplay;
else
  CurrentDateTime = sc.CurrentSystemDateTime;

bool Locked = false;
if (!Locked && sc.TradingIsLocked)
{
LockDateTime = CurrentDateTime;
Locked = true;
}

if (Locked && (CurrentDateTime - LockDateTime ) >= SCDateTime::MINUTES(15))
{
sc.SetTradingLockState(0);
Locked = false;
}


Date Time Of Last Edit: 2024-07-04 15:33:02
[2024-07-05 22:05:28]
User194332 - Posts: 35
i'm still getting the issue with the lock being applied and positions open with Stop & Targets.
imageSIM Lock taking positions 2.PNG / V - Attached On 2024-07-05 22:01:04 UTC - Size: 200.72 KB - 10 views
Attachment Deleted.
attachmentAvi_Lockout.cpp - Attached On 2024-07-05 22:01:17 UTC - Size: 2.23 KB - 19 views
[2024-07-11 10:21:55]
User194332 - Posts: 35
Hi Bradh

any thoughts on why the orders and position doesn't get cancelled? Cant seem to find out why its skipping the Order Cancelled/Flatten position and setting the lock.
[2024-07-11 11:22:23]
User431178 - Posts: 452
Here are some clues, the top one being the likely cause here.

ACSIL Interface Members - Variables and Arrays: sc.MaintainTradeStatisticsAndTradesData

Automated Trading Management: SendOrdersToTradeService
ACSIL Interface Members - Variables and Arrays: sc.IsAutoTradingEnabled
ACSIL Interface Members - Variables and Arrays: sc.IsAutoTradingOptionEnabledForChart



Cant seem to find out why its skipping the Order Cancelled/Flatten position and setting the lock.

Check the trade service log

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

Login

Login Page - Create Account