# 海龟交易系统

hrdeng 2013-03-03 19:25

Entry Orders:
VARIABLES: longEntryPrice, shortEntryPrice, unitIncrementATR, unitEntryPrice TYPE: Price
VARIABLES: entryOffsetATR TYPE: Price
VARIABLES: unitsOn, unitIndex TYPE: Integer
VARIABLES: brokerPrice TYPE: Price

' Calculate the unit increment, stop amount, and entry offset
stopWidth = stopInATR * averageTrueRange

'Total units currently on for this instrument
unitsOn = instrument.totalUnits

'----------------------------------------------------------------
'Place initial entry orders when out of the market
'----------------------------------------------------------------

' If we are not long (we are short or out) then place orders for tomorrow
' We will place orders for all potential units in case it's a big day
IF instrument.position <> LONG THEN

' Determine whether to use the regular entry high or the failsafe

' Loop over the potential units and place an order
FOR unitIndex = 1 TO maxUnits
brokerPrice = longEntryPrice + ( ( unitIndex - 1 ) * unitIncrementATR )
broker.EnterLongOnStop( brokerPrice, brokerPrice - stopWidth )
NEXT

ENDIF

' If we are not short (we are long or out) then place orders for tomorrow
' We will place orders for all potential units in case it's a big day
IF instrument.position <> SHORT THEN

' Determine whether to use the regular entry low or the failsafe

' Loop over the potential units and place an order
FOR unitIndex = 1 to maxUnits
brokerPrice = shortEntryPrice - ( ( unitIndex - 1 ) * unitIncrementATR )
broker.EnterShortOnStop( brokerPrice, brokerPrice + stopWidth )
NEXT

ENDIF

'----------------------------------------------------------------
'----------------------------------------------------------------

'If long and can add more units
IF ( instrument.position = LONG ) AND ( unitsOn < maxUnits ) THEN

' Loop over potential units to add, placing orders at maximum of regular entryHigh or unit increment
FOR unitIndex = unitsOn + 1 TO maxUnits
unitEntryPrice = instrument.unitEntryFill[ unitsOn ] + ( unitIndex - unitsOn ) * unitIncrementATR
brokerPrice = max( offsetAdjustedEntryHigh, unitEntryPrice )
broker.EnterLongOnStop( brokerPrice, brokerPrice - stopWidth)
NEXT
ENDIF

'If short and can add more units
IF ( instrument.position = SHORT ) AND ( unitsOn < maxUnits ) THEN

' Loop over potential units to add, placing orders at minimum of regular entryLow or unit increment
FOR unitIndex = unitsOn + 1 TO maxUnits
unitEntryPrice = instrument.unitEntryFill[ unitsOn ] - ( unitIndex - unitsOn ) * unitIncrementATR
brokerPrice = min( offsetAdjustedEntryLow, unitEntryPrice )
broker.EnterShortOnStop( brokerPrice, brokerPrice + stopWidth )
NEXT
ENDIF

Entry Order Filled:
VARIABLES: stopAmount, stopPrice, originalATR TYPE: Price
VARIABLES: unitFillPrice, newStopPrice TYPE:Price
VARIABLES: newUnitExitStop, originalExitStop, newExitStop TYPE: Price
VARIABLES: entryDayIndex TYPE: Integer
VARIABLES: unitsOn, unitIndex TYPE: Integer

'----------------------------
' Get the total number of units on now, including this new unit
unitsOn = instrument.totalUnits

' Calculate the new exit stop for this new unit, based on the fill price
if instrument.position = LONG then newUnitExitStop = order.fillPrice - order.entryRisk
if instrument.position = SHORT then newUnitExitStop = order.fillPrice + order.entryRisk

' Set exit stop for this new unit added
instrument.setExitStop( unitsOn, newUnitExitStop )

'------------------------------
' If more than one unit on, then
' Reset the stops for all units, except the current one, based on this new unit
' For each unit, look for the original ATR and calculate the original stop price
' Add to the original stop
'------------------------------

IF unitsOn > 1 THEN
IF instrument.position = LONG THEN
FOR unitIndex = 1 TO unitsOn - 1

' Compute the ATR TYPE: of the entry for this unit.
entryDayIndex = instrument.bar - instrument.unitEntryDayIndex[ unitIndex ]
originalATR = averageTrueRange[ entryDayIndex ]

' Get the fill price.
unitFillPrice = instrument.unitentryfill[ unitIndex ]

' Compute the original exit stop.
originalExitStop = unitFillPrice - stopInATR * originalATR

' Now compute the new stop based on the original ATR.
newStopPrice = originalExitStop + ( unitsOn - unitIndex ) * unitAdd * originalATR

' Constrain the stop to the fill price or the new unit stop.
newExitStop = min( newStopPrice, unitFillPrice, newUnitExitStop )

' Give the instrument this new stop.
instrument.setExitStop( unitIndex, newExitStop )
NEXT
ENDIF

IF instrument.position = SHORT THEN
FOR unitIndex = 1 TO unitsOn - 1

' Compute the ATR TYPE: of the entry for this unit.
entryDayIndex = instrument.bar - instrument.unitEntryDayIndex[ unitIndex ]
originalATR = averageTrueRange[ entryDayIndex ]

' Get the fill price.
unitFillPrice = instrument.unitentryfill[ unitIndex ]

