backtesting code required.

iamaaditya

Active Member
#1
Buy = Cross(StochK(8,3), StochD(8,3,4));
Sell = Cross(StochD(8,3,4), StochK(8,3));

ApplyStop(stopTypeLoss, stopModePoint, 20);
ApplyStop(stopTypeProfit, stopModePoint, 20);

i hope it is exactly what you want ?
Amibroker Help contains Tutorial pages and believe they are easy and concise. Go thru' once.
 

iamaaditya

Active Member
#2
dear aditya .. this is not working .. i mean when i backtested the same on nifty daily data.. it shows all values zero zero.. first of all they asked me to define short and cover also .. i defined the same as buy and sell.. though i think its wrong.. in a simple way there should be only 4 statements buy short and stoplosses and profits.. thats it.. but i could not do it... some how.
Dear Vishal,

I checked the code (copied from here, pasted in AB, backtested ) and it seems to be working fine.

See the nifty backtest result for yourself. (starting 2011)
Ticker Trade Date Price Ex. date Ex. Price % chg Profit % Profit Shares Position value Cum. Profit # bars Profit/bar MAE MFE Scale In/Out
NSENIFTY Long (max loss) 01/13/2011 5850.75 01/14/2011 5752.1 -1.69% -2215.57 -2.22% 17.0458 99730.73 -2215.57 2 -1107.79 -1.95% 0.12% 0/0
NSENIFTY Long (max loss) 01/19/2011 5737.35 01/20/2011 5656 -1.42% -1905.63 -1.95% 16.9976 97521.12 -4121.21 2 -952.82 -1.42% 0.18% 0/0
NSENIFTY Long (max loss) 02/03/2011 5430.45 02/04/2011 5410.45 -0.37% -867.57 -0.91% 17.6082 95620.62 -4988.77 2 -433.78 -0.37% 1.88% 0/0
NSENIFTY Long (profit) 02/11/2011 5219.65 02/14/2011 5340.25 2.31% 1671.73 1.76% 18.1536 94755.39 -3317.04 2 835.87 -0.80% 2.31% 0/0
NSENIFTY Long (profit) 03/01/2011 5382 03/03/2011 5478.45 1.79% 1202.63 1.25% 17.9158 96422.62 -2114.41 2 601.31 -0.16% 2.81% 0/0
NSENIFTY Long (max loss) 03/16/2011 5475.95 03/17/2011 5455.4 -0.38% -892.52 -0.91% 17.8274 97622.01 -3006.94 2 -446.26 -0.38% 1.08% 0/0
NSENIFTY Long (profit) 03/23/2011 5411.4 03/24/2011 5501.8 1.67% 1089.24 1.13% 17.8756 96731.89 -1917.70 2 544.62 -0.17% 1.67% 0/0
NSENIFTY Long (max loss) 04/05/2011 5923.85 04/06/2011 5903.85 -0.34% -857.58 -0.88% 16.5126 97818.19 -2775.28 2 -428.79 -1.15% 0.08% 0/0
NSENIFTY Long (max loss) 04/21/2011 5882.85 04/25/2011 5859.6 -0.40% -905.78 -0.93% 16.4823 96962.92 -3681.06 2 -452.89 -0.40% 0.51% 0/0
NSENIFTY Long (profit) 05/06/2011 5477.65 05/09/2011 5575.2 1.78% 1187.36 1.24% 17.5366 96059.59 -2493.70 2 593.68 -0.09% 1.78% 0/0
NSENIFTY Long (max loss) 05/20/2011 5450.65 05/23/2011 5430.65 -0.37% -880.97 -0.91% 17.8408 97243.74 -3374.67 2 -440.48 -0.37% 1.23% 0/0
NSENIFTY Long (profit) 05/27/2011 5413.7 05/30/2011 5493.75 1.48% 900.69 0.93% 17.8002 96365.15 -2473.98 2 450.34 -0.00% 1.48% 0/0
NSENIFTY Long (profit) 06/21/2011 5280.8 06/22/2011 5304.65 0.45% -87.13 -0.09% 18.4183 97263.41 -2561.11 2 -43.57 -0.45% 0.79% 0/0
NSENIFTY Long (max loss) 07/15/2011 5602.95 07/18/2011 5581.75 -0.38% -891.45 -0.92% 17.3438 97176.52 -3452.56 2 -445.72 -0.72% 0.51% 0/0
kindly check all the Settings (if you have to keep short and cover, make them 0 and not same as buy and sell, it will surely not give proper results by giving same formula, better in settings window -> Positions = Long ,

do let us know if it worked !
 

iamaaditya

Active Member
#3
yes brother this I am getting but there are following problems

total trades are showing 434 but net profit 0.00 ...
secondly i dont want to test for long only i want to test for short also...
if u can give me ur yahoo chat id i can show u ...
any ways if u got the result .. what is the result ?? i mean profit or loss ?? how much ???
If you want to trade the short side too then you can revert the positions when you are selling the long positions..
(and by Sell if you meant Short Sell, then sorry my mistake)
like----

Buy = Cross(StochK(8,3), StochD(8,3,4));
Sell = Cross(StochD(8,3,4), StochK(8,3));

Short = Sell;
Cover = Buy;

ApplyStop(stopTypeLoss, stopModePoint, 20);
ApplyStop(stopTypeProfit, stopModePoint, 20);
Dear Vishal, please don't take this as being rude but if you thought such a mundane strategy would really make money (atleast in backtest) (and as a pure system trading, ofcourse discretionary traders could make even profitable, but that is different point altogether) ?
you are in going to be in a rude awakening when you realise ---
1. System Trading has gone much complex (because simple strategies don't work, atleast not without using human skills)
2. There are other so many , so many factors involved in making a strategy winner, like position size, risk level, trailing stop, volatility check, trend confirmation.

I would personally suggest you to read the following books please before you dive further into strategy development and trading
1. The Evaluation & Optimisation of trading strategies - Pardo
2. Breaking the Black Box - Pring
3. Both books by Howard Bandy, just google his name you will get the site.
4. Beyond Technical Analysis: How to Develop and Implement a Winning Trading System - tushar chande
5. Evidence-Based Technical Analysis: Applying the Scientific Method and Statistical Inference to Trading Signals - Aronson

You can read in any order, but I would recommend the order as given above.
 
Last edited:

iamaaditya

Active Member
#4
for your reference I am giving you the result for the backtest of the strategy as given above (originally requested by you) (trades both long and short)

Ticker <Portfolio>
Net Profit -12459.71
Net % Profit -12.46
Exposure % 20
CAR -20.91
RAR -104.57
Max. Trade Drawdown -2214.24
Max. Trade % Drawdown -2.22
Max. Sys Drawdown -13314.48
Max. Sys % Drawdown -13.31
Recovery Factor -0.94
CAR/MDD -1.57
RAR/MDD -7.85
Profit Factor 0.33
Payoff Ratio 1.27
Standard Error 1040.36
RRR -17.73
Ulcer Index 8.5
Ulcer Perf. Index -3.1
Sharpe Ratio -5.87
K-Ratio -0.2411
# Trades 29
Avg Profit/Loss -429.65
Avg % Profit/Loss -0.45
Avg Bars Held 2
# of winners 6
% of Winners 20.69
W. Tot. Profit 6184.95
W. Avg. Profit 1030.82
W. Avg % Profit 1.13
W. Avg. Bars Held 2
# of losers 23
% of Losers 79.31
L. Tot. Loss -18644.66
L. Avg. Loss -810.64
L. Avg % Loss -0.87
L. Avg. Bars Held 2
 

iamaaditya

Active Member
#5
thanks for your reply .. you are completely right.. i was searching for a system which if followed blindly could have given good results in past. ... but as u said human interference is always required. but I would like to share with u something.. if u see any chart daily or intraday etc.. and if u hold only one technical parameter like for example rsi of 14 days.. whenever it has gone below 30 market has always bounced back. so what i actually wanted and test was to find the back testing results of only such a single parameter.. that was the basic idea.. if it works on daily charts.. it should work on intraday also...
I am glad that you understand why such a simple strategy wouldn't work.

But I am afraid you got me wrong, on the need to human intervention. What I am saying is "A great discretionary trader" could trade even poor looking strategies (on computer results) and make profit, but that is not the point of discussion.

yes completely automatic strategies can be build and traded profitably but my point is you will have to do more research and work (kind of gold hunt, if you will) to find profitable strategy and then you will have to work towards making a strategy out of it. (MM, Risk Mangement, Stops, Scaling, etc)

and if u hold only one technical parameter like for example rsi of 14 days.. whenever it has gone below 30 market has always bounced back
Yes visually it looks as if the RSI or any other oscillating indicators always bounce back or fall back, but do keep in mind that these are oscillators designed to swivle b/n 0 to 100 (or some other values ), but only because RSI has moved from say 20 to 50 doesn't mean that the price has also moved (either in same magnitude or different). Money comes from trading price and not RSI, alas !!!

Please don't take this as a wrong way but regarding your understanding of Indicators I believe is not evidence based (only visual based) so I honestly urge you to read Evidence-Based Technical Analysis: Applying the Scientific Method and Statistical Inference to Trading Signals - Aronson . (pun certainly not intentional)

You should decide what you want to be discretionary trader or system trader ? because the road to each of them are different and unless you are a workaholic and voracious reader , I am afraid, you will have to choose either one of them.

Also if you are serious please read all the books which I had shared in earlier post. If you need pdf version of any of those and don't find it on the net, do PM me.

Like in Sports or business or any profession for that matter, Success is the result of hardwork, lots and lots of hardwork. I hope I haven't disheartened you or discouraged you. But reality should be told, there is no Holy Grail.

P.S:
All the so called "secrets of success" will not work unless YOU DO !
 
Last edited:

iamaaditya

Active Member
#6
Unfortunately due to time crunch will not be able to meet you in ym , but please feel free to post any of your doubt here of if its personal then PM me.

to get you started why don't you code a simple MA crossover system, this is generally "Hello World!" equivalent of strategy trading.

if you add chandelier exit it will be profitable just little of optimisation will be required. I am trying to make it too simplistic, just beware that a profitable strategy in backtest need not necessarily be profitable in live trading and moreover ma crossover system exhibit larger drawdowns and if you don't have appetite for that kind of risk then you should not trade it live, but since you want to practice and learn it will be fine.

also please read this before you start

http://www.amibroker.org/userkb/2007/07/21/system-design-pitfalls/
 

iamaaditya

Active Member
#7
if you add the code below it will plot bollinger.
_SECTION_BEGIN("Bollinger Bands");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 300, 1 );
Width = Param("Width", 2, 0, 10, 0.05 );
Color = ParamColor("Color", colorCycle );
Style = ParamStyle("Style");
Plot( BBandTop( P, Periods, Width ), "BBTop" + _PARAM_VALUES(), Color, Style );
Plot( BBandBot( P, Periods, Width ), "BBBot" + _PARAM_VALUES(), Color, Style );
_SECTION_END();
remember BBandTop and BBandBot are the functions which plots bollinger top and bottom lines.

bollinger band are very good tool to understand the volatility and sometimes to detect patterns of consolidation to trend, so they can yield good setup but they cannot be traded through on it's own i.e you would need any other indicator.

no indicator is inferior or superior to other, each of them gives you idea about the market. it is your understanding of market and indicator and confidence in yourself and also the indicator which yields good trading results.
 
#8
find here backtest code:


//Settings for Backtester
SetOption("InitialEquity", 100000);
SetOption("AllowSameBarExit", False);
SetOption("AllowPositionShrinking", False);
SetOption("FuturesMode", True);
SetOption("InterestRate",0);
SetOption("MaxOpenPositions",1);
RoundLotSize = 50;
SetOption("MinShares",RoundLotSize);
SetOption("PriceBoundChecking",False);
//SetOption("CommissionMode",3);
//SetOption("CommissionAmount",12.5/RoundLotSize);
SetOption("AccountMargin",True);
SetOption("ReverseSignalForcesExit",True);
SetOption("UsePrevBarEquityForPosSizing",True);
SetOption("GenerateReport",1);
SetOption("MaxOpenLong",1);
SetOption("MaxOpenShort",1);
SetOption("RefreshWhenCompleted",True);

//PositionSize = C*RoundLotSize*Param_NoOfLots;
SetTradeDelays(1,1,1,1);
BuyPrice = Close;
SellPrice = Close;
ShortPrice = Close;
CoverPrice = Close;
//End of Settings for Backtester
 
#9
Hello aditya,
iam following ur threads silently. U r recommending good books to read. Presently iam reading Evidence based tech analysis. I down loaded Phantom of pits. As i lost 2.50 lakhs by trading wildly and without any game plan. Now advice me , how to get good control over Mechanical trading.