inputs:
BreakOutTicksRqd( 5 ),
NumBarsToSetRng( 3 ),
DollarTgtPerUnit( 0.5 ),
DollarStopPerUnit( 0.5 ),
MaxEntriesPerDay( 1 ),
RiskAmount (100),
DrawTrendLines( true );
variables:
TradeQuanity (0), //
OpeningRngHigh( 0 ),
OpeningRngLow( 0 ),
OpeningBarNum( 0 ),
OneTick( 0 ),
TL_ID_High( 0 ),
TL_ID_Low( 0 );
Once
begin
OneTick = MinMove / PriceScale ;
SetStopShare ;
end ;
// Get the opening range
if CurrentSession(0) <> CurrentSession(0)[1] then
begin
OpeningBarNum = CurrentBar ;
OpeningRngHigh = High ;
OpeningRngLow = Low ;
if DrawTrendLines then
begin
TL_ID_High = TL_New( Date, Time, OpeningRngHigh, Date, Time, OpeningRngHigh ) ;
TL_ID_Low = TL_New( Date, Time, OpeningRngLow, Date, Time, OpeningRngLow ) ;
end ;
end
else
if CurrentBar - OpeningBarNum < NumBarsToSetRng then
begin
if High > OpeningRngHigh then OpeningRngHigh = High ;
if Low < OpeningRngLow then OpeningRngLow = Low ;
end ;
// Adjust the TrendLines
if CurrentBar - OpeningBarNum = NumBarsToSetRng - 1 then
begin
if TL_ID_High > 0 then
TL_SetBegin( TL_ID_High, Date, Time[NumBarsToSetRng - 1], OpeningRngHigh );
if TL_ID_Low > 0 then
TL_SetBegin( TL_ID_Low, Date, Time[NumBarsToSetRng - 1], OpeningRngLow );
end ;
if TL_ID_High > 0 then
TL_SetEnd( TL_ID_High, Date, Time, OpeningRngHigh );
if TL_ID_Low > 0 then
TL_SetEnd( TL_ID_Low, Date, Time, OpeningRngLow );
// Issue Buy/Sell Short Stop orders
// After Range is Set
// Only if Flat (ie don't reverse)
// Limit the Number of Entries per Day
// Don't issue order on last bar of Day
if CurrentBar - OpeningBarNum >= NumBarsToSetRng - 1 and
MarketPosition = 0 and
EntriesToday( Date ) < MaxEntriesPerDay and
Time <> SessionEndTime( 0,1 ) then
begin
if OpeningRngHigh > OpeningRngLow then Begin
TradeQuanity = round(RiskAmount / ( (OpeningRngHigh + BreakOutTicksRqd * OneTick) - (OpeningRngLow - OneTick) ),0);
if TradeQuanity > 0 then Begin
Buy ( "BrkOut LE" ) next bar TradeQuanity shares at OpeningRngHigh + BreakOutTicksRqd * OneTick Stop ;
end;
TradeQuanity = round(RiskAmount / ( (OpeningRngHigh + OneTick ) - (OpeningRngLow - BreakOutTicksRqd * OneTick) ),0);
if TradeQuanity > 0 then Begin
Sell Short ( "BrkOut SE" ) next bar TradeQuanity shares at OpeningRngLow - BreakOutTicksRqd * OneTick Stop ;
end;
end;
end ;
if MarketPosition <> 0 then
begin
// Exit If reverse through opposite side of Range
Sell ( "OpRng LX" )next bar at OpeningRngLow - OneTick Stop ;
Buy To Cover ( "OpRng SX" ) next bar at OpeningRngHigh + OneTick Stop ;
end ;
// Built in Stops and Targets
SetStopLoss( DollarStopPerUnit ) ;
SetProfitTarget( DollarTgtPerUnit ) ;
// Set Exit on Close for Backtesting
SetExitonClose;