AmiBroker 控制下單口數、使用下單大師、輸出文字檔
本帖最後由 enochyu 於 11-12-30 12:47 AM 編輯程式範例分享 ~ AmiBroker 控制下單口數、使用下單大師、輸出文字檔
Title = Date() + " [ Go Order Sample ] by E.Y. @2011/12/29";
//
// compute long, short, clearing condition
//
_MA3= Ref( MA( Close, 3 ), -1 );
_MA22 = Ref( MA( Close, 22 ), -1 );
Buy = ( _MA3 > _MA22 ); // long condition
Short = ( _MA3 < _MA22 ); // short condition
Sell= Cover = ( TimeNum() > 132000 ); // clearing condition
//
// declare signal's parameter
//
_LongSharp = False; // log long signal
_ShortSharp = False; // log short signal
_CloseShape = False; // log clearing signal
// ------ >>> START <<< -------------------------------------- //
// ----------------------------------------------------------- //
//此區塊為控制交易口數之程式碼,應該是不少網友的擾 ~ 請笑納
// ----------------------------------------------------------- //
// ------ >>> START <<< ------------------------------------- //
//
// declare order's parameter
//
_ContractUnit = 1; // go order contract unit (指定下單口數,亦可由下單機設定)
_CurrentContracts = 0; // log go order contracts
_OrderPrice = 0; // log go order price
for ( i = 0; i < BarCount; i++ )
{
_EntryLong= ( Buy[ i ] )
AND !( Sell[ i ] AND Cover[ i ] );
_EntryShort = ( Short[ i ] )
AND !( Sell[ i ] AND Cover[ i ] );
// long entry
if ( ( _EntryLong ) AND ( _CurrentContracts <= 0 ) )
{
_CurrentContracts = _ContractUnit;
_OrderPrice = Close[ i ];
_LongSharp[ i ] = True;
}
// short entry
if ( ( _EntryShort ) AND ( _CurrentContracts >= 0 ) )
{
_CurrentContracts = -1 * _ContractUnit;
_OrderPrice = Close[ i ];
_ShortSharp[ i ]= True;
}
// clearing
if ( ( Sell[ i ] OR Cover[ i ] ) AND ( _CurrentContracts != 0 ) )
{
_CurrentContracts = 0;
_OrderPrice = Close[ i ];
_CloseShape[ i ]= True;
}
}
// ------ >>> END <<< ---------------------------------------- //
// ----------------------------------------------------------- //
//此區塊為控制交易口數之程式碼,應該是不少網友的擾 ~ 請笑納
// ----------------------------------------------------------- //
// ------ >>> END <<< ---------------------------------------- //
//
// sned order command to [ OrderMaster API ]
// 調用下單大師進行下單
_OMComAPI = CreateStaticObject( "OMSignAPI.OMCOMAPI" );
_OMComAPI.IniDllAndPosition( "EYDEMO", _CurrentContracts );
_OMDateTime = Now( 1 ) + " " + StrRight( "00" + floor( Now( 4 ) / 10000 ), 2 )
+ ":" + StrRight( "00" + floor( Now( 4 ) / 100 ), 2 )
+ ":" + StrRight( "00" + floor( Now( 4 ) % 100 ), 2 );
_OMEntryPrice = NumToStr( _OrderPrice, 1 );
_OMComAPI.GoOrder( "EYDEMO", "", _OMDateTime, _CurrentContracts, _OMEntryPrice );
//
// output text file for auto record
// 輸出文字檔供下單機讀取,此例為雅策[免費績效紀錄軟體]使用格式,請各位網友是需求自行調整
_YS_ORDER_CMD = "R:\\YS_TEMP\\EYDEMO.txt";
if ( ( Now( 4 ) % 2 ) == 0 )
{
_fcsv = fopen( _YS_ORDER_CMD, "w" );
_content = StrFormat( "%04.0f/%02.0f/%02.0f %02.0f:%02.0f:%02.0f %g %g %02.0f:%02.0f:%02.0f"
, Year(), Month(), Day()
, Hour(), Minute(), Second()
, _CurrentContracts
, _OrderPrice
, Hour(), Minute(), Second()
);
fputs( _content, _fcsv );
fclose( _fcsv );
}
//
// draw ma line
//
Plot( _MA3, "MA3", colorWhite, styleLine );
Plot( _MA22, "MA22", colorBlue, styleLine );
//
// draw candle line
//
Plot( C, "Close", ParamColor( "Color", colorBlack ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );
//
// draw long & short signal
//
PlotShapes( _LongSharp * shapeUpArrow + _ShortSharp * shapeDownArrow
, IIf( _LongSharp, colorYellow, colorBlue )
, 0
, IIf( _LongSharp, Low, High )
, -80
);
//
// draw clearing signal
//
PlotShapes( _CloseShape * shapeStar
, colorWhite
, 0
, Low
, -30
);
------
祝各位 AmiBroker 戰友順心 ~ {:4_82:}
轉載至 C.A.B.E. 非常感謝 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 大大 {:5_260:} 本帖最後由 GnuHomot 於 11-12-30 06:54 AM 編輯
回復 1# enochyu
先感謝enochyu大的分享
if ( ( _EntryLong ) AND ( _CurrentContracts <= 0 ) )
{
_CurrentContracts = _ContractUnit;
_OrderPrice = Close[ i ];
_LongSharp[ i ] = True;
}
如果小弟沒誤會的話,這段程式碼看起來只有計算到單次進出。
另外因為實際上線時,會影響下單大師的只有最後一根K棒,所以其實for迴圈不用從0開始算,檢查最後一根K棒就可以了。如果要算加碼最後的總部位,又要用另外的算法了。
順便借這篇問一下,該不會全COCO討論區只有我是用Amibroker+下單大師萬用API在下單吧,似乎大家都用文字檔在下單{:4_623:} 讚啦,希望起拋磚引玉之效 ~ 有機會教學相長 {:4_113:}
哈哈 ~ 小弟就是用這最笨的方法來控制策略加減碼、停損/停利、折返停利...等機制,還請各位高手們多多指點,一來讓小弟學習成長,二來造福有需要的 AmiBroker 戰友們 ~
感恩啦 {:5_260:} 回復 3# GnuHomot
目前小弟也是 AB + 下單大師配合使用中,只是部分策略會另輸出一份文字檔給雅X紀錄器做紀錄,請多多指教了。 讚啦~感謝分享 :) 謝謝了...........感謝分享 :) 感谢分享,……谢谢 本帖最後由 enochyu 於 12-1-9 12:48 AM 編輯
程式範例分享 ~ AmiBroker 控制下單口數、使用下單大師、輸出文字檔
------
祝各位 AmiBroker 戰友順心 ...
enochyu 發表於 11-12-30 12:43 AM http://www.coco-in.net/images/common/back.gif
來個小改版,簡化不必要之程式碼(如:for loop)達到一樣的效果,請參考囉
//
// title
//
SetChartOptions( 0, chartShowArrows | chartShowDates | chartWrapTitle );
_N(Title = StrFormat( "{{NAME}} - {{DATE}} O %g, H %g, L %g, C %g,(%.1f%%)", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Title = Title + " [ Go Order Sample ] Create by E.Y. @2012/01/08 \n{{VALUES}}";
Plot( Day() != Ref( Day(), -1 ), "", ColorRGB( 50, 50, 50 ), styleHistogram | styleOwnScale, 0, 1 );
// --- >>> START <<<
// -------------------------------------------------------------------------
// computation buy, short, sell and cover condition
// -------------------------------------------------------------------------
//
// computation buy, short, sell and cover condition
//
_Condition1= Ref( MA( Close, 3 ), -1 );
_Condition2= Ref( MA( Close, 22 ), -1 );
Buy = ( _Condition1 > _Condition2 ) AND ( TimeNum() <= 132000 ); // buy condition
Short = ( _Condition1 < _Condition2 ) AND ( TimeNum() <= 132000 ); // short condition
Sell= Cover = ( TimeNum() > 132000 ); // sell and cover condition
//
// computation buyprice, shortprice, sellprice and coverprice
//
BuyPrice = ShortPrice = SellPrice= CoverPrice = Close;
//
// filter buy, short, sell and cover signal
//
_LongEntry= ExRem( Buy, Short ) OR ExRem( Buy, Sell );
_ShortEntry = ExRem( Short, Buy ) OR ExRem( Short, Cover );
_ClearAll = ( Sell OR Cover ) AND ( Ref( Buy, -1 ) OR Ref( Short, -1 ) );
//
// -------------------------------------------------------------------------
// computation buy, short, sell and cover condition
// -------------------------------------------------------------------------
// --- >>> END <<<
// --- >>> START <<<
// -------------------------------------------------------------------------
// computation order date, time, contracts, price
// -------------------------------------------------------------------------
//
// contracts per order
//
_TXFContractUnit = 1;
//
// txf order contracts
//
_TXFOrderContracts = IIf( Sell OR Cover
// sell or cover contracts
, 0
, IIf( Buy
// buy contracts
, _TXFContractUnit
// short contracts
, _TXFContractUnit * -1
)
);
_TXFOrderPrice = Close;
//
// -------------------------------------------------------------------------
// computation order contracts, price
// -------------------------------------------------------------------------
// --- >>> END <<<
// --- >>> START <<<
// -------------------------------------------------------------------------
// go order
// -------------------------------------------------------------------------
//
// prepare go order parameters
//
_GoOrderContracts = _TXFOrderContracts;
_GoOrderPrice = _TXFOrderPrice;
_GoOrderNowDate = Now( 1 );
_GoOrderNowTime = StrRight( "00" + floor( Now( 4 ) / 10000 ), 2 )
+ ":" + StrRight( "00" + floor( Now( 4 ) / 100 ), 2 )
+ ":" + StrRight( "00" + floor( Now( 4 ) % 100 ), 2 );
//
// sned order command to [ OrderMaster API ]
//
_OMComAPI = CreateStaticObject( "OMSignAPI.OMCOMAPI" );
_OMComAPI.IniDllAndPosition( "EYDEMO", NumToStr( _TXFOrderContracts, 0 ) );
_OMComAPI.GoOrder( "EYDEMO"
, ""
, _GoOrderNowDate + " " + _GoOrderNowTime
, NumToStr( _GoOrderContracts, 0 )
, NumToStr( _GoOrderPrice, 0 )
);
//
// output text file for auto record
//
_YS_ORDER_CMD = "R:\\YS_TEMP\\EYDEMO.txt";
if ( ( Now( 4 ) % 2 ) == 0 )
{
_fcsv = fopen( _YS_ORDER_CMD, "w" );
_content = _GoOrderNowDate
+ " " + _GoOrderNowTime
+ " " + StrFormat( "%g %g", _GoOrderContracts, _GoOrderPrice )
+ " " + _GoOrderNowTime
;
fputs( _content, _fcsv );
fclose( _fcsv );
}
// -------------------------------------------------------------------------
// go order
// -------------------------------------------------------------------------
// --- >>> END <<<
// --- >>> START <<<
// -------------------------------------------------------------------------
// drawing
// -------------------------------------------------------------------------
//
// draw the ma line
//
Plot( _Condition1, "MA1", colorYellow, styleDashed );
Plot( _Condition2, "MA2", ColorRGB( 0, 80, 255 ), styleDots );
//
// draw the buy or short signal
//
PlotShapes( _LongEntry * shapeUpArrow + _ShortEntry * shapeDownArrow
, IIf( _LongEntry, colorYellow, ColorRGB( 0, 80, 255 ) )
, 0
, IIf( _LongEntry, Low, High )
, -30 );
//
// draw the clear signal
//
PlotShapes( _ClearAll * shapeSmallCircle
, colorLightGrey
, 0
, Close
, -30 );
PlotShapes( _ClearAll * shapeHollowSquare
, colorLightGrey
, 0
, Close
, -30 );
//
// draw the candle line
//
Plot( Close
, "Close"
, ParamColor( "Color", colorBlack )
, styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );
//
//
//
Title = Title + EncodeColor( colorGold ) + " ( " + _GoOrderContracts + ", " + _GoOrderPrice + " )";
//
// -------------------------------------------------------------------------
// drawing
// -------------------------------------------------------------------------
// --- >>> END <<<
相信以各位大大功力一看就懂,小弟在此就不在贅述了 {:4_82:}
轉載至 C.A.B.E. 感谢分享,……谢谢{:4_113:} 感謝分享
感激不盡 enochyu 發表於 12-1-9 00:47 static/image/common/back.gif
來個小改版,簡化不必要之程式碼(如:for loop)達到一樣的效果,請參考囉
請問將此程式碼在5分線執行時,在現K棒還未完成收定時,買賣條件忽多忽空,造成重複下單,請問要如何解決?謝謝您
So
http://p13.freep.cn/p.aspx?u=v20_p13_photo_1306041512125513_0.jpg
_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
主要改了下開單時間,均線參數,圖上尖頭信號真多,請指正哪裏錯了...謝謝
頁:
[1]