Overcoming the lack of tick by tick data with simulation.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #86566 quote
    Vonasi
    Moderator
    Master

    I wrote a long only daily strategy that bought at calculated support prices and sold at resistance prices and that required the use of tick by tick data as it used LIMIT and STOP pending orders. It looked great but using tick by tick limited the data sample to starting from just August 2010. The strategy never bought and sold on the same day so I decided to write an indicator that simulated the strategy so that I could test further back – this way I could have a much bigger data sample to test on. Obviously this will only work if your strategy has no zero bar trades – that is ones that open and close in the same bar.

    As well as graphing my simulated equity curve I graphed my ‘onmark’ variable multiplied by the equity to show when the simulated strategy was on market.

    [attachment file=86567]

    Here is the simulated bit of code – I have removed the actual main parts of the strategy:

    Spread = 3.8
    
    //simulated entry
    if not onmark and close[1] > supportprice[1] and low < supportprice and open > supportprice and filter then 
    entryprice = supportprice
    onmark = 1
    stoplossprice = (my calculated stoploss price)
    endif
    
    //simulated win exit
    if onmark and high > resistanceprice then
    exitprice = resistanceprice
    onmark = 0
    equity = (equity + (exitprice - entryprice)) - spread
    endif
    
    //simulated hit stoploss
    if onmark and low < stoplossprice then
    exitprice = stoplossprice
    onmark = 0
    equity = (equity + (exitprice - entryprice)) - spread
    endif
    
    if onmark then 
    floatingequity = (equity + (close - entryprice)) 
    else
    floatingequity = equity
    endif
    
    return floatingequity coloured(0,0,255) style(line,2) as "Equity", (onmark * floatingequity) coloured(0,128,0,150) style(histogram,2) as "OnMarket"

    Obviously my strategies entry and exit points are very simple to simulate and so if your strategy is very complicated then simulating it might not be quite so easy – but I post this here just in case someone else might find the concept useful for their own back testing of any strategy with STOP and LIMIT pending orders and no zero bar entry and exits.

    Technically this is an indicator but as it is really a strategy equity curve I post it here rather than in the indicator section.

    Nicolas thanked this post
    Screenshot_10.png Screenshot_10.png
    #86570 quote
    Vonasi
    Moderator
    Master

    I made some slight improvements by adding a buy and hold line and also calculating the win rate and average gain per trade. In fact this is better than the PRT results as you can see historically how the win rate and average return has been rather than just the final totals.

    [attachment file=86571]

    Spread = 3.8
    
    //simulated entry
    if not onmark and close[1] > supportprice[1] and low < supportprice and open > supportprice and filter then 
    entryprice = supportprice
    onmark = 1
    stoplossprice = (my calculated stop loss price)
    endif
    
    //simulated win exit
    if onmark and high > resistanceprice then
    exitprice = resistanceprice
    onmark = 0
    equity = (equity + (exitprice - entryprice)) - spread
    endif
    
    //simulated hit stoploss
    if onmark and low < stoplossprice then
    exitprice = stoplossprice
    onmark = 0
    equity = (equity + (exitprice - entryprice)) - spread
    if equity > equity[1] then
    win = win + 1
    count = count + 1
    endif
    if equity < equity[1] then
    count = count + 1
    endif
    endif
    
    if onmark then 
    floatingequity = (equity + (close - entryprice)) 
    else
    floatingequity = equity
    if equity > equity[1] then
    win = win + 1
    count = count + 1
    endif
    if equity < equity[1] then
    count = count + 1
    endif
    endif
    
    buyandhold = buyandhold + (close - open)
    
    winrate = (win/count) * 100
    avegain = (equity /count)
    
    return floatingequity coloured(0,0,255) style(line,2) as "Equity", buyandhold as "Buy and Hold", winrate as "Win Rate", avegain as "Average Gain", (onmark * floatingequity) coloured(0,128,0,150) style(histogram,2) as "OnMarket"
    Screenshot_11.png Screenshot_11.png
    #86575 quote
    Vonasi
    Moderator
    Master

    I’ve just realised that if your strategy has no zero bar issues of trades opening and closing on the same bar then by just turning off the tick by tick you will get an accurate equity graph – so my above code is a waste of time! I suppose at least with it you can see a running win rate and average gain per trade.

    I won’t delete the posts so that someone might read it and use my experience/stupidity and not waste their own time.

    #86576 quote
    Vonasi
    Moderator
    Master

    I modified it to just show the win rate and average gain. Maybe it is not all wasted time.

    Interesting to note the win rate dropping but the average gain increasing.

    [attachment file=86577]

    Spread = 3.8
    
    //simulated entry
    if not onmark and close[1] > supportprice[1] and low < supportprice and open > supportprice and filter then
    entryprice = supportprice
    onmark = 1
    stoplossprice = (my calculated stop loss price)
    endif
    
    //simulated win exit
    if onmark and high > resistanceprice then
    exitprice = resistanceprice
    onmark = 0
    equity = (equity + (exitprice - entryprice)) - spread
    endif
    
    //simulated hit stoploss
    if onmark and low < stoplossprice then
    exitprice = stoplossprice
    onmark = 0
    equity = (equity + (exitprice - entryprice)) - spread
    if equity > equity[1] then
    win = win + 1
    count = count + 1
    endif
    if equity < equity[1] then
    count = count + 1
    endif
    endif
    
    if equity > equity[1] then
    win = win + 1
    count = count + 1
    endif
    if equity < equity[1] then
    count = count + 1
    endif
    
    winrate = (win/count) * 100
    avegain = (equity /count)
    
    return winrate as "Win Rate", avegain as "Average Gain"
    
    Screenshot_12.png Screenshot_12.png
    #86940 quote
    pippo999
    Participant
    Junior

    Great Vanosi very interisting Thing.

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

Overcoming the lack of tick by tick data with simulation.


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Vonasi @vonasi Moderator
Summary

This topic contains 4 replies,
has 2 voices, and was last updated by pippo999
7 years, 2 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 12/08/2018
Status: Active
Attachments: 3 files
Logo Logo
Loading...