fbpx ...

The code below is for the Index Buy At Open strategy for the dax (Ger40) as per the youtube video.

//+——————————————————————+
//| IndexBuyAtOpen.mq5 |
//| Global Finance Trading |
//| www.globalfinancetrading.com |
//+——————————————————————+
#property copyright “Global Finance Trading”
#property link “www.globalfinancetrading.com”
#property version “1.00”

#include<Trade\Trade.mqh>
CTrade trade;

///FTMO

input double Target = 180.00;
double Lots = 0.00;
double LotCalc = 0.00;
double SymbolMax = 0.00;
double SymbolMin = 0.00;
double AccountBalance = 0.00;
double Equity = 0.00;
double TargetEquity = 110000;

input double Stops = 400;
input double MaxSpread = 2.00;
input double PercentReturn = 1.1;
double SpreadCheck = 0.00;
input int Magic1 = 1005;
double StoppedPips=0;
input int seconds = 330;
input int Hour = 3;
int MinuteTime = 0;
int HourOne = 0;
int PositionsCountLong1 = 0;
int DayMonth =0;
input int DaytoTrade = 0;
int WeekDay = 0;
input int Weekday = 0;
int Bollinger;
int MovingAverage;
double Risk=0.00;
double StopTotal =0.00;
double LastLow =0.00;
input double Deviation = 2.80;
input double RiskPercent = 0.003;

double Bid = 0.00;
double Ask =0.00;

double ClosePrice = 0.00;
double HighPrice = 0.00;
double LowPrice = 0.00;
int CandleLow = 0;
int CandleHigh = 0;
double High = 0.00;
int DayNumber = 0;

double BreakoutLong;
double Pivot;
double R3;
double R2;
double R1;
double S1;
double S2;
double S3;
double BreakoutShort;
//+——————————————————————+
//| Expert initialization function |
//+——————————————————————+
int OnInit()
{
Bollinger = iBands(Symbol(), PERIOD_H4, 20, 0, Deviation, PRICE_CLOSE);

MovingAverage = iMA(Symbol(), PERIOD_D1, 200, 0, MODE_EMA, PRICE_CLOSE);

return(INIT_SUCCEEDED);
}
//+——————————————————————+
//| Expert deinitialization function |
//+——————————————————————+
void OnDeinit(const int reason)
{
//—

}
//+——————————————————————+
//| Expert tick function |
//+——————————————————————+
void OnTick()
{
MqlDateTime DayofMonth;
TimeCurrent(DayofMonth);
DayMonth = DayofMonth.day;

MqlDateTime DayofWeek;
TimeCurrent(DayofWeek);
WeekDay = DayofWeek.day_of_week;

MqlDateTime HourOfDay;
TimeCurrent(HourOfDay);
HourOne = HourOfDay.hour;

MqlDateTime Minute;
TimeCurrent(Minute);
MinuteTime = Minute.min;

datetime datestart = iTime(Symbol(), PERIOD_D1, 0);
datetime dateend = iTime(Symbol(), PERIOD_D1, 0)+PeriodSeconds(PERIOD_D1);
string objectnamer =IntegerToString(DayNumber,3, ‘ ‘);

double risk = 0.00;

Risk = TotalRisk(risk);

double stoprisk = 0.00;

StopTotal = StopRisk(stoprisk);

int typebuy1 = 0;
PositionsCountLong1 = PositionTypeLong1(typebuy1);


static datetime lastlevels=0; datetime now=TimeCurrent();
if( (HourOne==4) && (now – lastlevels >= 86400))
{
lastlevels=now;
//Get data from previous day
ClosePrice = iClose(Symbol(), PERIOD_H1, 9);
CandleLow = iLowest(Symbol(), PERIOD_H1, MODE_LOW, 15, 9);
CandleHigh = iHighest(Symbol(), PERIOD_H1, MODE_HIGH, 15, 9);
LowPrice = iLow(Symbol(), PERIOD_H1, CandleLow);
HighPrice = iHigh(Symbol(), PERIOD_H1, CandleHigh);

//set pivot points
BreakoutLong = (HighPrice-LowPrice)*(1.1/2)+ClosePrice;
Pivot = (HighPrice+LowPrice+ClosePrice)/3;
R3 = HighPrice+(2*(Pivot-LowPrice));
R2 = Pivot+(HighPrice-LowPrice);
R1 = ((2*Pivot)-LowPrice);
S1 = ((2*Pivot)-HighPrice);
S2 = Pivot-(HighPrice-LowPrice);
S3 = LowPrice-(2*(HighPrice-Pivot));
BreakoutShort = ClosePrice-(HighPrice-LowPrice)*(1.1/2);

ObjectCreate(0, “BreakoutLong”+objectnamer, OBJ_TREND, 0, datestart, BreakoutLong, dateend, BreakoutLong);
ObjectCreate(0, “R3″+objectnamer, OBJ_TREND, 0, datestart, R3, dateend, R3);
ObjectCreate(0, “R2″+objectnamer, OBJ_TREND, 0, datestart, R2, dateend, R2);
ObjectCreate(0, “R1″+objectnamer, OBJ_TREND, 0, datestart, R1, dateend, R1);
ObjectCreate(0, “Pivot”+objectnamer, OBJ_TREND, 0, datestart, Pivot, dateend, Pivot);
ObjectCreate(0, “S1″+objectnamer, OBJ_TREND, 0, datestart, S1, dateend, S1);
ObjectCreate(0, “S2″+objectnamer, OBJ_TREND, 0, datestart, S2, dateend, S2);
ObjectCreate(0, “S3″+objectnamer, OBJ_TREND, 0, datestart, S3, dateend, S3);
ObjectCreate(0, “BreakoutShort”+objectnamer, OBJ_TREND, 0, datestart, BreakoutShort, dateend, BreakoutShort);
ObjectCreate(0, “High”+objectnamer, OBJ_TREND, 0, datestart, HighPrice, dateend, HighPrice);
ObjectCreate(0, “Low”+objectnamer, OBJ_TREND, 0, datestart, LowPrice, dateend, LowPrice);
ObjectCreate(0, “Close”+objectnamer, OBJ_TREND, 0, datestart, ClosePrice, dateend, ClosePrice);
ObjectSetInteger(0, “BreakoutLong”+objectnamer, OBJPROP_COLOR, clrYellow);
ObjectSetInteger(0, “R3″+objectnamer, OBJPROP_COLOR, clrMagenta);
ObjectSetInteger(0, “R2″+objectnamer, OBJPROP_COLOR, clrOrange);
ObjectSetInteger(0, “R1″+objectnamer, OBJPROP_COLOR, clrSandyBrown);
ObjectSetInteger(0, “Pivot”+objectnamer, OBJPROP_COLOR, clrBlueViolet);
ObjectSetInteger(0, “S1″+objectnamer, OBJPROP_COLOR, clrSandyBrown);
ObjectSetInteger(0, “S2″+objectnamer, OBJPROP_COLOR, clrOrange);
ObjectSetInteger(0, “S3″+objectnamer, OBJPROP_COLOR, clrMagenta);
ObjectSetInteger(0, “BreakoutShort”+objectnamer, OBJPROP_COLOR, clrYellow);
ObjectSetInteger(0, “High”+objectnamer, OBJPROP_COLOR, clrRed);
ObjectSetInteger(0, “Low”+objectnamer, OBJPROP_COLOR, clrRed);
ObjectSetInteger(0, “Close”+objectnamer, OBJPROP_COLOR, clrSilver);
ObjectSetInteger(0, “Close”+objectnamer, OBJPROP_LEVELWIDTH, 4);
}

double TotalSL = 0.00;
double Volume = 0.00;
StoppedPips = StopsCalc(TotalSL, Volume);
Equity = AccountInfoDouble(ACCOUNT_EQUITY);
AccountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
SymbolMin = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_MIN);
SymbolMax = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_MAX);
Bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);
Ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
double UpBand[];
ArraySetAsSeries(UpBand, true);
double LowBand[];
ArraySetAsSeries(LowBand, true);
CopyBuffer(Bollinger, 1, 0, 3, UpBand);
CopyBuffer(Bollinger, 2, 0, 3, LowBand);
//// Closing at equity targets
LastLow = iLow(Symbol(), PERIOD_H4, 1);

double MA[];
ArraySetAsSeries(MA, true);
CopyBuffer(MovingAverage, 0, 0, 11, MA);

double MovingAverageValue = NormalizeDouble(MA[0], 4);
double MovingAverageValueOld = NormalizeDouble(MA[10], 4);
/*
//close if oipen equity hits %
if(Equity>AccountBalance*PercentReturn && PositionsTotal()>0)
{
int i = PositionsTotal()-1;
for(i; i>=0; i–)
{
ulong PositionTicket = PositionGetTicket(i);
if(((PositionGetInteger(POSITION_TYPE))==POSITION_TYPE_BUY) && PositionGetInteger(POSITION_MAGIC)==Magic1)
{
trade.PositionClose(PositionTicket, 0); ///modify the order short
Print(“Closed at Equity Target”);
}
else
{
GetLastError();
}
}
}
*/
SpreadCheck = (Bid-Ask);



//Initial Trade
if( (HourOne==10) && (MinuteTime<01) && PositionsCountLong1<1 )
{
if((SpreadCheck<MaxSpread) && PositionsCountLong1<1 && HourOne>=Hour)
{
trade.SetExpertMagicNumber(Magic1);
trade.Buy(10, Symbol(), Ask, (Bid-Stops), Bid+Target, “Index Buying”);
}
}


}
//+——————————————————————+


// Check for long orders
double StopsCalc (double TotalSL, double Volume)
{
int i = PositionsTotal()-1;
for(i; i>=0; i–)
{
ulong PositionTicket = PositionGetTicket(i);
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY &&
PositionGetString(POSITION_SYMBOL)==Symbol() &&
(PositionGetInteger(POSITION_MAGIC)==Magic1) )
{
double TotalSL = PositionGetDouble(POSITION_PRICE_CURRENT) – PositionGetDouble(POSITION_SL);
double Volume = PositionGetDouble(POSITION_VOLUME);
TotalSL = TotalSL + TotalSL;
Volume = Volume + Volume;
}
}
return TotalSL;
return Volume;
}

// Check for long orders
int PositionTypeLong1(int typebuy1)
{
int i = PositionsTotal()-1;
for(i; i>=0; i–)
{
ulong PositionTicket = PositionGetTicket(i);
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY &&
PositionGetString(POSITION_SYMBOL)==Symbol() &&
(PositionGetInteger(POSITION_MAGIC)==Magic1)
)
{
typebuy1 = typebuy1 + 1;
}
}
return typebuy1;
}


// Check for long orders
double TotalRisk (double risk)
{
int i = PositionsTotal()-1;
for(i; i>=0; i–)
{
ulong PositionTicket = PositionGetTicket(i);

if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY &&
PositionGetString(POSITION_SYMBOL)==Symbol() &&
(PositionGetInteger(POSITION_MAGIC)==Magic1)
)
{
risk = risk + PositionGetDouble(POSITION_PROFIT);
}
}
return risk;
}


// Check for long orders
double StopRisk (double stoprisk)
{
int i = PositionsTotal()-1;
for(i; i>=0; i–)
{
ulong PositionTicket = PositionGetTicket(i);

if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY &&
PositionGetString(POSITION_SYMBOL)==Symbol() &&
(PositionGetInteger(POSITION_MAGIC)==Magic1) &&
(PositionGetDouble(POSITION_SL)<PositionGetDouble(POSITION_PRICE_OPEN) )
)
{
stoprisk = stoprisk + PositionGetDouble(POSITION_SL);
}
}
return stoprisk;
}


Categories: Uncategorized

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *