Tuesday, January 3, 2012

CodingBat String-3 sumNumbers

It puzzled me a bit so I thought to share this...

public int sumNumbers(String str) {
  int len=str.length(),sum=0,count=0;
  boolean num=false;
  String tmp="";
  for (int i=0;i<len;i++){
    if (Character.isDigit(str.charAt(i))){
      num=false;
      tmp="";
      count=0;
      for (int j=i;j<len;j++){
        if (Character.isDigit(str.charAt(j))){
          count++;
          num=true;
          tmp+=str.charAt(j);
        }
        else{
          if (num) {
            break;
          }
        }
      }
      if (tmp.length()>0){
        i+=count;
        sum+=Integer.parseInt(tmp);
      }
    }
  }
  return sum;
}

Monday, December 26, 2011

Pair Trading

Currently trying to feed a Pair Trading System to a machine learning algorithm.
The system is from "Professional Stock Trading, System Design and Automation" by MARK R. CONWAY & AARON N. BEHLE.
The source of Spread Indicator which is the "Kernel" of the system in Tradestation's EasyLanguage:
{
**************************************************
Acme Spread: Plot the Spread between 2 Securities
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);

Variables:
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
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);
Plot1(Spread, "Spread");
Plot2(Upperband, "UB");
Plot3(0, "Zero");
Plot4(Lowerband, "LB");

The idea is to train a Support Vector Machine to model the "Neutrality" of the pair. Has to be applied a Neural Network before the SVM in order to regress to the "horizontal".

The source code of the decision system in Tradestation's Easylanguage:

{
*****************************************************
Acme P System: Pairs Trading


Requirements
------------
Data1: Stock 1 Intraday
Data2: Stock 2 Intraday
Data3: Stock 1 Daily (Hidden)
Data4: Stack 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),
{TradeLogging}
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 Short("Acme P SE") N Shares This Bar on Close;

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

{Log Trades for Spreadsheet Export}

Condition1 = AcmeLogTrades(LogTrades, Logfile, "P") ;