MT4 Expert Advisor Full Script for Martingale & Grid Express in Less than 50 Lines of Code
EA automated trading using Martingale and Grid strategy
There are many variants of this script but I have simplified it to only 50 lines with the following benefits.
- If your server is closed by whatever reason, simply reopen any MT4, login to the same account and plugin this script where all previous trades will be continued.
- As the take profit point is set, whether the server is down or the account being disconnected, the orders will close automatically. This also reduces the server message to your brokerage.
- All variables are optimised to the datatype that they are required, unnecessary global variables are being taken out, the loopings are being reduced and no additional functions to reduce the system's memory.
There are only 4 external variables and it can be configured to all timeframe and symbols.
- Lot = 0.01
This is the lot size for the script to start trading. Do note that if a $4,000 account can withstand 0.01 lot, it does not mean $8,000 can withstand a 0.02 lot. Mostly it will be in a multiplier of 2.5 meaning a $10,000 is necessary for a 0.02 lot account. You may need to do a lot of trial and error to find the most optimised number. - LotExponent = 1.667
The multiplier for each trade after the first. As the next lot size is rounded off, anything below 1.5 will result in the same lot size. To ride through longer trend, it can even be set to 1.1 but the profit will also be greatly affected. - PipStep = 180
180 is ideal for hourly trade but it still depends on the pair you are trading with. Whether the order is a buy or sell, as long as a new time bar does not exceed or fall short of the previous entry price, the script will not make a new trade. - TakeProfit=70
When the trend reverses, this is how many points you want to take the profit at. 70 is good for H1 and reduce it if the time period is shortened.
I have tried M1 with PipStep=10 and TakeProfit=12, it can generate more than 80% at some months while the account can also be wiped out within a day. This script is not rocket science but hardwork to find out what are the best settings and pairs.
extern double Lots=0.01,LotExponent=1.667;
extern short PipStep=180,TakeProfit=70;
int ihl;
int start() {
if(ihl!=Time[0]) { // Not trading in the same candle
ihl=Time[0];
bool c; // Local variables
char li,i,id=2;
double dP=PipStep*Point,dL,dT,dO;
for(i=OrdersTotal()-1; i>=0; i--) { // To select number of orders and last price (if any)
c=OrderSelect(i,0);
if(OrderSymbol()==Symbol()){
if(dL==0) dL=OrderOpenPrice();
li++;
}
}
for(i=OrdersTotal()-1; i>=0; i--) { // Select the previous Order direction
c=OrderSelect(i,0);
if(OrderSymbol()==Symbol()){
id=OrderType();
break;
}
}
if(li==0||(id==0&&dL-Ask>=dP)||(id==1&&Bid-dL>=dP)) { // Entering the trade
if((li==0&&iHigh(Symbol(),0,1)>iLow(Symbol(),0,2)&&iRSI(0,60,14,0,1)>30)||id==1)
c=OrderSend(Symbol(),1,NormalizeDouble(Lots*MathPow(LotExponent,li),2),Bid,3,0,0);
else
c=OrderSend(Symbol(),0,NormalizeDouble(Lots*MathPow(LotExponent,li),2),Ask,3,0,0);
for(i=OrdersTotal()-1; i>=0; i--) { // Recalculating the price to take profit
c=OrderSelect(i,0);
if(OrderSymbol()==Symbol()) {
dT+=OrderOpenPrice()*OrderLots();
dO+=OrderLots();
}
}
dT=NormalizeDouble(dT/dO,Digits);
for(i=OrdersTotal()-1; i>=0; i--) { // Modifying all orders
c=OrderSelect(i,0);
if(OrderSymbol()==Symbol())
c=OrderModify(OrderTicket(),dT,OrderStopLoss(),OrderType()==0?dT+TakeProfit*Point:dT-TakeProfit*Point,0);
}
}
}
return 0;
}
And once again do support me by subscribing under my introductory broker with IC Markets at https://icmarkets.com/?camp=34606. I have also written another article to create cheap hosting and run the MT4 script. Do comment or email me at aloycwl@gmail.com if there is any question or feedback.
Comments
Post a Comment