Professional Documents
Culture Documents
#include <stderror.mqh>
#include <stdlib.mqh>
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
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;
// 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));
return(lotsize);
bool NewBar()
{
{
barsTotal = Bars;
return(true);
}
return(false);
}
bool checkOpenOrder(){
bool pendingOrder=false;
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);
}
RefreshRates();
double buyPrice = MarketInfo(Symbol(), MODE_ASK) ;
if(!UseFixedLot){
lotSize = getLotSize(buyPrice - buySL);
}
// 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));
}
RefreshRates();
double sellPrice = MarketInfo(Symbol(), MODE_BID) ;
if(!UseFixedLot){
lotSize = getLotSize(sellSL - sellPrice);
}
Print("Placing sell order at "+sellPrice+" with SL at "+sellSL + " 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));
}
}