Moving Average Cross with RSI – 1% risk
Forums › ProRealTime English forum › ProBuilder support › Moving Average Cross with RSI – 1% risk
- This topic has 4 replies, 2 voices, and was last updated 1 year ago by JS.
-
-
06/06/2023 at 12:28 PM #215707
Hi team,
I am hoping you can help – I wondered if someone was willing to cast an eye over my code. Fairly simple moving average cross with some ‘noise cancelling’ elements.
Can I get this to calculate 1% risk of the account balance? I’d like to periodically update the value once or twice weekly to allow for compounding.
I have attempted to do this, but my maths and intelligence have failed me – it blows up my account.
Possible for someone more intelligent to take a look?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768defparam cumulateOrders = false// IndicatorsSMA50 = average[50](close)SMA200 = average[200](close)RSI14 = RSI[14](close)RSI233 = RSI[233](close)EMA20 = ExponentialAverage[20](close)EMA9 = ExponentialAverage[9](close)ATR14 = AverageTrueRange[14]ADX14 = ADX[14]BBUpper = BollingerUp[20](close)BBLower = BollingerDown[20](close)//Chandalier StopChandelierPeriod = 22ChandelierMultiplier = 8ChandelierStopLong = highest[ChandelierPeriod](high) - ChandelierMultiplier * ATR14ChandelierStopShort = lowest[ChandelierPeriod](low) + ChandelierMultiplier * ATR14//Account SettingsAccountBalance = 100000RiskPercentage = 1.25//Position SizingRiskAmount = AccountBalance * (RiskPercentage/100)// Entry rulesBuySignal = (SMA50 crosses over SMA200) AND (RSI14 > 50) AND (close > EMA20) AND (ADX14 > 20) AND (EMA9 > EMA20) AND (close > BBLower) AND (close < BBUpper)SellSignal = (SMA50 crosses under SMA200) AND (RSI14 < 50) AND (close < EMA20) AND (ADX14 > 20) AND (EMA9 < EMA20) AND (close < BBUpper) AND (close > BBLower)//Position sizing for each tradeBuyPositionSize = floor(RiskAmount/ (Close - ChandelierStopLong))SellPositionSize = floor(RiskAmount/ (ChandelierStopShort - Close))// Scaling in/out mechanismIf BuySignal AND LongOnMarket = 0 THENBuy BuyPositionSize shares at marketENDIFIf SellSignal AND ShortOnMarket = 0 THENSellshort SellPositionSize shares at marketENDIF// Exit rulesCloseBuyPosition = (SMA50 crosses under SMA200) OR (RSI233 < 50)CloseSellPosition = (SMA50 crosses over SMA200) OR (RSI233 > 50)// Risk managementIf LongOnMarket THENSell at ChandelierStopLong StopEndIfIf ShortonMarket THENExitshort at ChandelierStopShort StopEndIfIf CloseBuyPosition THENSell at marketENDIFIf CloseSellPosition THENExitshort at marketENDIF06/06/2023 at 4:34 PM #215726Hi @Mitchy14
I haven’t gone through your code all the way, but I see that you are using the ATR to calculate your “ChandelierStopLong” and “ChandelierStopShort” and that is fine, but you also use it to calculate your position size:
BuyPositionSize = floor(RiskAmount / (Close – ChandelierStopLong))
So, you are going to divide a sum of money (RiskAmount) by a number that can basically take all values… (depending on the volatility).
For example, if (Close – ChandelierStopLong) = 70 then your BuyPositionSize is 1250/70=17 contracts and at a value of 4 your position size becomes 1250/4=312 contracts… (it will blow up your account)
1 user thanked author for this post.
06/07/2023 at 6:19 AM #215745You can also divide your capital risk directly by the ATR (position size depending on the ATR/volatility) and set certain conditions for the maximum and minimum position size, for example:
Once MinPositionSize=1
Once MaxPositionSize=10
PositonSize = Risk / ATR14
If PositionSize >= MaxPositionSize then
PositionSize = MaxPositionSize
ElsIf PositionSize <= MinPositionSize then
PositionSize = MinPositionSize
EndIf
1 user thanked author for this post.
06/07/2023 at 1:53 PM #215758Hi JS,
Thanks so much for your suggestions. You’ll notice I have updated the code.
It doesn’t seem to amend the qty from 10 contracts (no matter what variables I amend). As you know, I’d like this number to change depending on the ATR value.
Again, I am sure it is something fairly simple, but I don’t seem to see it.
Any suggestions would be welcome!
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879defparam cumulateOrders = True// IndicatorsSMA50 = average[50](close)SMA200 = average[200](close)RSI14 = RSI[14](close)RSI233 = RSI[233](close)EMA20 = ExponentialAverage[20](close)EMA9 = ExponentialAverage[9](close)ATR14 = AverageTrueRange[14]ADX14 = ADX[14]BBUpper = BollingerUp[20](close)BBLower = BollingerDown[20](close)//Chandalier StopChandelierPeriod = 22ChandelierMultiplier = 4ChandelierStopLong = highest[ChandelierPeriod](high) - ChandelierMultiplier * ATR14ChandelierStopShort = lowest[ChandelierPeriod](low) + ChandelierMultiplier * ATR14//Account SettingsAccountBalance = 100000RiskPercentage = 1.25// Entry rulesBuySignal = (SMA50 > SMA200) AND (RSI14 > 50) AND (close > EMA20) AND (ADX14 >= 20) AND (EMA9 Crosses over EMA20) AND (close > BBLower) AND (close < BBUpper)SellSignal = (SMA50 < SMA200) AND (RSI14 < 50) AND (close < EMA20) AND (ADX14 >= 20) AND (EMA9 Crosses under EMA20) AND (close < BBUpper) AND (close > BBLower)//Position sizing for each tradeRiskAmount = AccountBalance * (RiskPercentage/100)MinPositionSize=1MaxPositionSize=10pipValuePerContract = 10 // change this based on the pip value per contract for the specific security tradingPositionSize = RiskAmount / (ATR14*pipValuePerContract)// Position sizing checkIf PositionSize >= MaxPositionSize thenPositionSize = MaxPositionSizeElsIf PositionSize <= MinPositionSize thenPositionSize = MinPositionSizeEndIf// Entry mechanismIf BuySignal AND LongOnMarket = 0 THENBuy PositionSize contract at marketENDIFIf SellSignal AND ShortOnMarket = 0 THENSellshort PositionSize contract at marketENDIF// Exit rulesCloseBuyPosition = (SMA50 crosses under SMA200) OR (RSI233 <= 50)CloseSellPosition = (SMA50 crosses over SMA200) OR (RSI233 >= 50)// Risk managementIf LongOnMarket THENSell at ChandelierStopLong StopEndIfIf ShortonMarket THENExitshort at ChandelierStopShort StopEndIfIf CloseBuyPosition THENSell at marketENDIFIf CloseSellPosition THENExitshort at marketEndIF06/07/2023 at 2:54 PM #215765Hi @Mitchy14
The ATR (volatility) in the calculation of your position size is always difficult because the volatility can fluctuate so much…
Alternatively, you can try a different money management system with an (automatic) reinvestment of your winnings:
RiskAmount = AccountBalance * (RiskPercentage/100)
Margin = 5 / 100 (margin percentage for the major US indices)
PositionSize = (RiskAmount + StrategyProfit) / (Close*Margin)And below you can of course have the “Position sizing check” performed…
-
AuthorPosts
Find exclusive trading pro-tools on