MnQInvestmentDevelopment/MnQInvestment/01_Amadeus/AMESTEROIDEUS-5.mq5

299 lines
13 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 15:08:44 +02:00
#property copyright "Copyright 2023, M & Q Investment Group"
#property link "https://www.mql5.com"
#property version "1.00"
#include <trade/trade.mqh>
CTrade trade;
//-------------------------------------------OVEROPTIMIZED VERSION 5 AFTER 2. TESTING -------------
//Added Dynamic Lotsizing
input double TrigPDif = 0.00005;
input double DesiredProfitPerc = 20;
input int ExpiryTime = 120;
input double SLtoTPRatio = 1;
input double RuleMinimumTPSLValue = 0.0001;
input double PufferDistance = 0.0001;
input double WhiteYellowProtRange = 0.00001;
input bool SWhiteProtection = true;
input double StaticLots = 1.0;
input bool DynamicLotsCalc = false;
input bool CondCandleLowtrueClosefalse = false;
input bool CondCountCandle1true2false = false;
//AUSSTEHEND input double TPSLFactor = 1.0;
input string Opentime = "04:30:00";
input string CloseTime = "23:45:";
long TickVolumes[];
double bid0 = 0;
double accountbalancebeginning;
double accountprofit;
double Lostrades = 0;
double Wintrades = 0;
double AllWinRatio = 0;
int WintradesCounter = 0;
int LostradesCounter = 0;
int NeutralDealCounter = 0;
int AlltradeCounter = 0;
double TotalComission = 0;
double TotalFees = 0;
ulong LastTicketNumber = 0;
int OnInit(){
accountbalancebeginning = AccountInfoDouble(ACCOUNT_BALANCE);
//OrderinAction = false;
return(INIT_SUCCEEDED);
}
void OnTick(){
//DETERMINING TIMEFRAME
datetime opentimenow = StringToTime(Opentime);
datetime closetimenow = StringToTime(CloseTime);
datetime currenttime = TimeCurrent();
if(currenttime > opentimenow && currenttime < closetimenow){
//GET YELLWOW AMA VALUES
double YellAMAPriceArray[];
int YellAMADefinition = iAMA (_Symbol,_Period,2,5,1,0,PRICE_CLOSE);
ArraySetAsSeries(YellAMAPriceArray,true);
//AMA DEF, one line, current candle, 3 candles, store result
CopyBuffer(YellAMADefinition,0,0,10,YellAMAPriceArray);
double YellAMACurrentValue = NormalizeDouble(YellAMAPriceArray[0],6);
//GET WHITE AMA VALUES
double WhiteAMAPriceArray[];
int WhiteAMADefinition = iAMA (_Symbol,_Period,2,5,1,1,PRICE_CLOSE);
ArraySetAsSeries(WhiteAMAPriceArray,true);
//AMA DEF, one line, current candle, 3 candles, store result
CopyBuffer(WhiteAMADefinition,0,0,10,WhiteAMAPriceArray);
double WhiteAMACurrentValue = NormalizeDouble(WhiteAMAPriceArray[0],6);
/*/GET SWHITE AMA VALUES
double SWhiteAMAPriceArray[];
int SWhiteAMADefinition = iAMA (_Symbol,_Period,2,5,1,2,PRICE_CLOSE);
ArraySetAsSeries(SWhiteAMAPriceArray,true);
//AMA DEF, one line, current candle, 3 candles, store result
CopyBuffer(SWhiteAMADefinition,0,0,10,SWhiteAMAPriceArray);
double SWhiteAMACurrentValue = NormalizeDouble(SWhiteAMAPriceArray[0],6);*/
//GET RED AMA VALUES
double RedAMAPriceArray[];
int RedAMADefinition = iAMA (_Symbol,_Period,5,2,30,0,PRICE_CLOSE);
ArraySetAsSeries(RedAMAPriceArray,true);
//AMA DEF, one line, current candle, 3 candles, store result
CopyBuffer(RedAMADefinition,0,0,10,RedAMAPriceArray);
double RedAMACurrentValue = NormalizeDouble(RedAMAPriceArray[0],6);
//GET ORANGE AMA VALUES
double OrangeAMAPriceArray[];
int OrangeAMADefinition = iAMA (_Symbol,_Period,5,2,30,1,PRICE_CLOSE);
ArraySetAsSeries(OrangeAMAPriceArray,true);
//AMA DEF, one line, current candle, 3 candles, store result
CopyBuffer(OrangeAMADefinition,0,0,10,OrangeAMAPriceArray);
double OrangeAMACurrentValue = NormalizeDouble(OrangeAMAPriceArray[0],6);
//SET THE ORDER
bid0 = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
double PufferPrice = RedAMACurrentValue + PufferDistance;
double InvalidStopProhibitorline = RuleMinimumTPSLValue + PufferDistance + RedAMACurrentValue;
double iLow1 = iLow(_Symbol,PERIOD_CURRENT,1);
double iLow2 = iLow(_Symbol,PERIOD_CURRENT,2);
double iClose1 = iClose(_Symbol,PERIOD_CURRENT,1);
double iClose2 = iClose(_Symbol,PERIOD_CURRENT,2);
double TrigP = bid0 - TrigPDif;
double TP = RedAMACurrentValue;
//SL wurde in Googleversion und V4 falsch berechnet (mit dem Bid0 Preis). Anpassung zu TriggerP vorgenommen
double SL = TrigP + (TrigP - RedAMACurrentValue) * SLtoTPRatio;
datetime OrderExpTime = TimeCurrent() + ExpiryTime;
//if(SWhiteProtection == false){SWhiteAMACurrentValue = 5;}
//MODELLING OF TRADING CONDITIONS FOR OPTIMIZER
if(CondCandleLowtrueClosefalse == false){iLow1 = 100; iLow2 = 100;}
if(CondCandleLowtrueClosefalse == true){iClose1 = 100; iClose2 = 100;}
if(CondCountCandle1true2false == true){iClose2 = 100; iLow2 = 100;}
if( iLow1 > OrangeAMACurrentValue + PufferDistance && iLow2 > OrangeAMACurrentValue + PufferDistance &&
iClose1 > OrangeAMACurrentValue + PufferDistance && iClose2 > OrangeAMACurrentValue + PufferDistance
){
if(WhiteAMACurrentValue - WhiteYellowProtRange > YellAMACurrentValue &&
YellAMACurrentValue > bid0 &&
YellAMACurrentValue > PufferPrice &&
//SWhiteAMACurrentValue > WhiteAMACurrentValue &&
TrigP > InvalidStopProhibitorline &&
PositionsTotal() == 0 &&
OrdersTotal() == 0){
//CALC LOTS
double Lots = 0.01;
if(DynamicLotsCalc){
double SLDistance = SL - TrigP;
Lots = CalcLots(DesiredProfitPerc,SLDistance);
}else{Lots = StaticLots;}
trade.SellStop(Lots,TrigP,_Symbol,SL,TP,ORDER_TIME_SPECIFIED,OrderExpTime);
Print(//"\nPinkAMA-Value ",PinkAMACurrentValue,
"\nYellAMA-Value ",YellAMACurrentValue,
"\nWhiteAMA-Value ",WhiteAMACurrentValue,
"\nRedAMA-Value ",RedAMACurrentValue,
"\nOrangeAMA-Value ",OrangeAMACurrentValue,
"\niLow 1: ",iLow1,
"\niLow2",iLow2,
"\nBid ",bid0);
}
}
/*double accountequity = AccountInfoDouble(ACCOUNT_EQUITY);
accountprofit = accountequity - accountbalancebeginning;
accountprofit = NormalizeDouble(accountprofit,2);*/
/*/LIVE WINRATIO CALCULATION
HistorySelect(0,TimeCurrent());
int AllDealCounter = HistoryDealsTotal();
for (int i = 0; i < AllDealCounter; i++){
ulong Ticketnumber = HistoryDealGetTicket(i);
if (Ticketnumber != 0 && HistoryDealGetInteger(Ticketnumber,DEAL_TYPE) == DEAL_TYPE_BUY &&
LastTicketNumber != Ticketnumber){
double DealProfit = HistoryDealGetDouble(Ticketnumber, DEAL_PROFIT);
double DealComission = HistoryDealGetDouble(Ticketnumber, DEAL_COMMISSION);
double DealFee = HistoryDealGetDouble(Ticketnumber, DEAL_FEE);
if(DealProfit < 0){LostradesCounter = LostradesCounter + 1;}
else if(DealProfit > 0){WintradesCounter = WintradesCounter + 1;}
else{NeutralDealCounter = NeutralDealCounter + 1;}
TotalComission = TotalComission + DealComission;
TotalFees = TotalFees + DealFee;
LastTicketNumber = Ticketnumber;
}
AlltradeCounter = LostradesCounter + WintradesCounter + NeutralDealCounter;
if(AlltradeCounter == 0){AllWinRatio = 0;}
else{AllWinRatio = NormalizeDouble(WintradesCounter / AlltradeCounter,2);}
Print("\nServertime: ", TimeCurrent(),
"\nCurrenttime: ",TimeCurrent(),
"\nProfit: ",accountprofit,
"\nEquity: ",accountequity,
//"\nPinkAMA-Value ",PinkAMACurrentValue,
"\nYellAMA-Value ",YellAMACurrentValue,
"\nWhiteAMA-Value ",WhiteAMACurrentValue,
"\nRedAMA-Value ",RedAMACurrentValue,
"\nOrangeAMA-Value ",OrangeAMACurrentValue,
"\niLow 1: ",iLow1,
"\niLow2",iLow2,
"\nBid ",bid0
//"\nAllTrades: ", AllDealCounter,
//"\nLoses: ",LostradesCounter,
//"\nWins: ",WintradesCounter,
//"\nAllwinratio: ",AllWinRatio,
//"\nTotalFees: ",TotalFees,
//"\nTotalcomission: ",TotalComission
);
}*/
// DISPLAY OF VALUES ON CHART
/*Comment("\nServertime: ", TimeCurrent(),
"\nCurrenttime: ",TimeCurrent(),
"\nProfit: ",accountprofit,
"\nEquity: ",accountequity,
//"\nPinkAMA-Value ",PinkAMACurrentValue,
"\nYellAMA-Value ",YellAMACurrentValue,
"\nWhiteAMA-Value ",WhiteAMACurrentValue,
"\nRedAMA-Value ",RedAMACurrentValue,
"\nOrangeAMA-Value ",OrangeAMACurrentValue,
"\niLow 1: ",iLow1,
"\niLow2",iLow2,
"\nBid ",bid0
/*"\nAllTrades: ", AlltradeCounter,
"\nLoses: ",LostradesCounter,
"\nWins: ",WintradesCounter,
"\nAllwinratio: ",AllWinRatio,
"\nTotalFees: ",TotalFees,
"\nTotalcomission: ",TotalComission);*/
}//END TIMEFRAME
}//END ONTICK
void OnDeinit(const int reason){
Lostrades = TesterStatistics(STAT_LOSS_TRADES);
Wintrades = TesterStatistics(STAT_PROFIT_TRADES);
if(Lostrades + Wintrades > 0){
AllWinRatio = NormalizeDouble(Wintrades / (Wintrades + Lostrades),2);
}else{AllWinRatio = 0;}
Print("Verlorene Trades: ",Lostrades,
"\nGewonnenen Trades: ",Wintrades,
"\nGewinnrate:________",AllWinRatio);
}
double OnTester(){
Lostrades = TesterStatistics(STAT_LOSS_TRADES);
Wintrades = TesterStatistics(STAT_PROFIT_TRADES);
if(Lostrades + Wintrades > 0){
AllWinRatio = NormalizeDouble(Wintrades / (Wintrades + Lostrades),2);
}else{AllWinRatio = 0;}
return(AllWinRatio);
}
//DYNAMIC LOT CALCULATION
double CalcLots(double riskPercent, double SLDistance){
double ticksize = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
double tickvalue = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);
double Lotstep = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);
if(ticksize == 0 || tickvalue == 0 || Lotstep == 0){
Print(__FUNCTION__,"Lotsize cannot be calculated"); return 0;}
double riskMoney = AccountInfoDouble(ACCOUNT_BALANCE) * riskPercent /100;
double MoneyperLotstep = (SLDistance / ticksize) * tickvalue * Lotstep;
if(MoneyperLotstep == 0){
Print(__FUNCTION__,"Lotsize cannot be calculated"); return 0;}
double lots = MathFloor(riskMoney / MoneyperLotstep) * Lotstep;
return lots;
}
//CHECK FOR TREND IN INDICATOR
double CheckINDITrend(datetime Currenttime, int EvaluRangeSec, int INDIHandle, bool Oscillator){
//SETTING ARRAYS AND SIZES UP
datetime EvaluStart = Currenttime - EvaluRangeSec;
double INDITrendValues[];
CopyBuffer(INDIHandle,0,EvaluStart,Currenttime,INDITrendValues);
ArraySetAsSeries(INDITrendValues,true);
int INDITrendSize = ArraySize(INDITrendValues);
int INDITrendFirstHalf = MathRound(INDITrendSize / 2);
int INDITrendSeccondhalf = INDITrendSize - INDITrendFirstHalf;
//FIRST HALF AVERAGE CALC
double INDITrendFirstSum = 0;
for(int i = 0; i < INDITrendFirstHalf; i++){
INDITrendFirstSum = INDITrendFirstSum + INDITrendValues[i];}
double INDITrendFirstAv = INDITrendFirstSum / INDITrendFirstHalf;
//SECCOND HALF AVERAGE CALC
double INDITrendSeccondSum = 0;
for(int i = INDITrendFirstHalf; i < INDITrendSize; i++){
INDITrendSeccondSum = INDITrendSeccondSum + INDITrendValues[i];}
double INDITrendSeccondAv = INDITrendSeccondSum / INDITrendSeccondhalf;
//COMPARING BOTH AVERAGES
double INDITrendAvDif = 0;
if(Oscillator){INDITrendAvDif = NormalizeDouble((INDITrendFirstAv - INDITrendSeccondAv),0);}
else{INDITrendAvDif = NormalizeDouble((INDITrendFirstAv - INDITrendSeccondAv) / _Point,0);}
//DEBUGGING
/*Print("\nStarttime: ",EvaluStart,
"\nCurrenttime: ",Currenttime,
"\nRSITrendSize: ",INDITrendSize,
"\nRSITrendFirstAv: ",INDITrendFirstAv,
"\nRSITrendSeccondAv: ",INDITrendSeccondAv,
"\nRSITrendAvDif: ",INDITrendAvDif);*/
if(INDITrendAvDif < 0 || INDITrendAvDif > 0){
return INDITrendAvDif;
}else{return 0;}
}