[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[EquisMetaStock Group] Unilateral Pairs Trading



PureBytes Links

Trading Reference Links

Here is the code for Unliateral Pairs Trading, from the book "Trade 
like a hedgefund". It is in wealthlab format. Can it be converted to 
metastock format? The formula's specfics areon page 24 and 25. But I 
will type them in later on for reference. The method compares the QQQ 
with SPY. There are 3 windows:

1) No of standard deviations
2) ratio of QQQ/SPY and 20 period MA
3) QQQ daily chart


Thank you,
Dennis

VAR Close1, Close2, RatioPane, StDevPane, TextPane, Bar, Sym, 
ActualRatioSeries,
    ActualRatioSMASeries, DeltaSeries, DeltaSMASeries,
    DeltaDifferenceSeries, DeltaNormSeries, MAPeriod: Integer;
VAR Up_Threshold, Down_Threshold, Exit_Up_Threshold, 
Exit_Down_Threshold,
    R, Value: float;
VAR Stock1, Stock2, LongSymbol, ShortSymbol, CurrentSymbol, 
TestSymbol: STRING;

// assigning values to variables
Up_Threshold := #OptVar1 / 10;
Down_Threshold := -(#OptVar2 / 10);
Exit_Up_Threshold := #OptVar4 / 10; // positions entered above 
Up_Threshold are exited here
Exit_Down_Threshold := -(#OptVar5 / 10); // positions entered below 
Down_Threshold are exited here
MAPeriod := #OptVar3;

// If you want to enter your symbols manually, comment the code 
between the ****
// out and enter your stocks in the lines between the ####, then 
click on one of
// the stocks of the chosen pair in your watchlist

// 
**********************************************************************
******
// This loop makes sure all series are synchronized
FOR SYM := 0 TO WatchListCount - 1 DO
  SetPrimarySeries( WatchListSymbol( SYM));
// end of synch loop

// find the best correlated symbol
R := 0.0;
LongSymbol := '';
Value := -1.0;
CurrentSymbol := GetSymbol;
FOR SYM := 0 TO WatchListCount - 1 DO
BEGIN
  TestSymbol := WatchListSymbol( SYM);
  SetPrimarySeries(TestSymbol);
  Close1 := #Close;
  RestorePrimarySeries;
  R := Correlation( Close1, #Close, 0, BarCount - 1);
  Print(TestSymbol + ' ' + FormatFloat('#.####', R));
  IF (R > Value) AND (TestSymbol <> GetSymbol) THEN
  BEGIN
    CurrentSymbol := TestSymbol;
    Value := R;
  END;
END;
// end of "find the best correlated symbol" code
// 
**********************************************************************
******
// 
######################################################################
######
Stock1 := GetSymbol;      // the symbol of the Price Series that is 
currently being operated on
Stock2 := CurrentSymbol;  // the symbol with the best correlation
// 
######################################################################
######

// compute series
SetPrimarySeries( Stock1);
Close1 := #Close;
SetPrimarySeries( Stock2);
Close2 := #Close;
RestorePrimarySeries;
ActualRatioSeries := DivideSeries( Close1, Close2);
ActualRatioSMASeries := SMASeries( ActualRatioSeries, MAPeriod);
DeltaSeries := SubtractSeries( ActualRatioSeries, 
ActualRatioSMASeries);
DeltaSMASeries := SMASeries( DeltaSeries, MAPeriod);
DeltaDifferenceSeries := SubtractSeries( DeltaSeries, DeltaSMASeries);
DeltaNormSeries := DivideSeries( DeltaDifferenceSeries, StdDevSeries( 
DeltaSeries, MAPeriod));

// correlation coefficient
Value := Correlation( Close1, Close2, 0, BarCount - 1);

// graphics
HideVolume;
EnableNotes(false);

RatioPane := CreatePane( 100, True, True);
PlotSeries( ActualRatioSeries, RatioPane, #Navy, #Thick);
PlotSeries( ActualRatioSMASeries, RatioPane, #Blue, #Thick);
DrawLabel('Ratio ' + Stock1 + '/' + Stock2 + ' and its '
          + IntToStr(MAPeriod) + ' Period Moving Average' , 
RatioPane);

StDevPane := CreatePane( 100, True, True);
PlotSeries( DeltaNormSeries, StDevPane, #Red, #ThickHist);
DrawHorzLine( Up_Threshold, StDevPane, #Black, #Thick);
DrawHorzLine( Down_Threshold, StDevPane, #Black, #Thick);
DrawHorzLine( Exit_Down_Threshold, StDevPane, #Blue, #Thick);
DrawHorzLine( Exit_Up_Threshold, StDevPane, #Blue, #Thick);
DrawLabel('nb of standard deviations', StDevPane);

TextPane := CreatePane( 40, False, False);
DrawText( 'Correlation: ' + FormatFloat('0.####', Value), TextPane, 
10, 10, #Black, 18 );

// procedures
PROCEDURE Close_Positions();
BEGIN
  if (ShortSymbol = Stock1) then LongSymbol := Stock2
    else LongSymbol := Stock1;
  SetPrimarySeries( ShortSymbol);
  CoverAtMarket( Bar + 1, LastPosition - 1, 'Cover ' + ShortSymbol);
  SetPrimarySeries( LongSymbol);
  SellAtMarket( Bar + 1, LastPosition, 'Sell ' + LongSymbol);
  RestorePrimarySeries;
END;

PROCEDURE Enter_Positions();
BEGIN
  if (ShortSymbol = Stock1) then LongSymbol := Stock2
    else LongSymbol := Stock1;
  SetPrimarySeries( ShortSymbol);
  ShortAtMarket( Bar + 1, 'ShortSell ' + ShortSymbol);
  SetPrimarySeries( LongSymbol);
  BuyAtMarket( Bar + 1, 'Buy ' + LongSymbol);
  RestorePrimarySeries;
END;

// main loop
FOR Bar := MAPeriod + 20 TO BarCount - 1 DO
BEGIN
  IF LastPositionActive THEN
  BEGIN
    IF ((GetSeriesValue( Bar, DeltaNormSeries) < Exit_Up_Threshold) 
AND (ShortSymbol = Stock1)) THEN
    BEGIN
      ShortSymbol := Stock1;
      Close_Positions();
    END; // < Exit_Up_Threshold
    IF ((GetSeriesValue( Bar, DeltaNormSeries) > Exit_Down_Threshold) 
AND (ShortSymbol = Stock2)) THEN
    BEGIN
      ShortSymbol := Stock2;
      Close_Positions();
    END; // < Exit_Up_Threshold
  END    // if LastPositionActive
  ELSE   // no position active
  BEGIN
    IF GetSeriesValue( Bar, DeltaNormSeries) > Up_Threshold THEN
    BEGIN
      ShortSymbol := Stock1;
      Enter_Positions();
    END; // if > Up_Threshold
    IF GetSeriesValue( Bar, DeltaNormSeries) < Down_Threshold THEN
    BEGIN
      ShortSymbol := Stock2;
      Enter_Positions();
    END; // if < Down_Threshold
  END;   // else
END;




------------------------ Yahoo! Groups Sponsor --------------------~--> 
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/BefplB/TM
--------------------------------------------------------------------~-> 

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/equismetastock/

<*> To unsubscribe from this group, send an email to:
    equismetastock-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/