position sizing - %risk

Questions about MultiCharts and user contributed studies.
fibonaccci
Posts: 49
Joined: 26 Dec 2009
Has thanked: 34 times
Been thanked: 1 time

position sizing - %risk

Postby fibonaccci » 01 Aug 2012

Hi,

can anyone help please with this part of money management code "%risk"
I just need some help to make a simple strategy with %risk-based MM for e.g. eurusd forex 100,000 standard lot.

Thank you.

Code: Select all


/////////////////////////// Percent Risk (based on CurrentBalance, RiskPct and StopLossAmount) ////////////////////////////////////////////////// /////////////////////////////////

Input: OriginalStake(100000); { how much money you started with }
Input: TradeLotSize(1); { generally, 1 for futures, 100 for stocks - multiplied by position size }
Input: MaxLotSize(100); { trade no more than this many contracts }
Input: Factor(1); { level of conservative/aggressive trading }
Input: IncludeOpenPL(false); { do we include Profit/Loss from current open trade in CurrentBalance? }
Input: RiskPct#3(0.3); { for %Risk #3 }
Input: StopLossPct#3#4(0.15); { for %Risk #3 and %Volatility #4 }


Var: CurrentBalance(0);
Var: ATRPeriod(20);
Var: FixedMarginLevel(1), FixedMarginBase(OriginalStake);

Value9 = 0; { default }
CurrentBalance = OriginalStake + NetProfit;
if IncludeOpenPL then CurrentBalance = CurrentBalance + OpenPositionProfit;

{ Percent Risk }
Value1 = C * BigPointValue * StopLossPct#3#4 / 100;
if Value1 <> 0 then Value9 = (RiskPct#3 / 100) * CurrentBalance / Value1;

if TradeLotSize > 0 then Value9 = Floor((Value9 + TradeLotSize / 2) / TradeLotSize) * TradeLotSize;
if Value9 < 1 then Value9 = 0;
if Value9 > MaxLotSize then Value9 = MaxLotSize;
PositionSizer = Value9 * Factor;


/////////////////////////// Percent Risk (based on CurrentBalance, RiskPct and StopLossAmount) ///////////////////////////////////////////////



Inputs: FastMA(7), MidMA(25), SlowMA(46), StopLossAmt(270), ProfitExitAmt(1280),BreakEven(400),FloorAmt(400),P ctTrailing(71);

Vars: PosSize(0), FastAvg(0), MidAvg(0), SlowAvg(0);
Var: TradeSignal (0); { 1 for long, -1 for short }

TradeSignal = 0;
FastAvg = Average(C,FastMA);
MidAvg = Average(C,MidMA);
SlowAvg = Average(C,SlowMA);
if FastAvg > SlowAvg and MidAvg > SlowAvg and FastAvg[1] < MidAvg[1] and FastAvg > MidAvg then TradeSignal = 1;
if FastAvg < SlowAvg and MidAvg < SlowAvg and FastAvg[1] > MidAvg[1] and FastAvg < MidAvg then TradeSignal = -1;

if TradeSignal <> 0 then begin
PosSize = PositionSizer( OriginalStake, MarginAmt, TradeLotSize, MaxLotSize, Factor, IncludeOpenPL,
RiskPct#3, StopLossPct#3#4, IncMarginPct#6#7, DecMarginPct#6#7, TrailingPct#7 );

if PosSize > 0 then begin
if TradeSignal = 1 then
Buy ("LE") next bar PosSize Contracts at market
else
Sell Short ("SE") next bar PosSize Contracts at market;
end;
end;

SetStopContract;
if StopLossAmt > 0 then SetStopLoss(StopLossAmt);
if ProfitExitAmt > 0 then SetProfitTarget(ProfitExitAmt);
SetBreakEven(BreakEven);
SetPercentTrailing(FloorAmt,PctTrailing);
SetExitOnClose;

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: position sizing - %risk

Postby TJ » 01 Aug 2012

Hi,

can anyone help please with this part of money management code "%risk"
I just need some help to make a simple strategy with %risk-based MM for e.g. eurusd forex 100,000 standard lot.

Thank you.
....
Can you describe in detail what type of operation you would want to do?

How many scenarios you will encounter? and what are their decision trees?

fibonaccci
Posts: 49
Joined: 26 Dec 2009
Has thanked: 34 times
Been thanked: 1 time

Re: position sizing - %risk

Postby fibonaccci » 01 Aug 2012

TJ,


I’d like to calculate the stop loss level and the PositionSize for a strategy with the %Risk formula. Then set a trailing stop.
PositionSize = (Equity*%Risk)/Volatility Std Dev
50,000 = (100,000 * 0.0015)/0.0030
1. Current(Starting)Balance = AccountEquity = i_ClosedEquity + AccountStart
or if possible AccountEquity = i_OpenEquity+ AccountStart
start with 100,000 EURUSD

2. StopLossLevel = a volatility-based (e.g. Volatility Std Dev(30)= 0.0030) StopLossLevel should be calculated e.g. 30Pips

3. RiskPct = maximum Risk per position (fixed level)
e.g. 0.15% of CurrentBalance

4. StopLossAmount = calculate USD amount at market risk
= 100,000 EURUSD x 0.0015 = 150 USD

5. PositionSize = 150 USD / 30 Pips = 50,000 USD

6. SetStopLoss = Volatility Std Dev(30)

7. If positionprofit > 0 then set trailing stop e.g. ATR


Code: Select all

Input: OriginalStake(100000); { starting capital }
Input: Risk(0.0015);

Inputs: FastMA(20), SlowMA(50);

Vars: CurrentBalance(0), PositionSize(0), VolatilityStdDev(30);
Vars: FastAvg(0), SlowAvg(0);

FastAvg = Average(C,FastMA);
SlowAvg = Average(C,SlowMA);



PositionSize = (CurrentBalance * Risk)/VolatilityStdDev;



if marketposition = 0 then
if FastAvg > SlowAvg and FastAvg[1] < SlowAvg[1] then
Buy ("LE") next bar PositionSize Contracts at market;

if marketposition = 0 then
if FastAvg < SlowAvg and FastAvg[1] > SlowAvg[1] then
Sell Short ("SE") next bar PositionSize Contracts at market;




// Stop loss //

if marketposition > 0 and positionprofit <= 0 then
begin
setstoploss(C-VolatilityStdDev);
end;

if marketposition < 0 and positionprofit <= 0 then
begin
setstoploss(C+VolatilityStdDev);
end;


// Stop loss becomes Trailing Stop //

[IntrabarOrderGeneration = true]

inputs: ATRLength( 10 ), NumATRs( 3 ) ;
variables: var0( 0 ), var1( 0 ), var2( 0 ) ;

var0 = AvgTrueRange( ATRLength ) * NumATRs ;
var1 = MarketPosition ;

if positionprofit > 0 then

if var1 = 1 then
begin
condition1 = var1[1] <> 1 or High > var2 ;
if condition1 then
var2 = High ;
Sell ( "AtrLX" ) next bar at var2 - var0 stop ;
end
else
Sell ( "AtrLX-eb" ) next bar at High - var0 stop ;


// Trailing Stop for Short position //

if positionprofit > 0 then

if var1 = -1 then
begin
condition1 = var1[1] <> -1 or Low < var2 ;
if condition1 then
var2 = Low ;
Buy To Cover ( "AtrSX" ) next bar at var2 + var0 stop ;
end
else
Buy To Cover ( "AtrSX-eb" ) next bar at Low + var0 stop ;


Return to “MultiCharts”