This script is designed to automatically trade on the Forex market, specifically focusing on "grabbing small profits and going". It's built around the concept of "envelopes" around a moving average, which are used to identify potential buy and sell zones.
Here's the breakdown:
-
Core Idea: The script watches how the price of a currency pair fluctuates around a moving average. A moving average is simply the average price over a certain period. The script then creates upper and lower "envelopes" based on a certain percentage above and below this moving average.
-
Order Placement: The script aims to place "buy limit" and "sell limit" orders. A "buy limit" order is an order to buy a currency pair if the price drops to a certain level. A "sell limit" order is an order to sell a currency pair if the price rises to a certain level.
- Buy Orders: The script looks to place "buy limit" orders when the recent closing price was above the moving average but still below the upper envelope.
- Sell Orders: Conversely, it looks to place "sell limit" orders when the recent closing price was below the moving average but still above the lower envelope.
-
Order Timing: The script is only active during specific hours of the day, defined by
TimeOpenandTimeClose. This helps focus trading activity during potentially more favorable market conditions. -
Take Profit and Stop Loss:
- Take Profit: When placing orders, the script sets a "take profit" level. This is the price at which the order will automatically close, securing a small profit. It has three take profit levels.
- Stop Loss: The script also sets a "stop loss" level. This is a price where the order will automatically close if the market moves against the trade, limiting potential losses. The initial stop loss for buy orders is set to the lower envelope line and the upper envelope line for sell orders.
-
Lot Size Management: The script dynamically adjusts the size of its trades ("lot size") based on the account balance and a risk percentage defined by the user. It also reduces the lot size if there has been a recent string of losing trades, helping to preserve capital during downturns.
-
Trailing Stop Loss: Once a trade is open, the script attempts to protect profits by using a "trailing stop loss". This means the stop-loss level is adjusted as the price moves in a favorable direction. It can use either the moving average or the opposite envelope line as a basis for the trailing stop loss.
-
Order Management: The script manages existing orders and deletes pending "buy limit" and "sell limit" orders at the end of the trading day (
TimeClose). The script also keeps track of the order ID's and resets them when the order closes so that new orders can be placed.
In essence, this script is an automated system designed to identify potential trading opportunities based on price fluctuations around a moving average and its "envelopes", aiming to capture small profits while limiting risk.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
/* this expert is a spinoff of the previous envelope ea mod's, however
this one uses Limit orders to buy and sell when Close[1] is above the ma but below
bline, & below the ma but above the sline respectively. initial SL is the sline for
buylimit orders and the bline for selllimit orders. TP's are much smaller, grab it and
go philosophy.
if one backtests on 5min data, then selects open chart, mt4 will open a chart with the
ma & it's accompanying envelopes along with all the buy and sell arrows, sl's & tp's.
this will aid in understanding what this ea is meant to do, and how it performed given
past data. i have more that a year's worth of 1 & 5 minute gbpusd data, email me should
you want to import it into your copy of mt4. gl/gt tag.
*/
//+------------------------------------------------------------------+
//| e.2.16 5 min.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, tageiger aka fxid10t@yahoo.com"
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "fxid10t@yahoo.com"
#property link "http://finance.groups.yahoo.com/group/MetaTrader_Experts_and_Indicators/join"
//---- input parameters
extern int EnvelopePeriod =144;//ma bars
extern int EnvTimeFrame =0; //envelope time frame: 0=chart,60=1hr,240=4hr, etc.
extern int EnvMaMethod =1; //0=sma,1=ema,2=smma,3=lwma.
extern double EnvelopeDeviation =0.05;//%shift above & below ma
extern int MaElineTSL =1;//0=iMA trailing stoploss 1=Opposite Envelope TSL
extern int TimeOpen =0;//time order placement can begin
extern int TimeClose =23;//open order deletion time
extern double FirstTP =8.0;
extern double SecondTP =13.0;
extern double ThirdTP =21.0;
extern double Lots =0.1;//initial lot size
extern double MaximumRisk =0.02;//percentage of account to risk each order
extern double DecreaseFactor =3;//lot size reducer during losing streak
int b1,b2,b3,s1,s2,s3;
double TSL =0;
double LotsOptimized() {
double lot=Lots;
int orders=HistoryTotal(); // history orders total
int losses=0; // number of losses orders without a break
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
if(DecreaseFactor>0) {
for(int i=orders-1;i>=0;i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
if(OrderProfit()>0) break;
if(OrderProfit()<0) losses++;
}
if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
}
if(lot<0.1) lot=0.1;
return(lot);
}
int init() { return(0); }
int deinit() { return(0); }
int start() {
int p=0; p=EnvelopePeriod;
int etf=0; etf=EnvTimeFrame;
int mam=0; mam=EnvMaMethod;
double d=0; d=EnvelopeDeviation;
double btp1,btp2,btp3,stp1,stp2,stp3;
double bline=0,sline=0,ma=0,spread,eup,edown;
int cnt, ticket, total;
ma=iMA(NULL,etf,p,0,mam,PRICE_CLOSE,0);
eup=iEnvelopes(NULL,etf,p,mam,0,PRICE_CLOSE,d,MODE_UPPER,0)+(Ask-Bid);
edown=iEnvelopes(NULL,etf,p,mam,0,PRICE_CLOSE,d,MODE_LOWER,0)-(Ask-Bid);
spread=Ask-Bid;
bline=eup+spread;
sline=edown-spread;
total=OrdersTotal();
if(OrdersTotal()==0) { b1=0;b2=0;b3=0;s1=0;s2=0;s3=0; }
if(OrdersTotal()>0) {
for(cnt=0;cnt<total;cnt++) {
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if(OrderMagicNumber()==2) { b1=OrderTicket(); }
if(OrderMagicNumber()==4) { b2=OrderTicket(); }
if(OrderMagicNumber()==6) { b3=OrderTicket(); }
if(OrderMagicNumber()==1) { s1=OrderTicket(); }
if(OrderMagicNumber()==3) { s2=OrderTicket(); }
if(OrderMagicNumber()==5) { s3=OrderTicket(); }
}
}
if(b1==0) {
if(Hour()>TimeOpen && Hour()<TimeClose) {
if(Close[1]>ma && Close[1]<bline) {
btp1=(NormalizeDouble(ma,4))+(FirstTP*Point);
ticket=OrderSend(Symbol(),
OP_BUYLIMIT,
LotsOptimized(),
(NormalizeDouble(ma,4)),
0,
(NormalizeDouble(sline,4)),
btp1,
"e.2.16 BL1",
2,
TimeClose,
Aqua);
if(ticket>0) {
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
{ b1=ticket; Print(ticket); }
else Print("Error Opening BuyLimit Order: ",GetLastError());
return(0);
}
}
}
}
if(b2==0) {
if(Hour()>TimeOpen && Hour()<TimeClose) {
if(Close[1]>ma && Close[1]<bline) {
btp2=(NormalizeDouble(ma,4))+(SecondTP*Point);
ticket=OrderSend(Symbol(),
OP_BUYLIMIT,
LotsOptimized(),
(NormalizeDouble(ma,4)),
0,
(NormalizeDouble(sline,4)),
btp2,
"e.2.16 BL2",
4,
TimeClose,
Aqua);
if(ticket>0) {
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
{ b2=ticket; Print(ticket); }
else Print("Error Opening BuyLimit Order: ",GetLastError());
return(0);
}
}
}
}
if(b3==0) {
if(Hour()>TimeOpen && Hour()<TimeClose) {
if(Close[1]>ma && Close[1]<bline) {
btp3=(NormalizeDouble(ma,4))+(ThirdTP*Point);
ticket=OrderSend(Symbol(),
OP_BUYLIMIT,
LotsOptimized(),
(NormalizeDouble(ma,4)),
0,
(NormalizeDouble(sline,4)),
btp3,
"e.2.16 BL3",
6,
TimeClose,
Aqua);
if(ticket>0) {
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
{ b3=ticket; Print(ticket); }
else Print("Error Opening BuyLimit Order: ",GetLastError());
return(0);
}
}
}
}
if(s1==0) {
if(Hour()>TimeOpen && Hour()<TimeClose) {
if(Close[1]<ma && Close[1]>sline) {
stp1=NormalizeDouble(ma,4)-(FirstTP*Point);
ticket=OrderSend(Symbol(),
OP_SELLLIMIT,
LotsOptimized(),
(NormalizeDouble(ma,4)),
0,
(NormalizeDouble(bline,4)),
stp1,
"e.2.16 SL1",
1,
TimeClose,
HotPink);
if(ticket>0) {
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
{ s1=ticket; Print(ticket); }
else Print("Error Opening SellLimit Order: ",GetLastError());
return(0);
}
}
}
}
if(s2==0) {
if(Hour()>TimeOpen && Hour()<TimeClose) {
if(Close[1]<ma && Close[1]>sline) {
stp2=NormalizeDouble(ma,4)-(SecondTP*Point);
ticket=OrderSend(Symbol(),
OP_SELLLIMIT,
LotsOptimized(),
(NormalizeDouble(ma,4)),
0,
(NormalizeDouble(bline,4)),
stp2,
"e.2.16 SL2",
3,
TimeClose,
HotPink);
if(ticket>0) {
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
{ s2=ticket; Print(ticket); }
else Print("Error Opening SellLimit Order: ",GetLastError());
return(0);
}
}
}
}
if(s3==0) {
if(Hour()>TimeOpen && Hour()<TimeClose) {
if(Close[1]<ma && Close[1]>sline) {
stp3=NormalizeDouble(ma,4)-(ThirdTP*Point);
ticket=OrderSend(Symbol(),
OP_SELLLIMIT,
LotsOptimized(),
(NormalizeDouble(ma,4)),
0,
(NormalizeDouble(bline,4)),
stp3,
"e.2.16 SL3",
5,
TimeClose,
HotPink);
if(ticket>0) {
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
{ s3=ticket; Print(ticket); }
else Print("Error Opening SellLimit Order: ",GetLastError());
return(0);
}
}
}
}
for(cnt=0;cnt<total;cnt++) {
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_BUY) {
if(MaElineTSL==0) {TSL=NormalizeDouble(ma,4); }
if(MaElineTSL==1) {TSL=NormalizeDouble(sline,4); }
if(Close[0]>OrderOpenPrice()) {
if((Close[0]>bline) && (TSL>OrderStopLoss())) {
double bsl;bsl=TSL;
OrderModify(OrderTicket(),
OrderOpenPrice(),
bsl,
OrderTakeProfit(),
0,//Order expiration server date/time
Green);
return(0);
}
}
}
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_SELL) {
if(MaElineTSL==0) {TSL=NormalizeDouble(ma,4); }
if(MaElineTSL==1) {TSL=NormalizeDouble(bline,4); }
if(Close[0]<OrderOpenPrice()) {
if((Close[0]<sline) && (TSL<OrderStopLoss())) {
double ssl;ssl=TSL;
OrderModify(OrderTicket(),
OrderOpenPrice(),
ssl,
OrderTakeProfit(),
0,//Order expiration server date/time
Red);
return(0);
}
}
}
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(Hour()==TimeClose && OrderType()==OP_BUYLIMIT) {
OrderDelete(OrderTicket());
if(OrderTicket()==b1) {b1=0; return;}
if(OrderTicket()==b2) {b2=0; return;}
if(OrderTicket()==b3) {b3=0; return;}
}
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(Hour()==TimeClose && OrderType()==OP_SELLLIMIT) {
OrderDelete(OrderTicket());
if(OrderTicket()==s1) {s1=0; return;}
if(OrderTicket()==s2) {s2=0; return;}
if(OrderTicket()==s3) {s3=0; return;}
}
OrderSelect(b1,SELECT_BY_TICKET);
if(OrderClosePrice()>0) {b1=0;}
OrderSelect(b2,SELECT_BY_TICKET);
if(OrderClosePrice()>0) {b2=0;}
OrderSelect(b3,SELECT_BY_TICKET);
if(OrderClosePrice()>0) {b3=0;}
OrderSelect(s1,SELECT_BY_TICKET);
if(OrderClosePrice()>0) {s1=0;}
OrderSelect(s2,SELECT_BY_TICKET);
if(OrderClosePrice()>0) {s2=0;}
OrderSelect(s3,SELECT_BY_TICKET);
if(OrderClosePrice()>0) {s3=0;}
}
return(0);
}
Comments