Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.