2017年3月6日 星期一

MultiCharts 寫Log 函數

開發 MultiCharts 策略,Debug是一個必經的過程,但如果想在PLE中輸出檔案或記錄某些內容,就需要再PLE中寫記錄Log的函數,筆者這裡分享一下有在使用的Log函數。
{write log function}

inputs: 
       FolderPath(StringSimple),
       Messagetext(StringSimple);
 
vars : Now(0),
       MsgText(""),
       Millisecond(""),
       TimeText(""),
       DateText(""),
       FileName("");
 
{Format Date and Time}
Now = computerdatetime;
Millisecond = NumToStr(MillisecondsFromDateTime(Now), 0);
if StrLen(Millisecond) = 1 then Millisecond = "00" + Millisecond
else if StrLen(Millisecond) = 2 then Millisecond = "0" + Millisecond;
 
{Log format}
DateText = FormatDate("yyyy-MM-dd", Now);
TimeText = FormatTime("HH:mm:ss", Now) + "." + Millisecond;
MsgText = DateText + " " + TimeText + " " + Messagetext + NewLine;

{File name}
DateText = FormatDate("yyyyMMdd", Now);
FileName = FolderPath + "_" + DateText + "_log.txt";
FileAppend(FileName, MsgText);
第27行:檔名,讀者可自行修改