Tutti i Sistemi che Volete (codificati) - randello
¶
By: Missing10 on Venerdì 14 Dicembre 2001 20:33
Trading strategy
and system code
Any questions about the code on this page should be directed to theoptisizer@yahoo.com.
Sept. 2000
Oct. 2000
Nov. 2000
Dec. 2000
Jan./Feb. 2001 March 2001
April 2001 May 2001 June 2001 July 2001 Aug. 2001 Sep. 2001
Oct. 2001 Nov. 2001 Dec. 2001 Jan. 2002
September 2000 Trading System Lab (variation):
Inputs: LookBack(9), WhereToBuy(0.5);
Variables: HighValue(0), LowValue(99999), BuyValue(0);
LowValue = Lowest(Low, LookBack);
HighValue = Highest(High, LookBack);
BuyValue = (HighValue - LowValue) * WhereToBuy ;
If MarketPosition = 0 Then Begin
Buy tomorrow at BuyValue + LowValue Stop;
If Close < LowValue[1] Then
ExitLong at Open;
If BarsSinceEntry = 9 and OpenPositionProfit < 0 Then
ExitLong on Open;
For "Pivotal Trading" by John Ehlers in the September 2000 issue:
Vars: Pivot(0), S1(0), S2(0), R1(0), R2(0);
Pivot = (H[1] + L[1] + C[1]) / 3;
R1 = 2*Pivot - L[1];
S1 = 2*Pivot - H[1];
R2 = Pivot + (R1 - S1);
S2 = Pivot - (R1 - S1);
Plot1(R1, "R1");
Plot2(S1, "S1");
October 2000
The following TradeStation code is from "Moving beyond the closing price" in the issue.
For TradeStation users, the EasyLanguage code is:
Inputs: LookBack(20);
Variable: PercChange(0),
NewLevel(0),
AvgPercChange(0);
PercChange = (Close -
Close[1])/Close[1];
AvgPercChange =
Average(PercChange,20);
NewLevel = Close *
(1 + AvgPercChange);
Plot1[1](NewLevel,ää);
**
Inputs: StDevs(1);
Variable:StdDevChange(0),
UpLevel(0), DownLevel(0);
StdDevChange =
StdDev(PercChange, 20);
UpLevel = Close *
(1 + AvgPercChange +
StDevs * StdDevChange);
DownLevel = Close *
(1 + AvgPercChange -
StDevs * StdDevChange);
Plot2[-1](UpLevel,ää);
Plot3[-1](DownLevel,ää);
**
HighPercChange = (High - Close[1])/Close[1];
**
Input: VSStd(1);
Vars: SumVS(0), AvgVS(0), DiffVS(0), StdVS(0), SetArr(0),
SumArr(0), DiffArr(0), VSLow(0), VSMid(0), VSHigh(0);
{First we define the array}
Array: VS[20](0);
{Then weâre using a loop function to fill it with the different price changes}
For SetArr = 0 To 4 Begin
VS[SetArr * 4 + 0] = (O[SetArr] - C[SetArr + 1]) / C[SetArr + 1];
VS[SetArr * 4 + 1] = (H[SetArr] - C[SetArr + 1]) / C[SetArr + 1];
VS[SetArr * 4 + 2] = (L[SetArr] - C[SetArr + 1]) / C[SetArr + 1];
VS[SetArr * 4 + 3] = (C[SetArr] - C[SetArr + 1]) / C[SetArr + 1];
End;
For SumArr = 0 To 19 Begin
If SumArr = 0 Then
SumVS = 0;
SumVS = SumVS + VS[SumArr];
If SumArr = 19 Then
{Here we calculate the average price change over the period}
AvgVS = SumVS / 20;
For DiffArr = 0 To 19 Begin
If DiffArr = 0 Then
DiffVS = 0;
{Then we calculate the standard deviation}
DiffVS = DiffVS + Square(VS[DiffArr] - AvgVS);
If DiffArr = 19 Then
StdVS = SquareRoot(DiffVS / 20);
End;
End;
{Finally, we add the moving average (and the standard deviations) to the latest close for an indication of tomorrowâs trading range}
VSLow = C * (1 + (AvgVS - StdVS * VSStd));
VSMid = C * (1 + AvgVS);
VSHigh = C * (1 + (AvgVS + StdVS * VSStd));
Plot1[-1](VSLow, ãVS Lowä);
Plot2[-1](VSMid, ãVS Midä);
Plot3[-1](VSHigh, ãVS Highä);
October 2000 Trading System Lab:
Variables: RelStr(0), AvgRelStr(0), CalcRelStr(0), AvgOBV(0), CalcOBV(0),
CalcStr(0);
RelStr = IFF(AvgPrice = 0, 0, AvgPrice / AvgPrice Data2);
AvgRelStr = Average(RelStr, 5);
CalcRelStr = IFF(RelStr = 0, 0, RelStr / AvgRelStr);
AvgOBV = Average(OBV, 5);
CalcOBV = IFF(OBV = 0, 0, OBV / AvgOBV);
CalcStr = CalcRelStr * CalcOBV;
If CalcStr Crosses Above 1.0 and MarketPosition = 0 Then
Buy on Close;
If CalcStr Crosses Below 1 Then
ExitLong on Close;
If Close < EntryPrice * 0.96 Then
ExitLong on Close;
November 2000 Trading System Lab:
Condition1 = MarketPosition = 0 and Close Average(Close, 50) and High
High[1] and
(close[1] < close[2] and close[2] < close[3]);
If Condition1 = True Then
Buy tomorrow at Market;
ExitLong ("Stop") tomorrow at Lowest(Low, 2) Stop;
If PositionProfit 0 Then
ExitLong ("Exit") tomorrow at Market;
December 2000 issue:
For "Trading the momentum of market breadth:"
{Data1 = Either the SPY S&P 500 index tracking stock or the S&P 500 commodity futures contract.
Data2 = Advancing issues of the Nyse.}
Inputs: RL(7), BZ(3), SZ(1);
If ROC(Close Data2, RL) > BZ Then Buy;
If ROC(Close Data2, RL) < SZ Then Sell;
For "Doubly adaptive profit targets:"
Vars: TrendIndicator(0), RiskReward(0);
If ADX(14) > ADX(14)[1] Then
TrendIndicator = 4;
If ADX(14) <= ADX(14)[1] Then
TrendIndicator = 2;
Condition1 = MarketPosition = 0 and Close > Average(Close, 50) and Open
Tomorrow > High and TrendIndicator = 4;
If Condition1 Then Begin
Buy Tomorrow at High Limit;
RiskReward = AvgTrueRange(20);
End;
If MarketPosition = 1 Then Begin
ExitLong ("Profit") Tomorrow at (EntryPrice + RiskReward * TrendIndicator)
Limit;
ExitLong ("Loss") Tomorrow at (EntryPrice - RiskReward * TrendIndicator)
Stop;
If Close < Average(Close, 50) Then ExitLong ("Trend") at Market;
End;
Jan./Feb. 2001
From "Better System Performance measures," p. 62.
Vars: TotTr(0), Prof(0), TradeStr2(“”), CumProf(1), ETop(0), TradeStr2(""),CumProf(1), EBot(0), EDraw(0), TradeStr2("");
TotTr = TotalTrades;
If TotTr > TotTr[1] Then Begin
Prof = 1 + PositionProfit(1) / (EntryPrice(1) * BigPointValue);
CumProf = CumProf * Prof;
ETop = MaxList(ETop, CumProf);
EBot = MinList(EBot, CumProf);
EDraw = CumProf / ETop;
TradeStr2 = NumToStr((Prof - 1) * 100, 2) + “,” + NumToStr((CumProf - 1) * 100, 2) + "," + NumToStr((ETop - 1) * 100, 2) + “,” + NumToStr((EBot - 1) * 100, 2) + "," + NumToStr((EDraw - 1) * 100, 2) + NewLine;
{Make sure that you have a temp directory on your hard drive, or re-write the path inside the FileAppend command to an exsisting directory of your choice.}
FileAppend("C:\Temp\Chap1-2.csv", TradeStr2);
End;
For the Trading System Lab page:
Vars: SumVS(0), AvgVS(0), DiffVS(0), StdVS(0), SetArr(0), SumArr(0), DiffArr(0), VSLow(0), VSMid(0), VSHigh(0), RiskReward(0);
Array: VS[20](0);
For SetArr = 0 To 4 Begin
VS[SetArr * 4 + 0] = (Open[SetArr] - AvgPrice[SetArr + 1]) / AvgPrice[SetArr + 1];
VS[SetArr * 4 + 1] = (High[SetArr] - AvgPrice[SetArr + 1]) / AvgPrice[SetArr + 1];
VS[SetArr * 4 + 2] = (Low[SetArr] - AvgPrice[SetArr + 1]) / AvgPrice[SetArr + 1];
VS[SetArr * 4 + 3] = (Close[SetArr] - AvgPrice[SetArr + 1]) / AvgPrice[SetArr + 1];
End;
For SumArr = 0 To 19 Begin
If SumArr = 0 Then
SumVS = 0;
SumVS = SumVS + VS[SumArr];
If SumArr = 19 Then
AvgVS = SumVS / 20;
For DiffArr = 0 To 19 Begin
If DiffArr = 0 Then
DiffVS = 0;
DiffVS = DiffVS + Square(VS[DiffArr] - AvgVS);
If DiffArr = 19 Then
StdVS = SquareRoot(DiffVS / 20);
End;
End;
VSLow = AvgPrice * (1 + (AvgVS - StdVS * 2));
VSMid = AvgPrice * (1 + AvgVS);
VSHigh = AvgPrice * (1 + (AvgVS + StdVS * 2));
If MarketPosition = 0 Then Begin
Buy ("Buy") Tomorrow at VSLow Limit;
RiskReward = VSMid-VSLow;
End;
If MarketPosition = 1 Then
ExitLong ("PT") Tomorrow At VSHigh Limit;
If MarketPosition = 1 Then
ExitLong ("TS") Tomorrow At VSLow Stop;
If Open Tomorrow >= VSLow Then
ExitLong ("SLa") From Entry ("Buy") At (VSLow-(VSMid-VSLow)) Stop;
If Open Tomorrow < VSLow Then
ExitLong ("SLb") From Entry ("Buy") At (Open Tomorrow-(VSMid-VSLow)) Stop;
March 2001
For the Trading System Lab:
Vars: MaLen(21), Ratio2(0), Ratio3(0), MaRatio2(0), MaRatio3(0), DiffMaRatio2(0), DiffMaRatio3(0), ProdDiff(0), UpperProdDiff(0), LowerProdDiff(0);
Ratio2 = C / C Data2;
Ratio3 = C / C Data3;
MaRatio2 = Average(Ratio2, MaLen);
MaRatio3 = Average(Ratio3, MaLen);
DiffMaRatio2 = Ratio2 / MaRatio2;
DiffMaRatio3 = Ratio3 / MaRatio3;
ProdDiff = DiffMaRatio2 * DiffMaRatio3;
UpperProdDiff = 1 + StdDev(ProdDiff, MaLen) * 1;
LowerProdDiff = 1 - StdDev(ProdDiff, MaLen) * 2;
If MarketPosition = 0 and BarsSinceExit(1) > 1 and ProdDiff > UpperProdDiff and ProdDiff > ProdDiff[2] Then
Buy ("Go Long") at Close;
If MarketPosition = 1 and ProdDiff < ProdDiff[4] Then
ExitLong ("End Long") at Market;
ExitLong ("Trailing Long") tomorrow at Lowest(Low, 2) Stop;
If MarketPosition = 0 and BarsSinceExit(1) > 1 and ProdDiff < LowerProdDiff and ProdDiff < ProdDiff[2] Then
Sell ("Go Short") at Close;
If MarketPosition = -1 and ProdDiff > ProdDiff[4] Then
ExitShort ("End Short") at Market;
ExitShort ("Trailing Short") tomorrow at Highest(High, 2) Stop;
If BarsSinceEntry = 8 and OpenPositionProfit < 0 Then Begin
ExitLong at Market;
ExitShort at Market;
End;
April 2001
"Trading the VIX," by Price Headley: This is the system code (written in EasyLanguage for TradeStation), where Data1 is the OEX and Data2 is the VIX index.
{Code by Price Headley, BigTrends.com}
If Close of Data1 > Average(Close of Data1, 10) and Close of Data2[5] < BollingerBand(Close of Data2, 21, 2)[5] and Close of Data2[6] > BollingerBand(Close of Data2, 21, 2)[6] Then
Buy on Close of Data1;
If BarsSinceEntry = 5 and Close of Data1 < Close of Data1[5] Then
ExitLong on Close of Data1;
If Close of Data1 < Average(Close of Data1, 10) and Close of Data2[10] > BollingerBand(Close of Data2, 21, -2)[10] and Close of Data2[11] < BollingerBand(Close of Data2, 21, -2)[11] Then
Sell on Close of Data1;
If BarsSinceEntry = 5 and Close of Data1 > Close of Data1[5] Then
ExitShort on Close of Data1;
May 2001
Avoiding system testing traps
Inputs: Len(63);
Vars: PointRange(0), SumPosRange(0), SumNegRange(0), AvgPosRange(0), AvgNegRange(0);
PointRange = High - Low;
If Close > Close[1] Then
SumPosRange = SumPosRange * (Len - 1) / Len + PointRange;
If Close < Close[1] Then
SumNegRange = SumNegRange * (Len - 1) / Len + PointRange;
AvgPosRange = SumPosRange / Len;
AvgNegRange = SumNegRange / Len;
{Plot statements to use in indicator only}
Plot1(AvgPosRange, "");
Plot2(AvgNegRange, "");
{Buy/sell statements to use in system only}
{This identifies the up trend}
If PosRange > NegRange and MarketPosition = 0 Then
Buy on O;
If PosRange < NegRange Then
ExitLong ("Down Trend") on Open;
{This identifies the down trend}
If NegRange > PosRange * 1.06 and NegRange >= NegRange[1] and PosRange < PosRange[1] and MarketPosition = 0 Then
Sell on O;
If NegRange < PosRange * 1.06 Then
ExitShort ("Up Trend") on O;
{This is the short-term trading rules within the up trend}
If BarsSinceEntry >=1 Then Begin
ExitLong ("Loss Long") tomorrow at EntryPrice - NegRange stop;
ExitLong ("Trail Long") tomorrow at Open tomorrow - NegRange stop;
ExitLong ("Profit Long 2") tomorrow at Open tomorrow + 3*PosRange limit;
ExitLong ("Profit Long 1") tomorrow at EntryPrice + 6*PosRange limit;
End;
{This is the short-term trading rules within the down trend}
If BarsSinceEntry >=1 Then Begin
ExitShort ("Loss Short") tomorrow at EntryPrice + PosRange stop;
ExitShort ("Trail Short") tomorrow at Open tomorrow + PosRange stop;
ExitShort ("Profit Short 2") tomorrow at Open tomorrow - 2*NegRange limit;
ExitShort ("Profit Short 1") tomorrow at EntryPrice - 4*NegRange limit;
End;
Spreadsheet for "Building a better trend indicator."
June 2001
For the Trading System Lab:
Input: VSStd(1);
Vars: SumVS(0), AvgVS(0), DiffVS(0), StdVS(0), SetArr(0), SumArr(0), DiffArr(0), VSLow(0), VSMid(0), VSHigh(0), RiskReward(0);
Array: VS[20](0);
For SetArr = 0 To 4 Begin
VS[SetArr * 4 + 0] = (O[SetArr] - C[SetArr + 1]) / C[SetArr + 1];
VS[SetArr * 4 + 1] = (H[SetArr] - C[SetArr + 1]) / C[SetArr + 1];
VS[SetArr * 4 + 2] = (L[SetArr] - C[SetArr + 1]) / C[SetArr + 1];
VS[SetArr * 4 + 3] = (C[SetArr] - C[SetArr + 1]) / C[SetArr + 1];
End;
For SumArr = 0 To 19 Begin
If SumArr = 0 Then
SumVS = 0;
SumVS = SumVS + VS[SumArr];
If SumArr = 19 Then
AvgVS = SumVS / 20;
For DiffArr = 0 To 19 Begin
If DiffArr = 0 Then
DiffVS = 0;
DiffVS = DiffVS + Square(VS[DiffArr] - AvgVS);
If DiffArr = 19 Then
StdVS = SquareRoot(DiffVS / 20);
End;
End;
VSLow = C * (1 + (AvgVS - StdVS * VSStd));
VSMid = C * (1 + AvgVS);
VSHigh = C * (1 + (AvgVS + StdVS * VSStd));
If MarketPosition = 0 and BarsSinceExit(1) > 1 Then Begin
If Average(Close, 80) > Average(Close, 80)[11] Then
Buy ("Buy") tomorrow at VSLow limit;
If Average(Close, 80) < Average(Close, 80)[11] Then
Sell tomorrow at VSHigh limit;
End;
If BarsSinceEntry >= 1 Then Begin
ExitLong on Close;
ExitShort on Close;
End;
From "Measuring Trend Momentum:"
{Tushar Chande 2001: VIDYA/CDMA}
Input: Len(10);
Vars: Diff(0), MyConst(0), MyAdx(0), Varma(0), EmaIndex(0);
{… Index of EMA …}
If Len > 0 then EmaIndex = (2 / (1 + Len)) else EmaIndex = 0.20;
{… Stochastic oscillator using ADX …}
MyAdx = ADX(20);
Diff = Highest(MyAdx, 20) - Lowest(MyAdx, 20);
If Diff > 0 then MyConst = (MyAdx - Lowest(MyAdx, 20))/Diff else MyConst = EmaIndex;
{… Clamp length to that implied by input value of Len …}
If MyConst > EmaIndex then MyConst = EmaIndex;
{… Create the variable MA …}
If CurrentBar < 50 then Varma = Close else Varma = (1 - MyConst) * Varma[1] + MyConst * Close;
Plot1(Varma, “VarMA”) ;
Plot2(XAverage(Close, Len), ”XAvg”
July 2001
Trading System Lab: Volume-weighted average
Vars: MaLen(9), AvgVolume(0), Turbo(0), InvTurbo(0), MaWeight(0), TurboMA(0);
AvgVolume = Average(V, MaLen);
Turbo = (AvgVolume - Lowest(AvgVolume, MaLen)) / (Highest(AvgVolume, MaLen) - Lowest(AvgVolume, Malen));
InvTurbo = 1 - Turbo;
If MaLen > 2 Then MaWeight = (2 / (1 + MaLen)) Else MaWeight = 0.67;
TurboMA = TurboMA * InvTurbo + AvgPrice * Turbo;
If Date < 1000401 Then Begin
If MarketPosition = 0 and C < TurboMA and TurboMA < TurboMA [1] Then
Buy Tomorrow on Highest(High, 2) Stop;
End;
If MarketPosition = 1 and C < TurboMA Then Begin
ExitLong on Close;
ExitLong Tomorrow on EntryPrice * 0.96 Stop;
End;
If Date >= 1000401 Then Begin
If MarketPosition = 0 and C > TurboMA and TurboMA > TurboMA [1] Then
Sell Tomorrow on Lowest(Low, 2) Stop;
End;
If MarketPosition = -1 and C > TurboMA Then Begin
ExitShort on Close;
ExitShort Tomorrow on EntryPrice * 1.04 Stop;
End;
August 2001
Trading System Lab:
If (Maxlist(High[3], Close[4]) > High[4] Or
Maxlist(High[3], Close[4]) > High[2] Or
Maxlist(High[3], Close[4]) > High[1]) and
Close < Close[1] and
Open Tomorrow <= High[3] Then
Buy 1 Contract Tomorrow on (Maxlist(High[3], Close[4]) * 1.001) Stop;
If (Minlist(Low[3], Close[4]) < Low[4] Or
Minlist(Low[3], Close[4]) < Low[2] Or
Minlist(Low[3], Close[4]) < Low[1]) and
Close > Close[1] and
Open Tomorrow >= Low[3] Then
Sell 1 Contract Tomorrow on (Minlist(Low[3], Close[4]) * 0.999) Stop;
If EntryPrice > 0 Then Begin
If MarketPosition = 1 Then Begin
ExitLong on EntryPrice * 0.96 Stop;
ExitLong on EntryPrice * 1.12 Limit;
End;
If MarketPosition = -1 Then Begin
ExitShort on EntryPrice * 1.04 Stop;
ExitShort on EntryPrice * 0.88 Limit;
End;
End;
If BarsSinceEntry >= 3 Then SetExitOnClose;
ExitLong on Close;
ExitShort on Close;
End;
September 2001
ADX spreadsheet from Indicator Insight
Beta values spreadsheet (Note: If prompted to update links within the spreadsheet when Excel opens, click "No.")
Programming code for 3L-R and GAP-2H patterns (from "Keeping it simple," by Michael Harris).
{*****************************************************
Description : 3L-R Pattern Entry Signal
Developed By : Michael Harris, Tradingpatterns.com
*****************************************************}
if l[1] < l[2] and l[2] < l[3] and h[0] > h[3] then
buy ("3L-R") next bar on the open;
{*****************************************************
Description : 3L-R Pattern Long Exit Signal
Developed By : Michael Harris, Tradingpatterns.com
Comments: ptarget and stopl are in percentage terms
*****************************************************}
variables: profitprice(0), stopprice(0);
input: ptarget(8), stopl(8);
profitprice = entryprice*(1+ptarget/100);
stopprice = entryprice*(1-stopl/100);
if marketposition =1 then begin
exitlong ("3L-R Exit") at profitprice limit;
exitlong ("3L-R Stop") at stopprice stop;
end;
**
{***********************************************
Description : GAP-2H Pattern Entry Signal
Developed By : Michael Harris, Tradingpatterns.com
***********************************************}
if h[0] > h[1] and h[2] > l[2] and l[0] > l[1] and l[1] > h[2] and
h[1] > l[0] then
buy ("GPA-2H") next bar on the open;
{***********************************************
Description : GAP-2H Pattern Long Exit Signal
Developed By : Michael Harris, Tradingpatterns.com
Comments: ptarget and stopl are in percentage terms
***********************************************}
variables: profitprice(0), stopprice(0);
input: ptarget(7), stopl(7);
profitprice = entryprice*(1+ptarget/100);
stopprice = entryprice*(1-stopl/100);
if marketposition =1 then begin
exitlong ("GAP-2H Exit") at profitprice limit;
exitlong ("GAP-2H Stop") at stopprice stop;
end;
**
January 2002
Formula for the True Strength Index (TSI):
TSI(close,r,s) = 100*EMA(EMA(mtm,r),s)/EMA(EMA(/mtm/,r),s)
where mtm = closetoday - closeyesterday
EMA(mtm,r) = exponential moving average of mtm with alpha = r
EMA(EMA(mtm,r),s) = exponential moving average of EMA(mtm,r) with alpha = s
/mtm/ = absolute value of mtm
r = 25, s = 13
**
For MetaStock, use the Indicator Builder:
100*(Mov(Mov((CLOSE - Ref(CLOSE,-1)),25,E),13,E))/(Mov(Mov(Abs(CLOSE - Ref(CLOSE,-1)),25,E),13,E))
For CQGNet, you first create the momentum (MTM) value, then create the TSI indicator in the custom studies module.
Under the User Values tab create a new User Value "MTM" (Figure 2):
Close(@) - Close (@)[-1]
Next, under the Custom Studies tab create the new Custom Study TSI (Figure 3):
100*( MA(MA( V.mtm(@),Exp,25.000),Exp,13.000) )/( MA(MA( Abs( V.mtm(@)),Exp,25.000),Exp,13.000) )