BTC USD 84,729.0 Gold USD 4,285.46
Time now: Jun 1, 12:00 AM

Mari Buat the BEST EA together!

ExpertTrader

Fun Poster
Messages
889
Joined
Dec 25, 2009
Messages
889
Reaction score
1,128
Points
91
In this thread, hopefully, together, we will develop the BEST EA - I believe now, more minds put into one EA is better -

There are only 2 criteria to develop the BEST EA in the world:
1) We have the BEST Trading Strategy (jemput sapa2 ada idea)
2) We have the BEST EA coding (juga sesapa rasa terer)

Here are the steps necessary in order to achieve this Big Target :

1) Money Management - defining LotSize to trade
2) Defining Stop Loss Points, Magic Number

(the rest of the space is reserved for future)
 
The Basic Codes

Let us start!

This is the Basic Codes :

//+------------------------------------------------------------------+
//| Ultimate1.mq4
//|
//| First Update : 23 July 2010 - Description : Developing the Best EA
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, UltimateForexEA"
#property link "http://www.ultimateforexea.com"


extern string Pair = "EURUSD";
extern int TimeFrame = 60;
extern int Slippage = 5;

extern double Risk_Percent = 2.0;
extern double TP_Percent = 2.0;



int init()
{

CheckGlobalVariable();


return(0);
}

int deinit()
{

return(0);
}

int start()
{

LotSizeCalculation();

//The Rule of Opening Trades


//The Rule of Closing Trades


return(0);
}

void LotSizeCalculation()
{


}

void CheckGlobalVariable()
{

}



(the next comment is also reserved for future used)
 
The Money Management - The LotSize Management

1) Money Management - Defining Lotsize and TP and SL

I decided to cover this topic, because failure to understand this, will lead to Margin Call, as fast as before you can even enjoy the money you make from the forex market - manually or by EA.

First, we need to understand that Money Management and Lot Management is quite different thing.

It is well said, hundreds of time, from the Otais out there, we should be trading with 2% risk at any time. What does this actually mean ?

This actually means that, let say you have USD 1000 as your trading capital to start.

2% risk is by definition if you open your trade and it hits the Stop Loss - you should ONLY be losing by 1000x0.02 = USD 20 each trade.

I will explain further by question and answer -

With USD 1000 trading capital, using Standard Account, how can I only risk 2% each trade ? Can I open 1 Standard Lot ?

Ans : Following the rule (bold letter) above, it does not say that how much Lot we should open, only saying that for USD1000, if we loss, at 2% is only equal to USD 20. BUT, think - can we open 1 Standard Lot, trading this Standard Account ?

Say we use 1 Standard Lot and the Leverage that we select is 1:200 - so, where do we set our SL, so that if we hit SL, we are only risking 2% of our account ?

First of ALL, we need to understand, whether we can OPEN that 1 Lot based on our Margin Requirement and the Leverage selected:
In Standard Account, it is called 100K account - and if you have leverage of 1:200, this means that the margin required to open 1 Lot Standard is :

Margin Required :
==> (Price of the Pair x Volume)/200 ==> there are several distinction between brokers to calculate the margin required, but this is fxopen eg.
==> meaning if the price of EURUSD now is 1.2887 and Volume is the size of the account that you want to trade, that is 1 Lot of Standard account = 100,000
==> (1.2887 x 100,000)/200 = USD 644.35 ==> this means that YES, you can trade 1 Lot if your equity is 1000USD

Also, you must understand that, trading 1 Lot Standard Account, 1 pip move is USD 10.

So, to risk 2% of your account, u can only afford to risk only 2 pip, which, by no means workable, because due to spread already, your 2% is reached.

So, the answer is with 1000usd as your start trading capital, YOU cannot trade 1 Standard Lot, if you want to Risk only 2%!!

Next Question - so, again with my 1000usd as trading capital, how much lot should I open and SL should I set, in order to only risk 2% of my equity ?
Ans : There is ONE rule of thumb that I read somewhere saying that To KNOW YOUR LOTSIZE to trade, always divide your starting equity with equityx10, ie, if your equity is 1000usd, then you should be trading with 1000/(1000x10) = 0.1 Lot Standard.

With 0.1 Lot Standard, 1 pip move will use only 1usd, meaning to say, to risk USD20 per trade, your SL should be set at -20 pip from the BUY price or +20 pip from the SELL price.

Now, tell me, do you have A TRading Strategy that is so good that it WONT always hit the SL set at -20 pip ?
 
continue.. 1) Money Management - Defining Lotsize and TP and SL


Q : So, is the rule of LotSize = Equity/(Equityx10) is Correct for you to risk 2% of your account ?

A : Yes, if you have Trading Strategy that is SO GOOD that the rate of hitting SL is less than hitting TP that is set much higher than SL.
BUT, by experience - it is NOT - the answer is NO.

Q : So, again, what is the Rule of Opening the LotSize in order to ONLY risk 2% of my account ?

A : It always depend on the Trading Strategies. There are several KNOWN trading stragies out there - SCALP, Long Term, Directional, Hedging, Pyramiding, Correlation, Martingle, GRID, etc. Each strategy requires different LotSize management. Money Management is Simple = how much Risk you want to set, every time trade is open. LotSize Management is quite complex and it is a subset of Money Management.

So, with 1000usd, if we have a trading strategy that is Directional, based on Indicator, and our SL is normally set at -40 pips, we need to use 0.05 Lot Standard every time we open a trade.

Q : But, somehow, my Broker, eg FxOpen, do not allow me to Trade with 0.05 Lot, the minimum is 0.1 Lot, so what should I do ?

A : The obvious answer is to raise your equity / trading capital, but right now, almost all Brokers have either Mini or Micro account, so u need to open either Mini or Micro account.

For example, say in IBFX - with 1000usd, mini account (also called 10K account), if your strategy says -40 pip SL, you want that 1 pip = 0.5usd. 1 Mini Lot = 1usd, meaning to say, we need to open 0.5 Mini Lot in order to risk 2%.

However, there are variations among Forex Brokers, with regards to Minimum Lot to Open, Maximum Lot to Open, Highest Leverage, Margin requirement, Lot Step Size, which can be determined by UNDERSTANDING the MarketInfo() function :

In this LotSize Management Case, The most important parameters are :

MODE_LOTSIZE ==> LotSize in base currency , 100000 = standard account, 10000 = mini account
MODE_MINLOT ==> minimum permitted amount of a lot
MODE_LOTSTEP ==> steps for changing lot
MODE_MAXLOT ==> maximum permitted amount of a lot
MODE_MARGINREQUIRED ==> free margin required to open one lot
MODE_TICKVALUE ==> Tick Value in Deposit Currency
(detail here :http://docs.mql4.com/constants/marketinfo)

We need to understand this function as well : AccountFreeMargin()

LotSizes for other strategies :
For Grid strategy, how many grid you want to open, if say your max grid to open is 10, then split the lotsize to another 10, ie, with 1000usd, mini account, IBFX for example, our EA should open 0.05 mini Lot per grid line(if we satisfy the broker's MODE_MINLOT)
For Martingale Strategy, the First Open Lot, is way much smaller, and it depends on what's the maximum layer the martingle need to open the positions. I would start my Martingale EA with 0.005 mini Lot or 0.5 micro lot as a start. (My Special Martingale EA does not use this Rule, just fyi)

Q : So, what then, the Best Formula of LotSizeCalculation, regardless of What Equity do I start and What Leverage do I select, in order for me to Trade with my EA ?

This is quite TRICKY, but you have to understand : - If you want to risk 2% then you have to fix one of the variables: the stop loss level or the LotSize
If we want to fix the SL, then the LotSize is varied, and if we want to fix the LotSize, the SL is varied.
The better way is always to find a way so that the LotSize can be incremented, based on the Equity that we have.


//Define Variables
double LotSize;
extern double Risk_Percent = 2.0;
extern double TP_Percent = 4.0;
extern double StopLoss = 40;
extern double LotFactor = 1.0; //1 = Directional, 0.1 = Grid, 0.01 = Martingale

void LotSizeCalculation()
{

if(MarketInfo(Pair,MODE_MINLOT) == 0.1) int LotSizeDigit = 1;
else if(MarketInfo(Pair,MODE_MINLOT) == 0.01) LotSizeDigit = 2;
double MinLots = NormalizeDouble(MarketInfo(Pair,MODE_MINLOT),LotSizeDigit);
double MaxLots = NormalizeDouble(MarketInfo(Pair,MODE_MAXLOT),LotSizeDigit);
double AccountFM = NormalizeDouble(AccountFreeMargin(),2);

if (MarketInfo(Pair, MODE_LOTSIZE) == 100000) LotSize = (AccountFM*(Risk_Percent/100) * LotFactor) / (StopLoss * MarketInfo(Pair, MODE_TICKVALUE));
else if (MarketInfo(Pair, MODE_LOTSIZE) == 10000) LotSize = (AccountFM*(Risk_Percent/100) * LotFactor)/ (StopLoss * MarketInfo(Pair, MODE_TICKVALUE));
else if (MarketInfo(Pair, MODE_LOTSIZE) == 1000) LotSize = (AccountFM*(Risk_Percent/100) * LotFactor)/ (StopLoss * MarketInfo(Pair, MODE_TICKVALUE));


LotSize = NormalizeDouble(LotSize,LotSizeDigit);

if(LotSize > MaxLots) LotSize = MaxLots;
if(LotSize < MinLots)
{
LotSize = MinLots;
Alert("Your Equity is Below Tradeable Lots, EA changed to Min Lot possible."); //Message Box will pop up
Print("Your Equity is Below Tradeable Lots, EA changed to Min Lot possible."); //This message will appear in Expert tab

}

}


Q : Is there any way that I can do in the Program so that I dont have to key in the 'm' for mini or '_" for the Symbol and let the Program automatically figure it out ?

A : Add this function -

void AdjustSymbolForMiniMicro()
{
if (StringLen(Symbol()) == 7)
{
Pair = Pair + StringSubstr(Symbol(), 6, 1);

}
}


Q : Why don't u include the Margin Requirement and Leverage calculation into the LotSize Calculation ?

A : Although there are other people who include the Margin Requirement into the calculation of the LotSize, however, in my opinion, and in this context of defining that 2% Risk - we have achieved the target.
However, the Margin Requirement and the Leverage selection, is very important, if our strategy involves Grid, Pyramiding or Martingale, simply because, we need a lot of margin in order to open more trades.
This is where the money management topic is really an issue - you have to find out, how to risk only 2% of your equity when ALL the positions hit the stop loss and not only a single position.
I will leave this to your own judgment once you have more experience in this.
 
Last edited:
kalau saya x pernah coding ea blh buat juga ke masta?atau perlu kemahiran dlm coding je blh buat
 
Salam tt,

kalau kita buat ea based on moving average n macd saja, ok ke. Cth kalau ma(ema, lmwa n bla2)confirm silang selepas 2nd bar closed and macd bersilang pun 2nd bar closed. Take profit 10-20 pip je dlm market trending. Sideway ea dok diam2 sengih je.
 

Latest Posts

Live Forex Chart

Currency
Rates
EUR / USD
1.14250
USD / JPY
157.245
GBP / USD
1.32505
USD / CHF
0.82827
USD / CAD
1.41455
EUR / JPY
179.652
AUD / USD
0.70550
Back
Top
Log in Register