How to improve a strategy with simulated trades – part 1

This new article deals with a code snippet that can improve a strategy by stopping the real trading when the performance of the strategy decreases significantly, to simulate new orders by the code so that, as soon as the strategy performs again, restart auto trading in real time.

The simulation of trading orders is a point that has been addressed already on the site, especially in this old article of the blog equity curve trading and also in some topics of the forum.

I had not yet managed to find the time to put the idea into ProBuilder language so as to allow the first tests and application but it is now done with this new article, I hope it will arouse interest and that the bravest of you will be able to carry out tests and extensions on the subject, from this code 😉

Trading is a game, a statistical simulation

Even knowing that a strategy is profitable, it often happens that the distribution of orders can act in this way:
Out of 20 orders in total, 12 were winners and we closed in loss 8 times, nothing very extraordinary, yes but if these 8 losses had been successive … What a pity! This will have the effect, very often, to no longer trust the strategy, to stop it or again to restart working on it so that it can no longer suffer from this type of series of losses.

The code that follows will allow us, for example, to stop the strategy when the performance, due to these successive losses allows us to say: “ok it’s time to take a break”.
We will simply perform an average of the last X performances to assess whether the strategy currently performs or not. If the performance falls below this average, then we simulate false orders and test them to see if the performance has returned. If so, then we will re-engage automatic trading. Easy! 🙂

Application on a trend following strategy

A simple strategy made of moving averages crosses (7 and 21 periods), buying only, with takeprofit and fixed stoploss, let’s look at its equity curve:

(backtest performed on Mini DAX FUTURES, 5 minutes, 2 points spread).
There is uneven performance over time, but logically expected due to rather bearish periods and range areas where a trend following strategy is not suitable.

Let’s look at the cumulative performance of the orders and make an average of the last 20 performances:

When the performances fall below this blue line, we will start our “fake orders”, here is the complete code:


Explanations about the function code and how it works are inside the code itself.

It should be noted that in order to calculate this average, it will of course be necessary to wait for a sufficient number of real orders to have taken place (ie 20 in the present case), therefore during the first 20 orders, the strategy will be trading all by itself, help! 🙂

The results are encouraging, the strategy stops trading during bad times, compare the 2 equities curves, below is the basic strategy, above the strategy with the simulated orders:


The false orders are represented by the green line at the bottom of the graph, we note the performances that accumulates well on the black curve (and on the average in blue), while there is no order really at the market!

A magic function? never lose again?

We could indeed go to that conclusion. The worst strategy deserves this feature to be added. However, a strategy that performs well in the contexts for which it has been studied and coded, should not find any use there. In any case, that’s the conclusion I draw from it.
However and in summary, I will say that indeed, this function, added to your strategy will allow you not to go beyond serious problems and could even improve these performances, in most of the cases.

Here, we can conclude that the strategy benefits from the principle of orders simulation, since we see that the equity curve follows a constant rise. Even if the actual result, it is not entirely satisfactory, we managed to significantly improve the performance!

Interested in adding to your strategy, privately? Contact our programming service.

What is the next step ?

This concept demonstrated and coded in this article exists only at an experimental stage and should undoubtedly be experienced further by the community 🙂
In addition, fake orders are tested only once per bar when the code is read, which would not be the case with stoploss and takeprofit in real time since these levels are registered
in the order book of the broker. Also, I did not use takeprofit and dynamic stoploss, but only fixed levels … some work is still to do if we want to simulate all possibilities of a real strategy and real orders facing the market!

In the next article, I will add the code to handle short selling positions. I will also discuss the use of other calculation methods to consider whether it is whether or not to return to real positions again, using technical indicators such as the RSI calculated on the last X performances, for example.

Share this

  1. criscolci • 09/13/2018 #


  2. Frankyboy • 303 days ago #

    Thank you, Nicolas, for this really creative idea! I think this can be an asset for many trading strategies!

    I just tried the code you posted above for the DAX, but there was a problem: the variable “avg” was always 0 in the entire backtest, and as a consequence simulated orders only occurred when the strategy profit dipped below 0. I don’t know exactly what is going on, but I suspect it could have to do with the FOR xx TO yy DO loop in the code. I have experienced earlier that a “FOR x=1 TO 1000 DO” loop of an indicator worked perfectly when launching the indicator, but the same loop performed only until 500 after it was integrated in a trading strategy (either directly or via CALL). Have you encountered similar things before? Could this be a bug in PorOrder? (I am using PRT Premium, v. 10.3)

    Thanks in advance for helping to clarify this!

    • Nicolas • 298 days ago #

      To correctly compute an average of N periods, you need N elapsed bars. Or N closed orders if N is the amount of orders to calculate the average.

    • waid • 243 days ago #

      Same problem as Franky boys
      I’d just directly put the code into DAX with 5-minutes timeframe without any modification. But the blue line (avg) is always flat (zero).
      Not sure whether the cause is due to the loop limit or not. Keep experimenting.
      version last update: 2019/01/03

  3. Frankyboy • 296 days ago #

    Thanks Nicolas! The problem is that in my version of ProOrder (trading strategies), the “FOR x=1 TO 1000 DO” only loops until 500 even if many more than 1000 bars are available. Strangely, in ProBuilder (for indicators), it works fine. I am already in contact with people from PRT. I hope this issues can be resolved soon…

  4. CharlesUmani • 276 days ago #

    can i modify to use it without any stop loss and take profit ? I just need fake orders nothing else please 🙂

    • Nicolas • 276 days ago #

      If your orders exit rely on conditions, I think that is possible to adapt the above codes.

  5. pro-index • 178 days ago #

    what is real strategy ? I can not understand.

    • Nicolas • 170 days ago #

      You mean “real orders”?

  6. Gianluca • 2 hours ago #

    Ciao you is there avaible also the part 2 for short orders?

Register or