Code for bet size as Equity % using ATR

Forums ProRealTime English forum ProOrder support Code for bet size as Equity % using ATR

Viewing 7 posts - 1 through 7 (of 7 total)
  • #168163

    Dear Traders and Coders,

    I’ve coded a system that takes very short term entries using a simple moving average cross over, but also looks at the higher time frame to filter the direction it takes.

    I also use the ATR as the value for a trailing stop loss.

    My question is this:

    Is there a way to adjust bet size so that it’s 0.5% of account equity for each trade?

    This would need to take the ATR value and account equity into account and then adjust the bet size accordingly for each trade.

    Here is what I’ve cobbled together so far:

    Many Thanks

    Matt

    #168168

    This was my attempt, it seems to work, but the worst trade is always larger than 5% of 50,000. So that makes me think that at some point it’s not always doing it right. Because with a trailing SL the absolute maximum starting risk should be at £250 (5% of £50k) and often when booking a losing trade it would have moved into profit at least a bit and this would then have trailed the stop up meaning the ‘Loss of Worst Trade’ should always be under £250 regardless of the number of points the trailing stop loss used (taking 5x ATR as the max/start point).

    Anybody have any other suggestions or alternatives methods?

    Cheers

    #168218

    Try adding this snippet between lines 14 and 15 of your first code:

    it exits as soon as the current loss reaches your RISK level. Of course on a low TF this will be quite accurate, while on a higher TF (such as 1+ hour) the loss can easily exceed your planned risk (MTF might help much in this case).

    Change your Capital as needed.

    0,5% is quite low, it may turn your code into a hugely losing strategy.

     

     

    1 user thanked author for this post.
    #168302

    Thank you for the reply Roberto. I’ve added that snippet to the existing code, and added a few time restrictions. So now the code looks like this:

    It appears to be working, but it’s hard to tell as the backtest results at present on PRT seem to be giving some suspicious data back, occasionally only returning like 5 trades over 2 years, when clearly it should have easily placed hundreds. However, for the time being these are the results I’m seeing when run on the 5minute charts on the DOW.

    1 user thanked author for this post.
    #168306

    One issue is line 61, since it is executed every bar, it changes while a trade is open, affecting the trailing stop each time. It should be left unchanged when OnMarket:

    another issue is line 64, since SET STOP TRAILING only sets a pace, not a start. Most people (and myself as well) use the redy-to-run snippet by Nicolas from lines 17 to 56 at https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/.

     

     

    #168308

    Do I also need to repeat this snippet to apply to the short trades too? Or does adding it in between lines 14 and 15 work for both long and short orders?

    I’m afraid my novice skills are being tested here and I’m probably making more of a mess of it.

    Using Nicholas’ trailing SL code and your snippet above, this is what I have. It’s incredibly profitable, but it’s not right haha.

    There may well be some redundant code in there now, I really appreciate your help and patience here.

    Cheers

    #168309

    It works for both Long and Short trades.

    If you use MyATR then you have to change line 65:

    because MyATR is a (difference in) price, while that code requires pips.

    It works fine as it is now with Nasdaq, Dax and other indices, but not with other instruments such as fx currency pairs.

     

Viewing 7 posts - 1 through 7 (of 7 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login