by: avi thaker algorithmic trading: an introduction · hurst(gbm): 0.498349157279 hurst(mr):...

47
Algorithmic Trading: An Introduction BY: AVI THAKER [email protected] 1

Upload: others

Post on 07-Jul-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

AlgorithmicTrading:AnIntroduction

BY:AVI THAKER

[email protected] 1

Page 2: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

TableofContentsLetsPlayaGame

ETFCreation/Redemption

ETF/IndexArbitrage◦ HFTandFlow◦ DealFlow

StatisticalTesting◦ AugmentedDickey-Fuller(ADF)Test◦ HurstExponent◦ VarianceandTermStructure

NeuralNetworks

PersonalLearnings

[email protected] 2

Page 3: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

LetsPlayaGame:TradingObjective:MakeasmuchmoneyaspossibleReward:$5AmazonGiftCardperteammemberTeamSize3-4◦ 1Trader– tradesonthefloorwithothertraders◦ 1Runner– Runestheordersmadebythetrader◦ 1+Backend”PrimeBroker”– Clearthetradesmadewiththeteams

Tradingageometricrandomwalk;6rounds45secondseachMustquoteaspread(unitvalues),e.g.30-32Mustaccepttradeifsomeonehits (acceptsyourbid)ortakes (acceptsyourask)Tradesmustbecleared(thebackendandrunnersverifytradewithotherbackend)tocountWillfillallunfilledordersattheendwith2unitspenaltycost

[email protected] 3

Page 4: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

LetsPlaya Game:TheTicketTEAMID:0001TRADEPRICE:32TRADESIZE:10TRADEDIRECTION:BUY

TRADEDWITH:0003INITIAL:TS

TEAMID:0003TRADEPRICE:32TRADESIZE:10TRADEDIRECTION:SELL

TRADEDWITH:0001INITIAL:MK

GlobalWorkbook(GoogleSheet) PersonalWorkbook

[email protected] 4

Page 5: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

ETFCreation/RedemptionAuthorizedparticipant(marketmaker,intuitionalinvestor,specialist)borrowsstocksharesandplacestheminatrusttoformETFcreationunits – bundlesofstockunits

TrustprovidessharestotheAP,andsharessoldtopubliconopenmarket

RedeemingETF◦ Sellsharesonopenmarket◦ Formacreationunitandandexchangeforunderlyingsecurity

◦ Taxefficient

CreationUnit LastTrade Bid Ask Size Net PercentageAMD 13.7 13.69 13.7 100 1370 16.31% INTC 35.16 35.16 35.17 100 3516 41.85% AAPL 140.64 140.63 140.64 25 3516 41.85% CreationUnit 84.02 100 8402 100.00%

[email protected] 5

Page 6: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

ETF/IndexArbitrageThisisthemostcommonstrategyemployedbymostquantfirmsandbanks◦ JaneStreet,AQR,JumpTrading

Youneedtobefast orhaveflow◦ Someofyoumayhavedonethisinthegame

ArbitragehappenswhenETFtradesatadiscountorpremiumtotheNAV◦ Institutional:WhenETFprice>NAV,theAPwillsellsharesitreceivedduringcreationandmakeaspreadbetweenthecostoftheassetsitboughtfortheETFissuerandthesellingpricefromtheETFshares.APcanalsobuytheunderlyingsharesthatcomposetheETFdirectlyatlowerprices,sellETFsharesontheopenmarketatthehigherprice,capturingthespread.

◦ Individuals:WhentheETFissellingatapremium(ordiscount),individualscanbuy(short)theunderlyingsecuritiesinthesameproportionsandshort(orbuy)theETF.Limitedbyliquidityandspread◦ IfinsidethespreadneedtoknowiftheETFgoestosharepriceorsharepricegoestoETFprice

DothisataninternationallevelwithADR’s

Unit LastTrade Bid Ask Size Net PercentageAMD 13.7 13.69 13.7 100 1370 16.31% INTC 35.16 35.16 35.17 100 3516 41.85% AAPL 140.64 140.63 140.64 25 3516 41.85% CreationUnit 84.02 84.01 84.03 100 8402 100.00%

Example PriceCalculatedAsk 84.03CalculatedBid 84.0075ETFBid 84.04ETFAsk 84.05

