Strategy as indicator to overcome limits of tick by tick

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

    I’m not sure if this should be in ProOrder or Probuilder as it is both a strategy and an indicator!

    I wanted to test some daily strategy ideas that entered long on dips to a price and closed out at a candles close but having to use pending orders for the entry meant that the test data was very limited and on indices such as the SP500 there had been no major downturns within the data so the test results told me nothing about strategy performance in a market crash. So I decided to code an indicator that simulated the strategy and calculated an equity curve.

    The strategy is just basic rubbish and really it was just an experiment to see how easy or difficult it was to code something. I think I achieved my goal and so can at least test strategies that have one pending order. It is still however impossible to simulate strategies that enter and exit using pending orders within one candle due to us not knowing which order was hit first.

    I post it here in case the theory is of interest to anyone.

    p = 300
    
    //upper = open + average[p](high[1]-open[1])
    lower = average[p](close[2]-low[1])
    
    if (not flag) and (low < (close[1] - lower[1])) and (low[1] > average[p,2](close[1])) then
    drawarrowup(barindex, low) coloured(0,128,0)
    entryprice = close[1] - lower[1]
    startequity = equity
    flag = 1
    endif
    
    if flag[1] then
    floatingequity = startequity + (close - entryprice)
    endif
    
    if (flag[1] = 1) and ((close > entryprice) or (high < average[p,2](close[1]))) and (entryprice <> 0) then
    equity = equity + (close - entryprice)
    flag = 0
    drawarrowdown(barindex, high) coloured(128,0,0)
    endif
    
    return floatingequity as "equity"
    Screenshot_9.png Screenshot_9.png
    #106594 quote
    GraHal
    Participant
    Master

    Interesting and could be very useful!

    Link added to here

    Snippet Link Library

    #106601 quote
    Vonasi
    Moderator
    Master

    I made a few improvements to the strategy, the simulation and the presentation.

    It is now possible to turn the arrows on or off as they can make it difficult to see the equity curve.

    The average for both the bands and the entry and exit filter and their average type can be played with to see what historically worked best.

    You can set a start date. I like to test from around 1995 onward as this is when algorithmic trading started taking off. The equity curve is shown from the first trades entry price so it is easy to compare to buy and hold.

    I also removed the if low < average exit filter and added close if close > an upper band based on average rise from close[1].

    I switched to the SP500 weekly and with p1=52, p2=52, t1=2 and t2=0 it gives a pretty nice return beating buy and hold and missing out on all the major crashes and downturns. It hasn’t performed well in the last year (but it also hasn’t lost much) but then that is not surprising considering the sideways volatile market we have had.

     

    //p1 = 52 // band average period
    //p2 = 52 // filter average period
    //t1 = 0 // band average type
    //t2 = 0 // filter average type
    //arrows = 1 //turn arrows on/off
    //startdate = 19950101 //date to start test from
    
    t1 = min(t1,6)
    t2 = min(t2,6) 
    
    upper = close[1] + average[p1,t1](high[2]-open[1])
    lower = average[p1,t1](close[2]-low[1])
    
    if opendate >= startdate then
    if (not flag) and (low < (close[1] - lower[1])) and (low[1] > average[p2,t2](close[1])) then
    if arrows then
    drawarrowup(barindex, low) coloured(0,128,0)
    endif
    entryprice = close[1] - lower[1]
    startequity = equity
    flag = 1
    if not eqflag then
    startequity = entryprice
    equity = entryprice
    eqflag = 1
    endif
    endif
    
    if flag[1] then
    floatingequity = startequity + (close - entryprice)
    endif
    
    if (flag[1] = 1) and ((close > entryprice) or (close > upper)) and (entryprice <> 0) then
    equity = equity + (close - entryprice)
    flag = 0
    if arrows then
    drawarrowdown(barindex, high) coloured(128,0,0)
    endif
    endif
    else 
    floatingequity= close
    endif
    
    return floatingequity as "equity"
    GraHal thanked this post
    Screenshot_10-1.png Screenshot_10-1.png Simulated-Stop-Entry-Strategy.itf
    #106629 quote
    Vonasi
    Moderator
    Master

    I made some further changes to my simulated tick by tick with one pending order simulator.

    It now also shows lines for:

    • trade quantity
    • quantity of winning trades
    • quantity of losing trades
    • average gain/loss per trade,
    • win rate%
    • gain/loss percentage compared to buy and hold
    • biggest draw down
    • maximum draw down % from the maximum equity prior to the draw down

     

    There is also now a band width multiplier to allow testing of slightly narrower or wider bands for the entry and exit points.

    Also there is the option to turn on or off the exit condition to exit any time that you are in profit at the close of a candle.

    I’ve added spread into the equity calculation.

    I’ve also added some comments to the code.

    The images show the strategy on the weekly SP500 with and without the ‘get out if in profit’ turned on and with a 1.5 spread. With the settings as shown it beats buy and hold by 9% and 10% with a biggest draw down of 23% and 24% and a win rate of 78% and 80%. Perhaps it isn’t such a bad strategy idea after all – well it would have been a good one at least if I’d started trading it in 1995!

    //p1 = 52 // band average period
    //p2 = 52 // filter average period
    //t1 = 0 // band average type
    //t2 = 0 // filter average type
    //arrows = 1 //turn arrows on/off
    //startdate = 19950101 //date to start test from
    //bandmultiple = 1 // band multiplier
    //spread = 1.5
    
    //make sure average types are between 0 and 6
    t1 = max(0, min(t1,6))
    t2 = max(0, min(t2,6))
    
    //calculate mae and mfe bands
    upper = close[1] + average[p1,t1](high[2]-open[1])
    lower = (average[p1,t1](close[2]-low[1])) * bandmultiple
    
    //check date is ok to trade
    if opendate >= startdate then
    
    //simulated long entry
    if (not flag) and (low < (close[1] - lower[1])) and (low[1] > average[p2,t2](close[1])) then
    //draw buy arrow if on.
    if arrows then
    drawarrowup(barindex, low) coloured(0,128,0)
    endif
    
    entryprice = close[1] - lower[1]// simulated entry price
    tradecount = tradecount + 1 // add one to trade count
    startequity = equity
    flag = 1 // we are on the market
    
    //on the first trade set starting point for equity curve
    if not eqflag then
    startequity = entryprice 
    equity = entryprice 
    eqflag = 1 // make sure we don't do this again
    endif
    endif
    
    //if on the market calculate the floating equity, max drawdown and max drawdown %
    if flag[1] then
    floatingequity = startequity + (close - entryprice)
    
    maxequity = max(maxequity, floatingequity)
    biggestdd = max(maxequity - floatingequity, biggestdd)
    if biggestdd = maxequity - floatingequity then
    ddperc = max((biggestdd/maxequity)*100, ddperc)
    endif
    endif
    
    //simulated long exit including if in profit
    if outinprofit and (flag[1] = 1) and (entryprice <> 0) and ((close > upper) or (close > entryprice)) then
    equity = equity + (close - entryprice) - spread // calculate profit/loss
    flag = 0 //we are not on market
    
    //check if it was a win
    if equity > equity[1] then
    win = win + 1
    endif
    
    //draw sell arrow if on
    if arrows then
    drawarrowdown(barindex, high) coloured(128,0,0)
    endif
    endif
    
    //simulated long exit not including if in profit
    if not outinprofit and (flag[1] = 1) and (entryprice <> 0) and (close > upper) then
    equity = equity + (close - entryprice) - spread// calculate profit/loss
    flag = 0//we are not on market
    
    //check if it was a win
    if equity > equity[1] then
    win = win + 1
    endif
    
    //draw sell arrow if on
    if arrows then
    drawarrowdown(barindex, high) coloured(128,0,0)
    endif
    endif
    
    //calculate average gain, win rate and difference to buy and hold %
    avggain = floatingequity/tradecount
    winrate = (win/tradecount)*100
    buyholddiff = ((floatingequity - close)/close)*100
    endif
    
    //calculate quantity of losing trades
    losses = tradecount - win
    
    return floatingequity as "Equity", tradecount as "Trades", avggain as "P/L per Trade", winrate as "Win Rate%", win as "Winning Trades", losses as "Losing Trades", buyholddiff as "Diff to Buy and Hold %", biggestdd as "Max Draw Down", ddperc as "Max Draw Down %"//,maxequity as "Max Equity"
    
    Screenshot_13-1.png Screenshot_13-1.png Screenshot_14-1.png Screenshot_14-1.png Simulated-Stop-Entry-v2.itf
    #106643 quote
    GraHal
    Participant
    Master

    Wow!

    I added below to the entry in the Snippet Library

    • loads of useful features!
    Vonasi thanked this post
    #106644 quote
    Vonasi
    Moderator
    Master

    Thanks GraHal – after about five minutes testing it on the DJI instead of the SP500 it became apparent that like all average filter based strategies it is heavily curve fitted/tuned to whatever average type and period has worked well in the past. Oh well at least it was an interesting coding challenge and I reminded myself so much why I hate averages.

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

Strategy as indicator to overcome limits of tick by tick


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Vonasi @vonasi Moderator
Summary

This topic contains 5 replies,
has 2 voices, and was last updated by Vonasi
6 years, 5 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 09/05/2019
Status: Active
Attachments: 6 files
Logo Logo
Loading...