Skip to content

Instantly share code, notes, and snippets.

@roveltwoo
Last active May 12, 2019 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roveltwoo/0d500631682cf992f9c46a8b31ebb02c to your computer and use it in GitHub Desktop.
Save roveltwoo/0d500631682cf992f9c46a8b31ebb02c to your computer and use it in GitHub Desktop.
ExportHistoryCSV Source
//+------------------------------------------------------------------+
//| ExportHistoryCSV.mq4 |
//| Rovelt.woo |
//| https://rovelt-woo.hatenablog.com/ |
//+------------------------------------------------------------------+
#property copyright "Rovelt.woo"
#property link "https://rovelt-woo.hatenablog.com/"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
int fileHandle = -1;
datetime openTime;
double openPrice;
datetime closeTime;
double closePrice;
int i;
// 取引履歴の件数取得
int hstTotal=OrdersHistoryTotal();
if( hstTotal > 0){// 1件もないときはファイルオープンしない
// ファイル名の日付部分を作成
string str = TimeToStr(TimeLocal(),TIME_DATE|TIME_SECONDS);
StringReplace( str,".","");
StringReplace( str,":","");
StringReplace( str," ","");
string fileName = StringConcatenate("ExportHistory_",str,".csv");
// CSVファイルオープン
fileHandle = FileOpen(fileName, FILE_CSV|FILE_WRITE, ",");
}
// ファイルオープンできていない時は終了
if( fileHandle < 0 ){
Print("No Histroy or Open Error");
return;
}
// ヘッダ出力
FileWrite(fileHandle,
"ticketno",
"entrydate",
"profitdate",
"symbol",
"type",
"openprice",
"profitprice",
"lots",
"swap",
"Commission",
"profit",
"comment");
// 取引履歴を1件ずつ処理
for(i=0;i<hstTotal;i++)
{
// 決済したものでBUYとSELLのみ対象
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true){
if(OrderType() == OP_BUY || OrderType() == OP_SELL){// ←これいらないかも・・・
openTime = OrderOpenTime();
openPrice = OrderOpenPrice();
closeTime = OrderCloseTime();
closePrice = OrderClosePrice();
FileSeek(fileHandle, 0, SEEK_END);// ファイル末尾へポインタ変更
// 日付形式をYY.MM.DD→YY/MM/DDへ変換
string strOpenTime = TimeToStr(openTime);
string strCloseTime = TimeToStr(closeTime);
StringReplace( strOpenTime,".","/");
StringReplace( strCloseTime,".","/");
// オーダータイプを文字列に変換
string type = "SELL";
if( OrderType() == OP_BUY ) type = "BUY";
// CSV1件出力
FileWrite(fileHandle,
OrderTicket(),
strOpenTime,
strCloseTime,
OrderSymbol(),
type,
openPrice,
closePrice,
OrderLots(),
OrderSwap(),
OrderCommission(),
OrderProfit(),
OrderComment()
);
}
}
}
FileClose(fileHandle);
MessageBox("ExportCSV Success!");
}
//+------------------------------------------------------------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment