MnQInvestmentDevelopment/MnQInvestment/00_StrategyswithSimon/Moneymakerv3.mq5

540 lines
19 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 15:08:44 +02:00
#include <trade/trade.mqh>
// DECLARE VARIABELES
input double Lots = 0.1;
input double Desired_Profit = 0.2;
input string Opentime = "22:55:30";
input string ClosTime = "03:35";
input double Difference_buy = 0.01;
input double Difference_sell = 0.01;
input double Difference_on_targetprice_SLbuy = 0.01;
input double Difference_on_targetprice_SLsell = 0.01;
input double Difference_on_targetprice_TPbuy = 0.02;
input double Difference_on_targetprice_TPsell = 0.02;
input bool Spread_Average = true;
ulong posTicket;
ulong posTicketBuy;
ulong posTicketSell;
int countalltrades;
int countSStrades;
int countBStrades;
bool OrderdeletableBuy;
bool OrderdeletableSell;
double accountbalancebeginning;
datetime expirytime;
double pricebuy;
double pricesell;
double askit;
double bidit;
double valuespreadav;
ulong lastdeletedorderbuy;
ulong lastdeletedordersell;
datetime lastSignal;
datetime DateTestStart;
datetime DateTestEnd;
input int SameDay0NextDay1etc = 0;
input string DocumentationPath = "";
CTrade trade;
bool Dayyycheck;
//---------------------------------------------------------------------
int OnInit(){
datetime DateTestStart = TimeCurrent();
accountbalancebeginning = AccountInfoDouble(ACCOUNT_BALANCE);
return(INIT_SUCCEEDED);
}
// IMPORTANT FUNCTION
void OnTick()
{
// CONVERT INPUT TIME, UPDATE IT TO CURRENT DAY
datetime opentime = StringToTime(Opentime);
datetime closetime = StringToTime(ClosTime);
datetime expirytime = closetime + (SameDay0NextDay1etc * 24 * 60 * 60);
double accountbalance = AccountInfoDouble(ACCOUNT_BALANCE);
double accountprofit = AccountInfoDouble(ACCOUNT_PROFIT);
double accountequity = AccountInfoDouble(ACCOUNT_EQUITY);
// ---- EXECUTE TRADES --- OPENING TRADE ON TIME
if(lastSignal != opentime && TimeCurrent() > opentime)
{
lastSignal = opentime;
//CALCULATING TRADE ORDER VALUES
if(Spread_Average == true)
{
askit = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
bidit = SymbolInfoDouble(_Symbol,SYMBOL_BID);
valuespreadav = askit - ((askit - bidit)/2);
askit = valuespreadav;
bidit = valuespreadav;
}
else
{
askit = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
bidit = SymbolInfoDouble(_Symbol,SYMBOL_BID);
}
askit = NormalizeDouble(askit,_Digits);
bidit = NormalizeDouble(bidit,_Digits);
pricebuy = askit + Difference_buy;
pricebuy = NormalizeDouble(pricebuy,_Digits);
pricesell = bidit - Difference_sell;
pricesell = NormalizeDouble(pricesell,_Digits);
double stoplossbuy = askit + Difference_buy - Difference_on_targetprice_SLbuy;
stoplossbuy = NormalizeDouble(stoplossbuy,_Digits);
double stoplosssell = bidit - Difference_sell + Difference_on_targetprice_SLsell;
stoplosssell = NormalizeDouble(stoplosssell,_Digits);
double takepbuy = askit + Difference_buy + Difference_on_targetprice_TPbuy;
takepbuy = NormalizeDouble(takepbuy,_Digits);
double takepsell = bidit - Difference_sell - Difference_on_targetprice_TPsell;
takepsell = NormalizeDouble(takepsell,_Digits);
//DYNAMIC CALCULATION OF LOT SIZE
//double lotsizebuy = (accountbalance * 0.2) / 146;
//double lotsizesell = (accountbalance * 0.2) / 146;
//DYNAMIC CALCULATION OF LOT SIZE V2
//double lotsizegeneral = (accountbalance * Desired_Profit) / 146;
//lotsizegeneral = NormalizeDouble(lotsizegeneral,1);
//CALCULATION OF EXPIREIY DATe
Print("-----------------------------------------------------------------------------------"
"\nMoin Moin! Es ist Zeit Geld zu verdienen... Kurswert heute Abend:"
"\n ASK: ",askit,
"\n BID: ",bidit);
// EXECUTE BUY
//ENDLAGER: D'2023.01.30 00:30:00'
if(trade.BuyStop(100.0,pricebuy,_Symbol,stoplossbuy,takepbuy,ORDER_TIME_SPECIFIED, expirytime,"Evening Buytrade"))
{
posTicketBuy = trade.ResultOrder();
OrderdeletableBuy = true;
}
// EXECUTE SELL
if(trade.SellStop(100.0,pricesell,_Symbol,stoplosssell,takepsell,ORDER_TIME_SPECIFIED, expirytime,"Evening Selltrade"))
{
posTicketSell = trade.ResultOrder();
OrderdeletableSell = true;
}
}
//--- CLOSING TRADES --- ON CONDITION
if (PositionsTotal() > 0)
{
ulong ExecutedOrder = PositionGetTicket(0);
if(ExecutedOrder == posTicketBuy && posTicketSell != lastdeletedordersell)
{
trade.OrderDelete(posTicketSell);
lastdeletedordersell = posTicketSell;
countBStrades = countBStrades + 1;
countalltrades = countBStrades + countSStrades;
}
else if(ExecutedOrder == posTicketSell && posTicketBuy != lastdeletedorderbuy)
{
trade.OrderDelete(posTicketBuy);
lastdeletedorderbuy = posTicketBuy;
countSStrades = countSStrades+ 1;
countalltrades = countBStrades + countSStrades;
}
}
accountprofit = accountequity - accountbalancebeginning;
accountprofit = NormalizeDouble(accountprofit,2);
// DISPLAY OF VALUES ON CHART
Comment("\nServertime: ", TimeCurrent(),
"\nOpentime: ", opentime,
"\nClosetime: ", closetime,
"\nCurrenttime: ",TimeCurrent(),
"\nProfit: ",accountprofit,
"\nEquity: ",accountequity);
}//close on Tick
void OnDeinit(const int reason)
{
datetime DateTestEnd = TimeCurrent();
HistorySelect(0,TimeCurrent());
double AllDealCounter = HistoryDealsTotal();
Print("ALL DEALS:",AllDealCounter);
double AllRealDealCounter = 0;
double AllCountWins = 0;
double AllCountLosses = 0;
double AllWinRatio = 0;
double AllAverageProfit = 0;
double AllTotalProfit = 0;
double AllAverageComission = 0;
double AllTotalComission = 0;
double AllAverageFee = 0;
double AllTotalFee = 0;
datetime AllDealTimeTotal = 0;
datetime AllDealTimeAverage = 0;
double AllAverageProfitNet = 0;
double AllTotalProfitNet = 0;
double BuyStDealCounter = 0;
double BuyStCountWins = 0;
double BuyStCountLosses = 0;
double BuyStWinRatio = 0;
double BuyStAverageProfit = 0;
double BuyStTotalProfit = 0;
datetime BuyStTotalDealTime = 0;
datetime BuyStAverageDealTime = 0;
double BuyStTotalComission = 0;
double BuyStTotalFee = 0;
double SellStDealCounter = 0;
double SellStCountWins = 0;
double SellStCountLosses = 0;
double SellStWinRatio = 0;
double SellStAverageProfit = 0;
double SellStTotalProfit = 0;
datetime SellStTotalDealTime = 0;
datetime SellStAverageDealTime = 0;
double SellStTotalComission = 0;
double SellStTotalFee = 0;
ulong Ticketnumber = 0;
long DealType = 0;
long DealEntry = 0;
datetime DealTime = 0;
datetime DealTimeStart = 0;
datetime DealTimeEnd = 0;
datetime DealTimeSpan = 0;
double DealProfit = 0;
double DealComission = 0;
double DealFee = 0;
string CurrentSymbol = "";
// --- TRADES STATISTICAL CALULATION ---
for (double i = 0; i < AllDealCounter; i++)
{
ulong Ticketnumber = HistoryDealGetTicket(i);
if (Ticketnumber != 0)
{
DealType = HistoryDealGetInteger(Ticketnumber, DEAL_TYPE);
DealEntry = HistoryDealGetInteger(Ticketnumber, DEAL_ENTRY);
DealTime = HistoryDealGetInteger(Ticketnumber, DEAL_TIME);
DealProfit = HistoryDealGetDouble(Ticketnumber, DEAL_PROFIT);
DealComission = HistoryDealGetDouble(Ticketnumber, DEAL_COMMISSION);
DealFee = HistoryDealGetDouble(Ticketnumber, DEAL_FEE);
CurrentSymbol = HistoryDealGetString(Ticketnumber, DEAL_SYMBOL);
if(CurrentSymbol == _Symbol)
{
// --- BUYSTOP STATISTIC CALCULATION ---
if(DealEntry == DEAL_ENTRY_OUT && DealType == DEAL_TYPE_SELL)
{
AllRealDealCounter = AllRealDealCounter + 1;
BuyStDealCounter = BuyStDealCounter + 1;
AllTotalComission = AllTotalComission + DealComission;
AllTotalFee = AllTotalFee + DealFee;
BuyStTotalProfit = BuyStTotalProfit + DealProfit;
//Calculate the Time the Position was running
// !!! Only works if only one Poistion is opend at a time !!!
DealTimeEnd = DealTime;
DealTimeStart = HistoryDealGetInteger(HistoryDealGetTicket(i-1), DEAL_TIME);
DealTimeSpan = DealTimeEnd - DealTimeStart;
BuyStTotalDealTime = BuyStTotalDealTime + DealTimeSpan;
BuyStTotalComission = BuyStTotalComission + DealComission;
BuyStTotalFee = BuyStTotalFee + DealFee;
if(DealProfit > 0)
{
BuyStCountWins = BuyStCountWins + 1;
}
else if (DealProfit < 0)
{
BuyStCountLosses = BuyStCountLosses + 1;
}
}//end Buystop
// --- SELLSTOP STATISTIC CALCULATION ---
if(DealEntry == DEAL_ENTRY_OUT && DealType == DEAL_TYPE_BUY)
{
AllRealDealCounter = AllRealDealCounter + 1;
SellStDealCounter = SellStDealCounter + 1;
AllTotalComission = AllTotalComission + DealComission;
AllTotalFee = AllTotalFee + DealFee;
SellStTotalProfit = SellStTotalProfit + DealProfit;
//Calculate the Time the Position was running
// !!! Only works if only one Poistion is opend at a time !!!
DealTimeEnd = DealTime;
DealTimeStart = HistoryDealGetInteger(HistoryDealGetTicket(i-1), DEAL_TIME);
DealTimeSpan = DealTimeEnd - DealTimeStart;
SellStTotalDealTime = SellStTotalDealTime + DealTimeSpan;
SellStTotalComission = SellStTotalComission + DealComission;
SellStTotalFee = SellStTotalFee + DealFee;
if(DealProfit > 0)
{
SellStCountWins = SellStCountWins + 1;
}
else if (DealProfit < 0)
{
SellStCountLosses = SellStCountLosses + 1;
}
}//end Sellstop
}//end if Symbol
}//end if Ticket exists
}//end for Durchlauf Trades
// profit
AllTotalProfit = BuyStTotalProfit + SellStTotalProfit;
AllAverageProfit = AllTotalProfit / AllRealDealCounter;
BuyStAverageProfit = BuyStTotalProfit / BuyStDealCounter;
SellStAverageProfit = SellStTotalProfit / SellStDealCounter;
//winning and losing count
AllCountWins = BuyStCountWins + SellStCountWins;
AllCountLosses = BuyStCountLosses + SellStCountLosses;
AllWinRatio = AllCountWins / AllRealDealCounter;
BuyStWinRatio = BuyStCountWins / BuyStDealCounter;
SellStWinRatio = SellStCountWins / SellStDealCounter;
// deal time
BuyStAverageDealTime = BuyStTotalDealTime / BuyStDealCounter;
SellStAverageDealTime = SellStTotalDealTime / SellStDealCounter;
AllDealTimeTotal = BuyStTotalDealTime + SellStTotalDealTime;
AllDealTimeAverage = AllDealTimeTotal / AllRealDealCounter;
//fees & comission
AllTotalComission = BuyStTotalComission + SellStTotalComission;
AllAverageComission = AllTotalComission / AllRealDealCounter;
AllTotalFee = BuyStTotalFee + SellStTotalFee;
AllAverageFee = AllTotalFee / AllRealDealCounter;
AllTotalProfitNet = AllTotalProfit - AllTotalComission - AllTotalFee;
AllAverageProfitNet = AllAverageProfit - AllAverageComission - AllAverageFee;
Print
(
"\n",
"\n",
"\n _______________GENERAL INFO_______________",
"\n AllTotalProfit:___________________________", NormalizeDouble(AllTotalProfit,2),
"\n AllAverageProfit (per Trade):_____________", NormalizeDouble(AllAverageProfit,2),
"\n AllCountWins:_____________________________", AllCountWins,
"\n AllCountLosses:___________________________", AllCountLosses,
"\n AllWinRatio:______________________________", NormalizeDouble(AllWinRatio,2),
"\n AllDealTimeAverage (per Trade):___________", TimeToString(AllDealTimeAverage, TIME_SECONDS),
"\n AllTotalComission:________________________", NormalizeDouble(AllTotalComission,2),
"\n AllAverageComission (per Trade):__________", NormalizeDouble(AllAverageComission,2),
"\n AllTotalFee:______________________________", NormalizeDouble(AllTotalFee,2),
"\n AllAverageFee (per Trade):________________", NormalizeDouble(AllAverageFee,2),
"\n",
"\n _________________NET PROFIT_______________",
"\n AllTotalProfitNet:________________________", NormalizeDouble(AllTotalProfitNet,2),
"\n AllAverageProfitNet:______________________", NormalizeDouble(AllAverageProfitNet,2),
"\n",
"\n ________BUY Stop Statistics(Brutto)_______",
"\n BuyStTotalProfit:_________________________", NormalizeDouble(BuyStTotalProfit,2),
"\n BuyStAverageProfit:_______________________", NormalizeDouble(BuyStAverageProfit,2),
"\n BuyStDealCounter:_________________________", BuyStDealCounter,
"\n BuyStCountWins:___________________________", BuyStCountWins,
"\n BuyStWinRatio:____________________________", NormalizeDouble(BuyStWinRatio,2),
"\n BuyStAverageDealTime:_____________________", TimeToString(BuyStAverageDealTime, TIME_SECONDS),
"\n",
"\n _______SELL Stop Statistics(Brutto)_______",
"\n SellStTotalProfit:________________________", NormalizeDouble(SellStTotalProfit,2),
"\n SellStAverageProfit:______________________", NormalizeDouble(SellStAverageProfit,2),
"\n SellStDealCounter:________________________", SellStDealCounter,
"\n SellStCountWins:__________________________", SellStCountWins,
"\n SellStWinRatio:___________________________", NormalizeDouble(SellStWinRatio,2),
"\n SellStAverageDealTime:____________________", TimeToString(SellStAverageDealTime, TIME_SECONDS),
"\n",
"\n",
"\n"
);
//--- CREATION OF DOCUMENTATION ---
string fileName = "MoneymakerDocumentation.csv";
string WholePath;
StringConcatenate(WholePath, DocumentationPath, fileName);
string realfilename;
int fileHandle1 = FileFindFirst(fileName,realfilename,FILE_COMMON);
int fileHandle = FileOpen(fileHandle1, FILE_READ | FILE_WRITE | FILE_CSV | FILE_UNICODE | FILE_COMMON);
Print("TestHandle: ",fileHandle,"and WholePath",WholePath);
if (fileHandle != INVALID_HANDLE)
{
//check if file already exists
string FirstHeader = FileReadString(fileHandle);
string ExpectedHeader = "Symbol";
bool HasHeaders = (StringFind(FirstHeader,ExpectedHeader,0) == 0);
if (HasHeaders)
{
FileSeek(fileHandle,0,SEEK_END);
FileWrite(fileHandle, _Symbol, TimeCurrent(), DateTestStart, DateTestEnd, Opentime, ClosTime,
Difference_buy, Difference_on_targetprice_TPbuy,Difference_on_targetprice_SLbuy,
Difference_sell,Difference_on_targetprice_TPsell,Difference_on_targetprice_SLsell,
AllTotalProfit,AllAverageProfit,AllCountWins,AllCountLosses, AllWinRatio,AllDealTimeAverage,
AllTotalComission,AllAverageComission, AllTotalFee, AllAverageFee,
AllTotalProfitNet, AllAverageProfitNet,
BuyStTotalProfit, BuyStAverageProfit, BuyStDealCounter, BuyStCountWins, BuyStWinRatio, BuyStAverageDealTime,
SellStTotalProfit, SellStAverageProfit, SellStDealCounter, SellStCountWins, SellStWinRatio, SellStAverageDealTime);
FileClose(fileHandle);
}//end if file modification
//if not same execution but with creation of headers
else
{
FileWrite(fileHandle, "Symbol", "Date Created", "DateTestStart", "DateTestEnd","Opening Time","Expiry Time",
"BuyTriggerPriceDifference","BuyTPDifferencetoTrigger","BuySLDifferencetoTrigger",
"SellTriggerPriceDifference","SellTPDifferencetoTrigger","SellSLDifferencetoTrigger",
"AllTotalProfit","AllAverageProfit","AllCountWins","AllCountLosses","AllWinRatio","AllDealTimeAverage (per Trade)",
"AllTotalComission","AllAverageComission (per Trade)", "AllTotalFee","AllAverageFee (per Trade)",
"AllTotalProfitNet","AllAverageProfitNet",
"BuyStTotalProfit","BuyStAverageProfit","BuyStDealCounter","BuyStCountWins","BuyStWinRatio","BuyStAverageDealTime",
"SellStTotalProfit","SellStAverageProfit","SellStDealCounter","SellStCountWins","SellStWinRatio","SellStAverageDealTime");
FileSeek(fileHandle,0,SEEK_END);
FileWrite(fileHandle, _Symbol, TimeLocal(),DateTestStart, DateTestEnd, Opentime, ClosTime,
Difference_buy, Difference_on_targetprice_TPbuy,Difference_on_targetprice_SLbuy,
Difference_sell,Difference_on_targetprice_TPsell,Difference_on_targetprice_SLsell,
AllTotalProfit,AllAverageProfit,AllCountWins,AllCountLosses, AllWinRatio,AllDealTimeAverage,
AllTotalComission,AllAverageComission, AllTotalFee, AllAverageFee,
AllTotalProfitNet, AllAverageProfitNet,
BuyStTotalProfit, BuyStAverageProfit, BuyStDealCounter, BuyStCountWins, BuyStWinRatio, BuyStAverageDealTime,
SellStTotalProfit, SellStAverageProfit, SellStDealCounter, SellStCountWins, SellStWinRatio, SellStAverageDealTime);
FileClose(fileHandle);
}//end else header creation and file modification
Print("--- ATTENTION --- Statistical Data Saved --- ATTENTION ---",
"\n File Path: ", TerminalInfoString(TERMINAL_DATA_PATH));
}//end if file modification
else
{
Print("--- ATTENTION --- Unable to save Statistical Data --- ATTENTION ---");
}
}//end on deinit