;mc2x25 by Dave Serbin 06/04/2006
;Conf5 of ds25 and DAB's mc2x


[Expression]

;RUTTRFT.ini...FredT...9/29 and 30, 2004
;RUTMC2X.sig added 3/11/05..dab
;Vector version option added 3/06  >>>>>>>>>    LINES 81 and 87+88

PtChg = -10

;; rut-i = Edit(Shift(rut-i, -1), 12/31/06, Last(rut-i) + PtChg)

                 ;; rut-i = Edit(rut-i, 11/8/05

; PARAMETERS

ROCMin   = 1.1 ;1.08 ;1.0  ;1.1     ;1.08 to 1.11

StoLen1  = 65   ;22
StoLen2  = 75   ;1
StoLen3  = 13   ;27
StoMinS  = 87   ;83

MACDLen1 = 15 ;20 ;<<<<<<<<<<< dab..chg   ;1
MACDLen2 = 30                             ;71
MACDLen3 = 40                             ;50

RSILen   = 40   ;61
RSILB    = 4    ;54

StretchDays =  18     ;mkt days   best 18 (rocmin = 1.1), 12 (rocmin = 1.0)
                 
; STOCHASTIC

; Fast K
FastK    = 100 * (RUT-i - Min(RUT-i, StoLen1)) /
                 (Max(RUT-i, StoLen1) - Min(RUT-i, StoLen1))

; Fast D
FastD    = Ema(FastK, StoLen2)

; Slow D
SlowD    = Ema(FastD, StoLen3)

StoSig.Buy  = Signal(FastD - SlowD)
StoSig      = Edit(StoSig, 9/1/88, Buy)
StoSig.Sell = Signal(FastD - SlowD) And Signal(SlowD - StoMinS)
   
; MACD
; MACDLen1 = 20
; MACDLen2 = 30
; MACDLen3 = 40
MACD     = Ema(RUT-i, MACDLen1) - Ema(RUT-i, MACDLen2)
MACDema  = Ema(MACD, MACDLen3)
MACDSig  = Signal(MACD - MACDema)

Print(FastD - SlowD, "FastD-SlowD",
      FastD-SlowD - Shift(FastD-SlowD, 1), "1d PtChg\n",
      MACD - MACDema, "MACD-MACDema",
      MACD - MACDema - Shift(MACD - MACDema, 1), "1d PtChg\n\n") 

; RSI
; RSILen   = 40 
; RSILB    =  4
RSIx     = Rsi(RUT-i, RSILen)
SRSI     = 100 * (RSIx - Min(RSIx, RSILen)) / (Max(RSIx, RSILen) - Min(RSIx, RSILen))
SRSISig  = Signal(SRSI - Shift(SRSI, RSILB))

; ROCx
;;ROCMin   = 1.1       ;-1
ROCx     = 100 * Roc(RUT-i, 1)        ;1d chg
ROCSig   = Signal(ROCx + ROCMin)      ;1d chg + 1
;ROCSig   = Signal(100*Roc(rut-i, 1) + 1.1)   ;or 1.0

;;Print(100*Roc(rut-i, 1) + 1, "ROCSig\n", 6)

; RUTTR
;;RUTTR.Buy  = StoSig And MACDSig And SRSISig

;BELOW is Vector Version
 ruttr.Buy  = Signal(Vector(stosig) And Vector(macdsig) And Vector(srsisig))


;;RUTTR.Sell = StoSig And MACDSig And SRSISig And ROCSig

;BELOW is Vector Version
ruttr.Sell = Not Signal(Not Vector(stosig) And Not Vector(macdsig) And
             Not Vector(srsisig) And Not Vector(rocsig))

;RUTTR.Sell = RUTTR And Signal(Rsi(RUT-i, RsiDays) - NoSell)  ;nosell    > 60
RUTTR      = Stretch(RUTTR, StretchDays)  ;18  


;stoploss based on decline from peak
;fund = rut-i
;stoploss = 7.5  
;days = Shift(Days(ruttr) + 1, 0)   ;Days(ruttr) + 1
;peak = Max(fund, days)
;cdd = 100 * (peak - fund) / peak
;;ruttr = Signal(Vector(ruttr))                     ;;;suggested by brucer
;;ruttr.Buy = ruttr
;;ruttr.Sell = ruttr Or Signal(stoploss - cdd)

;stoploss based on decline from Buy price
fund     = rut-i
stoploss = 7.5   ;same to 7.3
days     = 18  ;same 13 to 23
cdd = 100*(Max(fund, days) - fund) / Max(fund, days)
Print(-cdd, "18d CDD\n\n")
;;ruttr = Signal(Vector(ruttr))                   ;;;;suggested by by brucer
ruttr.Buy = ruttr
ruttr.Sell = ruttr Or Signal(stoploss - cdd)


WriteFile(RUTTR, RUTTRFT, "RUTTRFT")
WriteFile(Signal(Shift(Vector(ruttrft), -1)), RUTTRFT0)

;MC2X.ini...06/05/04..dab

;NO added nosell until RUT drops >0.55% on one day..3/13/06  SEE LINE 559

;RUTec1.ini...RUTec optimized for Delay = 1 TRADES or HEDGES..06/04..dab 

;NSNHs..from ..BThrust.ini..3/20/02..and..06/30/03..dab..

;uses 10d Ema of NH/NH+NL  as a thrust oscillator. 

;nsnhq = Edit(nsnhq, 10/22/03, 102)
;nsnlq = Edit(nsnlq, 08/15/03, 4)


;manual iteration for delay = 0 signals.

NHtoNL = 2.57
        
;; nsnlq = Edit(Shift(nsnlq, -1),12/31/05, 50)

;; nsnhq = Edit(Shift(nsnhq, -1), 12/31/05, 50 * NHtoNL)

;;nsnlq = Edit(Shift(nsnlq, -1), 12/31/05, 29)
;;nsnhq = Edit(Shift(nsnhq, -1), 12/31/05, 45)

            
;create  nh thrust indicator.
            
BT = 100*Ema(nsnhq / (nsnhq + nsnlq), 10)

;create a signal that buys at BT = 81 and sells at 83.4

bthrust.Buy = Signal(bt -  81)   
 
bthrust.Sell = Signal(bt - 80.4)  

WriteFile(bthrust, NSNHs, "NS NH Signal")
WriteFile(bt, NSNHo,"NS NH Oscillator")

;calculate NS   NH/(NH+NL) percent for buy and sell signals

nhBuy = 100 * (5.5 *  (.810 - BT/100) + BT/100)    ;5.56
nhSell = 100 * (5.5 * (.804 - BT/100) + BT/100)

Print(nhBuy, "NS  NH/NH+NL reqd for Buy",
      nhBuy/(100 - nhSell), "NS NH/NL reqd for Buy\n",
      nhSell, "NS  NH/NH+NL reqd for Sell",
      nhSell/(100 - nhSell), "NS NH/NL reqd for Sell\n\n",3)

Print(nsnhq, "NS NH\n",
      nsnlq,  "NS NL\n",
      nsnhq/nsnlq, "NS NH/NL\n",
      100*nsnhq / (nsnhq+nsnlq), "NS NH/NH+NL\n",
      bt, "NS NHThrust\nBuy 81up\nSell 80.4dn",6)

Print(nsnhs, " NSNHs Signal")


[SignalPairTrade]

;Delay = 0
Signal = NSNHs
;Fund =   VLE-I
Fund =  DFSCX
Index = PTLDX
HoldMinDays = 1 
StartDate = 12/15/88
HistoryDays = 0 ;1000
FnuFile = M501

;[Expression]
;F = M501
;Print(-100*(Max(f,Sum(f/f)) - f)/Max(f,Sum(f/f)), "Current %DD",
;100*Roc(f,1), "1d %Chg",
;100*Roc(f,5), "5d %Chg",
;100*Roc(f,21), "1mo %Chg",
;100*Roc(f,42), "2mo %Chg",
;100*Roc(f,63), "3mo %Chg",
;100*Roc(f,84), "4mo %Chg",
;100*Roc(f,105), "5mo %Chg",
;100*Roc(f,126), "6mo %Chg",
;100*Roc(f,189), "9mo %Chg",
;100*Roc(f,252), "1yr Ann",
;100*(Pow((Roc(f,504) + 1), 1/2) - 1), "2yr Ann",
;100*(Pow((Roc(f,756) + 1), 1/3) - 1), "3yr Ann",
;100*(Pow((Roc(f,1260) + 1), 1/5) - 1), "5yr Ann",
;100*(Pow((Roc(f,2016) + 1), 1/8) - 1), "8yr Ann",
;100*(Pow((Roc(f,3780) + 1), 1/15) - 1), "15yr Ann\n\n")
                                                          
;--------------------------------------------------------------------------
[Expression]
;


Print("  >>>>>>>>> NOTE: NEXT BUY SIGNAL IS Nov. 30, 2006 <<<<<<<<\n\n") 

pointchg = -10

;;;  rut-i = Edit(Shift(rut-i, -1), 12/31/05, Last(rut-i) + pointchg)

;Buys are taken on the last mkt day of November 
;Sells are taken after the specified number of Stretch days.

xmas = Signal(Irate(0)) ;ruttr2   ;or use any existing signal in your \ft\sig file.
;;xmas = Edit(xmas, 09/01/88..12/31/2100, Sell) ;wipes out all signals
xmas = Edit(xmas, 09/01/88..12/19/88, Sell)
xmas = Edit(xmas, 11/30/88, Buy)
xmas = Edit(xmas, 12/30/88, Sell)
xmas = Edit(xmas, 11/30/89, Buy)
xmas = Edit(xmas, 12/29/89, Sell)
xmas = Edit(xmas, 11/30/90, Buy)
xmas = Edit(xmas, 12/31/90, Sell)
xmas = Edit(xmas, 11/29/91, Buy)
xmas = Edit(xmas, 12/31/91, Sell)
xmas = Edit(xmas, 11/30/92, Buy)  
xmas = Edit(xmas, 12/31/92, Sell)
xmas = Edit(xmas, 11/30/93, Buy)
xmas = Edit(xmas, 12/31/93, Sell)  
xmas = Edit(xmas, 11/30/94, Buy)
xmas = Edit(xmas, 12/30/94, Sell)      ;30
xmas = Edit(xmas, 11/30/95, Buy)
xmas = Edit(xmas, 12/29/95, Sell)      ;29
xmas = Edit(xmas, 11/29/96, Buy)
xmas = Edit(xmas, 12/31/96, Sell)      ;31
xmas = Edit(xmas, 11/28/97, Buy)
xmas = Edit(xmas, 12/31/97, Sell)      ;31
xmas = Edit(xmas, 11/30/98, Buy)
xmas = Edit(xmas, 12/31/98, Sell)      ;31
xmas = Edit(xmas, 11/30/99, Buy) 
xmas = Edit(xmas, 12/31/99, Sell)      ;31
xmas = Edit(xmas, 11/30/00, Buy)
xmas = Edit(xmas, 12/29/00, Sell)      ;29
xmas = Edit(xmas, 11/30/01, Buy)
xmas = Edit(xmas, 12/31/01, Sell)      ;31
xmas = Edit(xmas, 11/29/02, Buy) 
xmas = Edit(xmas, 12/31/02, Sell) 
xmas = Edit(xmas, 11/28/03, Buy)  
xmas = Edit(xmas, 12/31/03, Sell)
xmas = Edit(xmas, 11/30/04, Buy) ;open on 12/1/04
xmas = Edit(xmas, 12/31/04, Sell)
xmas = Edit(xmas, 11/30/05, Buy)
xmas = Edit(xmas, 12/30/05, Sell)
;xmas = Edit(xmas, 11/30/06, Buy)
 

;determine best buy and sell delays

BuyDelay =  0  ;buy 0 mkt days after end of November.  

SellDelay = 0  ;sell 0 mkt days after end of December.    

xmas.Buy = xmas And Signal(Shift(Vector(xmas), BuyDelay))
xmas.Buy = xmas And RUTTRFT

xmas.Sell = xmas And Signal(Shift(Vector(xmas), SellDelay))

xmas = Stretch(xmas, 25) ;best 22 to 25, ok to 36    ;24

WriteFile(xmas, Xmas2)

Print(Xmas2, "Xmas2 Signal")
Print(Days(xmas2), "Days this Signal\n HoldMin = 25 mkt days\n\n")
;--------------------------------------------------------------------------   

[Expression]
; 0up And DFSCX MacdH > 0.5up Or RUTRsi14 > 60\n
       Sell when RUT StocH < 2dn And DFSCX MacdH < -10dn\n
       NoSell when RUTOsc > 0.6\n\n") ; Or RUT Rsi14 > 60\n\n")


