Stock Portfolio Organizer
The ultimate porfolio management solution.
Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
WiseTrader Toolbox
#1 Selling Amibroker Plugin featuring:
Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
pt for Amibroker (AFL)
Copy & Paste Friendly
Back
// AFL code by E.M.Pottasch, Feb 2014 // testing Pair trading using stocks SetTradeDelays(0,0,0,0); SetOption("CommissionMode",3); SetOption("CommissionAmount",0.005); SetOption("MaxOpenPositions",2); SetOption("PriceBoundChecking",False); Symbol1="IWM"; Symbol2="SPY"; Symbol1_price=Foreign(Symbol1,"Close"); Symbol2_price=Foreign(Symbol2,"Close"); slip=0.0; per=20; // normalized price: (Price-Mean)/(Standard Deviation) Symbol1_Normalized_price=(Symbol1_price-MA(Symbol1_price,per))/StDev(Symbol1_price,per); Symbol2_Normalized_price=(Symbol2_price-MA(Symbol2_price,per))/StDev(Symbol2_price,per); spread=(Symbol1_Normalized_price-Symbol2_Normalized_price); YourSystemEntrySignal=Cross(-1,spread); YourSystemExitSignal=Cross(spread,1); if(Name()==Symbol1) { Buy=YourSystemEntrySignal;BuyPrice=C+slip; Sell=YourSystemExitSignal;SellPrice=C-slip; Buy=ExRem(Buy,Sell); Sell=ExRem(Sell,Buy); Short=Sell;ShortPrice=C-slip; Cover=Buy;CoverPrice=C+slip; SetPositionSize(5000,spsValue); } if(Name()==Symbol2) { Short=YourSystemEntrySignal;ShortPrice=C-slip; Cover=YourSystemExitSignal;CoverPrice=C+slip; Short=ExRem(Short,Cover); Cover=ExRem(Cover,Short); Buy=Cover;BuyPrice=C+slip; Sell=Short;SellPrice=C-slip; SetPositionSize(5000,spsValue); } SetChartOptions(0,chartShowDates); Plot(spread,"\nSI",colorBlue,styleThick); Plot(Symbol1_Normalized_price,"\n"+Symbol1,colorWhite,1); Plot(Symbol2_Normalized_price,"\n"+Symbol2,colorRed,1); if(Name()==Symbol1) { PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorDarkGreen,0,spread,-15); PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorRed,0,spread,-15); } if(Name()==Symbol2) { PlotShapes(IIf(Short,shapeSmallDownTriangle,shapeNone),colorRed,0,spread,-15); PlotShapes(IIf(Cover,shapeSmallUpTriangle,shapeNone),colorDarkGreen,0,spread,-15); }