耳朵好癢,有人叫我。
我不會轉成AB。我會看圖說故事。
下面註解的中文字是我寫的。
- {
- {這種符號是註解}
- Livermore Market Method Signal
- Ref[1]: Chapters VIII & IX of "How to Trade in Stocks", Jesse L. Livermore
- v1 ghkramer - 5Oct03
- v1.1 ghkramer - 11Oct03, Added switch to deactive Rule 10 logic
- ===============
- Program Overview
- This program classifies price action into the following states based upon
- rules in Ref[1]:
- - Up Trend
- - Natural Rally
- - Secondary Rally
- - Down Trend
- - Natural Reaction
- - Secondary Reaction
- State change is determined by a user specified threshold of price change.
- The program also determines a number of pivot points:
- - Peak Up Trend Price
- - Peak Natural Rally Price
- - Bottom Down Trend Price
- - Bottom Natural Reaction Price
- - Key Price (requires two stocks, i.e., 2 or more data streams (Rule 7), not yet implemented)
- This program may be used as a basis for a number of studies:
- - trend paint bars,
- - pivot price indicator lines
- - strategies (trend following and/or breakout/breakdown)
- For a detailed explanation of the system see Ref[1].
- This program reflects the author's interpretation of Livermore's writing.
- There is no guarantee the this program accurately or fully incorporates
- Livermore's Market Key system.
- }
- input: PtsPctATR(0), Threshold(6), ATRLength(14), TradeTrends(1);
- {輸入變成有4個,要最佳化時可以用這個變數來跑。}
- { PtsPctATR: 0 for Threshold in points,
- 1 for Threshold in Percent
- 2 for Threshold in multiples of ATR
- Threshold: in Points (if PtsPct = 0)
- in Percent (if PtsPct = 1)
- in ATR Multiples (if PtsPct = 2)
- ATRLength(14) : only used when PtsPctATR = 2
- TradeTrends: 1 = Trade Up and Down Trends only
- 0 = Long for Up Trends and Rallies, Short for Dn Trends and Reactions
- Note: Livermore's system used a threshold of 6 points for stocks priced over $30.
- This is the default (PtsPctATR = 0, Threshold = 6).
- }
- var:
- {內部變數}
- SecondaryRally(0), NaturalRally(0), UpTrend(0),
- SecondaryReaction(0), NaturalReaction(0), DnTrend(0),
- DnTrendBL(0), NaturalRallyBL(0), {BL = Black Line}
- UpTrendRL(0), NaturalReactionRL(0), {RL = Red Line}
- InSecRally(false), InNatRally(false), InUpTrend(false),
- InSecReact(false), InNatReact(false), InDnTrend(false),
- ResumeUpTrend(false), ResumeDnTrend(false),
- MA10(0), Thresh(0), HalfThresh(0),
- UseRule10(false), {set to true to use Livermore's Rule 10, note: system may become unstable}
- Debug(false); {set to true for output}
- {initialization}
- {初始化部份}
- if (CurrentBar = 1) then
- begin
- if (PtsPctATR = 0) {use Points} then
- begin
- Thresh = HalfThresh ;
- HalfThresh = Thresh/2;
- {HalfThresh的數值等於HalfThresh除以2}
- end;
- SecondaryRally = Close;
- NaturalRally = Close;
- UpTrend = Close;
- SecondaryReaction = Close;
- NaturalReaction = Close;
- DnTrend = Close;
- {然後這一串變數,都初始化為今天的收盤價}
- end;
- if (CurrentBar <= 21) then {initialization continued}
- begin
- MA10 = Average(Close, 10);
- {MA10的數值為今天的10日收盤平均價}
- if (CurrentBar = 21) then
- begin
- if (MA10 > MA10[10]) then {assume UpTrend}
- {奇怪了..MA10和MA10[10] 有何不同?看不懂}
- begin
- InUpTrend = true;
- {這種情況下,我們設InUpTrend為"真",即突破的上升趨勢}
- UpTrend = C;
- end
- else {assume DnTrend}
- begin
- InDnTrend = true;
- DnTrend = C;
- end;
- end;
- end
- else {Main}
- begin {calc current Threshold if required}
- if (PtsPctATR = 1) then {use Percent change Thresh, calc on every bar }
- begin
- Thresh = Threshold*Close[1]/100;
- HalfThresh = Thresh/2;
- end
- else if (PtsPctATR = 2) then {use ATR multiples for Thresh, calc on every bar }
- begin
- Thresh = Threshold*AvgTrueRange(14);
- {Thresh為Threshold乘上14天的ATR}
- HalfThresh = Thresh/2;
- end;
- {Process by Current State}
- {------Up Trend State-------}
- if InUpTrend then
- begin
- if (Close > (NaturalReaction + Threshold)) then
- NaturalReactionRL = NaturalReaction; {Rule 4b}
- if ResumeUpTrend then {Rule 10 logic. Note: system becomes unstable if used}
- begin
- if (Close > (UpTrendRL + HalfThresh)) then
- begin
- ResumeUpTrend = false; {Rule 10a}
- UpTrend = Close;
- if Debug then print(date, Close, " Node 1", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end
- else if (Close < (UpTrendRL - HalfThresh)) then {UpTrend Over, return to NaturalReaction}
- begin
- ResumeUpTrend = false;
- InUpTrend = false; {Rule 10b}
- InNatReact = true;
- NaturalReaction = Close;
- if Debug then print(date, Close, " Node 2", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end;
- end
- else if (Close < (UpTrend - Thresh)) then {start NaturalReaction}
- begin {Rules 4a, 6a}
- InUpTrend = false;
- InNatReact = true;
- NaturalReaction = Close;
- UpTrendRL = UpTrend; {Pivot Pt, Rule 8}
- ResumeUpTrend = false;
- if Debug then print(date, Close, " Node 3", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end
- else if (Close > UpTrend) then {remain in UpTrend, record higher price}
- UpTrend = Close; {Rule 1, 4b, 6d}
- end {InUpTrend}
- {------Natural Rally State-------}
- else if InNatRally then
- begin
- if (Close > (NaturalReaction + Threshold)) then
- NaturalReactionRL = NaturalReaction; {Rule 4b}
- if (Close > UpTrend) then {resume UpTrend}
- begin {Rules 6d, 6f}
- InUpTrend = true;
- InNatRally = false;
- UpTrend = Close;
- if UseRule10 then ResumeUpTrend = true;
- if Debug then print(date, Close, " Node 4", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end
- else if (Close > (NaturalRallyBL + HalfThresh)) then {resume UpTrend}
- begin {Rules 5a}
- InUpTrend = true;
- InNatRally = false;
- UpTrend = Close;
- if UseRule10 then ResumeUpTrend = true;
- if Debug then print(date, Close, " Node 5", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end
- else if (Close < DnTrend) then {start DnTrend}
- begin {Rule 6b}
- InNatRally = false;
- InDnTrend = true;
- DnTrend = Close;
- NaturalRallyBL = Close; {rule 4d}
- if Debug then print(date, Close, " Node 6", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end
- else if (Close < (NaturalRally - Thresh)) then
- begin
- if (Close < NaturalReaction) then {start NaturalReaction}
- begin {Rules 4d, 6b}
- InNatRally = false;
- InNatReact = true;
- NaturalReaction = Close;
- NaturalRallyBL = Close; {rule 4d} {Pivot Pt, Rule 9b}
- if Debug then print(date, Close, " Node 7", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end
- else {start SecondaryReaction}
- begin {Rule 6h}
- InNatRally = false;
- InSecReact = true;
- SecondaryReaction = Close;
- if Debug then print(date, Close, " Node 8", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end;
- end
- else if (Close > NaturalRally) then {remain in NaturalRally, record higher price}
- NaturalRally = Close; {Rule 3, 6c, 6d}
- end {InNatRally}
- {------Secondary Rally State-------}
- else if InSecRally then
- begin
- if (Close > UpTrend) then {resume UpTrend}
- begin {Rules 6d, 6f}
- InUpTrend = true;
- InSecRally = false;
- UpTrend = Close;
- if UseRule10 then ResumeUpTrend = true;
- if Debug then print(date, Close, " Node 9", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end
- else if (Close > (NaturalRallyBL + HalfThresh)) then {resume UpTrend}
- begin {Rules 5a}
- InUpTrend = true;
- InSecRally = false;
- UpTrend = Close;
- if UseRule10 then ResumeUpTrend = true;
- if Debug then print(date, Close, " Node 10", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end
- else if (Close > NaturalRally) then {start NaturalRally}
- begin {Rule 6g}
- InSecReact = false;
- InNatRally = true;
- NaturalRally = Close;
- if Debug then print(date, Close, " Node 11", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end
- else if (Close < DnTrend) then {start DnTrend}
- begin {Rule 6b}
- InSecRally = false;
- InDnTrend = true;
- DnTrend = Close;
- NaturalRallyBL = Close; {rule 4d} {Pivot Pt, Rule 9b}
- if Debug then print(date, Close, " Node 12", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end
- else if (Close > SecondaryRally) then {remain in SecondaryRally, record higher price}
- SecondaryRally = Close; {Rule 3, 6g}
- end {InSecRally}
- {------Down Trend State-------}
- else if InDnTrend then
- begin
- if (Close < (NaturalRally - Threshold)) then
- NaturalRallyBL = NaturalRally; {Rule 4d}
- if ResumeDnTrend then {Rule 10 logic. Note: system becomes unstable if used}
- begin
- if (Close < (DnTrendBL - HalfThresh)) then
- begin
- ResumeDnTrend = false; {Rule 10a}
- DnTrend = Close; {Rule 2, 6b}
- if Debug then print(date, Close, " Node 13", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end
- else if (Close > (DnTrendBL + HalfThresh)) then {DnTrend Over, return to NaturalRally}
- begin
- ResumeDnTrend = false;
- InDnTrend = false; {Rule 10c}
- InNatRally = true;
- NaturalRally = Close;
- if Debug then print(date, Close, " Node 14", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end;
- end
- else if (Close > (DnTrend + Thresh)) then {start NaturalRally}
- begin {Rules 4c, 6c}
- InDnTrend = false;
- InNatRally = true;
- NaturalRally = Close;
- DnTrendBL = DnTrend; {Pivot Pt, Rule 8}
- ResumeDnTrend = false;
- if Debug then print(date, Close, " Node 15", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end
- else if (Close < DnTrend) then {remain in DnTrend, record lower price}
- DnTrend = Close; {Rule 2, 6b}
- end {InSecRally}
- {------Natural Reaction State-------}
- else if InNatReact then
- begin
- if (Close < (NaturalRally - Threshold)) then
- NaturalRallyBL = NaturalRally; {Rule 4d}
- if (Close < DnTrend) then {resume DnTrend}
- begin {Rule 6b, 6e}
- InDnTrend = true;
- InNatReact = false;
- DnTrend = Close;
- if UseRule10 then ResumeDnTrend = true;
- if Debug then print(date, Close, " Node 16", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end
- else if (Close < (NaturalReactionRL - HalfThresh)) then {resume DnTrend}
- begin {Rules 5b}
- InDnTrend = true;
- InNatReact = false;
- DnTrend = Close;
- if UseRule10 then ResumeDnTrend = true;
- if Debug then print(date, Close, " Node 17", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end
- else if (Close > UpTrend) then {start UpTrend}
- begin {rule 6d}
- InNatReact = false;
- InUpTrend = true;
- UpTrend = Close;
- NaturalReactionRL = Close; {Rule 4b} {Pivot Pt, Rule 9c}
- if Debug then print(date, Close, " Node 18", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end
- else if (Close > (NaturalReaction + Thresh)) then
- begin
- if (Close > NaturalRally) then {start NaturalRally}
- begin {Rules 4b, 6d}
- InNatReact = false;
- InNatRally = true;
- NaturalRally = Close;
- NaturalReactionRL = Close; {Rule 4b} {Pivot Pt, Rule 9c}
- if Debug then print(date, Close, " Node 19", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end
- else {start SecondaryRally}
- begin {Rule 6g}
- InNatReact = false;
- InSecRally = true;
- SecondaryRally = Close;
- if Debug then print(date, Close, " Node 20", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end;
- end
- else if (Close < NaturalReaction) then {remain in NaturalReaction, record lower price}
- NaturalReaction = Close; {Rules 3, 6a, 6b}
- end {InNatReact}
- {------Secondary Reaction State-------}
- else if InSecReact then
- begin
- if (Close < DnTrend) then {resume DnTrend}
- begin {Rules 6b, 6e}
- InDnTrend = true;
- InSecReact = false;
- DnTrend = Close;
- if UseRule10 then ResumeDnTrend = true;
- if Debug then print(date, Close, " Node 21", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end
- else if (Close < (NaturalReactionRL - HalfThresh)) then {resume DnTrend}
- begin {Rules 5b}
- InDnTrend = true;
- InSecReact = false;
- DnTrend = Close;
- if UseRule10 then ResumeDnTrend = true;
- if Debug then print(date, Close, " Node 22", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end
- else if (Close > UpTrend) then {start UpTrend}
- begin {Rule 6d}
- InSecReact = false;
- InUpTrend = true;
- UpTrend = Close;
- NaturalReactionRL = Close; {Rule 4b} {Pivot Pt, Rule 9c}
- if Debug then print(date, Close, " Node 23", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end
- else if (Close < NaturalReaction) then {start NaturalReaction}
- begin {Rule 6h}
- InSecReact = false;
- InNatReact = true;
- NaturalReaction = Close;
- if Debug then print(date, Close, " Node 24", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
- end
- else if (Close < SecondaryReaction) then {remain in SecondaryReaction, record lower price}
- SecondaryReaction = Close; {Rule 6h}
- end {InSecReact}
- {------Error State-------}
- else
- begin
- {should not get here!!!}
- if Debug then
- print("Error in State Logic, Date: ", Date, " Time: ", Time);
- end; {Error state}
- end; {main}
- {------Trading Logic-------}
- {For TS 2000i only, non-op or delete for TS 6+ }
- {
- if (TradeTrends = 1) then
- begin
- if InUpTrend then
- Buy next bar at open
- else if (Marketposition = 1) then
- ExitLong next bar at open;
- if InDnTrend then
- Sell next bar at open
- else if (Marketposition = -1) then
- ExitShort next bar at open;
- end
- else {Trade Trends, Rallies, and Reactions}
- begin
- if (InUpTrend or InNatRally or InSecRally) then
- begin
- Buy next bar at open;
- ExitShort next bar at open;
- end;
- if (InDnTrend or InNatReact or InSecReact) then
- begin
- Sell next bar at open;
- ExitLong next bar at open;
- end;
- end;
- }
- { For TS 6+ versions}
- if (TradeTrends = 1) then
- begin
- if InUpTrend then
- Buy next bar at open
- else if (Marketposition = 1) then
- Sell next bar at open;
- if InDnTrend then
- Sell Short next bar at open
- else if (Marketposition = -1) then
- Buy to Cover next bar at open;
- end
- else
- begin
- if (InUpTrend or InNatRally or InSecRally) then
- begin
- Buy next bar at open;
- Buy to Cover next bar at open;
- end;
- if (InDnTrend or InNatReact or InSecReact) then
- begin
- Sell Short next bar at open;
- Sell next bar at open;
- end;
- end;
複製代碼
........
會寫的人加油...不會寫...
|