Created
August 16, 2016 11:44
-
-
Save darden1/1b916d9f9b6f6866471f227cd2648227 to your computer and use it in GitHub Desktop.
DecisionTree.mq4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//+------------------------------------------------------------------+ | |
//| DecisionTree.mq4 | | |
//| Copyright 2016, MetaQuotes Software Corp. | | |
//| https://www.mql5.com | | |
//+------------------------------------------------------------------+ | |
#property copyright "Copyright 2016, MetaQuotes Software Corp." | |
#property link "https://www.mql5.com" | |
#property version "1.00" | |
//#property strict | |
#define MAGIC 20160806 | |
#include <OTMql4/OTLibPyLog.mqh> | |
#include <OTMql4/OTLibPy27.mqh> | |
#include <WinUser32.mqh> | |
#include <OTMql4/OTLibLog.mqh> | |
extern int theNumberOfTrainData = 29; | |
extern int theNumberOfTrainAndKyoushiSet = 26; | |
extern int studyTrialTimes = 200; | |
extern int Slippage=2; | |
extern double Lots=1; | |
extern string sStdOutFile="../../Logs/_DecisionTree.txt"; | |
void vPanic(string uReason) { | |
//"A panic prints an error message and then aborts"; | |
vError("PANIC: " + uReason); | |
MessageBox(uReason, "PANIC!", MB_OK|MB_ICONEXCLAMATION); | |
ExpertRemove(); | |
} | |
//+------------------------------------------------------------------+ | |
// BUYポジションをクローズする | |
//+------------------------------------------------------------------+ | |
void Close_Buy_Positions() | |
{ | |
int res; | |
for(int i=OrdersTotal()-1; i>=0; i--) //オ-ダ-番号は0からOrdersTotal()-1まである | |
{ | |
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)continue; | |
if(OrderMagicNumber()!=MAGIC || OrderSymbol()!=Symbol())continue; | |
if(OrderType()==OP_BUY) | |
{ | |
res=OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,OrangeRed); | |
continue; | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ | |
// SELLポジションをクローズする | |
//+------------------------------------------------------------------+ | |
void Close_Sell_Positions() | |
{ | |
int res; | |
for(int i=OrdersTotal()-1; i>=0; i--) //オ-ダ-番号は0からOrdersTotal()-1まである | |
{ | |
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)continue; | |
if(OrderMagicNumber()!=MAGIC || OrderSymbol()!=Symbol())continue; | |
if(OrderType()==OP_SELL) | |
{ | |
res=OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,OrangeRed); | |
continue; | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ | |
// BUYポジションをカウントする | |
//+------------------------------------------------------------------+ | |
int Count_Buy_Positions() | |
{ | |
int buyPositionNumber=0; | |
for(int i=OrdersTotal()-1; i>=0; i--) //オ-ダ-番号は0からOrdersTotal()-1まである | |
{ | |
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)continue; | |
if(OrderMagicNumber()!=MAGIC || OrderSymbol()!=Symbol())continue; | |
if(OrderType()==OP_BUY) | |
{ | |
buyPositionNumber++; | |
continue; | |
} | |
} | |
return (buyPositionNumber); | |
} | |
//+------------------------------------------------------------------+ | |
//| SELLポジションをカウント | |
//+------------------------------------------------------------------+ | |
int Count_Sell_Positions() | |
{ | |
int sellPositionNumber=0; | |
for(int i=OrdersTotal()-1; i>=0; i--) //オ-ダ-番号は0からOrdersTotal()-1まである | |
{ | |
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)continue; | |
if(OrderMagicNumber()!=MAGIC || OrderSymbol()!=Symbol())continue; | |
if(OrderType()==OP_SELL) | |
{ | |
sellPositionNumber++; | |
continue; | |
} | |
} | |
return (sellPositionNumber); | |
} | |
//+------------------------------------------------------------------+ | |
//| Expert initialization function | | |
//+------------------------------------------------------------------+ | |
int OnInit() | |
{ | |
//--- | |
int iRetval; | |
string uArg, uRetval; | |
iRetval = iPyInit(sStdOutFile); | |
if (iRetval != 0) { | |
return(iRetval); | |
} | |
Print("Called iPyInit"); | |
//--- | |
return(INIT_SUCCEEDED); | |
} | |
//+------------------------------------------------------------------+ | |
//| Expert deinitialization function | | |
//+------------------------------------------------------------------+ | |
void OnDeinit(const int reason) | |
{ | |
//--- | |
vPyDeInit(); | |
} | |
//+------------------------------------------------------------------+ | |
//| Expert tick function | | |
//+------------------------------------------------------------------+ | |
void OnTick() | |
{ | |
//--- | |
if(Volume[0]>1 || IsTradeAllowed()==false) return; | |
int res; | |
int maxbars; | |
double upOrDownRatio; | |
double data[]; | |
maxbars=theNumberOfTrainData+theNumberOfTrainAndKyoushiSet; | |
ArrayResize(data, maxbars); | |
for (int i=0; i < maxbars; i++){ | |
data[i] = (Open[i] - Open[i + 1]) / Open[i + 1]; | |
} | |
iPySafeExec("import numpy as np"); | |
iPySafeExec("from sklearn import tree"); | |
iPySafeExec("data = []"); | |
iPyListAppendDouble("data", data); | |
iPySafeExec("theNumberOfTrainData = "+theNumberOfTrainData); | |
iPySafeExec("theNumberOfTrainAndKyoushiSet = " + theNumberOfTrainAndKyoushiSet); | |
iPySafeExec("studyTrialTimes = " + studyTrialTimes); | |
iPySafeExec("train_X = []"); | |
iPySafeExec("train_y = []"); | |
iPySafeExec("for i in range(0, theNumberOfTrainAndKyoushiSet):\n train_X.append(data[1 + i:theNumberOfTrainData + 1 + i])\n train_y.append(int(0 < data[i]))"); | |
iPySafeExec("X = [data[0:theNumberOfTrainData]]"); | |
iPySafeExec("y_PredictionTommorow = []"); | |
iPySafeExec("for i in range(0, studyTrialTimes):\n clf = tree.DecisionTreeClassifier()\n clf.fit(train_X, train_y)\n y_PredictionTommorow.append(clf.predict(X))"); | |
iPySafeExec("upOrDownRatio = float(sum(y_PredictionTommorow) * 1.0 / len(y_PredictionTommorow))"); | |
upOrDownRatio=fPyEvalDouble("upOrDownRatio"); | |
Print("upOrDownRatio:" + upOrDownRatio); | |
if(upOrDownRatio>=0.5)//上昇シグナル発生時 | |
{ | |
if(Count_Buy_Positions()==0) | |
{ | |
res=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"",MAGIC,0,Blue); | |
} | |
} | |
else//下降シグナル発生時 | |
{ | |
Close_Sell_Positions(); | |
Close_Buy_Positions(); | |
//res=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"",MAGIC,0,Red); | |
} | |
} | |
//+------------------------------------------------------------------+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment