COCO研究院

 找回密碼
 註冊
搜索
查看: 6023|回復: 12

[範例程式碼] AmiBroker 控制下單口數、使用下單大師、輸出文字檔

[複製鏈接]
發表於 11-12-30 00:43 | 顯示全部樓層 |閱讀模式
本帖最後由 enochyu 於 11-12-30 12:47 AM 編輯

程式範例分享 ~ AmiBroker 控制下單口數、使用下單大師、輸出文字檔



  1. Title = Date() + " [ Go Order Sample ] by E.Y. @2011/12/29";

  2. //
  3. // compute long, short, clearing condition
  4. //
  5. _MA3  = Ref( MA( Close, 3 ), -1 );
  6. _MA22 = Ref( MA( Close, 22 ), -1 );
  7. Buy   = ( _MA3 > _MA22 );                  // long condition
  8. Short = ( _MA3 < _MA22 );                  // short condition
  9. Sell  = Cover = ( TimeNum() > 132000 );    // clearing condition

  10. //
  11. // declare signal's parameter
  12. //
  13. _LongSharp        = False;    // log long signal
  14. _ShortSharp       = False;    // log short signal
  15. _CloseShape       = False;    // log clearing signal

  16. // ------ >>> START <<< -------------------------------------- //
  17. // ----------------------------------------------------------- //
  18. //  此區塊為控制交易口數之程式碼,應該是不少網友的擾 ~ 請笑納
  19. // ----------------------------------------------------------- //
  20. // ------ >>> START <<< ------------------------------------- //
  21. //
  22. // declare order's parameter
  23. //
  24. _ContractUnit     = 1;        // go order contract unit (指定下單口數,亦可由下單機設定)
  25. _CurrentContracts = 0;        // log go order contracts
  26. _OrderPrice       = 0;        // log go order price
  27. for ( i = 0; i < BarCount; i++ )
  28. {
  29.     _EntryLong  =      ( Buy[ i ] )
  30.                   AND !( Sell[ i ] AND Cover[ i ] );
  31.     _EntryShort =      ( Short[ i ] )
  32.                   AND !( Sell[ i ] AND Cover[ i ] );
  33.     // long entry
  34.     if ( ( _EntryLong ) AND ( _CurrentContracts <= 0 ) )
  35.     {
  36.         _CurrentContracts = _ContractUnit;
  37.         _OrderPrice       = Close[ i ];
  38.         _LongSharp[ i ]   = True;
  39.     }
  40.     // short entry
  41.     if ( ( _EntryShort ) AND ( _CurrentContracts >= 0 ) )
  42.     {
  43.         _CurrentContracts = -1 * _ContractUnit;
  44.         _OrderPrice       = Close[ i ];
  45.         _ShortSharp[ i ]  = True;
  46.     }
  47.     // clearing
  48.     if ( ( Sell[ i ] OR Cover[ i ] ) AND ( _CurrentContracts != 0 ) )
  49.     {
  50.         _CurrentContracts = 0;
  51.         _OrderPrice       = Close[ i ];
  52.         _CloseShape[ i ]  = True;
  53.     }
  54. }
  55. // ------ >>> END <<< ---------------------------------------- //
  56. // ----------------------------------------------------------- //
  57. //  此區塊為控制交易口數之程式碼,應該是不少網友的擾 ~ 請笑納
  58. // ----------------------------------------------------------- //
  59. // ------ >>> END <<< ---------------------------------------- //

  60. //
  61. // sned order command to [ OrderMaster API ]
  62. // 調用下單大師進行下單
  63. _OMComAPI   = CreateStaticObject( "OMSignAPI.OMCOMAPI" );
  64. _OMComAPI.IniDllAndPosition( "EYDEMO", _CurrentContracts );
  65. _OMDateTime   = Now( 1 ) + " " + StrRight( "00" + floor( Now( 4 ) / 10000 ), 2 )
  66.                 + ":" + StrRight( "00" + floor( Now( 4 ) / 100 ), 2 )
  67.                 + ":" + StrRight( "00" + floor( Now( 4 ) % 100 ), 2 );
  68. _OMEntryPrice = NumToStr( _OrderPrice, 1 );
  69. _OMComAPI.GoOrder( "EYDEMO", "", _OMDateTime, _CurrentContracts, _OMEntryPrice );

  70. //
  71. // output text file for [YasserSoft] auto record
  72. // 輸出文字檔供下單機讀取,此例為雅策[免費績效紀錄軟體]使用格式,請各位網友是需求自行調整
  73. _YS_ORDER_CMD = "R:\\YS_TEMP\\EYDEMO.txt";
  74. if ( ( Now( 4 ) % 2 ) == 0 )
  75. {
  76.     _fcsv = fopen( _YS_ORDER_CMD, "w" );
  77.     _content = StrFormat( "%04.0f/%02.0f/%02.0f %02.0f:%02.0f:%02.0f %g %g %02.0f:%02.0f:%02.0f"
  78.                           , Year(), Month(), Day()
  79.                           , Hour(), Minute(), Second()
  80.                           , _CurrentContracts
  81.                           , _OrderPrice
  82.                           , Hour(), Minute(), Second()
  83.                         );
  84.     fputs( _content, _fcsv );
  85.     fclose( _fcsv );
  86. }

  87. //
  88. // draw ma line
  89. //
  90. Plot( _MA3, "MA3", colorWhite, styleLine );
  91. Plot( _MA22, "MA22", colorBlue, styleLine );
  92. //
  93. // draw candle line
  94. //
  95. Plot( C, "Close", ParamColor( "Color", colorBlack ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );

  96. //
  97. // draw long & short signal
  98. //
  99. PlotShapes( _LongSharp * shapeUpArrow + _ShortSharp * shapeDownArrow
  100.             , IIf( _LongSharp, colorYellow, colorBlue )
  101.             , 0
  102.             , IIf( _LongSharp, Low, High )
  103.             , -80
  104.           );
  105. //
  106. // draw clearing signal
  107. //
  108. PlotShapes( _CloseShape * shapeStar
  109.             , colorWhite
  110.             , 0
  111.             , Low
  112.             , -30
  113.           );

複製代碼


------

祝各位 AmiBroker 戰友順心 ~

轉載至 C.A.B.E.

評分

參與人數 4金錢 +18 收起 理由
lwhuang + 1 按一個讚
joey0415 + 10 太強了!
lu0811 + 2 讚啦~感謝分享 :)
kilroy + 5 感謝感謝~~

