Auto Trade Managment
Introduction
This page explains all of the automated trading related Variables and Order Actions for the Worksheet System for Trading study and the trading related variables and functions in the Advanced Custom Study Interface and Language (ACSIL). These variables, Order Actions and functions together provide the automated trading logic in Sierra Chart.
^ Top
Variables
These auto trade variables apply to both the Advanced Custom Study Interface and Language (ACSIL) (in this case they must be prefixed with sc.) and they also apply to the Worksheet System for Trading study (in this case they are study Inputs).
When a reference is made to the Session Position data, this refers to the position data maintained by Sierra Chart for the symbol of the chart that the trading system is applied to. When a reference is made to Working orders, this refers to orders for the symbol of the chart that the trading system is applied to with an Order Status, as shown in the Trade >> Trade Orders Window, of PENDING OPEN, OPEN, PENDING CANCEL, or PENDING MODIFY.
AllowMultipleEntriesInSameDirection
The default value for this variable is FALSE (No). When this variable is set to FALSE (No), the auto trade management system will not allow multiple Entries in the same direction. More details about how this works can be found in the BuyEntry and SellEntry Order Action sections.
If you want to allow Multiple Entries in the Same Direction, set this variable to TRUE (Yes).
MaximumPositionAllowed
This variable controls the maximum position allowed when performing automated trading. An order that would increase the Session Position to a quantity greater than the specified maximum position allowed will be ignored. For example, if this variable is set to 5, a Buy order that would increase the existing position greater than 5 will be ignored . And a Sell order that would increase the existing position beyond -5 will be ignored. When an order is given, a comparison is made to the actual current session position and to the potential position. The potential position is determined by combining the current Session Position with the quantities of all working orders. Therefore, if you have a Session Position of +1 and a Buy order of quantity 1, then this is a potential position of 2. If your Maximum Position Allowed is 2 and you submit a Buy order, it will not be allowed.
SupportReversals
The default value for this variable is FALSE (No). If this variable is set to TRUE (Yes), then reversals are supported. A reversal is triggered when a Buy Entry or Sell Entry order is submitted and an actual opposite Session Position already exists. If there is not an actual position as represented by the Session Position data, then a reversal cannot happen. When a reversal order is given it will flatten the existing position, and create a new position which will have a quantity equal to the Order Quantity specified.
When calculating the quantity for flattening the existing position, the auto trade management system looks at the current Session Position quantity and the quantities of orders that can exit the position only. It does not consider working orders that can increase the size of the position. If you have set CancelAllOrdersOnEntriesAndReversals to TRUE (Yes), then only the actual Session Position is considered. For example, if the position is +5 and there is a Sell Entry, and there is a working sell order with a quantity of 2, then the order to flatten the position will be a Sell order of quantity 3. Additionally, if those working orders already indicate the potential position is flat or does not exist, then no reversal actually occurs. Merely a new entry.
For example, if your session position quantity is +3 (long position of quantity 3), and there is a Sell Entry with a quantity of 1, the auto trade management system will generate a Sell order with a quantity that would have a resulting position of -1 (the quantity sold would be 4).
Note, that when this is set to TRUE (Yes), then the AllowOppositeEntryWithOpposingPositionOrOrders variable is ignored.
SendOrderSignalsToTradeService
The default value for this variable is FALSE (No). Setting this variable to FALSE (No) will send all orders to the Sierra Chart Trade Simulation system. Set this variable to TRUE (Yes) to send an order to the connected trading service when an order signal is given. If you have Sierra Chart Trade Simulation Mode on, then order will still be simulated and not sent to the connected trade service even if this variable is set to TRUE. To send order signals to the connected trading service, this variable must be set to TRUE (Yes) and Trade Simulation Mode needs to be disabled by removing the check mark from Trade >> Trade Simulation Mode On on the menu.
Even though an order is sent to your trading service by disabling Trade Simulation Mode and by setting this variable to TRUE (Yes), you may be connected to a simulated account with your trading service. In this case the trades will still be simulated.
This variable also affects the functions for getting the position and orders. When this variable is set to FALSE (No), you will always get the simulated position and simulated orders. Even if this is set to TRUE (Yes), you will still get the simulated position and simulated orders if you are in the Sierra Chart Trade Simulation Mode.
AllowOppositeEntryWithOpposingPositionOrOrders
The default value for this variable is TRUE (Yes). When this variable is set to FALSE (No), then an entry is not allowed if there is a position or working orders already existing in the opposite direction. More details about how this variable works can be found in the BuyEntry and SellEntry Order Action sections.
SupportAttachedOrdersForTrading
It is not necessary to use the BuyExit or SellExit Order Actions to exit a position. Exits can be performed by using the Attached Orders feature (Targets and Stops). Although it's fine to use BuyExit or SellExit. Targets and Stops are configured on the Attached Orders tab of the Trade Window associated with the chart that the trading study is applied to. For more information about configuring Attached Orders through the Trade Window, please refer to the Attached Orders documentation page. Various order types for Attached Orders are supported including trailing stops. In the case of an ACSIL trading system, it can programmatically specify the Attached Orders or specify a Trade Window configuration file (explained below).
The default value for this variable is FALSE (No) for ACSIL trading system studies. For the Worksheet System for Trading study it is TRUE (Yes) and cannot be changed. When this variable is set to TRUE (Yes), the BuyEntry and the SellEntry Order Actions will use the Attached Orders that are specified on the associated Trade Window for the chart that your trading study is applied to. This only applies to the BuyEntry and SellEntry Order Actions. BuyExit and SellExit Order Actions cannot have Attached Orders attached to them.
Note: It definitely is not recommended for you to implement yourself in an ACSIL trading system, Target and Stop orders by sending both of them after the parent order fills, and handling the related OCO functionality. The reason for this, is there is a very high degree of sophistication involved to actually implement OCO functionality properly. And it is something that Sierra Chart internally will manage for you through the Attached Orders feature. Sierra Chart will properly manage this for you in a reliable way in a live trading environment.
A set of Attached Orders, which is entered on the Trade Window for a chart, is saved when a chartbook is saved. The Trade Window configuration, which also includes Attached Orders can also be saved to a trade window configuration file (.twconfig file extension). To save or load a configuration file select the More tab on the Trade Window. Press the Save Configuration button to save the configuration. If you are providing an automated trading system to users that uses Attached Orders, then you should provide them a saved chartbook file which contains a chart with your trading system study and it also will have the complete Trade Window configuration containing the Attached Orders. Or you can provide them a Study Collection file they can use to apply the automated trading system to the chart and also provide them a trade window configuration (.twconfig) file that they can use to load the Attached Orders set into the Trade Window for use with the automated trading system.
Programmatically Specifying A Trade Window Configuration File For Attached Orders: If your automated trading system uses Attached Orders, it's a good idea to have saved the Trade Window configuration to a file that contains the Attached Orders defined on the Trade Window. To specify this Trade Window Configuration file in your ACSIL Trading system, see this example:
// This line can be within sc.SetDefaults or outside of it.
sc.TradeWindowConfigFileName = "SimpleBracket.twconfig";
To use a set of Attached Orders for an ACSIL trading system and not have to rely on a chartbook or a Trade Window configuration file, refer to these members of the s_SCTradingOrderInput structure . For a complete code example, refer to the scsf_TradingExampleWithAttachedOrdersDirectlyDefined (scsf_TradingExampleWithAttachedOrders2) function in the tradingsystem.cpp file in the /ACS_Source folder in the folder where Sierra Chart is installed to.
CancelAllOrdersOnEntriesAndReversals
The default for this variable is TRUE (Yes). When CancelAllOrdersOnEntriesAndReversals is set to TRUE (Yes), then when there is a Buy Entry or a Sell Entry or a Reversal Order Action given, then all existing working orders for the symbol of the chart that the trading system study is applied to will be canceled before the order is entered. When CancelAllOrdersOnEntriesAndReversals is set to FALSE (No), then existing working orders for the symbol will not be canceled.
AllowEntryWithWorkingOrders
The default for this variable is FALSE (No). When AllowEntryWithWorkingOrders is set to TRUE (Yes), then a Buy Entry or a Sell Entry will be allowed if there are existing working orders. However, there can still be other exceptions to prevent an Entry order from being processed. When AllowEntryWithWorkingOrders is set to FALSE (No), then if there are existing working orders, then a Buy Entry or a Sell Entry Order Action, will not be allowed.
CancelAllWorkingOrdersOnExit
The default for this variable is FALSE (No). When CancelAllWorkingOrdersOnExit is set to TRUE (Yes), then when BuyExit or SellExit order actions are used, working orders that can exit the position will be ignored and only the actual position will be evaluated to determine if an exit is possible. In this case, if an exit is possible, then all working orders will be canceled and the position will be flattened or reduced by the specified quantity. One useful purpose for this variable is that it allows you to use Attached Orders and also use the BuyExit and SellExit Order Actions at the same time as Attached Orders. Therefore, when you use the BuyExit or SellExit Order Actions, the working Attached Orders will be canceled.
^ Top
Unmanaged Automated Trading
If you want to be able to enter Buy and Sell orders without any restriction and not use the automated trading management logic provided by Sierra Chart, then set the following as shown:
- AllowMultipleEntriesInSameDirection to TRUE (Yes)
- AllowOppositeEntryWithOpposingPositionOrOrders to TRUE (Yes)
- CancelAllOrdersOnEntriesAndReversals to FALSE (No)
- sc.AllowOnlyOneTradePerBar to FALSE (applies only to ACSIL trading systems)
By setting these automated trading variables as shown, this will give you unrestricted functionality. You can simply use the BuyEntry and the SellEntry Order Actions to freely enter Buy and Sell orders respectively. If you wish to flatten or reduce the position you can also use the BuyEntry and the SellEntry Order Actions or use the BuyExit or SellExit Order Actions as well.
^ Top
Order Actions
This section explains how the 4 Order Actions are managed by the Auto Trade Management system in Sierra Chart. When a reference is made to the Session Position data, this refers to the position data maintained by Sierra Chart for the symbol of the chart that the trading system is applied to. When a reference is made to Working orders, this refers to orders for the symbol of the chart that the trading system is applied to with an Order Status, as shown in the Trade >> Trade Orders Window, of PENDING OPEN, OPEN, PENDING CANCEL, or PENDING MODIFY.
Buy Entry
When the Buy Entry Order Action is triggered, a Buy order will be sent to your connected trading service or to the Trade Simulation System, with the specified quantity as long as certain exceptions are not met. The following are the exceptions:
- When AllowMultipleEntriesInSameDirection is set to FALSE (No), and the current Session Position is greater than 0, then the Order Action will not be allowed.
Or when AllowMultipleEntriesInSameDirection is set to FALSE (No), and the current Session Position combined with the quantities of all working orders is greater than 0, then the Order Action will not be allowed.
Note: When the quantities of working orders are added together and combined with the Session Position, they are all added up without regards to whether an order is a child order or not. If there is a Session Position of +1 for the symbol and there are two sell orders (target and stop) with a quantity of 1 each, then the combined effective position will be -1. - If AllowEntryWithWorkingOrders is set to FALSE (No) and there are working orders existing for the symbol and AllowMultipleEntriesInSameDirection is set to FALSE (No), then the Order Action will not be allowed.
- If AllowOppositeEntryWithOpposingPositionOrOrders is FALSE (No), and the current Session Position is less than 0 or the current Session Position combined with the quantities of all working orders is less than 0, then the Order Action will not be allowed. However, if SupportReversals is set to TRUE (Yes), then none of this applies.
- If the Order Action would result in a position greater than the MaximumPositionAllowed, then the Order Action will not be allowed. There are two ways this is determined. The new order quantity is combined with the current Session Position quantity to determine if the MaximumPositionAllowed will be exceeded. And the new order quantity is combined with both the Session Position quantity and with the quantities of all working orders, to determine if the MaximumPositionAllowed will be exceeded.
If one of these exceptions is met and the Order Action is not allowed, then the reason for the order not being allowed is displayed in the Trade >> Trade Service Log.
Buy Exit
When this Order Action is triggered, a Sell order will be sent to your connected trading service or to the Trade Simulation System, if certain exceptions are not met. The following are the exceptions:
- If CancelAllWorkingOrdersOnExit is FALSE (No) and the Session Position quantity combined with the quantities of Sell working orders are less than or equal to 0, the Order Action will be ignored (meaning there is no position or a short position). In this case Buy working orders are ignored in the calculation.
- If the Session Position quantity is less than or equal to 0, the Order Action will be ignored.
If one of these exceptions is met and the Order Action is not allowed, then the reason for the order not being allowed is displayed in the Trade >> Trade Service Log.
If you are using Attached Orders that will flatten your position, then it is not possible to use BuyExit unless CancelAllWorkingOrdersOnExit is set to TRUE (Yes).
The position will be flattened unless an order quantity is specified. In the case of sc.SCTrading_BuyExit(), the quantity is specified with the OrderQuantity member of the s_scTradingOrderInput structure. In the case of the Worksheet System for Trading study, the quantity can be specified with the Custom Order Quantity cell (J26). If Custom Order Quantity is blank or 0, then the position will be flattened. The Default Order Quantity input with the Worksheet Trading study is ignored for Exits. If an order quantity is specified and it exceeds the current Session Position minus any working exit orders that already exist, it will be reduced to match the current Session Position minus any working exit orders that already exist. For example, if the Session Position is +2 and there is one working sell order with a quantity of 1, and the specified order quantity is 3, the new order quantity will be reduced to 1.
A Buy Exit does not need to be used. A position can be manually exited or you can use the Support Attached Orders For Trading variable in the case of an ACSIL trading study. In the case of the Worksheet System for Trading study, please refer to the section Initiating Trades with Auto-Trading and Using Manual or Attached Orders Exits.
Using Exits Only for a Specialized Exit Strategy: You may want to develop a system for exiting manually entered positions that only use the BuyExit and SellExit Order Actions. For example, you may manually enter a position and then you want to have an exit based upon a formula involving a particular study subgraph. In this case you would not use the BuyEntry or SellEntry Order Actions. You would only use a formula that would trigger a BuyExit or SellExit. This provides very advanced exit trade management.
Sell Entry
When the Sell Entry Order Action is triggered, a Sell order will be sent to your connected trading service or to the Trade Simulation System, with the specified quantity as long as certain exceptions are not met. The following are the exceptions:
- When AllowMultipleEntriesInSameDirection is set to FALSE (No), and the current Session Position is less than 0, then the Order Action will not be allowed.
Or when AllowMultipleEntriesInSameDirection is set to FALSE (No), and the current Session Position combined with the quantities of all working orders is less than 0, then the Order Action will not be allowed.
Note: When the quantities of working orders are added together and combined with the Session Position, they are all added up without regards to whether an order is a child order or not. If there is a Session Position of +1 for the symbol and there are two sell orders (target and stop) with a quantity of 1 each, then the combined effective position will be -1. - If AllowEntryWithWorkingOrders is set to FALSE (No) and there are working orders existing for the symbol and AllowMultipleEntriesInSameDirection is set to FALSE (No), then the Order Action will not be allowed.
- If AllowOppositeEntryWithOpposingPositionOrOrders is FALSE (No), and the current Session Position is greater than 0 or the current Session Position combined with the quantities of all working orders is greater than 0, then the Order Action will not be allowed. However, if SupportReversals is set to TRUE (Yes), then none of this applies.
- If the Order Action would result in a position greater than the MaximumPositionAllowed, then the Order Action will not be allowed. There are two ways this is determined. The new order quantity is combined with the current Session Position quantity to determine if the MaximumPositionAllowed will be exceeded. And the new order quantity is combined with both the Session Position quantity and with the quantities of all working orders, to determine if the MaximumPositionAllowed will be exceeded.
If one of these exceptions is met and the Order Action is not allowed, then the reason for the order not being allowed is displayed in the Trade >> Trade Service Log.
Sell Exit
When this Order Action is triggered, a Buy order will be sent to your connected trading service or to the Trade Simulation System, if certain exceptions are not met. The following are the exceptions:
- If CancelAllWorkingOrdersOnExit is FALSE (No) and the Session Position quantity combined with the quantities of Buy working orders are greater than or equal to 0, the Order Action will be ignored (meaning there is no position or a long position). In this case Sell working orders are ignored in the calculation.
- If the Session Position quantity is greater than or equal to 0, the Order Action will be ignored.
If one of these exceptions is met and the Order Action is not allowed, then the reason for the order not being allowed is displayed in the Trade >> Trade Service Log.
If you are using Attached Orders that will flatten your position, then it is not possible to use SellExit unless CancelAllWorkingOrdersOnExit is set to TRUE (Yes).
The position will be flattened unless an order quantity is specified. In the case of sc.SCTrading_SellExit(), the quantity is specified with the OrderQuantity member of the s_scTradingOrderInput structure. In the case of the Worksheet System for Trading study, the quantity can be specified with the Custom Order Quantity cell (J26). If Custom Order Quantity is blank or 0, then the position will be flattened. The Default Order Quantity input with the Worksheet Trading study is ignored for Exits. If an order quantity is specified and it exceeds the current Session Position minus any working exit orders that already exist, it will be reduced to match the current Session Position minus any working exit orders that already exist. For example, if the Session Position is +2 and there is one working sell order with a quantity of 1, and the specified order quantity is 3, the new order quantity will be reduced to 1.
A Sell Exit does not need to be used. A position can be manually exited or you can use the Support Attached Orders For Trading variable in the case of an ACSIL trading study. In the case of the Worksheet System for Trading study, please refer to the section Initiating Trades with Auto-Trading and Using Manual or Attached Orders Exits.
Using Exits Only for a Specialized Exit Strategy: You may want to develop a system for exiting manually entered positions that only use the BuyExit and SellExit Order Actions. For example, you may manually enter a position and then you want to have an exit based upon a formula involving a particular study subgraph. In this case you would not use the BuyEntry or SellEntry Order Actions. You would only use a formula that would trigger a BuyExit or SellExit. This provides very advanced exit trade management.