2013年4月3日星期三

Add Trading Log on AmiBroker Chart(EOD)

将股票交易记录(交割单)的买卖信息画到Amibroker的chart中。

交易结果不佳的时候整理交易记录是个让人心烦的事情,3月份表现不好,干脆写了afl把所有交易记录都画到AB的chart上。

Step1. 用excel将通达信的交割单处理成txt文本文件,格式是“date,buy/sell,symbol,price,lots"。

TradeLog.txt sample(No title line):

2/28/2013,buy,000043,7.13,1000
3/1/2013,sell,000043,7.32,1000

Step2. 在AB中运行scan [Scan]ReadTradeLog.afl. 目的是读取txt文件信息,合并存储在静态变量中(code as followed).


/*  ---------------[Scan]ReadTradeLog.afl---------------------------------------
read trade log from TradeLog.txt and add buy@price text on the charts
  1. First, run scan using this afl to initialize static variables
  2. Then use TradeCheck layouts to check every operated stock chart(with indicator
TradeLogDrawing program    
                                        2013-3-23  ZYHuang                   */

Procedure getTradeLogInfo(sFileName)

/* get the trading log information and store in static variables
OpDate="20130303,20130403,20130202,...."
Op="buy,sell,buy,sell,buy,buy,"
Sbl="002234,002234,000043,000043,234543,234544,"
Prc="1.25,1.34,3.33,3.55,2.33,4.55,"
Lots="100,100,400,200,300,300," 
*/
OpDate=Op=Sbl=Prc=Lots="";
fh=fopen(sFileName,"r");
if(fh)
{
while(!feof(fh))
{
temp=fgets(fh);
OpDate=OpDate+strextract(temp,0)+",";
Op=Op+strextract(temp,1)+",";
Sbl=Sbl+strextract(temp,2)+",";
Prc=Prc+strExtract(temp,3)+",";
Lots=Lots+strLeft(strextract(temp,-1),strLen(strextract(temp,-1))-1)+","; //exclude the last Enter Code
}
fclose(fh);
}
staticVarSetTEXT("svOpDate",OpDate);
staticVarSetTEXT("svOp",Op);
staticVarSetTEXT("svSbl",Sbl);
staticVarSetTEXT("svPrc",Prc);
staticVarSetTEXT("svLots",Lots);
}

getTradeLogInfo("E:\\TradeLog.txt");
buy=0;  //for scan
  

Step3. 在AB中使用[I]DrawTradeLogOnChart.afl建立chart(code as followed).


/*       ----------------- [I]DrawTradeLogOnChart.afl  ---------------------------
read static variables to draw trading operation on the chart
TradeLogDrawing program    
                                        2013-3-23  ZYHuang                   */

//------Initial low-level graphic infomation and funcions -------begin----
lvb=status("lastvisiblebar");
fvb=status("firstvisiblebar");
chartwidth=status("pxchartwidth");
chartleft=status("pxchartleft");
barwidth=chartwidth/(lvb-fvb+1);

function GetVisibleBarCount()        
{
return min(lvb-fvb,barcount-fvb); //lastvisiblebar could include the "blank" bar at chart right
}                                     

function GfxConvertBarToPixelx(bar)  //bar is zero based visible barcount
{
return chartleft+bar*barwidth;
}

function GfxConvertValueToPixelY(value)
{
local miny,maxy,chartbottom,chartheight;
miny=status("axisminy");
maxy=status("axismaxy");
chartbottom=status("pxchartbottom");
chartheight=status("pxchartheight");
return chartbottom-floor(0.5+(value-miny)*chartheight/(maxy-miny));
}
//----------Initial low-level graphic infomation and funcions --------------end-----

Procedure subDrawLog(num)
{
oPdate=strtodatetime(strExtract(staticVarGetText("svOpDate"),num));
oP=strextract(staticvargettext("svOp"),num);
oPpc=strToNum(strextract(staticvargettext("svPrc"),num));
oPlt=strextract(staticvargettext("svLots"),num);
txtcolor=colorgreen;
if (oP=="buy") txtcolor=colorred;
GfxSetOverLayMode(0);
barNum=GetVisibleBarCount();
GfxSelectFont( "Tahoma",7,500,false,true);
Gfxsettextcolor(txtcolor);
ObjectDateBarcount=lookup(barindex(),oPdate,0);

if (!isnull(ObjectDateBarcount) )
{   //Draw 

left=GfxConvertBarToPixelx(objectdatebarcount-fvb)+barwidth/3;
bottom=GfxConvertValueToPixely(Oppc);
right=left+15*barwidth;
top=bottom-20;
txt=strformat(strtoupper(strleft(oP,1)))+oPlt+"@"+oPpc;
GfxDrawText(txt,left,top,right,bottom,32+8);
}
}  //sub end

//Main 
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
requesttimedrefresh(1);
stocklist=staticVarGetText("svSbl");
plot(c,"price",colorblack,stylecandle);

GfxSelectFont( "Tahoma",11.5,700,false,true);
GfxSetBKcolor(coloryellow);
Gfxsettextcolor(colorblue);
gfxtextout("Do scan first",865,620);
for (i=0; i<strCount(stocklist,",");i++)
{
if (strleft(name(),6)==strExtract(stocklist,i))
{
subDrawLog(i);
}
}

1 条评论:

  1. 博主,你的博文内容非常棒! 我从这里学到了很多,期待能够联系到你,向你讨教! ephram.wu@gmail.com 

    回复删除