____________________________________________
//+------------------------------------------------------------------+
//| WOC[1].0.1.5_No_SL&TP_iVAR.mq4 |
//+------------------------------------------------------------------+
#property version "1.00"
#property strict
#include <stderror.mqh>
#include <stdlib.mqh>
extern int StopLoss = 6;
extern int TrSt = 6;
extern int Speed = 5;
extern double timeSpeed = 3;
extern double Lots = 0.1;
extern double MaxLot = 100;
extern string moneymanagement="Money Management";
extern string dispMM = "Settings Money Management";
extern bool MM = true;
//Exemple de SizingRule: 1.0= 1 lot, 5%B=5% de la balance 5%F=5% du free margin, 100$= 100$
//extern double Size = 5.0;
extern string SizingRule = "1%B"; //Risk/Reward per trade. Or, can be fixed number of lots;
extern double ProfitLossfactor = 0.30; //Factor to calculate TP and SL. Multiple of the ADR
extern int LastXTrades = 10; //Used for TSSF calculation
extern int Risk = 20;
extern int Magic = 20080829;
// IVAR
extern int TimeFrameiVAR = PERIOD_M1;
extern double a1 = 0.0;
extern double a2 = 0.0;
extern double a3 = 0.54;
extern double a4 = 0.53;
double Point_;
datetime nextTime;
int cnt, total, lastBars;
int up, down, TimeSpeedUp, TimeSpeedDown;
bool TimBoolUp,TimBoolDown, OrderSal;
double priceUp, priceDown;
double vbuy,vsell;
int PipMultiplier=0;
string symbol;
double MINLOT;
double lotstep;
double maxlots;
bool iVAR()
{
bool Result=false;
double iVAR_1=iCustom(symbol,TimeFrameiVAR,"iVAR_nmc",5,1000,0,1);
if(iVAR_1<a3 && iVAR_1<a4)
Result=true;
return(Result);
}
int OnInit()
{
symbol = Symbol();
MINLOT = MarketInfo(symbol,MODE_MINLOT);
lotstep = MarketInfo(symbol,MODE_LOTSTEP);
maxlots = MarketInfo(symbol,MODE_MAXLOT);
Magic = Get_Magic();
if (Digits==3 || Digits==5)
PipMultiplier=10;
else PipMultiplier=1;
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double ask = StrToDouble(DoubleToStr(Ask,Digits-1));
double bid = StrToDouble(DoubleToStr(Bid,Digits-1));
Comment("\n"," ",Hour()," : ",Minute(),"\n",
" Bid = ",Bid ,"\n",
" Ask = ",Ask, "\n",
" vbuy = ",vbuy ,"\n",
" vsell = ",vsell, "\n"
);
total = OrdersTotal();
int i, currentSymbolOrderPos = -1;
// if (total==0) {vbuy=0; vsell=0;}
for (i = 0; i < total; i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol())
{
currentSymbolOrderPos = i;
break;
}
}
if (currentSymbolOrderPos < 0)
{
if(priceUp < ask)
{
up = up + 1;
priceUp = ask;
if(TimBoolUp == false)
{
TimeSpeedUp = TimeCurrent();
TimBoolUp = true;
}
}
else
{
priceUp = 0;
up = 0;
TimBoolUp = false;
TimeSpeedUp = 0;
}
if(priceDown > ask)
{
down = down + 1;
priceDown = ask;
if(TimBoolDown == false)
{
TimeSpeedDown = TimeCurrent();
TimBoolDown = true;
}
}
else
{
priceDown = 0;
down = 0;
TimBoolDown = false;
TimeSpeedDown = 0;
}
if(up == Speed || down == Speed)
{
openOrders();
}
if(priceUp == 0)
{
priceUp = ask;
}
if(priceDown == 0)
{
priceDown = ask;
}
}
else
{
OrderSelect(currentSymbolOrderPos, SELECT_BY_POS, MODE_TRADES);
if (OrderType() == OP_BUY)
{ if(Bid<=vbuy) {OrderClose(OrderTicket(), OrderLots(), Bid, 10,Red);vbuy=0;}
if(OrderSal == true)
{
// OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid - StopLoss*Point*PipMultiplier,Digits),'', 0, Red);
vbuy=NormalizeDouble(Bid - StopLoss*Point*PipMultiplier,Digits);
OrderSal = false;
}
if (Bid-OrderOpenPrice()>TrSt*Point*PipMultiplier)
{
if (vbuy<Bid-(TrSt)*Point*PipMultiplier)
{
vbuy=Bid - (TrSt)*Point*PipMultiplier ;
//OrderModify(OrderTicket(),OrderOpenPrice(),Bid - (TrSt)*Point*PipMultiplier,'', 0, Red);
}
}
}
if (OrderType() == OP_SELL)
{ if(Ask>= vsell) {OrderClose(OrderTicket(), OrderLots(), Ask, 10,Red);vsell=0;}
if(OrderSal == true)
{
// OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask + StopLoss*Point*PipMultiplier, Digits),'', 0, Red);
vsell=NormalizeDouble(Ask + StopLoss*Point*PipMultiplier, Digits);
OrderSal = false;
}
if (OrderOpenPrice()-Ask > TrSt*Point*PipMultiplier)
{
if (vsell > Ask +(TrSt)*Point*PipMultiplier)
{
vsell= Ask + (TrSt)*Point*PipMultiplier;
//OrderModify(OrderTicket(),OrderOpenPrice(),Ask + (TrSt)*Point*PipMultiplier,'', 0, Red);
}
}
}
}
}
int Get_Magic()
{
string mag;
int Magic_g;
Sleep(1000);
while (true)
{
MathSrand (TimeLocal());
mag = StringConcatenate(mag, MathRand()/15, MathRand()/8, MathRand()/32);
Magic_g = MathRound(MathAbs(StrToInteger(mag)));
mag = DoubleToStr(Magic_g,10);
if (StringLen (mag) >= 15) {Magic_g = StrToInteger(mag); break;}
}
return (MathRound(MathAbs(Magic_g)));
}
void openOrders()
{
int try;
if(up < down)
{
Print("TimeSpeedDown");
if(TimeCurrent() - TimeSpeedDown <= timeSpeed && iVAR() )
{
Print(" TimeSpeedDown < TimeSpeed");
RefreshRates();
for(try = 1; try <= 2; try++)
{
if (OrderSend(Symbol(),OP_SELL,CalculateLotSize(),Bid,10,0,0 ,"",Magic,0,Red) < 0)
{ Print("OP_SELL ", ErrorDescription(GetLastError()));
Print("try ", try);
Sleep(1000);
RefreshRates();
}
else
{
OrderSal = true;
break;
}
}
}
}
else
{
Print(" TimeSpeedUp");
if(TimeCurrent() - TimeSpeedUp <= timeSpeed && iVAR() )
{
Print("TimeSpeedUp < TimeSpeed");
RefreshRates();
for(try = 1; try <= 2; try++)
{
if (OrderSend(Symbol(),OP_BUY,CalculateLotSize(),Ask,10,0,0, "", Magic, 0, Green) < 0)
{ Print("OP_BUY ", ErrorDescription(GetLastError()));
Print("tru", try);
Sleep(1000);
RefreshRates();
}
else
{
OrderSal = true;
break;
}
}
}
}
priceUp = 0;
priceDown = 0;
up = 0;
down = 0;
TimBoolUp = false;
TimBoolDown = false;
TimeSpeedUp = 0;
TimeSpeedDown = 0;
}
double getTSSF()
{
double tradegagnant,tradeperdant;
double profit, perte, avgwin, avgloss, prcwin, tssf;
int orders=HistoryTotal();
if(orders <= LastXTrades) return(0.0);
tradegagnant=0.0;
tradeperdant=0.0;
profit=0.0;
perte=0.0;
avgwin=0.0;
avgloss=0.0;
prcwin=0.0;
tssf=0.0;
for(int j=orders-1;j>=orders-LastXTrades;j--)
{
if(OrderSelect(j,SELECT_BY_POS,MODE_HISTORY)==False)
{
Print("Erreur dans l historique!");
break;
}
if(OrderProfit() >= 0)
{
tradegagnant= tradegagnant+1.0;
profit=profit+OrderProfit();
}
else
{
tradeperdant= tradeperdant+1.0;
perte=perte-OrderProfit();
}
}
if (tradegagnant == 0.0){
avgwin=0.0;
}
else if (orders>LastXTrades)
{
avgwin=profit/tradegagnant;
}
if (tradeperdant == 0.0)
{
tssf = 1.0;
return(tssf);
}
else if (orders>LastXTrades)
{
avgloss=perte/tradeperdant;
}
if (orders>LastXTrades)prcwin=tradegagnant/(tradegagnant+tradeperdant);
else prcwin= 0.0;
if (avgloss == 0.0){
tssf = 1.0;
return(tssf);
}
else if (orders>LastXTrades)
{
if(prcwin == 0.1)prcwin= 0.11;
tssf=avgwin/avgloss*((1.1-prcwin)/(prcwin-0.1)+1);
}
return(tssf);
}
//+------------------------------------------------------------------+
//| expert Div function |
//| Avoid divide by zero errors |
//+------------------------------------------------------------------+
double Div(double a,double b,int loc=0)
{
if(b==0)
{
Alert("Divide by zero error! a="+a+" b="+b+", loc="+loc);
return(0);
}
return(a/b);
}
//+------------------------------------------------------------------+
//|StringUpper (used to convert string case ) |
//+------------------------------------------------------------------+
string StringUpper(string str)
//+------------------------------------------------------------------+
// Converts any lowercase characters in a string to uppercase
// Usage: string x=StringUpper("The Quick Brown Fox") returns x = "THE QUICK BROWN FOX"
//if using capitalise, only the first char is Capitalised
{
string outstr = "";
string lower = "abcdefghijklmnopqrstuvwxyz";
string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(int i=0; i<StringLen(str); i++) {
int t1 = StringFind(lower,StringSubstr(str,i,1),0);
if (t1 >=0)
outstr = outstr + StringSubstr(upper,t1,1);
else
outstr = outstr + StringSubstr(str,i,1);
}
return(outstr);
}
//+------------------------------------------------------------------+
//| expert StrToNum function |
//+------------------------------------------------------------------+
double StrToNum(string str)
{
//+------------------------------------------------------------------+
// Usage: strips all non-numeric characters out of a string, to return a numeric (double) value
// valid numeric characters are digits 0,1,2,3,4,5,6,7,8,9, decimal point (.) and minus sign (-)
int i0,dp = -1;
int sgn = 1;
double num = 0.0;
for (i0=0; i0<StringLen(str); i0++)
{
string s1 = StringSubstr(str,i0,1);
if (s1 == "-") sgn = -sgn; else
if (s1 == ".") dp = 0; else
if (s1 >= "0" && s1 <= "9")
{
if (dp >= 0) dp++;
if (dp > 0)
num = num + Div(StrToInteger(s1) , MathPow(10,dp),2421);
else
num = num * 10 + StrToInteger(s1);
}
}
return(num*sgn);
}
//+------------------------------------------------------------------+
//| expert GetLots function |
//+------------------------------------------------------------------+
double GetLots(string symbol_current,double profitfactor)
{
if(lotstep == 0) return(0.1);
int decimals = MathLog(1 / lotstep) / MathLog(10);
double AcctFreeMgn = AccountEquity();
int RiskType = 0; // default to N lots
double size;
double TSSFTrigger1=1;
double TSSFTrigger2=2;
double TSSFTrigger3=3;
double tssf= getTSSF();
if (StringFind(StringUpper(SizingRule),"B") >= 0) AcctFreeMgn = AccountBalance();
if (StringFind(StringUpper(SizingRule),"F") >= 0) AcctFreeMgn = AccountFreeMargin();
if (StringFind(SizingRule,"%") >= 0) RiskType = 1; // N% AcctFreeMgn
if (StringFind(SizingRule,"$") >= 0) RiskType = 2; // $N amount
double risk;
double ratio= 1.0+tssf;
if(tssf > 1.0){
ratio= 2.0;
}
double TickValue = MarketInfo(symbol_current,MODE_TICKVALUE);
if(Point == 0.001 || Point == 0.00001) TickValue *= 10;
switch (RiskType)
{
case 1 : risk = NormalizeDouble(AcctFreeMgn*ratio*StrToNum(SizingRule)/100,decimals); size= Div(Div(risk, StopLoss,1522), TickValue, 1524); break; // calc vol = N% of account equity
case 2 : risk = NormalizeDouble(StrToNum(SizingRule)*ratio,decimals); size= Div(Div(risk, StopLoss,1523), TickValue, 1525); break; // calc vol from entered $amount
default : risk = NormalizeDouble(StrToNum(SizingRule)*ratio,decimals); size= risk; break; // vol is simply no. of lots entered
}
double size1 = NormalizeDouble(size,decimals);
if(size1 < MINLOT) size1 = MINLOT;
if(size1 > maxlots) size1 = maxlots;
Print("tssf "+tssf+" ratio "+ratio+" size1 "+size1+" maxlots "+maxlots+" MINLOT "+MINLOT);
return(size1);
}
double CalculateLotSize()
{
double ilots;
if (MM == true)
ilots = GetLots(symbol,ProfitLossfactor);
return(ilots);
}