Simulated Trading

Viewing 15 posts - 1 through 15 (of 53 total)
  • Author
    Posts
  • #80482 quote
    Vonasi
    Moderator
    Master

    As Nicolas has posted a great new blog on using simulated trading to improve strategy performance at:

    https://www.prorealcode.com/blog/learning/how-to-improve-a-strategy-with-simulated-trades-1/

    I thought that I would post something a little similar (but not as good!) that I wrote a while back. I have never used it in anger in a live or demo strategy as I felt the value of the average period was too critical as to whether it worked well or not. Maybe a better filter can be used?

    x = 20 //average period
    positionsize = 5 //tradesize
    
    if equitycurve >= average[x](equitycurve) or tradecount <= x then
    trading = 1
    endif
    
    if equitycurve < average[x](equitycurve) and tradecount > x then
    trading = 0
    endif
    
    if trading and not onmarket then
    buy positionsize contracts at market
    buyprice = close
    tradecount = tradecount + 1
    endif
    
    if trading = 0 and not onmarket then
    buyprice = close
    tradeonmarket = 1
    endif
    
    if onmarket and close > positionprice or onmarket and close > high[1] then
    sell at market
    equitycurve = equitycurve + (close - buyprice)
    endif
    
    if tradeonmarket and close > buyprice or tradeonmarket and close > high[1] then
    tradeonmarket = 0
    equitycurve = equitycurve + (close - buyprice)
    endif
    
    graph equitycurve
    graph average[x](equitycurve)
    graph trading
    

    I tested it on the SP500 weekly. Obviously there is no real strategy involved as it just buys every week if it is not on the market and sells at the close of a week if it is in profit or if the week has closed higher than the previous weeks high.

    As with all uses of an average it will be curve fitted dependent on the average period selected. Also just like Nicolas’s example the filter does not come into play until x number of trades have passed. It would be possible to alter the code so that these are either real trades or simulated trades depending on your appetite for risk I guess.

    Here are the results with a period of 20:

    [attachment file=80483]

    Maybe a discussion on similar codes and the use of simulated trades will be of interest to someone?

    Nicolas thanked this post
    Screenshot_4-1.png Screenshot_4-1.png
    #80486 quote
    Nicolas
    Keymaster
    Master

    Thanks for sharing your version.

    In your code you are averaging the equity curve with a period set in time (quantity of candlesticks), so stop me if I’m wrong but the real trading could begin again if the time has passed by sufficiently for the equity curve to be computed with X same value.

    Average[X](data) will get the last X data in the last X candlesticks, so if data is not changing  you could have X times the same data, so the average is wrong 😐

    I made something similar in this old blog article: Trading the strategy profit curve

    #80488 quote
    Vonasi
    Moderator
    Master

    Yes – that is true. It is something I coded quite a long time ago – and that may have been the reason I gave up on it!

    Maybe it is time to re-visit the idea and see if it can be improved in some way.

    #80498 quote
    Vonasi
    Moderator
    Master

    Here’s the same strategy entry and exit criteria but this one keeps a simulated record of the last 10 trades. If the last 10 trades add up to an overall loss then real trading stops and simulated trades are recorded. Real trading does not resume until the last last ten trades have returned to an overall profit.

    Once again the filter is not applied to the first 10 trades.

    x = 10
    positionsize = 5
    
    lasttradestotal = trade1 + trade2 + trade3 + trade4 + trade5 + trade6 + trade7 + trade8 + trade9 + trade10
    
    if lasttradestotal >= 0 or tradecount <= x then
    trading = 1
    endif
    
    if lasttradestotal < 0 and tradecount > x then
    trading = 0
    endif
    
    if trading and not onmarket then
    buy positionsize contracts at market
    buyprice = close
    tradecount = tradecount + 1
    endif
    
    if trading = 0 and not onmarket then
    buyprice = close
    tradeonmarket = 1
    endif
    
    if onmarket and close > positionprice or onmarket and close > high[1] then
    sell at market
    trade1 = trade2
    trade2 = trade3
    trade3 = trade4
    trade4 = trade5
    trade5 = trade6
    trade6 = trade7
    trade7 = trade8
    trade8 = trade9
    trade9 = trade10
    trade10 = close - buyprice
    endif
    
    if tradeonmarket and close > buyprice or tradeonmarket and close > high[1] then
    tradeonmarket = 0
    trade1 = trade2
    trade2 = trade3
    trade3 = trade4
    trade4 = trade5
    trade5 = trade6
    trade6 = trade7
    trade7 = trade8
    trade8 = trade9
    trade9 = trade10
    trade10 = close - buyprice
    endif
    
    //graph trading
    graph lasttradestotal
    graph 0
    

    [attachment file=80499]

    Screenshot_5-1.png Screenshot_5-1.png
    #80501 quote
    Vonasi
    Moderator
    Master

    This version will start applying the filter after the first trade as I realised that it is not necessary to wait. If the first trade loses then simulated trades are recorded but if it is a winner then live trading continues until the strategy goes into its first 10 trade negative equity.

    positionsize = 5
    
    lasttradestotal = trade1 + trade2 + trade3 + trade4 + trade5 + trade6 + trade7 + trade8 + trade9 + trade10
    
    if lasttradestotal >= 0 then
    trading = 1
    endif
    
    if lasttradestotal < 0 then
    trading = 0
    endif
    
    if trading and not onmarket then
    buy positionsize contracts at market
    buyprice = close
    endif
    
    if trading = 0 and not onmarket then
    buyprice = close
    tradeonmarket = 1
    endif
    
    if onmarket and close > positionprice or onmarket and close > high[1] then
    sell at market
    trade1 = trade2
    trade2 = trade3
    trade3 = trade4
    trade4 = trade5
    trade5 = trade6
    trade6 = trade7
    trade7 = trade8
    trade8 = trade9
    trade9 = trade10
    trade10 = (close - buyprice) * positionsize
    endif
    
    if tradeonmarket and close > buyprice or tradeonmarket and close > high[1] then
    tradeonmarket = 0
    trade1 = trade2
    trade2 = trade3
    trade3 = trade4
    trade4 = trade5
    trade5 = trade6
    trade6 = trade7
    trade7 = trade8
    trade8 = trade9
    trade9 = trade10
    trade10 = (close - buyprice) * positionsize
    endif
    
    //graph trading
    graph lasttradestotal
    graph 0
    
    GraHal thanked this post
    #80511 quote
    Nicolas
    Keymaster
    Master

    Nice use of variables without using loops or arrays! 😆

    I’m wondering if this kind of orders management could improve or not a strategy you already know for sure it will not win at the end!

    #80519 quote
    Vonasi
    Moderator
    Master

    While typing it I was thinking that access to arrays would be nice!

    I guess it would be an improvement to most strategies even losing ones but then there is always the chance that you could have a big loss that keeps you out of the market for the next nine small wins and then just when you get back to live trading another big loss. I suppose that for trading strategies where the wins are a similar size to the losers then this sort of filter might be more beneficial.

    In the weekly SP500 example shown it seems like a reasonable filter to keep a long only strategy out of the market in major corrections.

    #80577 quote
    Vonasi
    Moderator
    Master

    I thought it would be nice to see if the ten day floating equity curve as a filter would improve a random strategy. So I wrote a strategy that bought the SP500 after a three day drop and sold if the price went over an average – nothing else – no stops or take profits and no more than one position held at a time. I used a spread of 1.6.

    Here are the results:

    [attachment file=80578]

    Then I added the ten trade floating equity curve filter to see if it improved it at all. Here are the results:

    [attachment file=80579]

    The conclusion was that using the filter resulted in more profit, slightly better gain per trade, slightly better win ratio, less time in the market but most importantly a big improvement in draw down.

    This sort of equity curve filtering may have some merit it seems but further testing is needed on the effect of such things such as number of trades used to calculate the floating equity curve before a full conclusion can be reached.

    GraHal thanked this post
    Screenshot_2-1.png Screenshot_2-1.png Screenshot_3-2.png Screenshot_3-2.png
    #80661 quote
    Nicolas
    Keymaster
    Master

    More patterns for your tests: http://paststat.com/quant-ideas

    Vonasi thanked this post
    #80683 quote
    Vonasi
    Moderator
    Master

    Another idea I am testing is comparing a five trade floating equity curve with a ten trade floating equity curve and only trading live if the ten trade curve is above the five trade curve.

    lasttradestotal = trade1 + trade2 + trade3 + trade4 + trade5 + trade6 + trade7 + trade8 + trade9 + trade10 
    
    lasttradestotal5 = trade1 + trade2 + trade3 + trade4 + trade5
    
    if lasttradestotal >= lasttradestotal5 then
    trading = 1
    endif
    
    if lasttradestotal < lasttradestotal5 then
    trading = 0
    endif
    
    if trading and not onmarket then
    buy 5 contracts at market
    buyprice = close
    endif
     
    if trading = 0 and not onmarket then
    buyprice = close
    tradeonmarket = 1
    endif
     
    if onmarket and close > positionprice or onmarket and close > high[1] then
    sell at market
    trade1 = trade2
    trade2 = trade3
    trade3 = trade4
    trade4 = trade5
    trade5 = trade6
    trade6 = trade7
    trade7 = trade8
    trade8 = trade9
    trade9 = trade10
    trade10 = (close - buyprice) * positionsize
    endif
    
    if tradeonmarket and close > buyprice or tradeonmarket and close > high[1] then
    tradeonmarket = 0
    trade1 = trade2
    trade2 = trade3
    trade3 = trade4
    trade4 = trade5
    trade5 = trade6
    trade6 = trade7
    trade7 = trade8
    trade8 = trade9
    trade9 = trade10
    trade10 = (close - buyprice) * positionsize
    endif
    Mags67 thanked this post
    Screenshot_1.png Screenshot_1.png Screenshot_2-3.png Screenshot_2-3.png
    #80703 quote
    Nicolas
    Keymaster
    Master

    You have almost rewrote “Dual momentum” theory from scratch 🙂

    #80735 quote
    Vonasi
    Moderator
    Master

    You have almost rewrote “Dual momentum” theory from scratch 🙂

    Almost… but with errors! I just noticed that I am comparing the first 5 trades in the 10 recorded trades instead of the last 5 trades. Here is the corrected code:

    lasttradestotal = trade1 + trade2 + trade3 + trade4 + trade5 + trade6 + trade7 + trade8 + trade9 + trade10 
     
    lasttradestotal5 = trade6 + trade7 + trade8 + trade9 + trade10
     
    if lasttradestotal >= lasttradestotal5 then
    trading = 1
    endif
     
    if lasttradestotal < lasttradestotal5 then
    trading = 0
    endif
     
    if trading and not onmarket then
    buy 5 contracts at market
    buyprice = close
    endif
     
    if trading = 0 and not onmarket then
    buyprice = close
    tradeonmarket = 1
    endif
     
    if onmarket and close > positionprice or onmarket and close > high[1] then
    sell at market
    trade1 = trade2
    trade2 = trade3
    trade3 = trade4
    trade4 = trade5
    trade5 = trade6
    trade6 = trade7
    trade7 = trade8
    trade8 = trade9
    trade9 = trade10
    trade10 = (close - buyprice) * positionsize
    endif
     
    if tradeonmarket and close > buyprice or tradeonmarket and close > high[1] then
    tradeonmarket = 0
    trade1 = trade2
    trade2 = trade3
    trade3 = trade4
    trade4 = trade5
    trade5 = trade6
    trade6 = trade7
    trade7 = trade8
    trade8 = trade9
    trade9 = trade10
    trade10 = (close - buyprice) * positionsize
    endif
    Mags67 thanked this post
    #153539 quote
    Fran55
    Participant
    Veteran

    My question is if trades could be simulated until it exceeds the drawdown.

    That is to say:

    Win

    Win

    Win

    Lose, and the drawdown begins

    Lose, sham operation

    Win, simulated trade, still not over drawdown

    Win, simulated trade, still not over drawdown

    Win, simulated trade, and beat the drawdown

    Start trading real money again.

     

    Its possible?… I need code!

    Thanks!!!

    #153983 quote
    Fran55
    Participant
    Veteran

    Thats possible Fran55?

    It is a very good idea.

    #154301 quote
    Fran55
    Participant
    Veteran

    Ale, merry christmas on the floor to all people and hippie new year!

Viewing 15 posts - 1 through 15 (of 53 total)
  • You must be logged in to reply to this topic.

Simulated Trading


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Vonasi @vonasi Moderator
Summary

This topic contains 52 replies,
has 7 voices, and was last updated by zilliq
5 years, 1 month ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 09/14/2018
Status: Active
Attachments: 11 files
Logo Logo
Loading...