Winson 發表於 13-7-23 16:40

如何能每日收市前強制平倉

各位大大好 , 有個問題想請教1.      現在有一張3個月的期貨1分鐘圖 , 我寫了程式做backtest , 我的策略是 daytrade , 不想過夜overnight , 但發覺常常overnight, 請問如何寫可以令在每日收市前強制平倉(close position)? 2.      在用buy / sell 的時候有沒有一個array variable 是儲起當時交易的bar number ? 因為buy = cross(ma1,ma2) 我好像只能在analysis result window 看到是那一個location 和buy price , 但在程次裡面有沒有這兩個資訊 ? 萬分感激!

Winson 發表於 13-7-23 18:05

其實我是想問有沒有function 是拿時間的{:4_82:}, 還是要重新的寫?

ys_chang 發表於 13-7-23 19:17

Winson 發表於 13-7-23 18:05 static/image/common/back.gif
其實我是想問有沒有function 是拿時間的, 還是要重新的寫?

CloseTime = 134400;
ExitTime = TimeNum() >= CloseTime;

Sell = ExitTime;
Cover = ExitTime;

merickelson 發表於 13-7-23 21:03

為何捨近求遠?
一個保證在1344平倉的code供參:
var: CloseTime(1344) ;
condition1= Time>= CloseTime ;
if condition1 then begin
Sell("-END")next bar at market;
BuytoCover("end-")next bar at market;
end;

thirtycm 發表於 13-7-23 21:11

merickelson 發表於 13-7-23 21:03 static/image/common/back.gif
為何捨近求遠?
一個保證在1344平倉的code供參:
var: CloseTime(1344) ;


他是問amibroker的語法,不是multicharts的。

Winson 發表於 13-7-24 13:34

感謝大家 ! 小弟用TimeNum() 可以處理了
==============================
OpenTime = 093000;   
CloseTime = 160000;   
ExitTime = TimeNum() >= CloseTime; //complusory Close position after 16:00
EntryTime = (TimeNum()>= OpenTime ) AND (TimeNum()<CloseTime);   //Entry only between 09:00 and 16:00
Buy=Cross( MA(C,10), MA(C,50) ) AND Month()==5 AND EntryTime;
Sell= (Cross( MA(C,50), MA(C,10) )AND Month()==5) OR ExitTime;
Short=Cross( MA(C,50), MA(C,10) )AND Month()==5 AND EntryTime;
Cover=(Cross( MA(C,10), MA(C,50) ) AND Month()==5) OR ExitTime;
=============================================
關於問題2 , 我找到一個叫 ValueWhen 的 function , 於是嘗試把它加進exploration window
abc=ValueWhen(Buy,C,1);
Filter=abc;
但它卻把所有buy 的"condition" 都加進去 , 而實際上因為我在setoption限制了合約張數 , 是不可能有那麼多進場點 , 想請教各位高手究竟buy / sell / short / cover 是不是array variable來的? 如果我用一些for loop 可否配合buy 來找出回測時「買入的位置」與「買入價」 ? 這兩個數據對日後編寫出場條件十分重要 , 但我對buy/sell指令背後的概念卻很模糊 , 謝謝大家。

joshsmi 發表於 13-7-24 17:47

本帖最後由 joshsmi 於 13-7-24 18:23 編輯

Stop writing "there is no function for this and there is no function for that" and "this seems to be impossible and that seems to impossible".

You can program everything! Learn how to code and look at the help file!

And since you are a total newbie here is a tip or one number one rule of programming:
Stop using the same expression or function over and over and over again (even more so if they are time consuming during processing). So put repeated calls to a variable! Otherwise you are limiting yourself.

// by joshsmi

OpenTime = 093000;
CloseTime = 160000;

tn = TimeNum();
mth = Month() == 7; // July only
MA1 = MA ( C, 10 );
MA2 = MA ( C, 50 );

CrossBuy = Cross ( MA1, MA2 );
CrossShort = Cross ( MA2, MA1 );

ExitTime = tn >= CloseTime; // compulsory Close position after 16:00
EntryTime = tn >= OpenTime AND tn < CloseTime; // Entry only between 09:00 AND 16:00

Buy = CrossBuy AND mth AND EntryTime;
Sell = CrossShort AND mth OR ExitTime;
Short = CrossShort AND mth AND EntryTime;
Cover = CrossBuy AND mth OR ExitTime;

BuyPrice = SellPrice = C;
ShortPrice = CoverPrice = C;


if ( Status( "action" ) == actionExplore )
{
// for exploration, not backtesting
    Filter = Buy || Short;

    AddColumn( IIf( Buy, BuyPrice, Null ), "BuyPrice", 1.4, colorLightYellow, colorDarkGreen );
    AddColumn( IIf( Short, ShortPrice, Null ), "ShortPrice", 1.4, colorLightYellow, colorDarkRed );
}


//for PLOT only :::::::::::::::::::::::::::::::::::::::::::::

