position sizing - %risk
Posted: Aug 01 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 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;