Login Page - Create Account

Support Board


Date/Time: Thu, 25 Apr 2024 00:03:49 +0000



[User Discussion] - ACSIL SCT_ORDERTYPE_TRAILING_STOP example needs updating

View Count: 2538

[2015-08-10 23:21:49]
bjohnson777 (Brett Johnson) - Posts: 284
I was banging my head on the table until I realized the old sc.BuyOrder/sc.SellOrder functions in the examples were replaced with sc.BuyExit and sc.SellExit.

New ACSIL example to set a trailing stop for an existing order:

int RC; //Return Code
s_SCNewOrder NewOrder;
s_SCPositionData PositionData;
sc.GetTradePosition(PositionData);
if(PositionData.PositionQuantity == 0) {return;} //no position to work with
NewOrder.Reset(); //good idea to do this if it's in a loop
NewOrder.OrderType = SCT_ORDERTYPE_TRAILING_STOP;
NewOrder.TimeInForce = SCT_TIF_GTC;
if(PositionData.PositionQuantity > 0) { //long position
  NewOrder.OrderQuantity = PositionData.PositionQuantity;
  NewOrder.Price1 = PositionData.AveragePrice - 0.001; //10pips below
  RC = sc.BuyExit(NewOrder); //BuyExit to exit an existing long
  }
else { //short position
  NewOrder.OrderQuantity = PositionData.PositionQuantity * -1.0; //keep it positive
  NewOrder.Price1 = PositionData.AveragePrice + 0.001; //10pips above
  RC = sc.SellExit(NewOrder); //SellExit to exit an existing short
  }
if(RC < 0) {sc.AddMessageToLog(sc.GetTradingErrorTextMessage(RC), 0);} //log errors

[2015-08-10 23:23:21]
bjohnson777 (Brett Johnson) - Posts: 284
A new question comes to mind: With the code above, is it possible in ACSIL to make the trailing stop an attachment order of the original order?

Thanks
[2015-08-11 00:27:39]
Sierra Chart Engineering - Posts: 104368
These functions are not old:
sc.BuyOrder/sc.SellOrder

Why does the example need updating? This is not clear for us.

If you want a Trailing stop attached to another order, it must be specified as an Attached Order at the time the parent order is sent.
Sierra Chart Support - Engineering Level

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

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
[2015-08-11 03:15:16]
bjohnson777 (Brett Johnson) - Posts: 284
My ACSIL program will wait for an active order and then add a trailing stop and a possible price target order (among other features still being programmed). I guess these can never be attached because the original order to start all this off will be entered by a human. Example being a market order when trade conditions are favorable.

When I use sc.BuyOrder/sc.SellOrder in the above code to add a trailing stop to a newly filled order (the only order that was active), I kept getting order failures. When I swapped to sc.BuyExit/sc.SellExit, the trailing stop entry worked the first time. Since a trailing stop is used to exit a position when things go unfavorable, this notation does make sense to me.

My trade environment variables:
sc.AllowOnlyOneTradePerBar = true;
sc.AllowMultipleEntriesInSameDirection = false; //one trade managed at a time.
sc.AllowEntryWithWorkingOrders = false; //one trade managed at a time.
sc.SupportReversals = false;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = false; //don't trade against yourself on the same chart.
sc.SupportAttachedOrdersForTrading = false; //don't use attached orders from the chart Trading Window.
sc.CancelAllOrdersOnEntriesAndReversals = true; //start fresh going in.
sc.CancelAllWorkingOrdersOnExit = true; //allows for exits with attached orders.

I chose these to make sure the ACSIL program would behave itself if something went wrong. I don't want it to make a trade even larger when it is supposed to be closed out. Did I miss something in my comment descriptions?

Elsewhere in the program I use sc.FlattenAndCancelAllOrders() as an "exit all" when certain events are met. That has always worked fine.

What I want is the SC controlled trailing stop at a larger price distance away as a safety stop in case something goes wrong. At the very least, a plain stop order will exist on the trade server if the connection is lost.

I want the SC controlled price target as a generic exit if a profit target is met. This will be a stop or limit order a distance away from the entry price.