查看全部評分

發表於 11-12-30 01:56 | 顯示全部樓層
非常感謝 enochyu 大大的幫忙

(每次看到大大的程式碼都覺得很整齊,而且很有邏輯程序感)

小弟來獻醜一下,簡單版的倉位判斷
(我只會非常非常非常簡單的語法來寫而已,汗顏啦)

EntryLongA=IIf(Buy,1,IIf(Short OR Sell OR Cover,-1,0));
EntryLongB=ValueWhen(EntryLongA!=0,EntryLongA,1);

LFlag=IIf(EntryLongB==1,1,0);

EntryShortA=IIf(Short,1,IIf(Buy OR Sell OR Cover,-1,0));
EntryShortB=ValueWhen(EntryShortA!=0,EntryShortA,1);

SFlag=IIf(EntryShortB==1,-1,0);

CurrentPosition = LFlag+SFlag;

---

因為 AB 在語法上沒 power/easy language 這麼簡單寫

小弟這個方法很笨,就是只有簡單的判斷

buy 多單 倉位為 1
short 空單 倉位為 -1

sell 多單平倉 cover 空單平倉 倉位為 0


AB 同好們再參考看看了,有更好的寫法也請大家分享一下唷

再次感謝 enochyu 大大

評分

參與人數 2金錢 +12 收起 理由
joey0415 + 10 太強了!
lu0811 + 2 讚啦~感謝分享 :)

查看全部評分

發表於 11-12-30 06:47 | 顯示全部樓層
本帖最後由 GnuHomot 於 11-12-30 06:54 AM 編輯

回復 1# enochyu

先感謝enochyu大的分享
  
  1.     if ( ( _EntryLong ) AND ( _CurrentContracts <= 0 ) )
  2.     {
  3.         _CurrentContracts = _ContractUnit;
  4.         _OrderPrice       = Close[ i ];
  5.         _LongSharp[ i ]   = True;
  6.     }
複製代碼

如果小弟沒誤會的話,這段程式碼看起來只有計算到單次進出。
另外因為實際上線時,會影響下單大師的只有最後一根K棒,所以其實for迴圈不用從0開始算,檢查最後一根K棒就可以了。如果要算加碼最後的總部位,又要用另外的算法了。

順便借這篇問一下,該不會全COCO討論區只有我是用Amibroker+下單大師萬用API在下單吧,似乎大家都用文字檔在下單
 樓主| 發表於 11-12-30 07:47 | 顯示全部樓層
讚啦,希望起拋磚引玉之效 ~ 有機會教學相長

哈哈 ~ 小弟就是用這最笨的方法來控制策略加減碼、停損/停利、折返停利...等機制,還請各位高手們多多指點,一來讓小弟學習成長,二來造福有需要的 AmiBroker 戰友們 ~

感恩啦
 樓主| 發表於 11-12-30 07:51 | 顯示全部樓層
