COCO研究院

 找回密碼
 註冊
搜索
查看: 3496|回復: 9

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

[複製鏈接]
發表於 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 , 但在程次裡面有沒有這兩個資訊 ?
萬分感激!
 樓主| 發表於 13-7-23 18:05 | 顯示全部樓層
其實我是想問有沒有function 是拿時間的, 還是要重新的寫?
發表於 13-7-23 19:17 | 顯示全部樓層
Winson 發表於 13-7-23 18:05
其實我是想問有沒有function 是拿時間的, 還是要重新的寫?

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

Sell = ExitTime;
Cover = ExitTime;

發表於 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;
發表於 13-7-23 21:11 | 顯示全部樓層
merickelson 發表於 13-7-23 21:03
為何捨近求遠?
一個保證在1344平倉的code供參:
var: CloseTime(1344) ;

他是問amibroker的語法,不是multicharts的。
 樓主| 發表於 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[i] 來找出回測時「買入的位置」與「買入價」 ? 這兩個數據對日後編寫出場條件十分重要 , 但我對buy/sell指令背後的概念卻很模糊 , 謝謝大家。
發表於 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 );
}

評分

參與人數 1金錢 +2 收起 理由
Winson + 2 joshsmi 大大的教誨, 我會緊記!

查看全部評分

發表於 13-7-24 17:52 | 顯示全部樓層
And another thing in "99% of cases" there is no loop needed.

發表於 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
  1. // by joshsmi

  2. OpenTime = 093000;
  3. CloseTime = 160000;

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

  8. CrossBuy = Cross ( MA1, MA2 );
  9. CrossShort = Cross ( MA2, MA1 );

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

  12. Buy = CrossBuy AND mth AND EntryTime;
  13. Sell = CrossShort AND mth OR ExitTime;
  14. Short = CrossShort AND mth AND EntryTime;
  15. Cover = CrossBuy AND mth OR ExitTime;

  16. BuyPrice = SellPrice = C;
  17. ShortPrice = CoverPrice = C;


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

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


  25. //for PLOT only :::::::::::::::::::::::::::::::::::::::::::::

  26. if( Status( "action" ) == actionIndicator )
  27. {
  28.     // remove excessive signals
  29.     Buy = ExRem( Buy, Sell );
  30.     Sell = ExRem( Sell, Buy );
  31.     Short = ExRem( Short, Cover );
  32.     Cover = ExRem( Cover, Short );
  33.    
  34.     SetChartBkColor( colorBlack );
  35.    
  36.     SetChartOptions( 0, chartShowDates );
  37.     SetBarFillColor( IIf( C > O, ColorRGB( 0, 75, 0 ), IIf( C <= O, ColorRGB( 75, 0, 0 ), colorLightGrey ) ) );
  38.     Plot( C, "", IIf( C > O, ColorRGB( 0, 255, 0 ), IIf( C <= O, ColorRGB( 255, 0, 0 ), colorLightGrey ) ), 64, 0, 0, 0, 0 );

  39.     Buycolor = colorGreen;
  40.     Sellcolor  = colorOrange;
  41.     Shortcolor = colorRed;
  42.     Covercolor = colorLightBlue;
  43.    
  44.     PlotShapes( Buy*shapeSmallUpTriangle, Buycolor, 0, L, -15 );
  45.     PlotShapes( Buy*shapeHollowSmallUpTriangle, colorLightGrey, 0, L, -15 );
  46.     PlotShapes( Buy*shapeHollowSmallCircle, Buycolor, 0, BuyPrice, 0 );
  47.    
  48.     PlotShapes( Sell*shapeDownArrow, Sellcolor, 0, H, -15 );
  49.     PlotShapes( Sell*shapeHollowDownArrow, colorLightGrey, 0, H, -15 );
  50.     PlotShapes( Sell*shapeHollowSmallCircle, Sellcolor, 0, SellPrice, 0 );
  51.    
  52.     Shortdist = IIf( Short AND Sell, -30, -15 );
  53.     PlotShapes( Short*shapeSmallDownTriangle, Shortcolor, 0, H, Shortdist );
  54.     PlotShapes( Short*shapeHollowSmallDownTriangle, colorLightGrey, 0, H, Shortdist );
  55.     PlotShapes( Short*shapeSmallCircle, Shortcolor, 0, ShortPrice, 0 );
  56.    
  57.     Coverdist = IIf( Cover AND Buy, -30, -15 );
  58.     PlotShapes( Cover*shapeUpArrow, Covercolor, 0, L, Coverdist );
  59.     PlotShapes( Cover*shapeHollowUpArrow, colorLightGrey, 0, L, Coverdist );
  60.     PlotShapes( Cover*shapeSmallCircle, Covercolor, 0, CoverPrice, 0 );
  61.    
  62.     dist = 1.20 * ATR( 7 );
  63.     pToggle = ParamToggle( "PlotText", "Disable|Enable", 1 );
  64.     firstbar = Status( "firstvisiblebar" );
  65.     lastbar = Min( BarCount, Status( "lastvisiblebar" ) );
  66.    
  67.     for ( i = firstbar; i < lastbar; i++ )
  68.     {
  69.         if( Short[ i ] AND Sell[ i ] )
  70.              dist2[ i ] = 2 * dist[ i ];
  71.         else dist2[ i ] = dist[ i ];
  72.         
  73.         if( Cover[ i ] AND Buy[ i ] )
  74.              dist3[ i ] = 2 * dist[ i ];
  75.         else dist3[ i ] = dist[ i ];
  76.       
  77.         if ( Buy[ i ] ) if ( pToggle )   PlotText( "Buy\n@" + BuyPrice[ i ], i, L[ i ] - dist[ i ], Buycolor );
  78.         if ( Sell[ i ] ) if ( pToggle )  PlotText( "Sell\n@" + SellPrice[ i ], i, H[ i ] + dist[ i ], Sellcolor );
  79.         if ( Short[ i ] ) if ( pToggle ) PlotText( "Short\n@" + ShortPrice[ i ], i, H[ i ] + dist2[ i ], Shortcolor );
  80.         if ( Cover[ i ] ) if ( pToggle )  PlotText( "Cover\n@" + CoverPrice[ i ], i, L[ i ] - dist3[ i ], Covercolor );
  81.     }
  82.    
  83.     _N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.2f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
  84.    
  85.     // stripes
  86.     Colperiod  = ParamColor( "Color of periodic stripes", colorDarkGrey );
  87.     Plot( EntryTime, "", Colperiod, styleArea | styleOwnScale | styleNoLabel, 0, 1, 0, -10 );
  88. }
複製代碼

評分

參與人數 1金錢 +2 收起 理由
Winson + 2 Joshsmi 大大真的很用心, 謝謝^^

查看全部評分

 樓主| 發表於 13-7-25 09:38 | 顯示全部樓層
Joshsmi 真的很用心, 小弟自中學後也沒有再編程, 和新手沒兩樣, 我會繼續努力 , 多謝指點^^
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

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

GMT+8, 24-11-23 08:45

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

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