#include "Backtester.mqh" #define MAX_DATETIME 32535244799 class TrainingTestBacktester : public Backtester { protected: datetime testDatetime; double grossTestWinArray[], grossTestLossArray[]; //Amount int totalTestWinsArray[], totalTestLosesArray[]; //Number virtual void AddProfits(double amount); public: TrainingTestBacktester(double trade_value, string &symbolsArray[], bool pullback, bool one_candle, bool bridge_tf, bool scale_out, bool draw_arrows=true, bool debug_trades=false, bool debug_virtual_trades=false, bool result_in_pips=false, bool use_main_exit=false, datetime testDate=MAX_DATETIME); ~TrainingTestBacktester(); virtual double TesterResult(int optimization_mode, int write_to_file); }; TrainingTestBacktester::TrainingTestBacktester(double trade_value,string &symbolsArray[],bool pullback,bool one_candle,bool bridge_tf,bool scale_out,bool draw_arrows=true,bool debug_trades=false,bool debug_virtual_trades=false,bool result_in_pips=false,bool use_main_exit=false,datetime testDate=MAX_DATETIME) :Backtester(trade_value, symbolsArray, pullback, one_candle, bridge_tf, scale_out, draw_arrows, debug_trades, debug_virtual_trades, result_in_pips, use_main_exit) { testDatetime = testDate; ArrayResize(grossTestWinArray, totalSymbols); ArrayFill(grossTestWinArray, 0, totalSymbols, 0.0); ArrayResize(grossTestLossArray, totalSymbols); ArrayFill(grossTestLossArray, 0, totalSymbols, 0.0); ArrayResize(totalTestWinsArray, totalSymbols); ArrayFill(totalTestWinsArray, 0, totalSymbols, 0); ArrayResize(totalTestLosesArray, totalSymbols); ArrayFill(totalTestLosesArray, 0, totalSymbols, 0); } TrainingTestBacktester::~TrainingTestBacktester(void) { } void TrainingTestBacktester::AddProfits(double amount) { if (amount>0) { if (TimeCurrent() 0) { minPF = MathMin(minPF, gw/gl); } else { minPF = MathMin(minPF, 1.0 + gw / 10000.0); } } returnValue = MathMin(minPF, pfLimit); break; } case PF_SQ_PER_W: if (finalValue == 0) return -40000; returnValue = MathMin(pfLimit, profitFactor) * (1 + MathLog(1.0+profitFactor)) * finalValue; break; } if (write_to_file == OPTIMIZE) { int filehandle=FileOpen("OPT_DATA.txt",FILE_READ|FILE_WRITE|FILE_TXT); if(filehandle<0) { Print("Failed to open the file by the absolute path "); Print("Error code ",GetLastError()); } if(filehandle!=INVALID_HANDLE) { FileSeek(filehandle,0,SEEK_END); //String fill para nivelar tabulaciones (usar espacios) //Procesar archivo con python string content = DoubleToString(finalValue, 0) + "\t" + DoubleToString(bestMin, 0) + "\t" + DoubleToString(profitFactor, 2) + "\t" + DoubleToString(returnValue, 2) + "\t" + DoubleToString(finalValueTest, 0) + "\t" + DoubleToString(bestMinTest, 0) + "\t" + DoubleToString(profitFactorTest, 2); FileWrite(filehandle, content); FileFlush(filehandle); FileClose(filehandle); } } else if (write_to_file == SUMMARY) { int filehandle=FileOpen("SUMMARY.txt",FILE_WRITE|FILE_TXT); if(filehandle<0) { Print("Failed to open the file by the absolute path "); Print("Error code ",GetLastError()); } if(filehandle!=INVALID_HANDLE) { string toWrite = ""; if (scaleOut) { for (int i = 0; i < totalSymbols; i++) { toWrite = toWrite + DoubleToString(grossWinArray[i]+grossLossArray[i], 0) + "\t"; } for (int i = 0; i < totalSymbols; i++) { if (grossLossArray[i] == 0) { if (grossWinArray[i] == 0) { toWrite = toWrite + DoubleToString(1.0, 2) + "\t"; } else { toWrite = toWrite + DoubleToString(999.99, 2) + "\t"; } } else { toWrite = toWrite + DoubleToString(grossWinArray[i]/(-grossLossArray[i]), 2) + "\t"; } } for (int i = 0; i < totalSymbols; i++) { toWrite = toWrite + DoubleToString(grossTestWinArray[i]+grossTestLossArray[i], 0) + "\t"; } for (int i = 0; i < totalSymbols; i++) { if (grossTestLossArray[i] == 0) { if (grossTestWinArray[i] == 0) { toWrite = toWrite + DoubleToString(1.0, 2) + "\t"; } else { toWrite = toWrite + DoubleToString(999.99, 2) + "\t"; } } else { toWrite = toWrite + DoubleToString(grossTestWinArray[i]/(-grossTestLossArray[i]), 2) + "\t"; } } FileWrite(filehandle, toWrite); FileWrite(filehandle, IntegerToString(numberOfTrades) + "\t" + DoubleToString(profitFactor, 2) + "\t" + IntegerToString(numberOfTradesTest) + "\t" + DoubleToString(profitFactorTest, 2)); } else { for (int i = 0; i < totalSymbols; i++) { toWrite = toWrite + IntegerToString(totalWinsArray[i]) + "\t"; } for (int i = 0; i < totalSymbols; i++) { toWrite = toWrite + IntegerToString(totalLosesArray[i]) + "\t"; } FileWrite(filehandle, toWrite); } FileFlush(filehandle); FileClose(filehandle); } } return returnValue; }