EasyLanguage Script for Position Sizing

I've been working on a day trading breakout system, using different time-frames. What I would like to do is risk the same amount of money per trade, even if the range is different. This would mean if a stock is trading has a low of day at $100, and a high of $110, I would want to buy it at $110.01 and put my stop-loss at $99.99. Say I wanted to risk $100 on this trade. I would want to have it automatically calculate to buy 10 shares if it break the high of day. It would look something like, TradeQuantity = RiskAmount / (OpeningRngLow / (OpeningRngHigh - OpeningRngLow));
and also make sure (OpeningRngHigh - OpeningRngLow) is not zero. My easylanguage knowledge is very basic and I've added the new inputs. I just can't figure out where to add the code to have my TradeQuantity automatically calculated.
 
This is the current breakout code I'm using.

inputs:
BreakOutTicksRqd( 5 ),
NumBarsToSetRng( 3 ),
DollarTgtPerUnit( 0.5 ),
DollarStopPerUnit( 0.5 ),
MaxEntriesPerDay( 1 ),
TradeQuanity (0),
RiskAmount (0),
DrawTrendLines( true );
variables:
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
Buy ( "BrkOut LE" )next bar at OpeningRngHigh + BreakOutTicksRqd * OneTick Stop ;
Sell Short ( "BrkOut SE" ) next bar at OpeningRngLow - BreakOutTicksRqd * OneTick Stop ;
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;
 
Code:
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;
 
Last edited:
Back
Top