拋磚引玉 AmiBroker 首發 ~
此範例說明 AmiBroker 中如何控制下單口數,並使用 [下單大師] 或 [輸出文字檔] 達到交易自動化。

/*
 - 
 - Create by Enoch Yu @2011/06/20
 - 
 ------------------------------------------------------------------------------- 
 - 
 ------------------------------------------------------------------------------- 
 - 
*/


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 [YasserSoft] 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
          );

 

其實就那麼點小訣竅,說穿看破就覺得沒甚麼 ~ 網路無遠弗屆臥虎藏龍,相信一定有其他方法,還請各方高手不吝賜教。

歡迎各位同好取用及轉載,但請標明出處 ~ ^o^

後記:
1. 為了說明方便程式碼中夾雜中文註解,使用前記得將之移除避免不必要的困擾
2. 有此程式架構,相信聰明的網友們,應該可以很容易地加入[加碼]、[停利]、[折返停利]...等機制

 


arrow
arrow
    全站熱搜

    隱弄客 發表在 痞客邦 留言(0) 人氣()