Broker

Top  Previous  Next

The broker object is used to place orders. All orders are good for the bar only, and are canceled if not filled in that bar. The entry and exit scripts are run every bar, so the orders are placed again every bar.

 

Entry Order Functions
Exit Order Functions
Position Adjustment Functions

 

Risk and Broker Orders

Stops are used to calculate the risk of the trade.  The difference between the order price and stop price is the risk of the trade.  This is used to generate risk/reward calculations and sometimes used for position sizing in the Money Manager. For instance, the Fixed Fractional Money Manager uses the entry risk to determine the unit size. If you have no stops, undefined risk is assumed.

 

Examples:

broker.EnterLongOnOpen               ' Places an order to buy at

                                    ' the open with no stop.

 

broker.EnterLongOnOpen( stopPrice )  ' Places an order to buy at

                                    ' the open with a protective

                                    ' stop at stopPrice.

 

The quantity of each order is determined by the Money Manager. The Broker object calls the Money Manager, and the Money Manager sets the quantity for each order.  If there is no Money Manager Block in the system, the unitSize defaults to 0. You must set the order quantity using order.SetQuantity() in the Unit Size script or the size will be 0 which will result in a trade that has no effect on your equity.

 

When a protective stop price is used in the entry order, that value is saved with the instrument and can be accessed using the instrument's unitExitStop property. The stop order itself, however, is only placed for the entry day. To 'hold' the stop and place it in the market every day, use the following code in your Exit Orders script:

 

broker.ExitAllUnitsOnStop( instrument.unitExitStop )

 

Many of our built-in systems use this method to "hold" stops. NOTE: this example is for a single unit system.

 

Symbol as the First Parameter: for all these broker functions, the instrument symbol can be passed in as the first parameter. This will place an order for the instrument, but only if the instrument is part of the system's portfolio. It will be ignored if the instrument is not part of the portfolio. If no symbol is passed in, the current default instrument context will be used. Passing no symbol is the recommended approach in the entry and exit scripts to avoid complications of placing orders on holidays and non trading days which can duplicate entry orders.

 

If manually looping over the instruments using the LoadSymbol function, in the Before Trading Day script as an example, be sure to check the instrument.tradesOnTradeDate property to make sure the instrument is trading before placing an order for the instrument.

 

 

broker.EnterLongOnOpen( instrument.symbol )

 

 

AlternateBroker

 

The alternateBroker is also a broker object and has all the same functions and properties. It is set by use of the test.SetAlternateSystem function. When the alternateSystem object is set, the alternateBroker object is also set to the same alternate system. In this way orders can be placed for any system, from any system, including from a GSS. So in a GSS the system can be looped over, and orders placed for any or all systems in the suite. From a GSS, the instrument.symbol needs to be the first parameter, as the GSS has no default instrument context.

 

Here is an example of placing an order for GC in system 1. This can be done from a GSS or any other system in the test. This assumes GC is in the portfolio for system 1.

 

 

IF inst.LoadSymbol( "F:GC", 1 ) THEN

 

 test.SetAlternateSystem( 1 )

 

 IF inst.isPrimed AND inst.position = OUT THEN

         alternateBroker.EnterLongOnopen( inst.symbol )

         

         IF alternateSystem.OrderExists() THEN

                 order.SetQuantity( 10 )

         ENDIF

 ENDIF

 

ELSE

 PRINT "Unabled to load symbol"

 

ENDIF