Strategy as indicator to overcome limits of tick by tick
Forums › ProRealTime English forum › ProBuilder support › Strategy as indicator to overcome limits of tick by tick
- This topic has 5 replies, 2 voices, and was last updated 5 years ago by Vonasi.
Tagged: Pending orders, tick by tick
-
-
09/05/2019 at 11:25 AM #106584
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.
1234567891011121314151617181920212223p = 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])) thendrawarrowup(barindex, low) coloured(0,128,0)entryprice = close[1] - lower[1]startequity = equityflag = 1endifif flag[1] thenfloatingequity = startequity + (close - entryprice)endifif (flag[1] = 1) and ((close > entryprice) or (high < average[p,2](close[1]))) and (entryprice <> 0) thenequity = equity + (close - entryprice)flag = 0drawarrowdown(barindex, high) coloured(128,0,0)endifreturn floatingequity as "equity"09/05/2019 at 11:45 AM #10659409/05/2019 at 12:20 PM #106601I 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.
1234567891011121314151617181920212223242526272829303132333435363738394041424344//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 fromt1 = 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 thenif (not flag) and (low < (close[1] - lower[1])) and (low[1] > average[p2,t2](close[1])) thenif arrows thendrawarrowup(barindex, low) coloured(0,128,0)endifentryprice = close[1] - lower[1]startequity = equityflag = 1if not eqflag thenstartequity = entrypriceequity = entrypriceeqflag = 1endifendifif flag[1] thenfloatingequity = startequity + (close - entryprice)endifif (flag[1] = 1) and ((close > entryprice) or (close > upper)) and (entryprice <> 0) thenequity = equity + (close - entryprice)flag = 0if arrows thendrawarrowdown(barindex, high) coloured(128,0,0)endifendifelsefloatingequity= closeendifreturn floatingequity as "equity"1 user thanked author for this post.
09/05/2019 at 5:34 PM #106629I 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!
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293//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 6t1 = max(0, min(t1,6))t2 = max(0, min(t2,6))//calculate mae and mfe bandsupper = close[1] + average[p1,t1](high[2]-open[1])lower = (average[p1,t1](close[2]-low[1])) * bandmultiple//check date is ok to tradeif opendate >= startdate then//simulated long entryif (not flag) and (low < (close[1] - lower[1])) and (low[1] > average[p2,t2](close[1])) then//draw buy arrow if on.if arrows thendrawarrowup(barindex, low) coloured(0,128,0)endifentryprice = close[1] - lower[1]// simulated entry pricetradecount = tradecount + 1 // add one to trade countstartequity = equityflag = 1 // we are on the market//on the first trade set starting point for equity curveif not eqflag thenstartequity = entrypriceequity = entrypriceeqflag = 1 // make sure we don't do this againendifendif//if on the market calculate the floating equity, max drawdown and max drawdown %if flag[1] thenfloatingequity = startequity + (close - entryprice)maxequity = max(maxequity, floatingequity)biggestdd = max(maxequity - floatingequity, biggestdd)if biggestdd = maxequity - floatingequity thenddperc = max((biggestdd/maxequity)*100, ddperc)endifendif//simulated long exit including if in profitif outinprofit and (flag[1] = 1) and (entryprice <> 0) and ((close > upper) or (close > entryprice)) thenequity = equity + (close - entryprice) - spread // calculate profit/lossflag = 0 //we are not on market//check if it was a winif equity > equity[1] thenwin = win + 1endif//draw sell arrow if onif arrows thendrawarrowdown(barindex, high) coloured(128,0,0)endifendif//simulated long exit not including if in profitif not outinprofit and (flag[1] = 1) and (entryprice <> 0) and (close > upper) thenequity = equity + (close - entryprice) - spread// calculate profit/lossflag = 0//we are not on market//check if it was a winif equity > equity[1] thenwin = win + 1endif//draw sell arrow if onif arrows thendrawarrowdown(barindex, high) coloured(128,0,0)endifendif//calculate average gain, win rate and difference to buy and hold %avggain = floatingequity/tradecountwinrate = (win/tradecount)*100buyholddiff = ((floatingequity - close)/close)*100endif//calculate quantity of losing tradeslosses = tradecount - winreturn 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"09/05/2019 at 7:35 PM #10664309/05/2019 at 8:10 PM #106644Thanks 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.
1 user thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on