Whatisthepotentialprofitofthistrade?

[email protected] 6

Page 7: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

HFTandFlowPureArbitrage◦ Fastestalwayswins

DealFlow◦ Ordersexecutedonbehalfofanotherclient◦ E*TRADE– guaranteed2secondexecutionmarketorder◦ SmartOrders

ExampleCompanies◦ Citadel,marketmakers,Goldman,JPMorgan,etc.

NYSE:GLD127.04-127.05

BuythenSell

LSE:GLD127.05-127.06

SellthenBuy

[email protected] 7

Page 8: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

DealFlow:TheRussellRebalanceBankwilltradeallofthepositionsonbehalfofFTSERussell(moves~20Billioninafewhours)forasingleclient◦ Buysupinanticipationofthetradeandsellstheirownsharestotheclient

◦ Massivemarketmoves

Legalizedinsidertradingandmarketmanipulationduetosheersizeoforders

GoldmanactuallypaysRussell(andthelike)fortheirorderflow!

[email protected] 8

Page 9: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

DealFlow:MarketMicrostructureCapturetheSpread

IncreaseLiquidity

Onbothsidesofthemarket

Riskyduringtimesofvolatility

Mustbefastandhaveexcellentqueueposition

Mathisgenerallymorecomplicated

Manipulatemarketwhenincomingmarketordertogetbetterprice

Bids Price Asks100.03 2,1

100.02 3,7,8100.01 5,2,15

100 1,2,5

1,2 99.99

2,5,8 99.98

3,8,1,5,3 99.97

2,3 99.85

Lotsofmachinelearning:thinkBayesianandneuralnetworks,ML

[email protected] 9

Page 10: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

StatisticalTestingMeanReversion◦ Aprocessthatreferstoatimeseriesthatdisplaysatendencytoreverttoitshistoricalmean

◦ Morespecifically:ifthepriceswithintheseriesmoveawayfromtheirinitialvaluefasterthanthatofGeometricBrownianMotion

◦ Ornstein-Uhlenbeck process(arandomwalkhasnomemory)

Momentum◦ Theexactoppositeofmeanreversion◦ Movementawayfromtheinitialvaluefasterthanthatofrandomwalk

Meanreversionandmomentumgohandinhand,inidentifyingoneyoumayidentifytheother

Willcovertwomethods:AugmentedDickey-Fullertest,andtheHurstExponent

PicturesFrom:http://marcoagd.usuarios.rdc.puc-rio.br/revers.htmlhttp://www.stockcharts.com

[email protected] 10

Page 11: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

StatisticalTesting:TermsOrenstein-Uhlenbeck SDE

ChangeinpriceseriesinnexttimeperiodisproportionaltothedifferencebetweenthemeanpriceandthecurrentpricewithGaussiannoise

MotivatesAugmentedDickey-Fuller(ADF)Test

𝒅𝒙𝒕 = 𝜽 𝝁 − 𝒙𝒕 𝒅𝒕 + 𝝈𝒅𝑾𝒕𝜃 = 𝑟𝑎𝑡𝑒𝑜𝑓𝑟𝑒𝑣𝑒𝑟𝑠𝑖𝑜𝑛𝑡𝑜𝑚𝑒𝑎𝑛𝜇 = 𝑚𝑒𝑎𝑛𝑣𝑎𝑙𝑢𝑒𝑜𝑓𝑝𝑟𝑜𝑐𝑒𝑠𝑠𝜎 = 𝑣𝑎𝑟𝑖𝑎𝑛𝑐𝑒𝑜𝑓𝑡ℎ𝑒𝑝𝑟𝑜𝑐𝑒𝑠𝑠𝑊@ = 𝑊𝑖𝑒𝑛𝑒𝑟𝑃𝑟𝑜𝑐𝑒𝑠𝑠𝑜𝑟

𝐵𝑟𝑜𝑤𝑛𝑖𝑎𝑛𝑀𝑜𝑡𝑖𝑜𝑛

[email protected] 11

Page 12: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

AugmentedDickey-Fuller(ADF)TestIdentifypresenceofaunitrootinautoregressivetimeseries

Reliesonthefactthatifapriceserieshasameanreversionthenthenextpricewillbeproportionaltothecurrentprice

