Programming Geek
Rated 4.7/5 based on 1446 reviews

CodeVita 2018 Round 1 Question: Supertrend

Supertrend

Problem Description
Write a program to analyze and back test stock data to find total profit and buy / sell order price using super trend indicator formula.
There is one indicator on chart data called super trend which is used in stock market to make buy and sell decisions.
If we see super trend indicator on stock chart and want to back test super trend trading strategy then we have to manually check each and every buy / sell occurrences. A program will make it easy to analyze or back test data pertaining to any number of periods.
It is calculated based on Average True Range (ATR) and a multiplier value. ATR measures the degree of volatility of the market.
Below are the mathematical formulae involved in calculation of super trend indicator:
True Range (TR) = Max of (Current High- Current Low, Current High- Previous Close, Current Low- Previous Close)
Note: TR value to be calculated starting from 2nd iteration as we are using Previous close in the formula
Average True Range (ATR) = Average of last n True Range values (Here n is by default 10. Means we have to find last 10 days average true range at every point.)
Basic Upper band = (Current High + Current Low) / 2 + Multiplier * ATR (Here multiplier is by default 3)
Basic Lower band = (Current High + Current Low) / 2 -– Multiplier * ATR
Final Upper band = IF( (Current Basic Upper band < Previous Final Upper band) or (Previous Close > Previous Final Upper band)) Then (Current Basic Upper band) ELSE Previous Final Upper band)
Final Lower band = IF( (Current Basic Lower band > Previous Final Lower band) or (Previous Close < Previous Final Lower band)) Then (Current Basic Lower band) ELSE Previous Final Lower band)
SUPERTREND = IF(Current Close <= Current Final Upper band ) Then Current Final Upper band ELSE Current Final Lower band
Buy & Sell Process
We have to buy stock on current close price when Super trend indicator value closes above the current close price and a sell stock when super trend value closes below the current closing price.
Note: Here ATR period is 10 so buy and sell must start after 10th period so that super trend values can be generated. In first initial 10 periods only true range (TR) would be calculated. Calculate total number of buy and sell transactions that were performed based on Super trend analysis.
Assumption
We are trading stock market index or stock futures contracts and not equities.
Constraints
1 <= N <= 1000
Input Format
First line contains single integer N that denotes the number of periods for which data exist
Second line contains N * 3 space delimited values which correspond to the sequence …repeated 3N times
Output
For each Buy / Sell transaction output the following
Buy at (New Position).
Sell at (Close Previous Position).
Profit
Where , and are actual values for that transaction accurate up to 2 digit after decimal point.
Explanation
Example 1
Input
14
335.4 317.3 321 336.7 319 322.75 338.1 325 325.05 333 318.8 324.2 334.3 320.45 326 331.8 323 327.45 324.95 314.65 319.9 325.9 317.1 322.1 324.35 318.5 323.7 330.05 307 315.55 329.45 310 316.8 0 0 326.05 323.5 312.2 315.45 318.95 315.75 316.85
Output
Buy at 316.8 (New Position).
Sell at 326.05 (Close Previous Position).
Profit 9.25
Sell at 326.05 (New Position).
Buy at 315.45 (Close Previous Position).
Profit 19.85
Buy at 315.45 (New Position).
Total orders 3
Example 2
Input
59
10315.3 10208.35 10301.4 10339 10248.45 10327.4 10345 10170.25 10194.7 10403 10288.45 10398.05 10420 10360 10399.3 10469.7 10410 10438.55 10482 10438.6 10468 10479.9 10411.5 10468.55 10520.95 10440.25 10507.5 10570 10501.5 10533.2 10590.55 10463.8 10585.5 10604.45 10540 10589.2 10634.05 10560.15 10577.85 10624.7 10591 10612.75 10632.6 10570 10627.6 10660.15 10589 10625.6 10670.9 10609.8 10654.35 10645.95 10568.25 10605.2 10651.6 10591.45 10639.65 10750 10660 10729.9 10796.25 10742.4 10788.9 10801.7 10720 10746.9 10745 10677 10719.45 10696.4 10654 10669.5 10754.4 10676.3 10748.6 10775 10690.1 10742.9 10795.5 10718.8 10777 10800 10724.05 10735.3 10833.65 10746.55 10827.8 10853.5 10792.35 10821.95 10955.35 10794.05 10824.25 10823.3 10731.6 10772.8 10789.7 10693.4 10709.05 10697.5 10599 10605.95 10639 10537.05 10552.35 10587 10521.2 10568 10558.45 10430.35 10442.85 10535.45 10426.05 10516.45 10650 10522.6 10616.2 10710.95 10646.05 10691.2 10714 10606.75 10628.05 10655 10556.2 10614.45 10715.4 10625.1 10702.15 10744 10669.95 10683.9 10732.2 10587.65 10598.7 10630 10552 10596.45 10701 10575 10686.25 10814.05 10725.25 10765.2 10759.4 10691 10748.1 10835 10761.25 10785.8 10849.9 10780.25 10839.05 10873.5 10831.5 10844.85 10834.05 10770.5 10813.9 10837.95 10735.5 10814.65 10817.3 10781 10789.6 10760 10704.55 10719.85 10796.9 10725.2 10790.65 10805.95 10716.65 10737.15 10849 10712.1 10833.95
Output
Buy at 10585.5 (New Position).
Sell at 10827.8 (Close Previous Position).
Profit 242.3
Sell at 10827.8 (New Position).
Buy at 10821.95 (Close Previous Position).
Profit 248.15
Buy at 10821.95 (New Position).
Sell at 10839.05 (Close Previous Position).
Profit 265.25
Sell at 10839.05 (New Position).
Buy at 10844.85 (Close Previous Position).
Profit 259.45
Buy at 10844.85 (New Position).
Total orders 5

No comments :

Post a Comment