;Compute filtered stochastic
;histogram signal for RUT-I

fund1 =     RUT-I       
Lookback =   57          
Smooth =     45    
Trigger =     2    
Buyvalue =    0   
Sellvalue =   2     
BuyRsi =     60   ;forces a buy at this Rsi level regardless 

;create rut price oscillator signal

r2osclevel1 =  4.4
r2osclevel2 =  0.6    

r2osc = 100*(Ema(rut-i, 19) - Ema(rut-i, 39)) / Ema(rut-i, 39)
r2oscs.Buy = Signal(r2osc - r2osclevel2)
r2oscs.Sell = Signal(r2osc - r2osclevel1) Or Signal(r2osc - r2osclevel2)
ptchg = 19*Ema(rut-i,39) - 18*Ema(rut-i,19)+.2*r2osclevel2*Ema(rut-i,39) - rut-i

WriteFile(r2oscs, R2OscS)

stoch = 10000 * (fund1 - Min(fund1, lookback)) /
 (Max(fund1, lookback) - Min(fund1, lookback))
stoch = Ema(stoch, Smooth)
average = Ema(stoch, Trigger)
histogram = stoch - average
buystate = (histogram - Buyvalue)
 And (Buyvalue - Shift(histogram, 1))
sellstate = (Sellvalue - histogram)
 And (Shift(histogram, 1) - Sellvalue)
stochsig.Buy = Signal(buystate)
stochsig.Sell = Signal(-sellstate)
;;WriteFile(stochsig, rutstoc, "StocH signal rut-i")
;;WriteFile(histogram, StoH,"StocH rut-i")

;compute filtered macd histogram for DFSCX index fund

fund2 =      DFSCX
longma =     83
shortma =    38      
signalma =    2       
buylevel =   .5                                           
selllevel = -10    

macd = Ema(fund2, shortma) - Ema(fund2, longma)
signalline = Ema(macd, signalma)
macdhisto = 10000*(macd - signalline)
macdsig.Buy = Signal(macdhisto - buylevel)
macdsig.Sell = Signal(macdhisto - selllevel)
;;WriteFile(macdsig, dfsmacd, "MacdH signal dfscx")

;Combine dfsmacd with rutstoc, buy both positive, sell both negative.
rutdfs.Buy = stochsig And macdsig  ;;dfsmacd
rutdfs.Buy = rutdfs Or Signal(Rsi(fund1, 14) - BuyRsi)

rutdfs.Sell = stochsig And macdsig  ;;dfsmacd

rutdfs.Sell = rutdfs And R2OscS  ;<<<<<<<<< 0.6\n",      
     ptchg, "RUT PtChg to 0.6\n")

Print(histogram, "RUT StoH,B: 0up,S: 2dn",
     histogram - Shift(histogram, 1), "PtChg\n",
macdhisto, "DFS MacdH, B: 0.5up,S: -10dn",
     macdhisto - Shift(macdhisto, 1), "PtChg\n",
(histogram + 0) / (Shift(histogram, 1) - histogram),
"Days to RUT StocH x/o",
macdhisto / (Shift(macdhisto, 1) - macdhisto),
"Days to DFS MacdH x/o\n",
Rsi(fund1, 14), "RUT Rsi14, B: 60up\n",
Days(dfsrut), "Days this signal\n",
fund1, "RUT-I Price",
100*Roc(fund1, 1), "RUT-I %Roc",
fund1 - Shift(fund1, 1), "RUT-I PtChg\n",
fund2, "DFSCX Price",
100*Roc(fund2, 1),"DFSCX %Roc",
fund2 - Shift(fund2, 1), "DFSCX PtChg")

;--------------------------------------------------------------------------

[Expression]

;= 58 or 48.5\n
        Or PSE Rsi14 >= 47\n
        Or Xmas2 on Buy\n
        Or NSNHs on Buy\n")
     
Print(emadiffbuy, "PSE Ema3 - Ema40 B*\n",
emadiffsell, "PSE Ema9 - Ema31 S*\n",                    ;9/31.. was 6/35
Ema(dfscx, 4) - Ema(dfscx, 11), "DFSCX Ema4 - Ema11 S*\n",
;;Ema(pse-x, 6) - Ema(pse-x, 35), "PSE Ema6 - Ema35 S\n",
;nextsellpts, "PSE Pts for Ema Sell x/o ",
;nextsellpcnt, "PSE Pcnt for Ema Sell x/o \n",
Rsi(rut-i, 14), "RUT Rsi14, NS>58, SS<42\n",
Rsi(pse-x, 14), "PSE Rsi14, NS>47\n")

