Login Page - Create Account

Support Board


Date/Time: Sat, 23 Nov 2024 21:23:11 +0000



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

View Count: 548

[2024-06-09 13:26:49]
User194332 - Posts: 40
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 - 66 views
attachmentAvi_Lockout.cpp - Attached On 2024-06-09 13:26:11 UTC - Size: 2.58 KB - 178 views
[2024-06-24 04:45:26]
User194332 - Posts: 40
Hi

Please can i follow up for an update on this.
[2024-06-24 13:57:03]
ForgivingComputers.com - Posts: 960
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: 40
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: 541

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: 40
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: 541

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: 40
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: 541
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: 960
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: 40
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: 960
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: 40
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 - 45 views
Attachment Deleted.
attachmentAvi_Lockout.cpp - Attached On 2024-07-05 22:01:17 UTC - Size: 2.23 KB - 134 views
[2024-07-11 10:21:55]
User194332 - Posts: 40
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: 541
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
[2024-07-21 15:08:45]
User194332 - Posts: 40
im using this piece of code to Flatten a position but when I test it, nothing happens.

Does this function return unrealized P&L ? sc.GetTotalNetProfitLossForAllSymbols(1)

I have the defaults set as follows
sc.MaintainTradeStatisticsAndTradesData = TRUE; // Maintain trade statistics and trades data
sc.SendOrdersToTradeService = FALSE; // Prevent sending orders

// Get trade position data


s_SCPositionData PositionData;
sc.GetTradePosition(PositionData);


double totalNetPL = sc.GetTotalNetProfitLossForAllSymbols(1);

// Check if the loss limit is exceeded
if (totalNetPL <= DAILY_LOSS_LIMIT) {
sc.AddMessageToLog("Daily PNL exceeds loss limit.", 1);
}

// Flatten position and cancel all orders if the loss limit is exceeded
if (PositionData.PositionQuantity != 0 && totalNetPL <= DAILY_LOSS_LIMIT) {
sc.FlattenPosition();
sc.AddMessageToLog("Flattened Position due to loss limit.", 1);
}
[2024-07-21 15:22:21]
User431178 - Posts: 541
Does this function return unrealized P&L?

Don't take this the wrong way, but why don't your read the information for that function?
The answer is written there plain as day.

sc.GetTotalNetProfitLossForAllSymbols()

im using this piece of code to Flatten a position but when I test it, nothing happens.
You mean none of the conditional lines are exceuted or just that sc.FlattenPosition does appear not work for you?

Are there any messages in the trade service log?
[2024-07-22 04:13:41]
User194332 - Posts: 40
I've read it and used daily values with the 1 in brackets but the sc.FlattenPosition is not working for some reason. Nothing mentioned in the Service Log.

Bradh helped me out with some code to check that the position is not equal to 0 and it should flatten the position.

Basically when the P&L crosses the limit , I would expect the position to flatten using the sc.FlattenPosition();
When I test it, the position remains open and does not flatten.

The code is running as I can see the messages in the message log.

Hope that clarifies
[2024-07-22 07:24:51]
User431178 - Posts: 541
The code is running as I can see the messages in the message log.
Ok, so the logic is working correctly, but sc.FlattenPosition does not do anything.

So do you have these settings enabled?
Note that the second one is particular to the chart.

Trade Menu: Auto Trading Enabled - Global (Trade menu)
Trade Menu: Auto Trading Enabled - Chart (Trade menu)
[2024-07-22 13:05:28]
User194332 - Posts: 40
correct not seeing any Flattening take place.

Yes both the auto trading settings are on.

I'm using the DOM to execute trades but the autotrading settings under the trade menu are both checked for Chart & Global.
[2024-07-22 16:10:25]
ondafringe - Posts: 283
Build a debug version and set some breakpoints so you can step through your code execution and check the values in relevant variables.

Otherwise, you're flying blind (like you are now).
Date Time Of Last Edit: 2024-07-23 01:50:39

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

Login

Login Page - Create Account