COCO研究院

 找回密碼
 註冊
搜索
123
返回列表 發新帖
樓主: 小娃

[教學] 如何用AmiBroker做績效回測(作者 michael-knight)

[複製鏈接]
發表於 09-12-15 19:38 | 顯示全部樓層
哇靠,寫蝦米,看攏某。
發表於 09-12-15 20:21 | 顯示全部樓層
希望能有更多AB達人,
才能讓高手不寂寞。
michael大真是太神了。
發表於 09-12-25 18:14 | 顯示全部樓層
網上有沒有利佛摩操盤手法的ab程式?
發表於 09-12-26 20:07 | 顯示全部樓層
李佛摩爾用突破法,
請問有詳細的公式嗎?
這樣子才能轉成AB程式。
發表於 10-1-5 21:45 | 顯示全部樓層
回復 36# 綠茶妹


    Livermore-strategy-TS-code.rar (25.95 KB, 下載次數: 529)
從海洋 Multichart ZONE copy paste a doc file .

只有TS CODE 不知這是不是LIVERMORE 精神 還沒詳細讀CODE.

我不太熟AB CODING 就靠你們了 .
    這些日子 AB C# RE OQ MC 在一個月內 SURVEY 完 眼睛都快 更老花了
    AB manual 是K 不下了 晚個幾個月再回頭 K
    以前 對 MC 觀念是 BUG 一堆 ( SOMEBODY POST in forum somewhere )
    不知現在是否依然故我 ??
發表於 10-1-5 21:54 | 顯示全部樓層
本帖最後由 綠茶妹 於 10-1-5 09:55 PM 編輯