If(Vector(RUTec1))
Print(nextsellpts, "PSE PtChg for Ema x/o Sell>\n",
      nextsellpcnt, "PSE PcntChg for Ema x/o Sell>\n\n")
EndIf
If(Not Vector(RUTec1))
Print(nextbuypts, "PSE PtChg for Ema x/o Buy>\n",
      nextbuypcnt, "PSE PcntChg for Ema x/o Buy>\n\n")

EndIf
          

[SignalPairTrade]             
Delay = 1                     
Signal = RUTec1
Fund =  HRTVX
;Fund = PRSVX
;Fund = OTCFX
;Fund = PENNX
Index = PTLDX   ;short term bond
HoldMinDays =   1      ;Iterate(1..30) ;ok 1 to 8
StartDate = 12/15/88   ;Iterate(9/1/88..12/31/88)
HistoryDays = 50
FnuFile = M502

[Expression]
f = M502
mdays = Sum(f / f)
peak = Max(f, mdays)
cdd = (peak - f) / peak
Print(-100*cdd, "Current %DD",
100*Roc(f, 1), "1d %Chg",
100*Roc(f, 5), "5d %Chg",
100*Roc(f, 21), "1mo %Chg",
100*Roc(f, 42), "2mo %Chg",
100*Roc(f, 63), "3mo %Chg",
100*Roc(f, 84), "4mo %Chg",
100*Roc(f, 105), "5mo %Chg",
100*Roc(f, 126), "6mo %Chg",
100*Roc(f, 189), "9mo %Chg",
100*Roc(f, 252), "1yr Ann",
100*(Pow((Roc(f,504) + 1), 1/2) - 1), "2yr Ann",
100*(Pow((Roc(f,756) + 1), 1/3) - 1), "3yr Ann",
100*(Pow((Roc(f,1008) + 1), 1/4) - 1), "4yr Ann",
100*(Pow((Roc(f,1260) + 1), 1/5) - 1), "5yr Ann",
100*(Pow((Roc(f,2268) + 1), 1/9) - 1), "9yr Ann",
100*(Pow((Roc(f,2520) + 1), 1/10) - 1), "10yr Ann",
100*(Pow((Roc(f,3906) + 1), 1/15.5) - 1), "15.5yr Ann")

[SignalPairTrade]
Delay = 1
Signal = RUTec1
;;;Signal = fubar5  ;w/ nsnh0
Fund =  BRSIX  ;7/31/97                         ;39.7, 6.0, 14/18
;;Fund =  MMEYX
;;Fund = RYVPX  ;1/15/02
;;Fund = HRTVX
;;Fund = RSPFX
;;Fund = FRMCX
Index = FDRXX ;PTLDX
HoldMinDays =   1 ;Iterate(1..30) ;ok 1 to 10 cal days
StartDate =  7/31/97 
;StopDate = 12/31/94
HistoryDays = 50
FnuFile = M503

[Expression]
F = M503
Print(-100*(Max(f,Sum(f/f)) - f)/Max(f,Sum(f/f)), "Current %DD",
100*Roc(f,1), "1d %Chg",
100*Roc(f,5), "5d %Chg",
100*Roc(f,21), "1mo %Chg",
100*Roc(f,63), "3mo %Chg",
100*Roc(f,84), "4mo %Chg",
100*Roc(f,105), "5mo %Chg",
100*Roc(f,126), "6mo %Chg",
100*Roc(f,189), "9mo %Chg",
100*Roc(f,252), "1yr %Chg",
100*(Pow((Roc(f,504) + 1), 1/2) - 1), "2yr Ann",
100*(Pow((Roc(f,756) + 1), 1/3) - 1), "3yr Ann",
100*(Pow((Roc(f,1260) + 1), 1/5) - 1), "5yr Ann",
100*(Pow((Roc(f,1713) + 1), 1/6.8) - 1), "6.8yr Ann\n")


Print(nsnhs,    "   NSNHs Signal",
      xmas2,    "   Xmas2 Signal",
      r2oscs,   "  R2OscS Signal",
      dfsrut,   "  DFSRUT Signal",
      rutec1,   "  RUTec1 Signal")


;---------------------------------------------------------------------------

;Micro2X.ini..emafast2.ini..modified 4/3/02..dab.
;last modified 06/05/04

;Uses PBHGX instead of vwegx.
;Creates a combo signal of all trades of RUTec1 (Delay = 1) and
;a modified version of EmaFast2 (Delay = 1).

;emafast.ini by Dave Serbin,  10/25/01
;This is a very simple market timing signal called emafast which is
;based on the exponential moving average (ema) crossovers of
;fusmx (Fremont US Microcap) and vwegx (Van Waggoner Emerging Growth).
;The signal is on a buy when both emas on a buy, and on a sell 
;when both emas are on a sell. This signal differs from emacombo in that
;the ema for vwegx for the buy state is tighter.


[Expression]

PBHGX = OBHGX
PLCVX = OLCVX


;manual iteration area
pointchg = .01

;;fusmx = Edit(Shift(fusmx, -1), 12/31/05, Last(fusmx) + pointchg)

pointchg2 = -.12

;;;pbhgx = Edit(Shift(pbhgx, -1), 12/31/05, Last(pbhgx) + pointchg2)

pointchg3 = -.21

;;;plcvx = Edit(Shift(plcvx, -1), 12/31/05, Last(plcvx) + pointchg3)

pointchg4 = -.52

;;;tmcgx = Edit(Shift(tmcgx, -1), 12/31/05, Last(tmcgx) + pointchg4)


;fund tests 
f1 =    tmcgx   ;mmeyx ;bmcfx ;rssgx ;safgx ;rhjsx


f2 = plcvx;pbhgx

fusemb.Buy = Signal(Ema(mmcfx, 3) - Ema(mmcfx, 21))      ;  20 to 22                                                     
pbcmb.Buy = Signal(Ema(pbhgx, 5) - Ema(pbhgx,  19))      ; 17 to 21  pbhgx
pbcmb.Sell = Signal(Ema(plcvx, 3) - Ema(plcvx, 41));12;9to12*+ or38to50;pbhgx
tmcgb.Sell = Signal(Ema(tmcgx, 3) - Ema(tmcgx, 41));52   ;  41* to 52*
                                               
                                                  
emafast.Buy = pbcmb And fusemb    ;pbhg 5/19 And fusm 3/21
emafast.Sell = emafast And pbcmb  ;plcv 3/41  ;pbgh 3/39
emafast.Sell = emafast And tmcgb  ;tmcg 3/41   

WriteFile(emafast, MC2)

;create combo between emafast and rutec1

combo.Buy = MC2  Or RUTec1
combo.Buy = combo Or Signal(Rsi(rut-i, 42) - 59)  ;-------------------

combo.Sell = MC2  Or RUTec1     ;trade on all signals

;;(not used) combo.Sell = combo And NSNHs   ;<<<<<<<<<<<  Added 8/12/03

combo.Sell = combo And Xmas2   ;marginal contribution, optional

;combo.Sell = combo And Signal(Rsi(rut-i, 42) - 59)  ;ok but no help

combo.Sell = combo And Signal(100*Roc(rut-i, 1) + 0.57)   ;<<<