LinearModelofOrderpΔ𝑦@ = 𝛼 + 𝛽𝑡 + 𝛾𝑦@JK + 𝛿KΔ𝑦@JK + ⋯+ 𝛿NJKΔ𝑦@JNOK + 𝜖@

𝛼 = 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡𝛽 = 𝑐𝑜𝑒𝑓𝑓𝑖𝑐𝑖𝑒𝑛𝑡𝑜𝑓𝑡𝑖𝑚𝑒𝑡𝑟𝑒𝑛𝑑(𝑙𝑜𝑛𝑔𝑡𝑒𝑟𝑚𝑑𝑟𝑖𝑓𝑡)

Δ𝑦@ = 𝑦 𝑡 − 𝑦 𝑡 − 1

Testingnullhypothesis:𝛾 = 0◦ Indicatesthatprocessisarandomwalk(𝛼 = 𝛽 = 0)

[email protected] 12

Page 13: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

AugmentedDickey-Fuller(ADF)TestTeststatistic:sampleproportionality/standarderrorofsampleproportionality

𝐷𝐹Y =𝛾Z

𝑆𝐸(𝛾Z)Negativenumber,andmustbelessthancriticalvaluestobesignificant

Codeadf_test.py

Calculated Test Statistic: -2.1900105031287529 P-Value: 0.2098910250427564# Datapoints: 210610%: -2.56750111766769565%: -2.86291337107029831%: -3.4334588739173006

Cannotrejectnullhypothesis,andunlikelytohavefoundameanrevertingtimeseries

[email protected] 13

Page 14: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

HurstExponentAstochasticprocessisstronglystationaryifitsjoinprobabilitydistributionisinvariantundertranslationsintimeorspace◦ Meanandvarianceofprocessdonotchangeovertimeanddonotfollowatrend

HurstExponenthelpstocharacterizethestationarityofatimeseries◦ Reverting,trending,orneither

Varianceofalogpriceseriestoidentifyrateofdiffusivebehavior𝑉𝑎𝑟 𝜏 = log 𝑡 + 𝜏 − log 𝑡 b

Sincelarge𝜏,varianceisproportionalto𝜏 forGeometricBrownianMotion𝜏~ log 𝑡 + 𝜏 − log 𝑡 b

Ifautocorrelationsexisttherelationshipisnotvalid,butcanbemodifiedtoinclude2HwiththeHurstExponentvalueH

𝜏bd~ log 𝑡 + 𝜏 − log 𝑡 b

[email protected] 14

Page 15: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

HurstExponent:Meaning𝐻 < 0.5meanrevertingprocess

𝐻 == 0.5 GBM𝐻 > 0.5 trendingprocessCharacterizesextent◦ Closerto0moremeanreverting◦ Closerto1moretrending

Trydifferenttimeperiods,differentstocks

Hurst(GBM):0.498349157279Hurst(MR):-6.26637088795e-05Hurst(TR):0.95964231812Hurst(GOOG):0.50788012279

[email protected] 15

Page 16: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

VarianceandTermStructure

Notincludedincode

Plotoflog 𝑉𝑎𝑟 𝜏 𝑣𝑠 log 𝜏 forSPY◦ Slope/2istheHurstexponent◦ Intraday

◦ Returnsofmid-pricesfrom1minuteto2^10minutes◦ H = 0.494 ± 0.003;slightlymeanreverting

◦ Daily◦ Returnsfrom1dayto2^8days◦ H = 0.469 ± 0.007;stronglymeanreverting

MeanreversionstrategiesshouldworkbetterthanintradaystrategiesonSPY

http://epchan.blogspot.com/2016/04/[email protected] 16

Page 17: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

VarianceandTermStructure:Gold

Intraday: 𝐻 = 0.505 ± 0.002

Daily: 𝐻 = 0.469 ± 0.007

16-32daysvolatilitiesdriftfromtheregression◦ Thisiswhereweshouldswitchfrommomentumtomeanreversionstrategies

ATrendingExample:USOIntraday 𝐻 = 0.515 ± 0.001Daily 𝐻 = 0.560 ± 0.020

Momentumstrategiesshouldworkwellhere http://epchan.blogspot.com/2016/04/mean-reversion-momentum-and-volatility.html

[email protected] 17

Page 18: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

ExampleStrategiesMomentum◦ Exponentialmovingaverages(MACD◦ Breakouts◦ VolatilitySurges◦ Newsdriven◦ Tendtohavelowwinratesbuthighprofitability

Reversion◦ Bollingerbands◦ Statisticalpairstradingandindextrading◦ Tendtohavehighwinratesandlowprofitability

[email protected] 18

Page 19: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

StrategyDetail:ExponentialMovingAverage:EMA

Infiniteimpulseresponsefilter

LesslagthanSMA

Commonlyusedsignal

𝛼 =2

𝑛 + 1𝐸𝑀𝐴stuuvw@=𝑝K + 1 − 𝛼 𝑝b + 1 − 𝛼 b + ⋯1 + 1 − 𝛼 + 1 − 𝛼 b +…

= 𝐸𝑀𝐴Nuvyz{t| + 𝛼 𝑝stuuvw@ − 𝐸𝑀𝐴Nuvyz{t|

[email protected] 19

Page 20: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

TradingTheEMA

EnterLong:Close>EMA&Prev_Close >EMA_PrevEnterShort:Close<EMA&Prev_Close <EMA_Prev

[email protected] 20

Page 21: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

StrategyDetail:CommonSignalsBollingerBands

VolatilityBands◦ Baseduponstandarddeviation◦ Identifiespointsofreversion

MiddleBand=50-DaySMA

UpperBand=50-DaySMA+50-DaySDofPrice

LowerBand=50-DaySMA- 50-DaySDofPrice

[email protected] 21

Page 22: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

Example:TradingtheBollingerBands

[email protected] 22

Page 23: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

NeuralNetworksThebelowlinkcontainsatutorialinwhichaneuralnetworkisusedtopredicttimeseriesstockdatausingMicrosoft’sdeeplearningplatformCNTKpublishedinconjunctionwithMSR

https://github.com/Microsoft/CNTK/blob/master/Tutorials/CNTK_104_Finance_Timeseries_Basic_with_Pandas_Numpy.ipynb

[email protected] 23

Page 24: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

PersonalLearningsLESSONSFROMFAILURE,SUCCESS,ANDPUREDUMBLUCK

[email protected] 24

Page 25: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

TechnicalIndicators

Technicalindicatorsaremostlyuselessontheirown

Mustidentifysomethingthathappensinthemarket,andusetheindicators(orcomeupwithyourown)torepresentthatsomething

Datavisualizationiscrucial

Simplicityisusuallybetter

RSIRelativeStrengthIndexParabolicSAR– ParabolicStopandReversePriceChannelsVWAP– VolumeWeightedAveragePriceZigZagMACD– MovingAverageConvergenceDivergencePPO– PercentagePriceOscillatorKST- KnowSureThingUltimateOscillatorVortexIndicator…Thelistgoesonforever

[email protected] 25

Page 26: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

Backtesting aStrategy/Risk

Provideevidenceofprofitability◦ Curvefitting/optimizationbias◦ In-samplevsout-of-sample◦ Forwardlookingbias

Risktolerance

KeyStatisticsAveragewins ::0.637USDAverageloss ::-0.438USD#Wins ::214#Losses::210#Neutrals ::3WinRate ::0.501PPC ::0.104USD#Traded ::427.0Ann.Sharpe ::2.335

[email protected] 26

Page 27: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

Backtesting aStrategyDoesthestrategyworkacrossmanyassets?

Howmanyyearsdoesitworkfor?

Doesitescapethebid-askbounce?

RiskTolerance?◦ MaximumDrawdown?

Fees?Tradingfrequency?

InSample:SPY2004-2010OutofSample:AssetsRandomlySelected:ADBEXLNXBBBYCFNEMCADPAFLDETSPLSDGADSALLMETCLPXWYN

Overall:19802016Sharpe:2.12PPC:0.13Wins:12634Losses:10527Trades:23666

Sharpe:1.299PPC:0.338Wins:255Losses:202Trades:463.0

Wouldyoutrademe?

[email protected] 27

Page 28: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

OrderSizing

Generallysizeordersinverselyproportionaltovolatility

Overall:20012016Sharpe:2.38PPC:0.19Wins:23448Losses:19719Trades:43378

Overall:20012016Sharpe:2.91PPC:0.12Wins:23448Losses:19719Trades:43378

NotVolatilitySizedOrders VolatilitySizedOrders

[email protected] 28

Page 29: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

BiasesandPitfallsThesecanbedoneunintentionally

CurveFittingBias◦ Adjusting/addingparametersuntilthestrategylooksattractiveinbacktest

Forwardlookingbias◦ Programlooksatfutureduetobugincode◦ Calculatingoptimalparameters,optimizations◦ Lookingatthedata!

SurvivorshipBias◦ Notincludingfulluniverse(pre2008crash,2007algo tradingblowup)

PsychologicalBias◦ Canyoutoleratea5monthdrawdown?Losehalfyourportfolio◦ Yourbacktests willsuggestpossibleseverity

[email protected] 29

Page 30: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

GeneralTipsThisisnotagetrichquickscheme

Findingalphaishard,donotgetdiscouraged

Drawdownarepainful,becarefulwithleverage

Trustyouralpha(ifyouhavesome),strategiesareusuallysimple

Performance◦ Outofsampleperformanceisgenerally½ofinsampleperformance◦ Livetradingperformanceisgenerally¼ofinsampleperformance◦ Duetocurvefitting,unexpectedslippage,etc.

Makesureyouaccountfortransactionfeesandslippageand ordersizes

Funandexcitingwaytolearnnotonlythemarketsbutalsocomputerscienceandmath

Dataisyourfriend

Buildyourownbacktester/executionenvironment

[email protected] 30

Page 31: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

SystemArchitectureOverview

PythonCompiledC

Multi-Threaded◦ Caninstantiatemultiplestrategies

EventDrivenBacktester◦ Eliminateserrors

Canusethesamestrategyfortradingandbacktesting

Strategy

RSI

BacktesterClient(IB) ClientA UDP ClientB

• Redundantinstances• MultipleinstancescommunicateoverUDPtocheckstate• Masterslave/slavesarchitecture• CanextendtoNinstances

• AWSandPersonalServer

[email protected] 31

Page 32: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

LimitOrderExecution– PlaceOrderBids Price Asks

100.03 2,1

100.02 3,7,8100.01 5,2,15

100 1,2,5

1,2 99.99

2,5,8 99.98

3,8,1,5,3 99.97

2,3 99.85Placelimitorderof2lotsat99.99

[email protected] 32

Page 33: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

LimitOrderExecution– BookMovementBids Price Asks

100.03 2,1

100.02 3,7,8

100.01 5,2,15

100 1,2,5

1,2,5 99.99

2,5,8 99.98

3,8,1,5,3 99.97

2,3 99.85

Fillat99.99,thisbecomesremoved,andpositionadvances.Atradehappens

Anotherorderisplacedbehindyou

Peoplecanceltheirorders

[email protected] 33

Page 34: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

LimitOrderExecution– OrderFillBids Price Asks

100.03 2,1

100.02 3,7,8

100.01 5,2,15

100 1,2,5

2,5 99.99

2 99.98

3,8,1,5,3 99.97

2,3 99.85

Afteranorderisfilledyoumoveupinthequeue,untilyoueitherarefilledorcanceltheorderWearenowfirstinthequeue

[email protected] 34

Page 35: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

Backtesting aStrategyBacktesting withLimitorderExecution◦ Simulatebyplacinglimitorders◦ Needtocheckforfills◦ Complexandrequirestime◦ Doesnotperfectlymodelslippage

Backtesting withCloseexecution◦ Ordersfilledoncloseofbar◦ Subjecttobid/askbounce◦ Mustsubtractslippagenumbers◦ Morethan2ticks?

EventDriven

[email protected] 35

Page 36: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

AppendixDETAILSTHATMIGHTBEINTERESTINGTOREAD

[email protected] 36

Page 37: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

Appendix:FurtherReadingsBestguidetostartingalgo trading(intro/backtester takenfromhere)◦ http://www.quantstart.com/

ExecutionEnvironment/Backtester/Community◦ https://www.quantopian.com/

CheaptradingplatformwithAPI◦ https://www.interactivebrokers.com/ind/en/main.php

◦ Stellardocumentationonhowtodoexecution

TechnicalAnalysisLibraryTA-Lib◦ http://ta-lib.org/◦ https://pypi.python.org/pypi/TA-Lib

Data:◦ Free:YahooFinance,GoogleFinance– errorprone◦ Cheap:PiTrading,Kibot,Tickwrite

[email protected] 37

Page 38: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

Appendix:SharpeRatio𝑆ℎ𝑎𝑟𝑝𝑒 =

𝑟N − 𝑟}𝜎N

𝑟N = 𝑝𝑜𝑟𝑡𝑓𝑜𝑙𝑖𝑜𝑟𝑒𝑡𝑢𝑟𝑛𝑟} = 𝑟𝑖𝑠𝑘𝑓𝑟𝑒𝑒𝑟𝑎𝑡𝑒

𝜎N = 𝑠𝑡𝑎𝑛𝑑𝑎𝑟𝑑𝑑𝑒𝑣𝑖𝑎𝑡𝑖𝑜𝑛𝑜𝑓𝑟𝑒𝑡𝑢𝑟𝑛Measuresriskadjustedperformance◦ Riskvs.Reward

HigherisusuallybetterRiskfreeratesometimesassumedtobe0Usuallyannualizedandvolatilitytakenasstandarddeviation◦ Monthly:Volatilitysampledmonthly*sqrt(12)◦ Daily:Volatilitysampleddaily*sqrt(252)◦ Minutely:Volatilitysampledminutely*sqrt(390*252)

[email protected] 38

Page 39: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

Appendix:Candlestick/BarDataOpen– priceatstartofbar

High– highestprice

Low– lowestprice

Close– priceatendofbar

Volume– numbertradedduringbar

Canbeonanytimescale:secondstomonthly

http://www.financial-spread-betting.com/course/candle-stick-charting.html

[email protected] 39

Page 40: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

Appendix:OrderSizingAverageTrueRangeScaling

Reducestradesizeduringtimesofvolatility,Increaseduringlowvolatility

IncreasesSharpeRatio

Canadjusttosizeofcontract,and/orcontractprice

𝐼𝑛𝑖𝑡𝑖𝑎𝑙𝐶𝑎𝑝𝑖𝑡𝑎𝑙 = $1,000

𝑇𝑟𝑎𝑑𝑒𝑆𝑖𝑧𝑒 = 𝐼𝑛𝑖𝑡𝑖𝑎𝑙𝐶𝑎𝑝𝑖𝑡𝑎𝑙𝐼𝑛𝑖𝑡𝑖𝑎𝑙𝐶𝑎𝑝𝑖𝑡𝑎𝑙

𝐴𝑇𝑅 10 ∗ 𝑀𝑖𝑛𝑇𝑖𝑐𝑘𝑆𝑖𝑧𝑒($)𝑇𝑟𝑢𝑒𝑅𝑎𝑛𝑔𝑒 = max ℎ𝑖𝑔ℎ − 𝑙𝑜𝑤 , 𝑎𝑏𝑠 ℎ𝑖𝑔ℎ − 𝑐𝑙𝑜𝑠𝑒Nuvy , 𝑎𝑏𝑠 𝑙𝑜𝑤 − 𝑐𝑙𝑜𝑠𝑒Nuvy

𝐴𝑇𝑅@ =𝐴𝑇𝑅@JK 𝑛 − 1 + 𝑇𝑟𝑢𝑒𝑅𝑎𝑛𝑔𝑒@

𝑛

[email protected] 40

Page 41: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

Appendix:PPCProfitPerContract

𝑟�𝑐 ∗ 𝑡|

𝑟� = 𝑎𝑣𝑒𝑟𝑎𝑔𝑒𝑟𝑒𝑡𝑢𝑟𝑛𝑐 = 𝑛𝑢𝑚𝑏𝑒𝑟𝑜𝑓𝑐𝑜𝑛𝑡𝑟𝑎𝑐𝑡𝑠𝑡𝑟𝑎𝑑𝑒𝑑

𝑡| = 𝑡𝑖𝑐𝑘𝑠𝑖𝑧𝑒

Ameasureofprofitability,measuredinticks

Ahighlyliquidstockusuallyhasaticksizeofapenny

Ifyourstrategyhasmorethan2ticks,itisconsideredprofitable(canescapethebid/askbounce),iftestingonbardatawithoutlimitorderexecutiononbarcloses◦ Youcansubmitmarketordersandstillmakemoney

◦ Assumesliquidity!!!!!

[email protected] 41

Page 42: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

Appendix:CAPMCapitalAssetPricingModel

𝑟� = 𝑟} + 𝐵� 𝑟� − 𝑟}𝑟} = 𝑅𝑖𝑠𝑘𝐹𝑟𝑒𝑒𝑅𝑎𝑡𝑒𝐵� = 𝐵𝑒𝑡𝑎𝑜𝑓𝑆𝑒𝑐𝑢𝑟𝑖𝑡𝑦

