You are on page 1of 8

//+------------------------------------------------------------------+

//| EMA Cut.mq4 |


//| Jasus |
//| |
//+------------------------------------------------------------------+
#property copyright "roundrock"
#property link ""

#include <stderror.mqh>
#include <stdlib.mqh>

extern int Slippage = 3;

extern string UseFixedLot_notes= "-------------- Lot Size is fixed


--------------";

extern bool UseFixedLot= false;

extern string LotSize_notes= "-------------- If UseFixedLot is true, then this


is the LotSize used --------------";

extern double LotSize= 0.1;

extern string RiskPercentage_notes= "-------------- If UseFixedLot is false,


Lot Size is derived using this risk percentage for each trade. Rounded to 1 decimal
--------------";

extern double RiskPercentage = 1;

extern double minStopLoss = 8;

extern int MagicNumber = 7709611;

int barsTotal = 0;

string ccy;
int tf;
double pnt;
int digits=4;
double pip = 0;

int init()
{
ccy = Symbol();
tf = Period();
pnt = MarketInfo(ccy,MODE_POINT);

digits=MarketInfo(Symbol(),MODE_DIGITS);

if (digits < 4)
pip = 0.01;
else
pip = 0.0001;
//---- indicators
//---- indicator buffers mapping

//---- initialization done


return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//---- TODO: add your code here
deleteAll();
//----
return(0);
}

void deleteAll(){

Comment("");

ObjectDelete("BuyStr" );
ObjectDelete("Buy" );
ObjectDelete("Sell" );
ObjectDelete("Time1" );
ObjectDelete("Time2" );
ObjectDelete("Time");

int start()
{

if(!NewBar())
return;

if(checkOpenOrder())
return;

bool prevGreen1=false;
bool prevGreen2=false;
bool prevGreen3=false;

bool prevRed1=false;
bool prevRed2=false;
bool prevRed3=false;

if(iClose(NULL,0,1) > iOpen(NULL,0,1)) prevGreen1=true;


else prevRed1=true;

if(iClose(NULL,0,2) > iOpen(NULL,0,2)) prevGreen2=true;


else prevRed2=true;
if(iClose(NULL,0,3) > iOpen(NULL,0,3)) prevGreen3=true;
else prevRed3=true;

// for long, we need 3 consequitive green bars each closing above prev bar close
if(prevGreen1 && prevGreen2 && prevGreen3 && iHigh(NULL,0,1) > iHigh(NULL,0,2)
&& iHigh(NULL,0,2) > iHigh(NULL,0,3)
&& iClose(NULL,0,1) > iClose(NULL,0,2) && iClose(NULL,0,2) > iClose(NULL,0,3))
placeBuyOrder(iLow(NULL,0,1));

else if(prevRed1 && prevRed2 && prevRed3 && iLow(NULL,0,2) > iLow(NULL,0,1) &&
iLow(NULL,0,3) > iLow(NULL,0,2)
&& iClose(NULL,0,2) > iClose(NULL,0,1) && iClose(NULL,0,3) > iClose(NULL,0,2))
placeSellOrder(iHigh(NULL,0,1));

// else Print("------- no 123 formation today ---------");

double getLotSize(double stoploss_pips){

double balance = AccountBalance();

double risk = (balance*RiskPercentage)/100;

double numerator = risk/stoploss_pips;

double lotsize = numerator/MarketInfo(Symbol(), MODE_LOTSIZE);

lotsize = StrToDouble(DoubleToStr(lotsize,1)); // precision 1 decimal

return(lotsize);

bool NewBar()
{

if(Bars > barsTotal )

{
barsTotal = Bars;

// Print("........ New Bar ............ "+barsTotal);

return(true);
}

return(false);
}

bool checkOpenOrder(){
bool pendingOrder=false;

for(int j = 0; j < OrdersTotal(); j++)


{
OrderSelect(j,SELECT_BY_POS,MODE_TRADES);

if (OrderMagicNumber() == MagicNumber && OrderType() == OP_BUY)


{
// move stops
double stops = iLow(NULL,0,1);
double curPrice = MarketInfo(Symbol(), MODE_BID);
// Print("================= long curPrice " + curPrice+ " stops
"+stops +" first "+ (curPrice - stops )+" second "+minStopLoss*pip);

if( (curPrice - stops ) < minStopLoss*pip) {


Print("================= Closing order as stop is too close,
stops "+stops +" curPrice " +curPrice);
bool status = OrderClose(OrderTicket(), OrderLots(),
curPrice,Slippage,Blue);
if(!status) {
int check=GetLastError();
if(check!=ERR_NO_ERROR) Print("Long OrderClose failed
with error: ",ErrorDescription(check));
}
}else {

Print("================= Moving stops to


"+NormalizeDouble(stops, digits) +" open price "+OrderOpenPrice()+" long order");
int ticketlong1 =
OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(stops,
digits),0,0,Blue);

if (ticketlong1 < 1) {
check=GetLastError();
if(check!=ERR_NO_ERROR) Print("Long Move Stops failed
with error: ",ErrorDescription(check));
}
}

Sleep(1000);

pendingOrder=true;
break;
} else if (OrderMagicNumber() == MagicNumber && OrderType() ==
OP_SELL)
{
// move stops
stops = iHigh(NULL,0,1);
curPrice = MarketInfo(Symbol(), MODE_ASK);
// Print("================= curPrice " + curPrice+ " stops
"+stops +" first "+ (stops - curPrice )+" second "+minStopLoss*pip);
if( (stops - curPrice ) < minStopLoss*pip) {
Print("================= Closing order as stop is too close,
stops "+stops +" curPrice " +curPrice);
status =
OrderClose(OrderTicket(),OrderLots(),curPrice,Slippage,Red);
if(!status) {
check=GetLastError();
if(check!=ERR_NO_ERROR) Print("Short OrderClose failed
with error: ",ErrorDescription(check));
}
}else {
Print("================= Moving stops to
"+NormalizeDouble(stops, digits) +" open price "+OrderOpenPrice()+" short order");
int ticketshort1 =
OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(stops, digits),0,0,Red);

if (ticketshort1 < 1) {
check=GetLastError();
if(check!=ERR_NO_ERROR) Print("Short Move Stops failed with
error: ",ErrorDescription(check));
}
}

Sleep(1000);
pendingOrder=true;
break;
}
}

return(pendingOrder);
}

void placeBuyOrder(double sl){

RefreshRates();
double buyPrice = MarketInfo(Symbol(), MODE_ASK) ;

double buySL = sl;

double lotSize = LotSize;

if(!UseFixedLot){
lotSize = getLotSize(buyPrice - buySL);
}

string tradeComment= "Buy above "+DoubleToStr(buyPrice,4)+ " stop


"+DoubleToStr(buySL,4) +" Lot "+DoubleToStr(lotSize,2) ;

Print("Placing buy order at "+buyPrice+" with SL at "+buySL + " Lot


"+DoubleToStr(lotSize,2) );

// comments

ObjectCreate("TradeComment",OBJ_LABEL,0,0,0);
ObjectSet("TradeComment",OBJPROP_XDISTANCE,5);
ObjectSet("TradeComment",OBJPROP_YDISTANCE,40);
ObjectSetText("TradeComment",tradeComment,14,"Arial",DodgerBlue);

// draw arrow

ObjectCreate("TradeArrow1",OBJ_ARROW,0,iTime(ccy,tf,0),iLow(ccy,tf,0)-
50*pnt);
ObjectSet("TradeArrow1",OBJPROP_ARROWCODE,241);
ObjectSet("TradeArrow1",OBJPROP_COLOR,DodgerBlue);
ObjectSet("TradeArrow1",OBJPROP_WIDTH,3);

int ticketlong =
OrderSend(Symbol(),OP_BUY,lotSize,NormalizeDouble(buyPrice, digits),
Slippage,0,0,"Long White
Soldier",MagicNumber,0,Blue);

if (ticketlong < 0) {
int check=GetLastError();
if(check!=0) Print("Long OrderSend OP_BUY failed with error:
",ErrorDescription(check));
return;
}
Sleep(3000);

int ticketlong1 =
OrderModify(ticketlong,OrderOpenPrice(),NormalizeDouble(buySL, digits),0,0,Blue);
Sleep(1000);

// Print("ticketlong1 "+ticketlong1);

if (ticketlong1 < 1) {
// Print("Long OrderModify failed with error #",GetLastError());
check=GetLastError();
// Print("check "+check);
if(check!=0) Print("Long OrderModify failed with error:
",ErrorDescription(check));
}

void placeSellOrder(double sl){

RefreshRates();
double sellPrice = MarketInfo(Symbol(), MODE_BID) ;

double sellSL = sl;

double lotSize = LotSize;

if(!UseFixedLot){
lotSize = getLotSize(sellSL - sellPrice);
}
Print("Placing sell order at "+sellPrice+" with SL at "+sellSL + " Lot
"+ DoubleToStr(lotSize,2) );

string tradeComment= "Sell below "+DoubleToStr(sellPrice,4)+ " stop


"+DoubleToStr(sellSL,4) + " Lot "+DoubleToStr(lotSize,2) ;

// comments

ObjectCreate("TradeComment",OBJ_LABEL,0,0,0);
ObjectSet("TradeComment",OBJPROP_XDISTANCE,5);
ObjectSet("TradeComment",OBJPROP_YDISTANCE,40);
ObjectSetText("TradeComment",tradeComment,14,"Arial",Red);

// draw arrow

ObjectCreate("TradeArrow2",OBJ_ARROW,0,iTime(ccy,tf,0),iHigh(ccy,tf,1)+50*pnt);
ObjectSet("TradeArrow2",OBJPROP_ARROWCODE,242);
ObjectSet("TradeArrow2",OBJPROP_COLOR,Red);
ObjectSet("TradeArrow2",OBJPROP_WIDTH,3);

int ticketshort =
OrderSend(Symbol(),OP_SELL,lotSize,NormalizeDouble(sellPrice, digits),
Slippage,0,0,"Short White
Soldier",MagicNumber,0,Red);

if (ticketshort < 0) {
int check=GetLastError();
if(check!=0) Print("Short OrderSend OP_SELL failed with error:
",ErrorDescription(check));
return;
}
Sleep(3000);

int ticketshort1 =
OrderModify(ticketshort,OrderOpenPrice(),NormalizeDouble(sellSL, digits),0,0,Blue);
Sleep(1000);

// Print("ticketlong1 "+ticketlong1);

if (ticketshort1 < 1) {
// Print("Long OrderModify failed with error #",GetLastError());
check=GetLastError();
// Print("check "+check);
if(check!=0) Print("Short OrderModify failed with error:
",ErrorDescription(check));
}
}

You might also like