When to stop a strategy and money management code snippet

Forums ProRealTime English forum ProOrder support When to stop a strategy and money management code snippet

Viewing 15 posts - 1 through 15 (of 22 total)
  • #109954

    Following on from a conversation about how to know when to stop an auto-trading strategy in another thread:

    https://www.prorealcode.com/topic/when-to-stop-a-live-automated-system/

    I decided to try and code something that could automate the whole idea. I also decided to add in some simple money management too. The code below can be added to a strategy and then ‘positionsize’ used for the number of contracts in any buy or sellshort order.

    The code works by checking every so many bars how the strategy is performing. It can check the following:

    • The win rate (after a minimum number of trades have happened) and then stop the strategy if the win rate is below a minimum desired win rate.
    • The draw down. If equity has dropped by a set percentage or more from the maximum profit ever achieved then the strategy is stopped.
    • The remaining capital. If the starting capital is reduced by a maximum allowed percentage then the strategy is stopped.
    • The percentage profit increase or decrease since the last position size adjustment. If the equity has risen or dropped by the set percentage or more then position size is increased or reduced to stay the same percentage of equity as it was percentage of capital when the strategy was first started.

     

    If using it then you have to be aware that decisions to quit or alter position size are only made on closed trades and only on every ‘barsbeforenextcheck’ bar and so a big losing trade still open is not taken into consideration and a bad losing run between checks can still wipe you out just before the strategy stops itself.

    Levels should be set based on your back test performance. For example if your strategy has a win rate of 60% then you might consider having it stop if win rate is halved at 30% as it is most likely under performing and you need to re-optimise it or just decide that it was over fitted and bin it.

    Hopefully this code is of use to someone.

     

    9 users thanked author for this post.
    #109976

    Added as Log 175 here …

    Snippet Link Library

     

    4 users thanked author for this post.
    #111051

    Thanks for the great money management and auto stop strategy snippet. Simple but powerful at the same time.

     

    #111054

    I also forgot to ask Vonasi, shouldn’t the last if clause on position size be minimum instead of maximum.

     

     

    #111056

    No. It is correct as it is. That line is to ensure that an order is never sent to market and then rejected because it is below the minimum order size allowed for the market.

    Set minpossize to whatever your broker allows as the smallest bet size for the instrument that you are trading.

    #174433

    With the Vonasi code. Reconfigure only so, if the average winning trades falls below 75% (example), the following trades are performed at half € pip than the last trade that was above 75% of the average winning trades. And if the average number of winning trades is above 75% again from now on, operate again at the usual € pip. And the same for if the drawdown is higher than 25%. Please!

     

    Serve as an example: The following code is used to operate at half € pip if the maxprofit is less than the maximum.
    ELEVEN MaxProfit = 0
    MaxProfit = max (MaxProfit, StrategyProfit)
    If MaxProfit> StrategyProfit Then
    Positionsize = max (1, Positionsize / 2)
    ENDIF

    #174434

    That is, I do not want the system to shut down with the “quit” function, but what is specified in the previous post

    #174442

    Firstly you have to tally all trades
    Secondly you have to tally winning trades
    Thirdly you have to make a percentage of winning trades
    Finally you write the instructions to accomplish to get to your goal.
    There you go (it won’t be accurate if accumulating positions):

     

    #174444

    Ok Roberto, thanks.

    And if  I have a drawdown of 25%???

    #174445

    What do you want to do in that case?

    #174447

    The same.

    #174460

    There you go:

    #174527

    Good Code Roberto!

     

    But i use only lines 10 to 15.

     

    Thanks!

    #174528

    ok … a new question.

     

    How would it be if, depending on the% drawdown, I wanted to trade with fractions of position.

     

    That is, if there is no drawdown I operate with € 10 pip …

    If the drawdown does not exceed 10% I will operate at € 10 pip.

    If the drawdown is between 10%, 30% will operate with € 5 pip.

    If the drawdown exceeds 30% I will operate with € 2.5 pip.

    #174535

    You cannot change the value of pips traded, ad this depends on the chosen position instrument (eg.; Dax 25€, Dax 5€ or Dax 1€).

    You can change your stop loss (usually not recommended) or lot size.

     

Viewing 15 posts - 1 through 15 (of 22 total)

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