Software to help in pairs trading

Discussion in 'Trading Software' started by NKNY, Oct 8, 2003.

  1. Bob111

    Bob111

    how to do it in tradestation is explained in this book-

    http://www.amazon.com/exec/obidos/A...5644552/sr=2-2/ref=sr_2_2/104-7601826-8989532


    here code for this system from book above-

    { AcmePSystem
    P266
    Signal
    Acme P System: Pairs Trading

    Requirements
    ------------

    Data1: Stock 1 Intraday
    Data2: Stock 2 Intraday
    Data3: Stock 1 Daily (hidden)
    Data4: Stock 2 Daily (hidden)}

    Inputs:
    Price1(Close of Data3),
    Price2(Close of Data4),
    StandardDeviations(1.5),
    Length(30),
    {Position Sizing Parameters}
    Equity(100000),
    RiskModel(3),
    RiskPercent(2.0),
    RiskATR(1.0),
    {Trade Logging}
    LogTrades(False),
    LogFile("Orders.txt");

    Variables:
    N(0),
    HV1(0.0),
    HV2(0.0),
    CV(0.0),
    VolatilityBand(0.0),
    VolatilityConstant(0.0523),
    UpperBand(0.0),
    LowerBand(0.0),
    Spread(0.0);

    If Date <> Date[1] Then Begin
    N = AcmeGetShares(Equity, RiskModel, RiskPercent, RiskATR)
    of Data3;
    HV1 = AcmeVolatility(Length) of Data3;
    HV2 = AcmeVolatility(Length) of Data4;
    CV = Correlation(Price1, Price2, Length);
    VolatilityBand = VolatilityConstant * (HV1 + HV2) * (1 - CV);
    UpperBand = StandardDeviations * VolatilityBand;
    LowerBand = StandardDeviations * (-VolatilityBand);
    End;

    Spread = (Close of Data1 / Price1) - (close of Data2 / Price2);

    If Spread crosses above LowerBand Then
    Buy("Acme P LE") N Shares This Bar on Close;

    If Spread crosses above 0 Then
    Sell("Acme P LX +") This Bar on Close
    Else If Spread <= StandardDeviations * LowerBand Then
    Sell("Acme P LX -") This Bar on Close;

    If Spread crosses below UpperBand Then
    Sell("Acme P SE") N Shares This Bar on Close;

    If Spread crosses below 0 Then
    ExitShort("Acme P SX +") This Bar on Close
    Else If Spread >= StandardDeviations * UpperBand Then
    ExitShort("Acme P SX -") This Bar on Close;

    {Log Trades for Spreadsheet Export}
    Condition1 = AcmeLogTrades(LogTrades, LogFile, "P");

    {***********************************************
    Acme Trade Manager: Set Stops and Profit targets
    **********************************************}

    Inputs:
    SystemID(""),
    {Position Management Parameters}
    ExitFactor(0.25),
    StopBars(1),
    ProfitTarget(True),
    ProfitFactor(0.9),
    HoldBars(5),
    DrawTargets(True),
    {Trade Logging}
    LogTrades(False),
    LogFile("Orders.txt");

    Variables:
    ATR(0.0),
    ATRLength(20),
    ATRFactor(0.0),
    ProfitBars(0),
    SellStop(0.0),
    SellTarget1(0.0),
    SellTarget2(0.0),
    CoverStop(0.0),
    CoverTarget1(0.0),
    CoverTarget2(0.0);

    ATR=Volatility(ATRLength);
    ATRFactor=ProfitFactor * ATR;
    ProfitBars=IntPortion(HoldBars / 2)+1;

    SellStop=Lowest(Low,StopBars)-(ExitFactor * ATR);
    ExitLong("Acme LX-") Next Bar at SellStop Stop;

    If ProfitTarget Then Begin
    SellTarget1=High + ATRFactor;
    ExitLong("Acme LX+") CurrentContracts / 2 Shares Next Bar at
    SellTarget1 Limit;
    SellTarget2=high[ProfitBars] + (2 * ATRFactor);
    ExitLong("Acme LX++") CurrentContracts / 2 Shares Next Bar at
    SellTarget2 Limit;
    End;

    If BarsSinceEntry >= HoldBars -1 Then
    ExitLong("Acme LX") Next Bar on Open;

    CoverStop = Highest(High, StopBars) + (ExitFactor * ATR);
    ExitShort("Acme SX-") Next Bar at CoverStop Stop;

    If ProfitTarget Then Begin
    CoverTarget1=Low - ATRFactor;
    ExitShort("Acme SX+") CurrentContracts / 2 Shares Next Bar at
    CoverTarget1 Limit;
    CoverTarget2 = Low[ProfitBars] - (2 * ATRFactor);
    ExitShort("AcmeSX++") CurrentContracts / 2 Shares Next Bar at
    CoverTarget2 Limit;
    End;

    If BarsSinceEntry >= HoldBars -1 Then
    ExitShort("Acme SX") Next Bar on Open;
    {Draw Exit Targets on the Chart}


    If DrawTargets Then
    If MArketPosition = 1 Then
    Condition1 = AcmeExitTargets(SystemID,SellStop,SellTarget1,SellTarget2)

    else if MarketPosition = -1 Then
    Condition1 = AcmeExitTargets(SystemID,CoverStop,CoverTarget1,CoverTarget2);

    { Log Targets for Spreadsheet Export}

    Condition1=AcmeLogTrades(LogTrades,LogFile,SystemID);





    {**************************************
    AcmeVolatility : Calculate the Annualized historic volatility
    **************************************}

    Inputs:
    Length(Numeric);

    Variables:
    DaysInYear(365),
    DaysInMonth(30),
    DaysInWeek(7),
    TimeFactor(0.0);

    AcmeVolatility= 0;

    If Close > 0 And Close[1] > 0 Then Begin
    If DataCompression >=2 and DataCompression < 5 Then Begin
    If DataCompression = 2 Then {Daily}
    TimeFactor= DaysInYear
    else if DataCompression = 3 Then {Weekly}
    TimeFactor=DaysInYear / DaysInWeek
    else if DataCompression = 4 Then {Monthly}
    TimeFactor = DaysInYear / DaysInMonth;

    AcmeVolatility=StdDev(Log(Close / Close[1]),Length) * SquareRoot(TimeFactor);
    End;
    End;




    {***********************************************************
    AcmeGetShares: Calculate number of shares based on risk model

    RiskModel=1, Equal Value Risk Model
    RiskModel=2, Percent Risk Model
    RiskModel=3, Percent Volatility Model

    ************************************************************}


    Inputs:
    Equity(Numeric),
    RiskModel(Numeric),
    RiskPercent(numeric),
    RiskUnits(Numeric);

    Variables:
    MinimumShares(200),
    RiskShares(0),
    ERP(0.0),
    Length(20);
    ERP=Equity * RiskPercent / 100;

    If RiskModel=1 and Close >0 then
    RiskShares=MaxList(MinimumShares,100 * IntPortion(Equity /(100 * Close)));

    If RiskModel=2 and RiskUnits > 0 then
    RiskShares=MaxList(MinimumShares,100 * IntPortion(ERP / (100 * RiskUnits)));

    If RiskModel=3 and Volatility(Length) > 0 Then
    RiskShares=MaxList(MinimumShares,100 * IntPortion(ERP / (100 * Volatility(Length))));


    AcmeGetShares=RiskShares;



    {********************************************
    AcmeLogTrades : Log Trades into a file for import into a spreadsheet
    ********************************************}

    Inputs:
    LoggingOn(TrueFalse),
    LogFileName(String),
    SystemID(String);

    Variables:
    ADXLength(14),
    Length(30),
    TradeString("");

    AcmeLogTrades = false;

    If LoggingOn and Category = 2 Then Begin
    {Log Closed Positions}
    If BarsSinceExit(1) = 1 Then Begin
    TradeString = GetSymbolName + "," +
    NumToStr(EntryDate(1),0) + "," +
    NumToStr(EntryPrice(1),3) + "," +
    NumToStr(ExitDate(1),3) + "," +
    NumToStr(ExitPrice(1),3) + "," +
    NumToStr(PositionProfit(1),3) + "," +
    NumToStr(ADX(ADXLength)[BarsSinceEntry(1)],0) + "," +
    NumToStr(Volatility(Length)[BarsSinceEntry(1)],2) + "," +
    NumToStr(AcmeVolatility(Length)[BarsSinceEntry(1)],3) +
    "," + SystemID + NewLine;

    FileAppend(LogFileName,TradeString);
    End;
    {Log Open Positions }
    If LastBarOnChart and CurrentEntries > 0 Then Begin
    TradeString = GetSymbolName + "," +
    NumToStr(EntryDate(0),0) + "," +
    NumToStr(EntryPrice(0),3) + "," +
    NumToStr(Date,0) + "," +
    NumToStr(Close,3) + "," +
    NumToStr(ExitPrice(1),3) + "," +
    NumToStr(OpenPositionProfit,3) + "," +
    NumToStr(ADX(ADXLength)[BarsSinceEntry(0)],0) + "," +
    NumToStr(Volatility(Length)[BarsSinceEntry(0)],2) + "," +
    NumToStr(AcmeVolatility(Length)[BarsSinceEntry(0)],3) +
    "," + SystemID + NewLine;

    FileAppend(LogFileName,TradeString);
    End;

    AcmeLogTrades = True;

    End;
     
    #11     Oct 9, 2003