Skip to content

Instantly share code, notes, and snippets.

@roveltwoo
Created July 8, 2018 05:10
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/629a0f07d5b24a8d3b167421f7351067 to your computer and use it in GitHub Desktop.
Save roveltwoo/629a0f07d5b24a8d3b167421f7351067 to your computer and use it in GitHub Desktop.
ExportHistoryCSVkai source
//+------------------------------------------------------------------+
//| ExportHistoryCSVkai.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 curShift;
double High24 = 0.0;
double Low24 = 999.9;
double High8 = 0.0;
double Low8 = 999.9;
double HighE = 0.0;
double LowE = 999.9;
double Ave24 = 0.0;
double Ave8 = 0.0;
double AveTotal24 = 0.0;
double AveTotal8 = 0.0;
int hstTotal=OrdersHistoryTotal();
if( hstTotal > 0){
string str = TimeToStr(TimeLocal(),TIME_DATE|TIME_SECONDS);
StringReplace( str,".","");
StringReplace( str,":","");
StringReplace( str," ","");
string fileName = StringConcatenate("ExportHistory_",str,".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",
"EntryHigh",
"EntryLow",
"24h High",
"24h Low",
"8h High",
"8h Low",
"8h Ave",
"24h Ave"
);
for(i=0;i<hstTotal;i++)
{
High24 = 0.0;
Low24 = 999.9;
High8 = 0.0;
Low8 = 999.9;
HighE = 0.0;
LowE = 999.9;
Ave24 = 0.0;
Ave8 = 0.0;
AveTotal24 = 0.0;
AveTotal8 = 0.0;
//---- check selection result
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true){
if(OrderType() == OP_BUY || OrderType() == OP_SELL){
openTime = OrderOpenTime();
openPrice = OrderOpenPrice();
closeTime = OrderCloseTime();
closePrice = OrderClosePrice();
// 決済後24H平均値(5足288本分の終値平均値)
// 決済後8H平均値(5足96本分の終値平均値)
// 決済後24H最高値、最安値
// 決済後8H最高値、最安値
// エントリー中最高値、最安値
int shiftOp5 = iBarShift(OrderSymbol(),PERIOD_M5,openTime,true);
int shiftCl5 = iBarShift(OrderSymbol(),PERIOD_M5,closeTime,true);
if( shiftCl5 > 288 ){
// クローズしてから1日以上経過しているものが対象
for(curShift = shiftCl5-1;curShift >= shiftCl5 - 288; curShift--){
if( curShift >= shiftCl5 - 96 ){
// クローズしてから8時間
if( High8 < iHigh(OrderSymbol(),PERIOD_M5,curShift) ){
High8 = iHigh(OrderSymbol(),PERIOD_M5,curShift);
}
if( Low8 > iLow(OrderSymbol(),PERIOD_M5,curShift) ){
Low8 = iLow(OrderSymbol(),PERIOD_M5,curShift);
}
AveTotal8 += iClose(OrderSymbol(),PERIOD_M5,curShift);
}
// クローズしてから24時間
if( High24 < iHigh(OrderSymbol(),PERIOD_M5,curShift) ){
High24 = iHigh(OrderSymbol(),PERIOD_M5,curShift);
}
if( Low24 > iLow(OrderSymbol(),PERIOD_M5,curShift) ){
Low24 = iLow(OrderSymbol(),PERIOD_M5,curShift);
}
AveTotal24 += iClose(OrderSymbol(),PERIOD_M5,curShift);
}
Ave24 = AveTotal24/288.0;// クローズ後24時間平均終値
Ave8 = AveTotal8/96.0;// クローズ後8時間平均終値
}
int shiftOp1 = iBarShift(OrderSymbol(),PERIOD_M1,openTime,true);
int shiftCl1 = iBarShift(OrderSymbol(),PERIOD_M1,closeTime,true);
// エントリー中の最高値、最安値
for(curShift = shiftOp1;curShift >= shiftCl1; curShift--){
if( HighE < iHigh(OrderSymbol(),PERIOD_M1,curShift) ){
HighE = iHigh(OrderSymbol(),PERIOD_M1,curShift);
}
if( LowE > iLow(OrderSymbol(),PERIOD_M1,curShift) ){
LowE = iLow(OrderSymbol(),PERIOD_M1,curShift);
}
}
FileSeek(fileHandle, 0, SEEK_END);// 末尾
string strOpenTime = TimeToStr(openTime);
string strCloseTime = TimeToStr(closeTime);
StringReplace( strOpenTime,".","/");
StringReplace( strCloseTime,".","/");
string type = "SELL";
if( OrderType() == OP_BUY ) type = "BUY";
FileWrite(fileHandle,
OrderTicket(),
strOpenTime,
strCloseTime,
OrderSymbol(),
type,
openPrice,
closePrice,
OrderLots(),
OrderSwap(),
OrderCommission(),
OrderProfit(),
OrderComment(),
HighE,
LowE,
High24,
Low24,
High8,
Low8,
Ave24,
Ave8
);
}
}
}
FileClose(fileHandle);
MessageBox("ExportCSV Success!");
}
//+------------------------------------------------------------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment