extern int MinBarsForBreakOut = 90;
extern bool UseMoneyManagement = false;
extern double Risk = 2;
extern double Lots = 1;
extern bool UseTrailingStop = false;
extern int TrailingStop = 30;
extern int BETrigger = 20;
extern int BEBuffer = 0;
extern int SL = 50;
extern int TP = 50;
extern int MagicNumber = 1;
extern int Slippage = 3;
extern bool BrokerECN = false;
extern bool Broker5Digits = true;
datetime bartime = 0;
int state = 0;
datetime lasttrade = 0;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
if(Broker5Digits)
{
SL *= 10;
TP *= 10;
Slippage *= 10;
TrailingStop *= 10;
BETrigger *= 10;
}
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
int hh;
int ll;
int lll;
int hhh;
int nOrderTicket = 0;
int nOrderType = -1;
int nTradesNumber = 0;
int nLongs = 0;
int nShorts = 0;
int iTicket = 0;
double sl = 0, tp = 0;
nLongs = GetLongsNumber();
nShorts = GetShortsNumber();
nTradesNumber = nLongs + nShorts;
for(int x = 0; x < OrdersTotal(); x++)
{
OrderSelect(x, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
nOrderTicket = OrderTicket();
nOrderType = OrderType();
break;
}
}
hh = iHighest(Symbol(), 1, MODE_HIGH, 500, 1);
ll = iLowest(Symbol(), 1, MODE_LOW, 500, 1);
if(hh >= MinBarsForBreakOut && Bid > High[hh])
{
if(Bid > iOpen(Symbol(), PERIOD_D1, 0))
{
state = 1;
}
}
if(ll >= MinBarsForBreakOut && Bid < Low[ll])
{
if(Bid < iOpen(Symbol(), PERIOD_D1, 0))
{
state = -1;
}
}
if(nLongs > 0 && Bid > High[hh])
{
CloseAllLongs();
}
if(nShorts > 0 && Bid < Low[ll])
{
CloseAllShorts();
}
if(nLongs == 0 && state == 1 && lasttrade < iTime(Symbol(), PERIOD_H1, 1))
{
hhh = iHighest(Symbol(), PERIOD_H1, MODE_HIGH, 5, 0);
lll = iLowest(Symbol(), PERIOD_H1, MODE_LOW, 5, 0);
if(lll == 0 && Bid == Low[lll])
{
if(nShorts > 0)
{
CloseAllShorts();
}
if(Bid > iOpen(Symbol(), PERIOD_D1, 0))
{
if(SL > 0)
sl = Ask - SL*Point;
if(TP > 0)
tp = Ask + TP*Point;
if(BrokerECN)
{
iTicket = OrderSend(Symbol(), OP_BUY, GetLots(), Ask, Slippage, 0, 0, "", MagicNumber, 0, Blue);
if(iTicket > 0)
{
OrderModify(iTicket, OrderOpenPrice(), sl, tp, 0, CLR_NONE);
}
}
else
{
OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, sl, tp, "", MagicNumber, 0, Blue);
}
lasttrade = Time[0];
}
}
}
if(nShorts == 0 && state == -1 && lasttrade < iTime(Symbol(), PERIOD_H1, 1))
{
hhh = iHighest(Symbol(), PERIOD_H1, MODE_HIGH, 5, 0);
lll = iLowest(Symbol(), PERIOD_H1, MODE_LOW, 5, 0);
if(hhh == 0 && Bid == High[hhh])
{
if(nLongs > 0)
{
CloseAllLongs();
}
if(Bid < iOpen(Symbol(), PERIOD_D1, 0))
{
if(SL > 0)
sl = Bid + SL*Point;
if(TP > 0)
tp = Bid - TP*Point;
if(BrokerECN)
{
iTicket = OrderSend(Symbol(), OP_SELL, GetLots(), Bid, Slippage, 0, 0, "", MagicNumber, 0, Red);
if(iTicket > 0)
{
OrderModify(iTicket, OrderOpenPrice(), sl, tp, 0, CLR_NONE);
}
}
else
{
OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, sl, tp, "", MagicNumber, 0, Red);
}
lasttrade = Time[0];
}
}
}
// bar counting
if(bartime != Time[0])
{
if(TimeDayOfYear(Time[0]) != TimeDayOfYear(Time[1]))
{
state = 0;
}
bartime = Time[0];
}
if(nTradesNumber > 0)
{
if(BETrigger > 0)
{
HandleBreakEven();
}
if(UseTrailingStop)
{
HandleTrailing();
}
}
//----
return(0);
}
int GetLongsNumber()
{
int n = 0;
for(int x = 0; x < OrdersTotal(); x++)
{
OrderSelect(x, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_BUY)
{
n++;
}
}
return(n);
}
int GetShortsNumber()
{
int n = 0;
for(int x = 0; x < OrdersTotal(); x++)
{
OrderSelect(x, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_SELL)
{
n++;
}
}
return(n);
}
void CloseAllLongs()
{
int total = OrdersTotal();
for(int i = total - 1; i >= 0; i--)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_BUY)
{
RefreshRates();
OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Blue);
}
}
}
void CloseAllShorts()
{
int total = OrdersTotal();
for(int i = total - 1; i >= 0; i--)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_SELL)
{
RefreshRates();
OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Red);
}
}
}
double GetLots()
{
double lot, dif;
double MinLot;
double MaxLot;
double LotStep;
double mpl;
if(UseMoneyManagement)
{
LotStep = MarketInfo(Symbol(),MODE_LOTSTEP);
mpl = MarketInfo(Symbol(), MODE_MARGINREQUIRED);
lot = ((AccountBalance() * Risk ) / 100) / mpl;
dif = MathMod(lot, LotStep);
if(dif != 0)
{
lot -= dif;
}
}
else
{
lot = Lots;
}
MinLot = MarketInfo(Symbol(), MODE_MINLOT);
MaxLot = MarketInfo(Symbol(), MODE_MAXLOT);
if(lot < MinLot) lot = MinLot;
if(lot > MaxLot) lot = MaxLot;
return(lot);
}
void HandleTrailing()
{
for (int i = 0; i < OrdersTotal(); i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderType() <= OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
if(OrderType() == OP_BUY)
{
//Trailing stop
if(UseTrailingStop && TrailingStop > 0)
{
if(Bid - OrderOpenPrice() > Point * TrailingStop)
{
if(OrderStopLoss() < Bid - Point * TrailingStop)
{
OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, CLR_NONE);
continue;
}
}
}
}
else
{
//Trailing stop
if(UseTrailingStop && TrailingStop > 0)
{
if((OrderOpenPrice() - Ask) > (Point * TrailingStop))
{
if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0))
{
OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, CLR_NONE);
continue;
}
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Handle Break Even function |
//+------------------------------------------------------------------+
void HandleBreakEven()
{
for(int x = 0; x < OrdersTotal(); x++)
{
OrderSelect(x, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
if(OrderType() == OP_BUY)
{
if(Bid >= OrderOpenPrice() + BETrigger*Point && OrderStopLoss() < OrderOpenPrice() + BEBuffer*Point)
{
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + BEBuffer*Point, OrderTakeProfit(), 0, CLR_NONE);
}
}
else if(OrderType() == OP_SELL)
{
if(Ask <= OrderOpenPrice() - BETrigger*Point && OrderStopLoss() > OrderOpenPrice() - BEBuffer*Point)
{
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() - BEBuffer*Point, OrderTakeProfit(), 0, CLR_NONE);
}
}
}
}
}
//+-------------------------------------------------
Dennis Meinecke
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!