<?xml version="1.0" encoding="US-ASCII" standalone="yes"?>

<block_definition name="Portfolio Risk Manager" group="Risk Manager" type="Auxiliary" sectionname="Risk Manager">

	<!-- Block Permanent Variables -->
	<block_global basic_type="Float" version="10">
		<script_name>reductionPercent</script_name>
		<display_name>reduction %</display_name>
		<plots>False</plots>
		<display>False</display>
		<plots_log>False</plots_log>
		<plot_area></plot_area>
		<color>0</color>
		<offsets_plot_one_day>False</offsets_plot_one_day>
		<plot_as_line>True</plot_as_line>
		<plot_style></plot_style>
		<variable_scope>Block</variable_scope>
		<auto_index>True</auto_index>
		<series_size>100</series_size>
		<variable_type>-1</variable_type>
		<external>False</external>
		<default_value>0.000000</default_value>
	</block_global>
	<block_global basic_type="Float" version="10">
		<script_name>riskPercent</script_name>
		<display_name></display_name>
		<plots>False</plots>
		<display>False</display>
		<plots_log>False</plots_log>
		<plot_area>Price Chart</plot_area>
		<color>16711680</color>
		<offsets_plot_one_day>False</offsets_plot_one_day>
		<plot_as_line>True</plot_as_line>
		<plot_style>Thin Line</plot_style>
		<variable_scope>Block</variable_scope>
		<auto_index>True</auto_index>
		<series_size>100</series_size>
		<variable_type>-1</variable_type>
		<external>False</external>
		<default_value>0.000000</default_value>
	</block_global>
	<block_global basic_type="Integer" version="10">
		<script_name>index</script_name>
		<display_name></display_name>
		<plots>False</plots>
		<display>False</display>
		<plots_log>False</plots_log>
		<plot_area>Price Chart</plot_area>
		<color>16711680</color>
		<offsets_plot_one_day>False</offsets_plot_one_day>
		<plot_as_line>True</plot_as_line>
		<plot_style>Thin Line</plot_style>
		<variable_scope>Block</variable_scope>
		<auto_index>True</auto_index>
		<series_size>100</series_size>
		<variable_type>-1</variable_type>
		<external>False</external>
		<default_value>0</default_value>
	</block_global>

	<!-- Instrument Permanent Variables -->

	<!-- Block Parameters -->
	<parameter parameter_type="Percent">
		<script_name>maximumRiskThreshold</script_name>
		<display_name>Maximum Risk Threshold %</display_name>
		<default_value>0.300000</default_value>
		<used_for_lookback>False</used_for_lookback>
		<variable_scope>Block</variable_scope>
		<stepping_priority>0</stepping_priority>
		<stepping_enabled>True</stepping_enabled>
		<dependent_source></dependent_source>
		<dependent_value>0</dependent_value>
	</parameter>
	<parameter parameter_type="Selector">
		<script_name>reductionAlgorithm</script_name>
		<display_name>Reduction Algorithm</display_name>
		<default_value>2</default_value>
		<used_for_lookback>False</used_for_lookback>
		<variable_scope>Block</variable_scope>
		<stepping_priority>0</stepping_priority>
		<stepping_enabled>True</stepping_enabled>
		<dependent_source></dependent_source>
		<dependent_value>0</dependent_value>
		<selector_entry>None</selector_entry>
		<selector_entry>Reduce Positions</selector_entry>
		<selector_entry>Move Stops</selector_entry>
	</parameter>
	<parameter parameter_type="Boolean">
		<script_name>useCanFill</script_name>
		<display_name>Process Fills</display_name>
		<default_value>TRUE</default_value>
		<used_for_lookback>False</used_for_lookback>
		<variable_scope>Block</variable_scope>
		<stepping_priority>0</stepping_priority>
		<stepping_enabled>True</stepping_enabled>
		<dependent_source></dependent_source>
		<dependent_value>0</dependent_value>
	</parameter>

	<!-- Block Indicators -->

	<!-- Block Scripts -->
	<script type="before_trading_day" name="Before Trading Day" version="200010">

		<!-- Break Points -->

		<!-- Script Code -->
		<![CDATA[
IF system.tradingEquity > 0 THEN

	' Compute the current risk.
	riskPercent = system.currentRisk / system.tradingEquity

	' If the risk is above our threshold...
	IF riskPercent > maximumRiskThreshold THEN

		reductionPercent = (riskPercent - maximumRiskThreshold) / riskPercent
		
	ELSE

		reductionPercent = 0.0
		
	ENDIF

ELSE

	reductionPercent = 0.0
	
ENDIF

]]>
	</script>
	<script type="before_instrument_day" name="Before Instrument Day" version="200010">

		<!-- Break Points -->

		<!-- Script Code -->
		<![CDATA[VARIABLES: quantity, reductionQuantity TYPE: Integer
VARIABLES: risk TYPE: Floating
VARIABLES: newStop TYPE: Price

' If we need to reduce risk
IF reductionPercent > 0.0 THEN

	IF reductionAlgorithm = REDUCE_POSITIONS THEN
	
		' Reduce the position by this amount.
		broker.AdjustPositionOnOpen( 1.0 - reductionPercent )
	
	ENDIF
	
	IF reductionAlgorithm = MOVE_STOPS THEN
	
		IF instrument.position = LONG THEN
		
			' Adjust the stops for each unit.
			FOR index = 1 to instrument.currentPositionUnits
			
				' Determine the current risk.
				risk = instrument.close - instrument.unitExitStop[ index ]
				
				' Determine the stop that corresponds with the reduced risk.
				newStop = instrument.close - ((1 - reductionPercent) * risk)
				
				' Set the new stop.
				instrument.SetExitStop( index, newStop )
				broker.ExitUnitOnStop( index, newStop )
			NEXT
	
		ENDIF ' Long
		
		IF instrument.position = SHORT THEN
		
			' Adjust the stops for each unit.
			FOR index = 1 to instrument.currentPositionUnits
			
				' Determine the current risk.
				risk = instrument.unitExitStop[index] - instrument.close
				
				' Determine the stop that corresponds with the reduced risk.
				newStop = instrument.close + ((1 - reductionPercent) * risk)
				
				' Set the new stop.
				instrument.SetExitStop( index, newStop )
				broker.ExitUnitOnStop( index, newStop )
			NEXT
			
		ENDIF ' Short

	ENDIF  ' Algorithm Move Stops

ENDIF ' There is a reduction required




]]>
	</script>
	<script type="can_add_unit" name="Can Add Unit" version="200010">

		<!-- Break Points -->

		<!-- Script Code -->
		<![CDATA[
IF system.currentRisk / system.totalEquity > maximumRiskThreshold THEN
	order.Reject("Portfolio Risk exceeds threshold." )
ENDIF



]]>
	</script>
	<script type="can_fill_order" name="Can Fill Order" version="200010">

		<!-- Break Points -->

		<!-- Script Code -->
		<![CDATA[IF useCanFill THEN
	IF system.currentRisk / system.totalEquity > maximumRiskThreshold THEN
		order.Reject("Portfolio Risk exceeds threshold." )
	ENDIF
ENDIF
]]>
	</script>

</block_definition>