回復 3# GnuHomot


目前小弟也是 AB + 下單大師配合使用中,只是部分策略會另輸出一份文字檔給雅X紀錄器做紀錄,請多多指教了。
發表於 11-12-30 08:44 | 顯示全部樓層
讚啦~感謝分享 :)
發表於 11-12-30 12:51 | 顯示全部樓層
謝謝了...........感謝分享 :)
發表於 11-12-30 16:16 | 顯示全部樓層
感谢分享,……谢谢
 樓主| 發表於 12-1-9 00:47 | 顯示全部樓層
本帖最後由 enochyu 於 12-1-9 12:48 AM 編輯
程式範例分享 ~ AmiBroker 控制下單口數、使用下單大師、輸出文字檔



------

祝各位 AmiBroker 戰友順心 ...
enochyu 發表於 11-12-30 12:43 AM



來個小改版,簡化不必要之程式碼(如:for loop)達到一樣的效果,請參考囉


  1. //
  2. // title
  3. //
  4. SetChartOptions( 0, chartShowArrows | chartShowDates | chartWrapTitle );
  5. _N(  Title = StrFormat( "{{NAME}} - {{DATE}}   O %g,   H %g,   L %g,   C %g,  (%.1f%%)", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
  6. Title = Title + "    [ Go Order Sample ] Create by E.Y. @2012/01/08 \n{{VALUES}}";
  7. Plot( Day() != Ref( Day(), -1 ), "", ColorRGB( 50, 50, 50 ), styleHistogram | styleOwnScale, 0, 1 );


  8. // --- >>> START <<<
  9. // -------------------------------------------------------------------------
  10. // computation buy, short, sell and cover condition
  11. // -------------------------------------------------------------------------
  12. //
  13. // computation buy, short, sell and cover condition
  14. //
  15. _Condition1  = Ref( MA( Close, 3 ), -1 );
  16. _Condition2  = Ref( MA( Close, 22 ), -1 );
  17. Buy   = ( _Condition1 > _Condition2 ) AND ( TimeNum() <= 132000 );   // buy condition
  18. Short = ( _Condition1 < _Condition2 ) AND ( TimeNum() <= 132000 );   // short condition
  19. Sell  = Cover = ( TimeNum() > 132000 );                // sell and cover condition
  20. //
  21. // computation buyprice, shortprice, sellprice and coverprice
  22. //
  23. BuyPrice = ShortPrice = SellPrice  = CoverPrice = Close;
  24. //
  25. // filter buy, short, sell and cover signal
  26. //
  27. _LongEntry  = ExRem( Buy, Short ) OR ExRem( Buy, Sell );
  28. _ShortEntry = ExRem( Short, Buy ) OR ExRem( Short, Cover );
  29. _ClearAll   = ( Sell OR Cover ) AND ( Ref( Buy, -1 ) OR Ref( Short, -1 ) );
  30. //
  31. // -------------------------------------------------------------------------
  32. // computation buy, short, sell and cover condition
  33. // -------------------------------------------------------------------------
  34. // --- >>> END <<<


  35. // --- >>> START <<<
  36. // -------------------------------------------------------------------------
  37. // computation order date, time, contracts, price
  38. // -------------------------------------------------------------------------
  39. //
  40. // contracts per order
  41. //
  42. _TXFContractUnit   = 1;
  43. //
  44. // txf order contracts
  45. //
  46. _TXFOrderContracts = IIf( Sell OR Cover
  47.                             // sell or cover contracts
  48.                             , 0                             
  49.                             , IIf( Buy
  50.                                    // buy contracts
  51.                                    , _TXFContractUnit
  52.                                    // short contracts
  53.                                    , _TXFContractUnit * -1
  54.                                  )
  55.                           );
  56. _TXFOrderPrice     = Close;
  57. //
  58. // -------------------------------------------------------------------------
  59. // computation order contracts, price
  60. // -------------------------------------------------------------------------
  61. // --- >>> END <<<


  62. // --- >>> START <<<
  63. // -------------------------------------------------------------------------
  64. // go order
  65. // -------------------------------------------------------------------------
  66. //
  67. // prepare go order parameters
  68. //
  69. _GoOrderContracts = _TXFOrderContracts;
  70. _GoOrderPrice     = _TXFOrderPrice;
  71. _GoOrderNowDate   = Now( 1 );
  72. _GoOrderNowTime   = StrRight( "00" + floor( Now( 4 ) / 10000 ), 2 )
  73.                     + ":" + StrRight( "00" + floor( Now( 4 ) / 100 ), 2 )
  74.                     + ":" + StrRight( "00" + floor( Now( 4 ) % 100 ), 2 );
  75. //
  76. // sned order command to [ OrderMaster API ]
  77. //
  78. _OMComAPI = CreateStaticObject( "OMSignAPI.OMCOMAPI" );
  79. _OMComAPI.IniDllAndPosition( "EYDEMO", NumToStr( _TXFOrderContracts, 0 ) );
  80. _OMComAPI.GoOrder( "EYDEMO"
  81.                  , ""
  82.                  , _GoOrderNowDate + " " + _GoOrderNowTime
  83.                  , NumToStr( _GoOrderContracts, 0 )
  84.                  , NumToStr( _GoOrderPrice, 0 )
  85.                  );
  86. //
  87. // output text file for [YasserSoft] auto record
  88. //
  89. _YS_ORDER_CMD = "R:\\YS_TEMP\\EYDEMO.txt";
  90. if ( ( Now( 4 ) % 2 ) == 0 )
  91. {
  92.     _fcsv = fopen( _YS_ORDER_CMD, "w" );
  93.     _content = _GoOrderNowDate
  94.              + " " + _GoOrderNowTime
  95.              + " " + StrFormat( "%g %g", _GoOrderContracts, _GoOrderPrice )
  96.              + " " + _GoOrderNowTime
  97.              ;
  98.     fputs( _content, _fcsv );
  99.     fclose( _fcsv );
  100. }
  101. // -------------------------------------------------------------------------
  102. // go order
  103. // -------------------------------------------------------------------------
  104. // --- >>> END <<<


  105. // --- >>> START <<<
  106. // -------------------------------------------------------------------------
  107. // drawing
  108. // -------------------------------------------------------------------------
  109. //
  110. // draw the ma line
  111. //
  112. Plot( _Condition1, "MA1", colorYellow, styleDashed );
  113. Plot( _Condition2, "MA2", ColorRGB( 0, 80, 255 ), styleDots );
  114. //
  115. // draw the buy or short signal
  116. //
  117. PlotShapes( _LongEntry * shapeUpArrow + _ShortEntry * shapeDownArrow
  118.           , IIf( _LongEntry, colorYellow, ColorRGB( 0, 80, 255 ) )
  119.           , 0
  120.           , IIf( _LongEntry, Low, High )
  121.           , -30 );
  122. //
  123. // draw the clear signal
  124. //
  125. PlotShapes( _ClearAll * shapeSmallCircle
  126.           , colorLightGrey
  127.           , 0
  128.           , Close
  129.           , -30 );
  130. PlotShapes( _ClearAll * shapeHollowSquare
  131.           , colorLightGrey
  132.           , 0
  133.           , Close
  134.           , -30 );
  135. //
  136. // draw the candle line
  137. //
  138. Plot( Close
  139.     , "Close"
  140.     , ParamColor( "Color", colorBlack )
  141.     , styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );
  142. //
  143. //
  144. //
  145. Title = Title + EncodeColor( colorGold ) + "    ( " + _GoOrderContracts + ", " + _GoOrderPrice + " )";
  146. //
  147. // -------------------------------------------------------------------------
  148. // drawing
  149. // -------------------------------------------------------------------------
  150. // --- >>> END <<<
複製代碼


相信以各位大大功力一看就懂,小弟在此就不在贅述了

轉載至 C.A.B.E.
發表於 12-1-20 16:31 | 顯示全部樓層
感谢分享,……谢谢
發表於 12-4-24 21:44 | 顯示全部樓層
感謝分享
感激不盡
發表於 12-11-14 16:32 | 顯示全部樓層
enochyu 發表於 12-1-9 00:47
來個小改版,簡化不必要之程式碼(如:for loop)達到一樣的效果,請參考囉

請問將此程式碼在5分線執行時,在現K棒還未完成收定時,買賣條件忽多忽空,造成重複下單,請問要如何解決?謝謝您

發表於 13-6-4 15:16 | 顯示全部樓層
So

_Condition1  = Ref( MA( Close, 5 ), -1 );
_Condition2  = Ref( MA( Close, 60 ), -1 );
Buy   = ( _Condition1 > _Condition2 ); //AND ( TimeNum() <= 132000 ); buy condition
Short = ( _Condition1 < _Condition2 ) ;//AND ( TimeNum() <= 132000 ); short condition
Sell  = Cover = ( TimeNum() > 0);                // sell and cover condition

主要改了下開單時間,均線參數,圖上尖頭信號真多,請指正哪裏錯了...謝謝
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

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

GMT+8, 24-4-29 23:38

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回復 返回頂部 返回列表
理財討論網站 | AI繪圖AI超擬真美女AI beauty AI Stable DiffusionAI正妹AI Lookbook