𝑟� = 𝐸𝑥𝑝𝑒𝑐𝑡𝑒𝑑𝑀𝑎𝑟𝑘𝑒𝑡𝑅𝑒𝑡𝑢𝑟𝑛𝑟� = 𝐴𝑠𝑠𝑒𝑡𝑅𝑒𝑡𝑢𝑟𝑛

Describestherelationshipbetweenriskandtheexpectedreturn

Investorsneedtobecompensatedfortime(riskfreerate)andrisk(beta)

[email protected] 42

Page 43: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

Appendix:DrawdownThemeasureofthelargestdropfrompeaktobottom(inpercentage)◦ Itisapainindexmeasure

Extremelyimportanttomeasurethedurationofthedrawdown◦ Doyouwanttobelosingmoneyforyears?

𝐷 𝑇 = max@∈(�,�)

{𝑋(𝑡) − 𝑋 𝑇 }

MDD 𝑇 = max@∈(�,�)

[ max@∈(�,Y)

{𝑋 𝑡 − 𝑋(𝜏)}]

Where𝑋 = 𝑋 𝑡 , 𝑡 ≥ 0 isarandomprocess

Simplyputmaximumdrawdownis:◦ (Peakvaluebeforelargestdrop– lowestvaluebeforenewhigh)/Peakvaluebeforedrop

[email protected] 43

Page 44: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

Appendix:UnderwaterCurve

Goodwaytovisualizehowmuchofthetimeyouareinadrawdown

Letsyouevaluatehowmuchpainyoushouldbeabletohandle

http://ctaperformance.com/wntn

[email protected] 44

Page 45: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

Appendix:DistributionofReturns

Generallyahistogramofreturns

Lookatcenter,shape,distribution,spread◦ Wantpositivecenter,andnomajoroutliers

http://ctaperformance.com/wntn

[email protected] 45

Page 46: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

Appendix:StrategyCorrelation

Generallyyouwanttomakesurethatyourstrategiesarenotcorrelatedtoeachother(lookatdailyreturns)◦ Youdonotwanteverythingtohaveabaddayatthesametime◦ Balancedreturnsaregood

UncorrelatedstrategiestendtoyieldhigherSharperatioswhenmixed

Correlatedstrategiestendtoreflectthesamealpha◦ Thesestrategiestendtocompetewitheachother

Negativelycorrelatedstrategiescanbegood◦ Highlynegativelycorrelatedstrategiescanindicateproblemswithyouralpha

ThankyouAaronRosenforyourfeedback

[email protected] 46

Page 47: BY: AVI THAKER Algorithmic Trading: An Introduction · Hurst(GBM): 0.498349157279 Hurst(MR): -6.26637088795e-05 Hurst(TR): 0.95964231812 Hurst(GOOG): 0.50788012279 AVI.THAKER@GMAIL.COM

Appendix:TradableAUMNotallstrategiesarecreatedequal

StrategyAmightbeabletotrade$1,000,000withoutincurringlargeslippagebuttrading$100,000,000itmightincurmuchmoreslippageandkillthestrategy◦ Marketmaking– yourabilitytocapturetheinsidebidofferdecreaseswithsize◦ Highfrequencystrategies◦ Somemomentumstrategies

SharperatiosandAUMtradableareusuallyinverselycorrelated◦ Therearesomeexceptions

Notethatthesenumbersareartificial

ThankyouAaronRosenforyourfeedback

[email protected] 47