謝謝分享!
ab的範例我覺得不夠多,
尤其是交易策略的部份。
我現在看到的afl程式都只是單一的程式,有點像畫圖功能。
但是自動回測的範例好少。
發表於 10-1-5 23:06 | 顯示全部樓層
這個file黃金鼠大大懂不懂轉成ab?
發表於 10-1-6 12:53 | 顯示全部樓層
耳朵好癢,有人叫我。
我不會轉成AB。我會看圖說故事。
下面註解的中文字是我寫的。


  1. {
  2. {這種符號是註解}
  3. Livermore Market Method Signal
  4. Ref[1]: Chapters VIII & IX of "How to Trade in Stocks", Jesse L. Livermore

  5. v1 ghkramer - 5Oct03
  6. v1.1 ghkramer - 11Oct03, Added switch to deactive Rule 10 logic
  7. ===============
  8. Program Overview

  9. This program classifies price action into the following states based upon
  10. rules in Ref[1]:

  11. - Up Trend
  12. - Natural Rally
  13. - Secondary Rally
  14. - Down Trend
  15. - Natural Reaction
  16. - Secondary Reaction

  17. State change is determined by a user specified threshold of price change.
  18. The program also determines a number of pivot points:

  19. - Peak Up Trend Price
  20. - Peak Natural Rally Price
  21. - Bottom Down Trend Price
  22. - Bottom Natural Reaction Price
  23. - Key Price (requires two stocks, i.e., 2 or more data streams (Rule 7), not yet implemented)

  24. This program may be used as a basis for a number of studies:
  25. - trend paint bars,
  26. - pivot price indicator lines
  27. - strategies (trend following and/or breakout/breakdown)

  28. For a detailed explanation of the system see Ref[1].



  29. This program reflects the author's interpretation of Livermore's writing.
  30. There is no guarantee the this program accurately or fully incorporates
  31. Livermore's Market Key system.
  32. }


  33. input: PtsPctATR(0), Threshold(6), ATRLength(14), TradeTrends(1);
  34. {輸入變成有4個,要最佳化時可以用這個變數來跑。}
  35. { PtsPctATR: 0 for Threshold in points,
  36. 1 for Threshold in Percent
  37. 2 for Threshold in multiples of ATR

  38. Threshold: in Points (if PtsPct = 0)
  39. in Percent (if PtsPct = 1)
  40. in ATR Multiples (if PtsPct = 2)

  41. ATRLength(14) : only used when PtsPctATR = 2

  42. TradeTrends: 1 = Trade Up and Down Trends only
  43. 0 = Long for Up Trends and Rallies, Short for Dn Trends and Reactions

  44. Note: Livermore's system used a threshold of 6 points for stocks priced over $30.
  45. This is the default (PtsPctATR = 0, Threshold = 6).

  46. }

  47. var:
  48. {內部變數}
  49. SecondaryRally(0), NaturalRally(0), UpTrend(0),
  50. SecondaryReaction(0), NaturalReaction(0), DnTrend(0),
  51. DnTrendBL(0), NaturalRallyBL(0), {BL = Black Line}
  52. UpTrendRL(0), NaturalReactionRL(0), {RL = Red Line}
  53. InSecRally(false), InNatRally(false), InUpTrend(false),
  54. InSecReact(false), InNatReact(false), InDnTrend(false),
  55. ResumeUpTrend(false), ResumeDnTrend(false),
  56. MA10(0), Thresh(0), HalfThresh(0),
  57. UseRule10(false), {set to true to use Livermore's Rule 10, note: system may become unstable}
  58. Debug(false); {set to true for output}



  59. {initialization}
  60. {初始化部份}
  61. if (CurrentBar = 1) then
  62. begin
  63. if (PtsPctATR = 0) {use Points} then
  64. begin
  65. Thresh = HalfThresh ;
  66. HalfThresh = Thresh/2;
  67. {HalfThresh的數值等於HalfThresh除以2}
  68. end;
  69. SecondaryRally = Close;
  70. NaturalRally = Close;
  71. UpTrend = Close;
  72. SecondaryReaction = Close;
  73. NaturalReaction = Close;
  74. DnTrend = Close;
  75. {然後這一串變數,都初始化為今天的收盤價}
  76. end;


  77. if (CurrentBar <= 21) then {initialization continued}
  78. begin
  79. MA10 = Average(Close, 10);
  80. {MA10的數值為今天的10日收盤平均價}
  81. if (CurrentBar = 21) then
  82. begin
  83. if (MA10 > MA10[10]) then {assume UpTrend}
  84. {奇怪了..MA10和MA10[10] 有何不同?看不懂}
  85. begin
  86. InUpTrend = true;
  87. {這種情況下,我們設InUpTrend為"真",即突破的上升趨勢}
  88. UpTrend = C;
  89. end
  90. else {assume DnTrend}
  91. begin
  92. InDnTrend = true;
  93. DnTrend = C;
  94. end;
  95. end;
  96. end
  97. else {Main}
  98. begin {calc current Threshold if required}
  99. if (PtsPctATR = 1) then {use Percent change Thresh, calc on every bar }
  100. begin
  101. Thresh = Threshold*Close[1]/100;
  102. HalfThresh = Thresh/2;
  103. end
  104. else if (PtsPctATR = 2) then {use ATR multiples for Thresh, calc on every bar }
  105. begin
  106. Thresh = Threshold*AvgTrueRange(14);
  107. {Thresh為Threshold乘上14天的ATR}
  108. HalfThresh = Thresh/2;
  109. end;


  110. {Process by Current State}


  111. {------Up Trend State-------}
  112. if InUpTrend then
  113. begin
  114. if (Close > (NaturalReaction + Threshold)) then
  115. NaturalReactionRL = NaturalReaction; {Rule 4b}
  116. if ResumeUpTrend then {Rule 10 logic. Note: system becomes unstable if used}
  117. begin
  118. if (Close > (UpTrendRL + HalfThresh)) then
  119. begin
  120. ResumeUpTrend = false; {Rule 10a}
  121. UpTrend = Close;
  122. if Debug then print(date, Close, " Node 1", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  123. end
  124. else if (Close < (UpTrendRL - HalfThresh)) then {UpTrend Over, return to NaturalReaction}
  125. begin
  126. ResumeUpTrend = false;
  127. InUpTrend = false; {Rule 10b}
  128. InNatReact = true;
  129. NaturalReaction = Close;
  130. if Debug then print(date, Close, " Node 2", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  131. end;
  132. end
  133. else if (Close < (UpTrend - Thresh)) then {start NaturalReaction}
  134. begin {Rules 4a, 6a}
  135. InUpTrend = false;
  136. InNatReact = true;
  137. NaturalReaction = Close;
  138. UpTrendRL = UpTrend; {Pivot Pt, Rule 8}
  139. ResumeUpTrend = false;
  140. if Debug then print(date, Close, " Node 3", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  141. end
  142. else if (Close > UpTrend) then {remain in UpTrend, record higher price}
  143. UpTrend = Close; {Rule 1, 4b, 6d}
  144. end {InUpTrend}


  145. {------Natural Rally State-------}
  146. else if InNatRally then
  147. begin
  148. if (Close > (NaturalReaction + Threshold)) then
  149. NaturalReactionRL = NaturalReaction; {Rule 4b}
  150. if (Close > UpTrend) then {resume UpTrend}
  151. begin {Rules 6d, 6f}
  152. InUpTrend = true;
  153. InNatRally = false;
  154. UpTrend = Close;
  155. if UseRule10 then ResumeUpTrend = true;
  156. if Debug then print(date, Close, " Node 4", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  157. end
  158. else if (Close > (NaturalRallyBL + HalfThresh)) then {resume UpTrend}
  159. begin {Rules 5a}
  160. InUpTrend = true;
  161. InNatRally = false;
  162. UpTrend = Close;
  163. if UseRule10 then ResumeUpTrend = true;
  164. if Debug then print(date, Close, " Node 5", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  165. end
  166. else if (Close < DnTrend) then {start DnTrend}
  167. begin {Rule 6b}
  168. InNatRally = false;
  169. InDnTrend = true;
  170. DnTrend = Close;
  171. NaturalRallyBL = Close; {rule 4d}
  172. if Debug then print(date, Close, " Node 6", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  173. end
  174. else if (Close < (NaturalRally - Thresh)) then
  175. begin
  176. if (Close < NaturalReaction) then {start NaturalReaction}
  177. begin {Rules 4d, 6b}
  178. InNatRally = false;
  179. InNatReact = true;
  180. NaturalReaction = Close;
  181. NaturalRallyBL = Close; {rule 4d} {Pivot Pt, Rule 9b}
  182. if Debug then print(date, Close, " Node 7", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  183. end
  184. else {start SecondaryReaction}
  185. begin {Rule 6h}
  186. InNatRally = false;
  187. InSecReact = true;
  188. SecondaryReaction = Close;
  189. if Debug then print(date, Close, " Node 8", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  190. end;
  191. end
  192. else if (Close > NaturalRally) then {remain in NaturalRally, record higher price}
  193. NaturalRally = Close; {Rule 3, 6c, 6d}
  194. end {InNatRally}


  195. {------Secondary Rally State-------}
  196. else if InSecRally then
  197. begin
  198. if (Close > UpTrend) then {resume UpTrend}
  199. begin {Rules 6d, 6f}
  200. InUpTrend = true;
  201. InSecRally = false;
  202. UpTrend = Close;
  203. if UseRule10 then ResumeUpTrend = true;
  204. if Debug then print(date, Close, " Node 9", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  205. end
  206. else if (Close > (NaturalRallyBL + HalfThresh)) then {resume UpTrend}
  207. begin {Rules 5a}
  208. InUpTrend = true;
  209. InSecRally = false;
  210. UpTrend = Close;
  211. if UseRule10 then ResumeUpTrend = true;
  212. if Debug then print(date, Close, " Node 10", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  213. end
  214. else if (Close > NaturalRally) then {start NaturalRally}
  215. begin {Rule 6g}
  216. InSecReact = false;
  217. InNatRally = true;
  218. NaturalRally = Close;
  219. if Debug then print(date, Close, " Node 11", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  220. end
  221. else if (Close < DnTrend) then {start DnTrend}
  222. begin {Rule 6b}
  223. InSecRally = false;
  224. InDnTrend = true;
  225. DnTrend = Close;
  226. NaturalRallyBL = Close; {rule 4d} {Pivot Pt, Rule 9b}
  227. if Debug then print(date, Close, " Node 12", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  228. end
  229. else if (Close > SecondaryRally) then {remain in SecondaryRally, record higher price}
  230. SecondaryRally = Close; {Rule 3, 6g}
  231. end {InSecRally}


  232. {------Down Trend State-------}
  233. else if InDnTrend then
  234. begin
  235. if (Close < (NaturalRally - Threshold)) then
  236. NaturalRallyBL = NaturalRally; {Rule 4d}
  237. if ResumeDnTrend then {Rule 10 logic. Note: system becomes unstable if used}
  238. begin
  239. if (Close < (DnTrendBL - HalfThresh)) then
  240. begin
  241. ResumeDnTrend = false; {Rule 10a}
  242. DnTrend = Close; {Rule 2, 6b}
  243. if Debug then print(date, Close, " Node 13", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  244. end
  245. else if (Close > (DnTrendBL + HalfThresh)) then {DnTrend Over, return to NaturalRally}
  246. begin
  247. ResumeDnTrend = false;
  248. InDnTrend = false; {Rule 10c}
  249. InNatRally = true;
  250. NaturalRally = Close;
  251. if Debug then print(date, Close, " Node 14", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  252. end;
  253. end
  254. else if (Close > (DnTrend + Thresh)) then {start NaturalRally}
  255. begin {Rules 4c, 6c}
  256. InDnTrend = false;
  257. InNatRally = true;
  258. NaturalRally = Close;
  259. DnTrendBL = DnTrend; {Pivot Pt, Rule 8}
  260. ResumeDnTrend = false;
  261. if Debug then print(date, Close, " Node 15", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  262. end
  263. else if (Close < DnTrend) then {remain in DnTrend, record lower price}
  264. DnTrend = Close; {Rule 2, 6b}
  265. end {InSecRally}


  266. {------Natural Reaction State-------}
  267. else if InNatReact then
  268. begin
  269. if (Close < (NaturalRally - Threshold)) then
  270. NaturalRallyBL = NaturalRally; {Rule 4d}
  271. if (Close < DnTrend) then {resume DnTrend}
  272. begin {Rule 6b, 6e}
  273. InDnTrend = true;
  274. InNatReact = false;
  275. DnTrend = Close;
  276. if UseRule10 then ResumeDnTrend = true;
  277. if Debug then print(date, Close, " Node 16", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  278. end
  279. else if (Close < (NaturalReactionRL - HalfThresh)) then {resume DnTrend}
  280. begin {Rules 5b}
  281. InDnTrend = true;
  282. InNatReact = false;
  283. DnTrend = Close;
  284. if UseRule10 then ResumeDnTrend = true;
  285. if Debug then print(date, Close, " Node 17", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  286. end
  287. else if (Close > UpTrend) then {start UpTrend}
  288. begin {rule 6d}
  289. InNatReact = false;
  290. InUpTrend = true;
  291. UpTrend = Close;
  292. NaturalReactionRL = Close; {Rule 4b} {Pivot Pt, Rule 9c}
  293. if Debug then print(date, Close, " Node 18", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  294. end
  295. else if (Close > (NaturalReaction + Thresh)) then
  296. begin
  297. if (Close > NaturalRally) then {start NaturalRally}
  298. begin {Rules 4b, 6d}
  299. InNatReact = false;
  300. InNatRally = true;
  301. NaturalRally = Close;
  302. NaturalReactionRL = Close; {Rule 4b} {Pivot Pt, Rule 9c}
  303. if Debug then print(date, Close, " Node 19", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  304. end
  305. else {start SecondaryRally}
  306. begin {Rule 6g}
  307. InNatReact = false;
  308. InSecRally = true;
  309. SecondaryRally = Close;
  310. if Debug then print(date, Close, " Node 20", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  311. end;
  312. end
  313. else if (Close < NaturalReaction) then {remain in NaturalReaction, record lower price}
  314. NaturalReaction = Close; {Rules 3, 6a, 6b}
  315. end {InNatReact}


  316. {------Secondary Reaction State-------}
  317. else if InSecReact then
  318. begin
  319. if (Close < DnTrend) then {resume DnTrend}
  320. begin {Rules 6b, 6e}
  321. InDnTrend = true;
  322. InSecReact = false;
  323. DnTrend = Close;
  324. if UseRule10 then ResumeDnTrend = true;
  325. if Debug then print(date, Close, " Node 21", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  326. end
  327. else if (Close < (NaturalReactionRL - HalfThresh)) then {resume DnTrend}
  328. begin {Rules 5b}
  329. InDnTrend = true;
  330. InSecReact = false;
  331. DnTrend = Close;
  332. if UseRule10 then ResumeDnTrend = true;
  333. if Debug then print(date, Close, " Node 22", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  334. end
  335. else if (Close > UpTrend) then {start UpTrend}
  336. begin {Rule 6d}
  337. InSecReact = false;
  338. InUpTrend = true;
  339. UpTrend = Close;
  340. NaturalReactionRL = Close; {Rule 4b} {Pivot Pt, Rule 9c}
  341. if Debug then print(date, Close, " Node 23", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  342. end
  343. else if (Close < NaturalReaction) then {start NaturalReaction}
  344. begin {Rule 6h}
  345. InSecReact = false;
  346. InNatReact = true;
  347. NaturalReaction = Close;
  348. if Debug then print(date, Close, " Node 24", UpTrend, DnTrend, NaturalRally, NaturalReaction, UpTrendRL, DnTrendBL, NaturalRallyBL, NaturalReactionRL);
  349. end
  350. else if (Close < SecondaryReaction) then {remain in SecondaryReaction, record lower price}
  351. SecondaryReaction = Close; {Rule 6h}
  352. end {InSecReact}


  353. {------Error State-------}
  354. else
  355. begin
  356. {should not get here!!!}
  357. if Debug then
  358. print("Error in State Logic, Date: ", Date, " Time: ", Time);
  359. end; {Error state}

  360. end; {main}



  361. {------Trading Logic-------}

  362. {For TS 2000i only, non-op or delete for TS 6+ }
  363. {
  364. if (TradeTrends = 1) then
  365. begin
  366. if InUpTrend then
  367. Buy next bar at open
  368. else if (Marketposition = 1) then
  369. ExitLong next bar at open;

  370. if InDnTrend then
  371. Sell next bar at open
  372. else if (Marketposition = -1) then
  373. ExitShort next bar at open;
  374. end
  375. else {Trade Trends, Rallies, and Reactions}
  376. begin
  377. if (InUpTrend or InNatRally or InSecRally) then
  378. begin
  379. Buy next bar at open;
  380. ExitShort next bar at open;
  381. end;

  382. if (InDnTrend or InNatReact or InSecReact) then
  383. begin
  384. Sell next bar at open;
  385. ExitLong next bar at open;
  386. end;
  387. end;

  388. }
  389. { For TS 6+ versions}

  390. if (TradeTrends = 1) then
  391. begin
  392. if InUpTrend then
  393. Buy next bar at open
  394. else if (Marketposition = 1) then
  395. Sell next bar at open;

  396. if InDnTrend then
  397. Sell Short next bar at open
  398. else if (Marketposition = -1) then
  399. Buy to Cover next bar at open;
  400. end
  401. else
  402. begin
  403. if (InUpTrend or InNatRally or InSecRally) then
  404. begin
  405. Buy next bar at open;
  406. Buy to Cover next bar at open;
  407. end;

  408. if (InDnTrend or InNatReact or InSecReact) then
  409. begin
  410. Sell Short next bar at open;
  411. Sell next bar at open;
  412. end;
  413. end;
複製代碼


........
會寫的人加油...不會寫...
發表於 10-2-22 12:17 | 顯示全部樓層
OMG, 有點給他嚇到的程式碼...這樣長
發表於 11-3-27 10:49 | 顯示全部樓層
好文章,要慢慢研究。thx
發表於 11-3-27 21:29 | 顯示全部樓層
小娃多謝..謝謝,長期看小娃 + coco 的投資者
發表於 11-5-12 16:56 | 顯示全部樓層
謝謝大大分享......
發表於 11-6-21 08:15 | 顯示全部樓層
這篇文章具獨創性。AmiBroker的朋友一同討論。
發表於 13-5-23 00:22 | 顯示全部樓層
這篇文章要慢慢研究。謝謝大大分享
發表於 13-5-30 18:05 | 顯示全部樓層
來到coco才知道有這個,很感謝michael-knight大與小娃大願意分享,小弟會用心學習
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

手機版|Archiver|站長信箱|廣告洽詢|COCO研究院

GMT+8, 24-12-23 17:51

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回復 返回頂部 返回列表
理財討論網站 |