Login Page - Create Account

Support Board


Date/Time: Tue, 04 Aug 2020 16:40:50 +0000



[User Discussion] - Stoploss rejected in ACSIL auto trading system

[2020-08-01 09:43:18]
User820318 - Posts: 10 | Ending Date: 2021-06-10
Hi,

I am developing an autotrading system for ETH/USD via Bitmex and have been live testing for the last week. Just now I had a very unfortunate event. It would appear that as the system was making an order modification the stoploss order was rejected and the position was therefore left unprotected. Obviously this is unsustainable for any trading system. I have the below piece of code which is supposed to protect against having no stops however because the profit target order was still in place this failed to flatten the position. Do you have any advice as to how to prevent this kind of event from happening in the future?

Thanks in advance for your assistance.

// Flatten if position open with no working orders
  if (PositionData.PositionQuantity != 0 &&
    !PositionData.WorkingOrdersExist)
  {
    sc.FlattenPosition();
    sc.AddMessageToLog("Position exists with no working orders. Position was Flattened.", 1);
  }
Date Time Of Last Edit: 2020-08-01 09:46:42
imageInkedCapture_LI.jpg / V - Attached On 2020-08-01 09:46:11 UTC - Size: 5.34 MB - 11 views
[2020-08-01 12:13:14]
User820318 - Posts: 10 | Ending Date: 2021-06-10
and another instance of this happening again today - see attached. Stop loss order cancelled leaving the position open and unprotected. What could be causing this behaviour?
imageCapture2.PNG / V - Attached On 2020-08-01 12:12:06 UTC - Size: 71.94 KB - 8 views
Attachment Deleted.
[2020-08-01 12:26:44]
Sierra Chart Engineering - Posts: 88299 | Ending Date: 2021-04-05
This is a problem with BitMex. It is just how the BitMex system works. Makes no sense.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. If possible please keep your questions brief and to the point. Please be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

If your question/request has been answered and you do not have anything further, then it is easiest for us if you do not reply again to say thank you.
[2020-08-01 12:32:39]
User820318 - Posts: 10 | Ending Date: 2021-06-10
Thanks for the quick reply. Is there anything I can do to mitigate this? For example the error seems to come when the order is being updated. This is a set and forget system in that once the stop and target are put in place they dont need to be updated until one or the other is hit. Is there a way to circumvent these order updates? Or at least put a mechanism in place to flatten the position if there is an open position without a working stoploss order? How would I do that?

Thanks for your time.
[2020-08-01 16:04:02]
User820318 - Posts: 10 | Ending Date: 2021-06-10
Following this thread... One way to do this would be to quantify the number of working orders and if it falls below 2 (i.e. one stop loss and one take profit) flatten the position. Is there a way in ACSIL to quantify the number of working orders?

I have found "PositionQuantityWithAllWorkingOrders" but it seems that wont work because the documentation says "In the case of when there are two orders in an OCO group, the quantity of only one of those orders is counted". Is there another mechanism to count the number of working orders?

Thanks in advance for your assistance.
[2020-08-01 19:40:50]
bradh - Posts: 358 | Ending Date: 2020-08-23
You can check the nearest stop and target individually and make sure they are both open.

  s_SCTradeOrder ExistingStopOrder;
  int OrderResult = sc.GetNearestStopOrder(ExistingStopOrder);

[2020-08-01 21:26:10]
User820318 - Posts: 10 | Ending Date: 2021-06-10
Thanks very much.

Something like the below?

// Flatten if position open with no working orders
s_SCTradeOrder ExistingStopOrder;
int OrderResult = sc.GetNearestStopOrder(ExistingStopOrder);

if (PositionData.PositionQuantity != 0 &&
!OrderResult)
{
sc.FlattenPosition();
sc.AddMessageToLog("Position exists with no stop. Position was Flattened.", 1);
}
[2020-08-01 21:29:54]
bradh - Posts: 358 | Ending Date: 2020-08-23
I would add a check of the order status to see if it is open.
[2020-08-01 21:47:34]
User820318 - Posts: 10 | Ending Date: 2021-06-10
Thanks.

Doesnt PositionData.PositionQuantity != 0 tell us that? if position quantity is not zero then there must be an open position?

Should I add

IsWorkingOrderStatus(ExistingOrder.OrderStatusCode) !=0

to the if statement also?
[2020-08-01 22:09:10]
bradh - Posts: 358 | Ending Date: 2020-08-23
PositionQuantity is open contracts. No order information there.

if (PositionData.PositionQuantity != 0 && ExistingOrder.OrderStatusCode != SCT_OSC_OPEN)
{
// flatten and cancel
}

Date Time Of Last Edit: 2020-08-01 22:12:31
[2020-08-01 22:26:38]
User820318 - Posts: 10 | Ending Date: 2021-06-10
Thanks. You are using

ExistingOrder.OrderStatusCode != SCT_OSC_OPEN

to make sure there are no unfilled entry orders?

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

Login

Login Page - Create Account