' Compute the original exit stop.
originalExitStop = unitFillPrice + stopInATR * originalATR

' Now compute the new stop based on the original ATR.
newStopPrice = originalExitStop - ( unitsOn - unitIndex ) * unitAdd * originalATR

' Constrain the stop to the fill price or the new unit stop.
newExitStop = max( newStopPrice, unitFillPrice, newUnitExitStop )

' Give the instrument this new stop.
instrument.setExitStop( unitIndex, newExitStop )
NEXT
ENDIF
ENDIF

' ----------------------------------
' Track theoretical positions
' ----------------------------------

' Only run these calcs if we have to
IF ( tradeIfWinner = FALSE ) THEN

' If this is the first unit on, then set the theoretical position at this point
IF unitsOn = 1 THEN
theoreticalPosition = instrument.position
theoreticalEntryPrice = order.fillprice
theoreticalExitStop = order.stopprice
ENDIF
ENDIF

Exit Orders;
VARIABLES: brokerPrice, exitPrice, unitCurrentStopPrice TYPE: Price
VARIABLES: unitsOn, unitIndex TYPE: Integer

unitsOn = instrument.totalUnits

' If long...
IF ( instrument.position = LONG ) THEN

' Loop over the units and place stop order at the best price,
' including current stop price.
FOR unitIndex = 1 TO unitsOn
unitCurrentStopPrice = instrument.unitExitStop[ unitIndex ]
brokerPrice = max( exitPrice, unitCurrentStopPrice )
broker.ExitUnitOnStop( unitIndex, brokerPrice )
NEXT
ENDIF

' If short...
IF ( instrument.position = SHORT ) THEN

'Loop over units and place stop order at best price,
' including curreen stop price.
FOR unitIndex = 1 TO unitsOn
unitCurrentStopPrice = instrument.unitExitStop[ unitIndex ]
brokerPrice = min( exitPrice, unitCurrentStopPrice )
broker.ExitUnitOnStop( unitIndex, brokerPrice )
NEXT
ENDIF

VARIABLES: stopPrice, bestExitPrice, unitCurrentStopPrice TYPE: Price
VARIABLES: unitsOn, unitIndex TYPE: Integer

' Get the total units on.
unitsOn = instrument.totalUnits

' If long...
IF ( instrument.position = LONG ) THEN

' Loop over the units and set the stop to the best price,
' including current stop price.
FOR unitIndex = 1 TO unitsOn

' Get the current exit stop.
unitCurrentStopPrice = instrument.unitExitStop[ unitIndex ]

' If the best stop is better than our current stop then
' exit on this stop.
IF bestExitPrice > unitCurrentStopPrice THEN
instrument.SetExitStop( unitIndex, bestExitPrice )
broker.exitUnitOnStop( unitIndex, bestExitPrice )
ENDIF
NEXT
ENDIF

' If short...
IF ( instrument.position = SHORT ) THEN

' Loop over the units and set the stop to the best price,
' including curreen stop price.
FOR unitIndex = 1 TO unitsOn

' Get the current exit stop.
unitCurrentStopPrice = instrument.unitExitStop[ unitIndex ]

' If the best stop is better than our current stop then
' exit on this stop.
IF bestExitPrice < unitCurrentStopPrice THEN
instrument.SetExitStop( unitIndex, bestExitPrice )
broker.exitUnitOnStop( unitIndex, bestExitPrice )
ENDIF
NEXT
ENDIF

After Instrument Day:
VARIABLES: theoreticalExitPrice TYPE: PRICE

' Calc stop width
stopWidth = stopInATR * averageTrueRange

' -----------------------------
' Track theoretical positions
' ----------------------------

IF ( tradeIfWinner = FALSE ) THEN

' Update theoretical profit mark to market
IF ( theoreticalPosition = LONG ) THEN
lastTradeProfit = ( instrument.close - theoreticalEntryPrice )
ENDIF

if ( theoreticalPosition = SHORT ) THEN
lastTradeProfit = ( theoreticalEntryPrice - instrument.close )
ENDIF

' If not long, then watch for long entry
IF ( theoreticalPosition <> LONG ) THEN
IF ( instrument.high > offsetAdjustedEntryHigh[1] ) THEN
theoreticalPosition = LONG
theoreticalExitStop = theoreticalEntryPrice - stopWidth
ENDIF
ENDIF

' If not short, then watch for short entry
IF ( theoreticalPosition <> SHORT ) THEN
theoreticalPosition = SHORT
theoreticalExitStop = theoreticalEntryPrice + stopWidth
ENDIF
ENDIF

' If long, then watch for exits
IF ( theoreticalPosition = LONG ) THEN
IF ( instrument.low < theoreticalExitPrice ) THEN
lastTradeProfit = ( theoreticalExitPrice - theoreticalEntryPrice )
theoreticalPosition = OUT
ENDIF
ENDIF

' If short, then watch for exits
IF ( theoreticalPosition = SHORT ) THEN
if ( instrument.high > theoreticalExitPrice ) THEN
lastTradeProfit = ( theoreticalEntryPrice - theoreticalExitPrice )
theoreticalPosition = OUT
ENDIF
ENDIF

ENDIF

http://blog.sina.com.cn/s/blog_55fe8db60100pvjd.html

Page 1 of 1 1