Utilisation en UT 1 Heure sur GBPUSD
Code
//+------------------------------------------------------------------+
//| PivotGBPUSD.mq4 |
//| Copyright 2020, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Lefeuvre"
#property link "https://www.forexfactory.com"
#property version "1"
#property strict
//----------------------- Include files ------------------------------------------------------------
#include <stdlib.mqh> // "stdlib.mqh" or "<sdlib.mqh>
#include <stderror.mqh> // "stderror.mqh" or <stderror.mqh>
//extern double VolatilityLimit = 180;
extern double LotFactor=450;
extern double Lot1Factor=18;
double Lots = 0.01;
double multiplier = 0;
double profit = 0;
double globalprofit = 0;
extern double pairglobalprofitEXPO= 3;//pairglobalprofitEXPO=7
bool openonnewcandle = true;
extern double spacePips = 6;
extern double spaceOrders = 2;
extern double space1Pips = 39;
extern int space1Orders = 9;
extern int magicbuy = 1;
string buycomment = "buy";
extern int magicsell = 2;
string sellcomment = "sell";
extern string Entry="Determine entry based on CCI";
extern int cciperiod = 5;
extern double ccimax = 100;
extern double ccimin = -100;
extern bool suspendtrades = false;
extern bool closeallsellsnow = false;
extern bool closeallbuysnow = false;
extern bool closeallnow = false;
double totalprofit;
bool sellallowed=false;
bool buyallowed=false;
bool firebuy=false;
bool firesell=false;
string stoptrading="0";
string error;
int LotDigits = 2;
//double volatility;
//double ihigh;
//double ilow;
int barnumber=5;
int nb=0;
int multiplcator=10;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
if (Digits==3 || Digits==5)
{
spacePips *= 10.0;
space1Pips *= 10.0;
}
RemoveAllObjects();
double lotStep = MarketInfo(Symbol(),MODE_LOTSTEP) ;
if (lotStep < 0.01) LotDigits=3;
else if (lotStep < 0.1) LotDigits=2;
else if (lotStep < 1.0) LotDigits=1;
else LotDigits=0;
double minLots = MarketInfo(Symbol(),MODE_MINLOT) ;
double maxLots = MarketInfo(Symbol(),MODE_MAXLOT) ;
if (multiplier==0)
if (cciperiod<0)
{
error="cciperiod invalid";
return 0;
}
if (cciperiod > 0)
{
if (ccimax < ccimin)
{
error="ccimax/ccimin invalid";
return 0;
}
if (ccimax <-100 || ccimax > 100)
{
error="ccimax invalid";
return 0;
}
if (ccimin <-100 || ccimin > 100)
{
error="ccimin invalid";
return 0;
}
}
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void RemoveAllObjects()
{
for(int i = ObjectsTotal() - 1; i >= 0; i--)
{
if (StringFind(ObjectName(i),"EA-",0) > -1) ObjectDelete(ObjectName(i));
}
}
//------------------------------------------------------------------+
// Generic Money management code
//------------------------------------------------------------------+
double GetLotSize()
{
double lots = Lots;
return(lots);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
int ticketBuyOrder = GetTicketOfLatestBuyOrder();
int ticketSellOrder = GetTicketOfLatestSellOrder();
bool isNewBar = IsNewBar();
int index;
// only show panel during live trading, not during optimization or backtesting
if ( !IsTesting() && !IsOptimization() )
{
if (GlobalVariableGet(stoptrading) == 1 && OrdersTotal() == 0 )
{
GlobalVariableSet(stoptrading, 0);
}
}
// determine entry based on CCI
if (cciperiod > 0 && isNewBar == true)
{
firebuy = false;
firesell = false;
double cci = iCCI(Symbol(), 0, cciperiod, PRICE_TYPICAL, 0);
if(sellallowed && cci < ccimin)
{
firesell = true;
sellallowed = false;
}
if(buyallowed && cci > ccimax)
{
firebuy = true;
buyallowed = false;
}
if (cci < ccimax && cci > ccimin)
{
buyallowed = true;
sellallowed = true;
}
}
// open 1st buy order
if (firebuy && ticketBuyOrder == 0 && suspendtrades==false && closeallnow==false )
{
if (GlobalVariableGet(stoptrading)==0)
{
index = OrderSend (Symbol(),OP_BUY, GetLotSize() , Ask , 3, 0, 0, buycomment, magicbuy, 0, Blue);
if (index >= 0)
{
firebuy = false;
}
}
}
// manage buy orders, open new buy orders when needed
if ( ticketBuyOrder != 0)
{
if ( isNewBar || openonnewcandle == 0)
{
if ( OrderSelect(ticketBuyOrder, SELECT_BY_TICKET))
{
double orderLots = OrderLots();
double orderPrice = OrderOpenPrice();
int buyOrderCount = GetBuyOrderCount();
double nextMultiplierLotSize = NormalizeDouble(orderLots * multiplier, LotDigits);
double Pivot = (((iHigh ( Symbol (), PERIOD_D1 , iHighest ( Symbol (), PERIOD_D1 , MODE_HIGH, barnumber , 1 ))) + (iLow ( Symbol (), PERIOD_D1 , iLowest ( Symbol (), PERIOD_D1 , MODE_LOW, barnumber , 1 ))) + Close[nb]) / 3);
double S1 = (multiplcator * Pivot) - (iHigh ( Symbol (), PERIOD_D1 , iHighest ( Symbol (), PERIOD_D1 , MODE_HIGH, barnumber , 1 )));
double S2 = Pivot - ((iHigh ( Symbol (), PERIOD_D1 , iHighest ( Symbol (), PERIOD_D1 , MODE_HIGH, barnumber , 1 ))) - (iLow ( Symbol (), PERIOD_D1 , iLowest ( Symbol (), PERIOD_D1 , MODE_LOW, barnumber , 1 ))));
double R1 = (multiplcator * Pivot) - (iLow ( Symbol (), PERIOD_D1 , iLowest ( Symbol (), PERIOD_D1 , MODE_LOW, barnumber , 1 )));
double R2 = Pivot + ((iHigh ( Symbol (), PERIOD_D1 , iHighest ( Symbol (), PERIOD_D1 , MODE_HIGH, barnumber , 1 ))) - (iLow ( Symbol (), PERIOD_D1 , iLowest ( Symbol (), PERIOD_D1 , MODE_LOW, barnumber , 1 ))));
if( Ask <= orderPrice - S1 * Point() && buyOrderCount < S1)
{
double lots= (multiplier > 0) ? nextMultiplierLotSize : (AccountBalance() * 0.01 /LotFactor);
index = OrderSend (Symbol(), OP_BUY, lots, Ask, 3, 0, 0, buycomment, magicbuy, 0, Blue);
}
else if( Ask <= orderPrice - S2 * Point() && buyOrderCount <= (S1 + S2 -1) && buyOrderCount >= S1)
{
double lots= (multiplier > 0) ? nextMultiplierLotSize : (AccountBalance() * 0.01 /Lot1Factor);
index = OrderSend (Symbol(), OP_BUY, lots, Ask, 3, 0, 0, buycomment, magicbuy, 0, Blue);
}
}
}
}
// open 1st sell order..
if (firesell == true && ticketSellOrder == 0 && suspendtrades == false && closeallnow == false)
{
// open 1st sell order
if ( GlobalVariableGet(stoptrading) == 0 )
{
index = OrderSend (Symbol(), OP_SELL, GetLotSize(), Bid, 3, 0, 0, sellcomment, magicsell, 0, Red);
if (index >= 0)
{
firesell = false;
}
}
}
// manage sell order. open new sell orders when needed
if ( ticketSellOrder != 0)
{
if ( isNewBar || openonnewcandle == 0)
{
if ( OrderSelect(ticketSellOrder, SELECT_BY_TICKET))
{
double orderLots = OrderLots();
double orderPrice = OrderOpenPrice();
int sellOrderCount = GetSellOrderCount();
double nextMultiplierLotSize = NormalizeDouble(orderLots * multiplier, LotDigits);
/* double Pivot = ((High[nb] + Low[nb] + Close[nb]) / 3);
double S1 = (2 * Pivot) - (High[nb]);
double S2 = Pivot - (High[nb] - Low[nb]);
double R1 = (2 * Pivot) - (Low[nb]);
double R2 = Pivot + (High[nb] - Low[nb]);*/
double Pivot = (((iHigh ( Symbol (), PERIOD_D1 , iHighest ( Symbol (), PERIOD_D1 , MODE_HIGH, barnumber , 1 ))) + (iLow ( Symbol (), PERIOD_D1 , iLowest ( Symbol (), PERIOD_D1 , MODE_LOW, barnumber , 1 ))) + Close[nb]) / 3);
double S1 = (multiplcator * Pivot) - (iHigh ( Symbol (), PERIOD_D1 , iHighest ( Symbol (), PERIOD_D1 , MODE_HIGH, barnumber , 1 )));
double S2 = Pivot - ((iHigh ( Symbol (), PERIOD_D1 , iHighest ( Symbol (), PERIOD_D1 , MODE_HIGH, barnumber , 1 ))) - (iLow ( Symbol (), PERIOD_D1 , iLowest ( Symbol (), PERIOD_D1 , MODE_LOW, barnumber , 1 ))));
double R1 = (multiplcator * Pivot) - (iLow ( Symbol (), PERIOD_D1 , iLowest ( Symbol (), PERIOD_D1 , MODE_LOW, barnumber , 1 )));
double R2 = Pivot + ((iHigh ( Symbol (), PERIOD_D1 , iHighest ( Symbol (), PERIOD_D1 , MODE_HIGH, barnumber , 1 ))) - (iLow ( Symbol (), PERIOD_D1 , iLowest ( Symbol (), PERIOD_D1 , MODE_LOW, barnumber , 1 ))));
if( Bid >= orderPrice + R1 * Point() && GetSellOrderCount() < R1)
{
double lots= (multiplier > 0) ? nextMultiplierLotSize : (AccountBalance() * 0.01 /LotFactor);
index = OrderSend(Symbol(), OP_SELL, lots, Bid, 3, 0, 0, sellcomment, magicsell, 0, Red);
}
else if( Bid >= orderPrice + R2 * Point() && sellOrderCount <= (R1 + R2 - 1) && sellOrderCount >= R1)
{
double lots= (multiplier > 0) ? nextMultiplierLotSize : (AccountBalance() * 0.01 /Lot1Factor);
index = OrderSend(Symbol(), OP_SELL, lots, Bid, 3, 0, 0, sellcomment, magicsell, 0, Red);
}
}
}
}
// calculate profit / loss of all open orders
double profitBuyOrders = 0;
double profitSellOrders = 0;
for(int k=OrdersTotal()-1; k >=0; k--)
{
if ( OrderSelect(k, SELECT_BY_POS))
{
if (Symbol() == OrderSymbol())
{
if (OrderType()==OP_BUY && OrderMagicNumber() == magicbuy)
{
profitBuyOrders = profitBuyOrders + OrderProfit() + OrderSwap() + OrderCommission();
}
if (OrderType()==OP_SELL && OrderMagicNumber() == magicsell)
{
profitSellOrders = profitSellOrders + OrderProfit() + OrderSwap() + OrderCommission();
}
}
}
}
// close all buy orders (for this pair) when total profit of buy orders > profit
if ((profit > 0 && profitBuyOrders >= profit) || closeallbuysnow == true)
{
CloseAllBuyOrders();
firebuy = false;
}
// close all sell orders (for this pair) when total profit of sell orders > profit
if ((profit > 0 && profitSellOrders >= profit) || closeallsellsnow == true)
{
CloseAllSellOrders();
firesell = false;
}
// close all orders when total profit on this pair > pairglobalprofit
if ((AccountBalance() * 0.01 /pairglobalprofitEXPO)> 0 && (profitBuyOrders + profitSellOrders) >= (AccountBalance() * 0.01 /pairglobalprofitEXPO))
{
CloseAllSellOrders();
CloseAllBuyOrders();
firebuy = false;
firesell = false;
}
// close all orders when total profit on all pairs > totalglobalprofit
// or if total loss on all pairs > maximaloss
double totalglobalprofit = TotalProfitOnAllPairs();
if((globalprofit > 0 && totalglobalprofit >= globalprofit) /*|| (maximaloss < 0 && totalglobalprofit <= maximaloss)*/)
{
GlobalVariableSet(stoptrading, 1);
CloseAllOrdersOnAllPairs();
firebuy = false;
firesell = false;
}
}
//+------------------------------------------------------------------+
// GetBuyOrderCount()
// returns the number of open buy orders
//+------------------------------------------------------------------+
int GetBuyOrderCount()
{
int count=0;
for (int k = OrdersTotal();k >=0 ;k--)
{
if (OrderSelect(k, SELECT_BY_POS))
{
if (OrderType()==OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == magicbuy)
{
count=count+1;
}
}
}
return count;
}
//+------------------------------------------------------------------+
// GetSellOrderCount()
// returns the number of open sell orders
//+------------------------------------------------------------------+
int GetSellOrderCount()
{
int count=0;
// find all open orders of today
for (int k = OrdersTotal(); k >=0 ;k--)
{
if (OrderSelect(k, SELECT_BY_POS))
{
if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == magicsell)
{
count=count+1;
}
}
}
return count;
}
//+------------------------------------------------------------------+
// GetTicketOfLatestBuyOrder()
// returns the ticket of the latest open buy order
//+------------------------------------------------------------------+
int GetTicketOfLatestBuyOrder()
{
double maxLots=0;
int orderTicketNr=0;
for (int l=OrdersTotal() - 1; l >= 0; l--)
{
if ( OrderSelect(l,SELECT_BY_POS))
{
if( OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == magicbuy)
{
return OrderTicket();
}
}
}
return 0;
}
//+------------------------------------------------------------------+
// GetTicketOfLatestSellOrder()
// returns the ticket of the latest open sell order
//+------------------------------------------------------------------+
int GetTicketOfLatestSellOrder()
{
double maxLots=0;
int orderTicketNr=0;
for (int l=OrdersTotal() - 1; l >= 0; l--)
{
if ( OrderSelect(l, SELECT_BY_POS) )
{
if(OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == magicsell)
{
return OrderTicket();
}
}
}
return 0;
}
//+------------------------------------------------------------------+
// CloseAllBuyOrders()
// closes all open buy orders
//+------------------------------------------------------------------+
void CloseAllBuyOrders()
{
for (int m=OrdersTotal(); m>=0; m--)
{
if ( OrderSelect(m, SELECT_BY_POS))
{
if(OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == magicbuy)
{
RefreshRates();
bool success = OrderClose(OrderTicket(), OrderLots(), Bid, 0, Blue);
}
}
}
}
//+------------------------------------------------------------------+
// CloseAllSellOrders()
// closes all open sell orders
//+------------------------------------------------------------------+
void CloseAllSellOrders()
{
for (int h=OrdersTotal();h>=0;h--)
{
if ( OrderSelect(h,SELECT_BY_POS) )
{
if(OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == magicsell)
{
RefreshRates();
bool success =OrderClose(OrderTicket(), OrderLots(), Ask, 0, Red);
}
}
}
}
//+------------------------------------------------------------------+
// CloseAllOrdersOnAllPairs()
// closes all open orders on all pairs
//+------------------------------------------------------------------+
void CloseAllOrdersOnAllPairs()
{
for (int h=OrdersTotal(); h>=0; h--)
{
if ( OrderSelect(h, SELECT_BY_POS) )
{
if (OrderType() == OP_SELL && OrderMagicNumber() == magicsell)
{
RefreshRates();
bool success =OrderClose(OrderTicket(), OrderLots(), Ask, 0, Red);
}
if (OrderType() == OP_BUY && OrderMagicNumber() == magicbuy)
{
RefreshRates();
bool success =OrderClose(OrderTicket(), OrderLots(), Bid, 0, Red);
}
}
}
}
//+------------------------------------------------------------------+
// TotalProfitOnAllPairs()
// returns the total profit for all open orders on all pairs
//+------------------------------------------------------------------+
double TotalProfitOnAllPairs()
{
double totalProfit = 0;
for (int j=OrdersTotal();j >= 0; j--)
{
if( OrderSelect(j,SELECT_BY_POS))
{
if (OrderMagicNumber() == magicsell || OrderMagicNumber() == magicbuy)
{
RefreshRates();
totalProfit = totalProfit + OrderProfit() + OrderSwap() + OrderCommission();
}
}
}
return totalProfit;
}
//+------------------------------------------------------------------+
// IsNewBar()
// returns if new bar has started
//+------------------------------------------------------------------+
bool IsNewBar()
{
static datetime time = Time[0];
if(Time[0] > time)
{
time = Time[0]; //newbar, update time
return (true);
}
return(false);
}
//--------------------------------------------------------------------------------
// TradesToday()
// return total number of trades done today (closed and still open)
//--------------------------------------------------------------------------------
int TradesToday()
{
int count=0;
if (IsTesting() || IsOptimization()) return 0;
// find all open orders of today
for (int k = OrdersTotal();k >=0 ;k--)
{
if (OrderSelect(k,SELECT_BY_POS))
{
if (OrderSymbol() == Symbol() )
{
if(OrderLots() == Lots)
{
if (OrderMagicNumber() == magicbuy || OrderMagicNumber() == magicsell)
{
if( TimeDay(OrderOpenTime()) == TimeDay(TimeCurrent()))
{
count=count+1;
}
}
}
}
}
}
for (int l=OrdersHistoryTotal(); l >= 0; l--)
{
if(OrderSelect(l, SELECT_BY_POS,MODE_HISTORY))
{
if (OrderSymbol() == Symbol() )
{
if (OrderMagicNumber() == magicbuy || OrderMagicNumber() == magicsell)
{
if(TimeDay(OrderOpenTime()) == TimeDay(TimeCurrent()))
{
if(OrderLots() == Lots)
{
count = count + 1;
}
}
else
{
return count;
}
}
}
}
}
return count;
}