In the code, the price target order is created first.

But now I have a new problem.

Price target in a long position will give this error with sc.SellOrder:
SellEntry signal is ignored because Long position quantity or working Buy order exists.

"Long position quantity" exists. A "working Buy order" does not exist. The trailing stop code is executed after this and enters the order fine.

Isn't a "Long position quantity" existing exactly what I want when I create a limit sell order above the entry price?

If the price target is set up as sc.BuyExit for a long position, it will enter but block the trailing stop with:
Order Action is ignored because sc.AllowOnlyOneTradePerBar is TRUE and a signal for the same Order Action (BuyExit) was already given on the bar.

I put an order in that wasn't going to result in a trade yet. Shouldn't sc.AllowOnlyOneTradePerBar allow that? An s_SCOrderFillData object wasn't created by the previous order entry. Or should "sc.AllowOnlyOneTradePerBar" really be called "sc.AllowOnlyOneOrderPerBar"?

All this is making me a bit confused. I need some help, please.
Date Time Of Last Edit: 2015-08-11 03:20:00
[2015-08-12 03:47:20]
Sierra Chart Engineering - Posts: 104368
You do want to use sc.BuyExit/sc.SellExit.

All of the order entry functions are documented here:
https://www.sierrachart.com/index.php?page=doc/doc_ACSILTrading.html#SubmittingAnOrder

I put an order in that wasn't going to result in a trade yet. Shouldn't sc.AllowOnlyOneTradePerBar allow that?
No, it does not work this way. You must set that to FALSE to do what you want.
Sierra Chart Support - Engineering Level

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

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2015-08-12 03:48:48
[2015-08-12 23:02:51]
bjohnson777 (Brett Johnson) - Posts: 284
I'm having a bad health week, so sorry for the sluggishness of the responses.

I did some testing yesterday and notice that sc.BuyExit/sc.SellExit will cancel any previous entries made by them. Can that behavior be updated in the documentation? I kept wondering why both orders would be accepted, but the first would always be immediately cancelled. Nothing showed up in the trade or message logs.

Can y'all modify sc.BuyExit/sc.SellExit to allow for 2 exit orders? One would be above the price line and the other below. This would be for take profit and safety stop setup. Having the functions still automatically cancel their last above or below matching order would make updates simple.

I set up a function to temporary disable the blocking variables. For those running into the same problem I have, here's the code. Place it above the scsf_ function.

//Disable or enable blocking variables.
void SetBlockingVariables(SCStudyInterfaceRef sc, int Status) {
  if(Status > 0) { //safety on
    sc.AllowOnlyOneTradePerBar = true;
    sc.AllowMultipleEntriesInSameDirection = false;
    sc.AllowEntryWithWorkingOrders = false;
    sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
    sc.CancelAllOrdersOnEntriesAndReversals = true;
    }
  else { //safety off
    sc.AllowOnlyOneTradePerBar = false;
    sc.AllowMultipleEntriesInSameDirection = true;
    sc.AllowEntryWithWorkingOrders = true;
    sc.AllowOppositeEntryWithOpposingPositionOrOrders = true;
    sc.CancelAllOrdersOnEntriesAndReversals = false;
    }
} //end SetBlockingVariables

Usage is:
//Turn them off.
SetBlockingVariables(sc, 0);

//order code goes here

//Turn them back on.
SetBlockingVariables(sc, 1);

I have concerns about disabling safeties, but I need to be moving forward on this program. I've identified a possible race condition in the program I'm working on. I'll handle that separately.
[2015-08-13 18:59:50]
Sierra Chart Engineering - Posts: 104368
For what you want to do the only way to do this is to use unmanaged automated trading:
https://www.sierrachart.com/index.php?page=doc/doc_AutoTradeManagment.php#UnmanagedAutomatedTrading


Can y'all modify sc.BuyExit/sc.SellExit to allow for 2 exit orders?
No. Definitely not. If we did that, these functions would not function correctly at all.
Sierra Chart Support - Engineering Level

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

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2015-08-13 19:02:01

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

Login

Login Page - Create Account