Orders Execution
Miscellaneous
0
Views
0
Downloads
0
Favorites
escape2display test2
//+------------------------------------------------------------------+
//| escape.mq4 |
//| Copyright © 2008, OGUZ BAYRAM |
//| es_cape77@hotmail.com |
//+------------------------------------------------------------------+
extern double lTakeProfit = 40;
extern double sTakeProfit = 40;
extern double lTrailingStop = 40;
extern double sTrailingStop = 40;
extern int Slippage = 3;
extern double Lots = 0.01;
extern int fma =12;
extern int sma =21;
extern int numbot = 1; // number of open trades
extern int StopL = 50; // stop loss
extern color clOpenBuy = Blue;
extern color clCloseBuy = Aqua;
extern color clOpenSell = Red;
extern color clCloseSell = Violet;
extern color clModiBuy = Blue;
extern color clModiSell = Red;
extern string Name_Expert = "Sappie Scalp";
extern bool UseSound = true;
extern bool FractionalPips = true;
extern bool Show_Settings = true;
extern string NameFileSound = "Alert.wav";
int init()
{
//----------------------- SHOW EA SETTING ON THE CHART
//----------------------- SOURCE : CODERSGURU
if(Show_Settings==true) subPrintDetails();
else Comment("");
}
void deinit() {
Comment("");
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start(){
//----------------------- SHOW EA SETTING ON THE CHART
//----------------------- SOURCE : CODERSGURU
// if bars less than 100 dont trade
if(Bars<100){
Print("bars less than 100");
return(0);
}
// number of digits after decimal
if (Digits==3 || Digits==5)
// end if long take profit less than 10
if(lTakeProfit<10){
Print("TakeProfit less than 10");
return(0);
}
// end if short take profit less than 10
if(sTakeProfit<10){
Print("TakeProfit less than 10");
return(0);
}
// assigns values of close price to di close (ie created shorthand for close price
double diClose0=iClose(NULL,5,0);
// assigns values of sma price to dima1 (ie created shorthand
double diMA1=iMA(NULL,5,sma,0,MODE_EMA,PRICE_CLOSE,0);
// assigns values of close price to di close (ie created shorthand for close price
double diClose2=iClose(NULL,5,0);
// assigns values of fma price to dima3 (ie created shorthand
double diMA3=iMA(NULL,5,fma,0,MODE_EMA,PRICE_CLOSE,0);
if(AccountFreeMargin()<(1000*Lots)){
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
//if not (! means not) exitpositions then buy/sell
if (!ExistPositions()){
//if closing price > SMA then buy
if ((diClose0>diMA3)&& (diMA1>diMA3)){
OpenBuy();
return(0);
}
if ((diClose0<diMA1) && (diMA1<diMA3)){
OpenSell();
return(0);
}
// if close price < fma then sell
}
TrailingPositionsBuy(lTrailingStop);
TrailingPositionsSell(sTrailingStop);
return (0);
}
/// egit if more than open trades = true
bool ExistPositions() {
for (int i=numbot; i<OrdersTotal(); i++) { /// changed i++ to i = 10
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol()) {
return(True);
}
}
}
return(false);
}
void TrailingPositionsBuy(int trailingStop) {
RefreshRates();
for (int i=numbot; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol()) {
if (OrderType()==OP_BUY) {
if (NormalizeDouble(Bid,4)-OrderOpenPrice()>trailingStop*Point) {
if (OrderStopLoss()<NormalizeDouble(Bid,4)-trailingStop*Point)
ModifyStopLoss(NormalizeDouble(Bid,4)-trailingStop*Point);
}
}
}
}
}
}
void TrailingPositionsSell(int trailingStop)
{
RefreshRates();
for (int i=numbot; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol()) {
if (OrderType()==OP_SELL) {
if (OrderOpenPrice()-NormalizeDouble(Ask, 4)>trailingStop*Point) {
if (OrderStopLoss()>NormalizeDouble(Ask, 4)+trailingStop*Point )
ModifyStopLoss(NormalizeDouble(Ask, 4)+trailingStop*Point);
}
}
}
}
}
}
void ModifyStopLoss(double ldStopLoss) {
bool fm;
ldStopLoss = ((NormalizeDouble(Bid,4))+StopL*Point); /// line added
fm = OrderModify(OrderTicket(),OrderOpenPrice
(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE);
if (fm && UseSound) PlaySound(NameFileSound);
}
void OpenBuy() {
double ldLot, ldStop, ldTake;
string lsComm;
ldLot = GetSizeLot();
ldStop = ((NormalizeDouble(Bid,4))+StopL*Point); // was 0
ldTake = GetTakeProfitBuy();
lsComm = GetCommentForOrder();
OrderSend(Symbol(),OP_BUY,ldLot,NormalizeDouble(Ask, 4),Slippage,ldStop,ldTake,lsComm,0,0,clOpenBuy);
if (UseSound) PlaySound(NameFileSound);
}
void OpenSell() {
double ldLot, ldStop, ldTake;
string lsComm;
ldLot = GetSizeLot();
ldStop = ((NormalizeDouble(Bid,4))+StopL*Point); // was 0
ldTake = GetTakeProfitSell();
lsComm = GetCommentForOrder();
OrderSend(Symbol(),OP_SELL,ldLot,NormalizeDouble(Bid,4),Slippage,ldStop,ldTake,lsComm,0,0,clOpenSell);
if (UseSound) PlaySound(NameFileSound);
}
string GetCommentForOrder() { return(Name_Expert); }
double GetSizeLot() { return(Lots); }
double GetTakeProfitBuy() { return(NormalizeDouble(Ask, 4)+lTakeProfit*Point); }
double GetTakeProfitSell() { return(NormalizeDouble(Bid,4)-sTakeProfit*Point); }
void subPrintDetails()
{
string sComment = "";
string sp = "----------------------------------------\n";
string NL = "\n";
sComment = sp;
sComment = sComment + "lTakeProfit=" + DoubleToStr(lTakeProfit,0) + NL;
sComment = sComment + "sTakeProfit=" + DoubleToStr(sTakeProfit,0) + NL;
sComment = sComment + "lTrailingStop=" + DoubleToStr(lTrailingStop,0) + NL;
sComment = sComment + "sTrailingStop=" + DoubleToStr(sTrailingStop,0) + NL;
sComment = sComment + sp;
sComment = sComment + "FractionalPips=" + FractionalPips + NL;
sComment = sComment + "Lots=" + DoubleToStr(Lots,2) + NL;
sComment = sComment + sp;
Comment(sComment);
}
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---