if( Status( "action" ) == actionIndicator )
{
    // remove excessive signals
    Buy = ExRem( Buy, Sell );
    Sell = ExRem( Sell, Buy );
    Short = ExRem( Short, Cover );
    Cover = ExRem( Cover, Short );
   
    SetChartBkColor( colorBlack );
   
    SetChartOptions( 0, chartShowDates );
    SetBarFillColor( IIf( C > O, ColorRGB( 0, 75, 0 ), IIf( C <= O, ColorRGB( 75, 0, 0 ), colorLightGrey ) ) );
    Plot( C, "", IIf( C > O, ColorRGB( 0, 255, 0 ), IIf( C <= O, ColorRGB( 255, 0, 0 ), colorLightGrey ) ), 64, 0, 0, 0, 0 );

    Buycolor = colorGreen;
    Sellcolor= colorOrange;
    Shortcolor = colorRed;
    Covercolor = colorLightBlue;
   
    PlotShapes( Buy*shapeSmallUpTriangle, Buycolor, 0, L, -15 );
    PlotShapes( Buy*shapeHollowSmallUpTriangle, colorLightGrey, 0, L, -15 );
    PlotShapes( Buy*shapeHollowSmallCircle, Buycolor, 0, BuyPrice, 0 );
   
    PlotShapes( Sell*shapeDownArrow, Sellcolor, 0, H, -15 );
    PlotShapes( Sell*shapeHollowDownArrow, colorLightGrey, 0, H, -15 );
    PlotShapes( Sell*shapeHollowSmallCircle, Sellcolor, 0, SellPrice, 0 );
   
    Shortdist = IIf( Short AND Sell, -30, -15 );
    PlotShapes( Short*shapeSmallDownTriangle, Shortcolor, 0, H, Shortdist );
    PlotShapes( Short*shapeHollowSmallDownTriangle, colorLightGrey, 0, H, Shortdist );
    PlotShapes( Short*shapeSmallCircle, Shortcolor, 0, ShortPrice, 0 );
   
    Coverdist = IIf( Cover AND Buy, -30, -15 );
    PlotShapes( Cover*shapeUpArrow, Covercolor, 0, L, Coverdist );
    PlotShapes( Cover*shapeHollowUpArrow, colorLightGrey, 0, L, Coverdist );
    PlotShapes( Cover*shapeSmallCircle, Covercolor, 0, CoverPrice, 0 );
   
    dist = 1.20 * ATR( 7 );
    pToggle = ParamToggle( "PlotText", "Disable|Enable", 1 );
    firstbar = Status( "firstvisiblebar" );
    lastbar = Min( BarCount, Status( "lastvisiblebar" ) );
   
    for ( i = firstbar; i < lastbar; i++ )
    {
      if( Short[ i ] AND Sell[ i ] )
             dist2[ i ] = 2 * dist[ i ];
      else dist2[ i ] = dist[ i ];
      
      if( Cover[ i ] AND Buy[ i ] )
             dist3[ i ] = 2 * dist[ i ];
      else dist3[ i ] = dist[ i ];
      
      if ( Buy[ i ] ) if ( pToggle )   PlotText( "Buy\n@" + BuyPrice[ i ], i, L[ i ] - dist[ i ], Buycolor );
      if ( Sell[ i ] ) if ( pToggle )PlotText( "Sell\n@" + SellPrice[ i ], i, H[ i ] + dist[ i ], Sellcolor );
      if ( Short[ i ] ) if ( pToggle ) PlotText( "Short\n@" + ShortPrice[ i ], i, H[ i ] + dist2[ i ], Shortcolor );
      if ( Cover[ i ] ) if ( pToggle )PlotText( "Cover\n@" + CoverPrice[ i ], i, L[ i ] - dist3[ i ], Covercolor );
    }
   
    _N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.2f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
   
    // stripes
    Colperiod= ParamColor( "Color of periodic stripes", colorDarkGrey );
    Plot( EntryTime, "", Colperiod, styleArea | styleOwnScale | styleNoLabel, 0, 1, 0, -10 );
}

joshsmi 發表於 13-7-24 17:52

And another thing in "99% of cases" there is no loop needed.

joshsmi 發表於 13-7-24 17:57

本帖最後由 joshsmi 於 13-7-24 18:27 編輯

above code seems to have some formatting errors so html paste doesn't seem work properly.

here is again using code tags to be sure
// by joshsmi

OpenTime = 093000;
CloseTime = 160000;

tn = TimeNum();
mth = Month() == 7; // July only
MA1 = MA ( C, 10 );
MA2 = MA ( C, 50 );

CrossBuy = Cross ( MA1, MA2 );
CrossShort = Cross ( MA2, MA1 );

ExitTime = tn >= CloseTime; // compulsory Close position after 16:00
EntryTime = tn >= OpenTime AND tn < CloseTime; // Entry only between 09:00 AND 16:00

Buy = CrossBuy AND mth AND EntryTime;
Sell = CrossShort AND mth OR ExitTime;
Short = CrossShort AND mth AND EntryTime;
Cover = CrossBuy AND mth OR ExitTime;

BuyPrice = SellPrice = C;
ShortPrice = CoverPrice = C;


if ( Status( "action" ) == actionExplore )
{
// for exploration, not backtesting
    Filter = Buy || Short;

    AddColumn( IIf( Buy, BuyPrice, Null ), "BuyPrice", 1.4, colorLightYellow, colorDarkGreen );
    AddColumn( IIf( Short, ShortPrice, Null ), "ShortPrice", 1.4, colorLightYellow, colorDarkRed );
}


//for PLOT only :::::::::::::::::::::::::::::::::::::::::::::

if( Status( "action" ) == actionIndicator )
{
    // remove excessive signals
    Buy = ExRem( Buy, Sell );
    Sell = ExRem( Sell, Buy );
    Short = ExRem( Short, Cover );
    Cover = ExRem( Cover, Short );
   
    SetChartBkColor( colorBlack );
   
    SetChartOptions( 0, chartShowDates );
    SetBarFillColor( IIf( C > O, ColorRGB( 0, 75, 0 ), IIf( C <= O, ColorRGB( 75, 0, 0 ), colorLightGrey ) ) );
    Plot( C, "", IIf( C > O, ColorRGB( 0, 255, 0 ), IIf( C <= O, ColorRGB( 255, 0, 0 ), colorLightGrey ) ), 64, 0, 0, 0, 0 );

    Buycolor = colorGreen;
    Sellcolor= colorOrange;
    Shortcolor = colorRed;
    Covercolor = colorLightBlue;
   
    PlotShapes( Buy*shapeSmallUpTriangle, Buycolor, 0, L, -15 );
    PlotShapes( Buy*shapeHollowSmallUpTriangle, colorLightGrey, 0, L, -15 );
    PlotShapes( Buy*shapeHollowSmallCircle, Buycolor, 0, BuyPrice, 0 );
   
    PlotShapes( Sell*shapeDownArrow, Sellcolor, 0, H, -15 );
    PlotShapes( Sell*shapeHollowDownArrow, colorLightGrey, 0, H, -15 );
    PlotShapes( Sell*shapeHollowSmallCircle, Sellcolor, 0, SellPrice, 0 );
   
    Shortdist = IIf( Short AND Sell, -30, -15 );
    PlotShapes( Short*shapeSmallDownTriangle, Shortcolor, 0, H, Shortdist );
    PlotShapes( Short*shapeHollowSmallDownTriangle, colorLightGrey, 0, H, Shortdist );
    PlotShapes( Short*shapeSmallCircle, Shortcolor, 0, ShortPrice, 0 );
   
    Coverdist = IIf( Cover AND Buy, -30, -15 );
    PlotShapes( Cover*shapeUpArrow, Covercolor, 0, L, Coverdist );
    PlotShapes( Cover*shapeHollowUpArrow, colorLightGrey, 0, L, Coverdist );
    PlotShapes( Cover*shapeSmallCircle, Covercolor, 0, CoverPrice, 0 );
   
    dist = 1.20 * ATR( 7 );
    pToggle = ParamToggle( "PlotText", "Disable|Enable", 1 );
    firstbar = Status( "firstvisiblebar" );
    lastbar = Min( BarCount, Status( "lastvisiblebar" ) );
   
    for ( i = firstbar; i < lastbar; i++ )
    {
      if( Short[ i ] AND Sell[ i ] )
             dist2[ i ] = 2 * dist[ i ];
      else dist2[ i ] = dist[ i ];
      
      if( Cover[ i ] AND Buy[ i ] )
             dist3[ i ] = 2 * dist[ i ];
      else dist3[ i ] = dist[ i ];
      
      if ( Buy[ i ] ) if ( pToggle )   PlotText( "Buy\n@" + BuyPrice[ i ], i, L[ i ] - dist[ i ], Buycolor );
      if ( Sell[ i ] ) if ( pToggle )PlotText( "Sell\n@" + SellPrice[ i ], i, H[ i ] + dist[ i ], Sellcolor );
      if ( Short[ i ] ) if ( pToggle ) PlotText( "Short\n@" + ShortPrice[ i ], i, H[ i ] + dist2[ i ], Shortcolor );
      if ( Cover[ i ] ) if ( pToggle )PlotText( "Cover\n@" + CoverPrice[ i ], i, L[ i ] - dist3[ i ], Covercolor );
    }
   
    _N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.2f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
   
    // stripes
    Colperiod= ParamColor( "Color of periodic stripes", colorDarkGrey );
    Plot( EntryTime, "", Colperiod, styleArea | styleOwnScale | styleNoLabel, 0, 1, 0, -10 );
}

Winson 發表於 13-7-25 09:38

Joshsmi 真的很用心, 小弟自中學後也沒有再編程, 和新手沒兩樣, 我會繼續努力 , 多謝指點^^
頁: [1]
查看完整版本: 如何能每日收市前強制平倉