BTC USD 77,443.9 Gold USD 4,366.84
Time now: Jun 1, 12:00 AM

Belajar Coding EA (mql) asas

tq.. sy cuma penin logik dia tu je.. ada mql help sy bukukan.. sampai 400 muka surat print.. tapi tak paham.. english :(
 
jumlah semua pos guna OrdersTotal(). tapi dia akan beri jumlah semua pos termasuk lain2 pair dan pending order
kalau nak kira current chart aje kena iterate guna for dan filter yang mana tak nak

contoh ea boleh jadi macam ni

Code:
int start ()
{
   [COLOR="Gray"]// kira total pos untuk current chart[/COLOR]
   int totalPos = 0; [COLOR="gray"]// declare variable untuk total pos[/COLOR]
   for (int i=0; i<OrdersTotal(); i++) [COLOR="gray"]// loop 1 per 1 [/COLOR]
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES); [COLOR="Gray"]// select pos ikut turutan ticket #[/COLOR]
      if (OrderSymbol()!=Symbol) continue;  [COLOR="Gray"]// sekiranya pair bukan current chart.... skip[/COLOR]
      if (OrderType()!=OP_BUY || OrderType()!=OP_SELL) continue; [COLOR="gray"]// kalau bukan BUY atau SELL.... skip[/COLOR]
      totalPos++; [COLOR="gray"]// totalPos tambah 1[/COLOR]
   }


   if (totalPos==0) [COLOR="gray"]// sekiranya totalpos = 0 [/COLOR]
   {
      for (int i=0; i<10; i++) [COLOR="Gray"]// repeat 10 kali[/COLOR][COLOR="Lime"] <--- camne komand ni aktif? padahal bro hanya tulis ordersend skali??[/COLOR]
      {
         OrderSend(Symbol(),OP_BUY,1,Ask,3,0,Ask+10*Point,"My order buy",0,0,Green); [COLOR="gray"]// buy 1 lot tp 10[/COLOR]
         Sleep (1000); [COLOR="gray"]// delay 1 saat[/COLOR]
         OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Bid-10*Point,"My order sell",0,0,Red); [COLOR="gray"]// sell 1 lot tp 10[/COLOR]
         Sleep (1000); [COLOR="gray"]// delay 1 saat[/COLOR]
      }
   }
   
   return (0);
}

Untuk open pos, guna function OrderSend. OrderSend boleh diguna banyak cara, boleh jugak untuk PO. Sila refer ke help library dalam metaeditor untuk syntax OrderSend

Kalau nak set variable, contoh nilai lot dan tp jadi variable

Code:
extern double [COLOR="Red"]lot [/COLOR]= 1.0;
extern int [COLOR="red"]takeProfit [/COLOR]= 10.0;

lepas tu dalam OrderSend jadi

Code:
[COLOR="Red"]double tpBuy=0;
double tpSell=0;
if (takeprofit>0)
{
  tpBuy = Ask+takeProfit*Point;
  tpSell = Bid-takeProfit*Point;
}[/COLOR]
OrderSend(Symbol(),OP_BUY,[COLOR="Red"]lot[/COLOR],Ask,3,0,[COLOR="Red"]tpBuy[/COLOR],"My order buy",0,0,Green);
OrderSend(Symbol(),OP_SELL,[COLOR="Red"]lot[/COLOR],Bid,3,0,[COLOR="Red"]tpSell[/COLOR],"My order sell",0,0,Red);

tq bro aiman.. sangat sangat membantu.. er.. nak tanye.. ni boleh terus compile ke? atau camne nak jadikan code ni ea? :)
 
Last edited:
for function

boleh cuba compile terus guna metaeditor, saya taip terus kat sini, mungkin ada silap mana2 bro kena check.
MQL ni case sensitive, typo salah sikit je terus keluar error

untuk for () function mesti ada 3 bahagian dalam bracket. Ini merupakan loop function

Code:
for ([COLOR="SeaGreen"]int i=0[/COLOR]; [COLOR="Purple"]i<10;[/COLOR] [COLOR="Blue"]i++[/COLOR])
{
	[COLOR="Gray"]// code anda akan diulang 10 kali[/COLOR]
}

1. int i=0 = number loop start
2. i<10; = berapa kali nak ulang
3. i++ = update loop


kalau nak loop 5 kali
Code:
for (int i=0; i<[COLOR="Red"]5[/COLOR]; i++)
{
	[COLOR="Gray"]// code anda akan diulang 5 kali[/COLOR]
}

boleh juga buat macam ni, reverse style

Code:
for (int i=10; i>0; i--)
{
	[COLOR="gray"]// code anda akan diulang 10 kali[/COLOR]
}

maksud i++ adalah i=i+1
maksud i-- adalah i=i-1

segala apa yang berada di antara { dan } akan diulang
boleh faham tak penerangan saya? maaf lah saya explain ni tak pandai sangat.
 
untuk EA 5 dicimel mcm maner pulak

Saya tak pernah trade dengan 5 dec broker. dan tak pernah buat ea specific untuk 5 decimal.
Setahu saya bezanya 5 dec ada extra 0 pada price.
kalau takeprofit untuk 4 decimal = 10, pada 5 decimal akan jadi 100
Tapi sekiranya tuan nak auto convert 10 jadi 100, cari kat dalam EA tuan setiap Point mesti darab 10.

contoh

4 dec broker
Ask+10*Point

5 dec broker
Ask+(10*Point*10)
 
int start ()
{
// kira total pos untuk current chart
int totalPos = 0; // declare variable untuk total pos
for (int i=0; i<OrdersTotal(); i++) // loop 1 per 1
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES); // select pos ikut turutan ticket #
if (OrderSymbol()!=Symbol) continue; // sekiranya pair bukan current chart.... skip
if (OrderType()!=OP_BUY || OrderType()!=OP_SELL) continue; // kalau bukan BUY atau SELL.... skip
totalPos++; // totalPos tambah 1
}


if (totalPos==0) // sekiranya totalpos = 0
{
for (int i=0; i<10; i++) // repeat 10 kali

{
OrderSend(Symbol(),OP_BUY,1,Ask,3,0,Ask+10*Point,"My order buy",0,0,Green); // buy 1 lot tp 10
Sleep (1000); // delay 1 saat
OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Bid-10*Point,"My order sell",0,0,Red); // sell 1 lot tp 10
Sleep (1000); // delay 1 saat
}
}

return (0);
}

<---- saya tak compile lagi tapi saya cuba nak pahamkan kod ni..
kalau salah tulung perbetulkan ye..

***** apa yang saya paham dengan kod ni ialah dia akan buy dan sell serentak betul tak?..buy 10 post + sell 10 post..dgn tp 10 pip .. kalo takde post akan topup post..kan?

kalo betul camtu maksudnye masih tak kena dgn apa yg saya nak.

camne saya nak setkan hanya buy atau sell post sahaja? (kalo tak silap saya kena pakai extern bool buy = false; kan?? ( setkan kat variable je ) buy = true (buy post) / buy = false ( sell post )


saya cuba koding yek.


if (totalPos==0) // sekiranya totalpos = 0
{
for (int i=0; i<10; i++) // repeat 10 kali
{
(if (buy==true)) //buy position

OrderSend(Symbol(),OP_BUY,1,Ask,3,0,Ask+10*Point,"My order buy",0,0,Green); // buy 1 lot tp 10
Sleep (1000); // delay 1 saat

else

OrderSend(Symbol(),OP_SELL,1,Ask,3,0,Ask+10*Point,"My order sell",0,0,Green); // buy 1 lot tp 10

+++maafla bracket tak brape betul lagi :) hehikx

*alhamdulillah makin bertambah kefahaman sy sbb ada //comment bahasa melayu.. tq bro aiman*:)cgrock
 
Code:
extern bool buy = true;
extern bool sell = true;

int start ()
{
   int totalBuy = 0; [COLOR="Gray"][COLOR="gray"]// buy counter bermula 0[/COLOR][/COLOR]
   int totalSell = 0; [COLOR="gray"][COLOR="gray"]// sell counter bermula 0[/COLOR][/COLOR]
   for (int i=0; i<OrdersTotal(); i++)
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol()!=Symbol) continue;[COLOR="gray"] // bukan current pair... skip[/COLOR]
      if (OrderType()==OP_BUY) totalBuy++; [COLOR="gray"]// sekiranya buy, buy counter tambah 1[/COLOR]
      if (OrderType()==OP_SELL) totalSell++;[COLOR="gray"] // sekiranya sell, sell counter tambah 1[/COLOR]
   }


   if (totalBuy ==0 && buy==true) [COLOR="gray"]// sekiranya buy 0 dan buy variable true[/COLOR]
   {
      for (int i=0; i<10; i++) [COLOR="gray"]// 10 kali buy[/COLOR]
      {
         OrderSend(Symbol(),OP_BUY,1,Ask,3,0,Ask+10*Point,"My order buy",0,0,Green); [COLOR="gray"]// buy 1 lot tp 10[/COLOR]
         Sleep (1000); 
      }
   }

   if (totalSell==0 && sell==true) [COLOR="gray"]// sekiranya sell dan sell variable true[/COLOR]
   {
      for (int i=0; i<10; i++)[COLOR="gray"] // 10 kali sell[/COLOR]
      {
         OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Bid-10*Point,"My order sell",0,0,Red); [COLOR="gray"]// sell 1 lot tp 10[/COLOR]
         Sleep (1000); 
      }
   }  
   return (0);
}

EA ni takkan buy 10 serentak, tapi buy 10 kali dengan delay 1 saat setiap 1.
Mungkin akan ambil masa 10 saat untuk keseluruhan process.
Ia juga tak menjamin akan buka 10 pos, boleh jadi requote atau server busy.
Dan kesemua pos juga berkemungkinan tidak akan dapat sama price, ada bezqa beberapa pips.
 
EA ni takkan buy 10 serentak, tapi buy 10 kali dengan delay 1 saat setiap 1.<---sbb sleep 1000 tu kan?
Mungkin akan ambil masa 10 saat untuk keseluruhan process.<- takkisah (sbab sblum ni sy buy/sell secara manual.. nak bukak 10 post pun dekat 3 minet.
Ia juga tak menjamin akan buka 10 pos, boleh jadi requote atau server busy. <--- boleh letak slippage kan? :) ..sebab kita dah add kommand for (int i=0; i<10; i++) kan? dia akan tambah kalo post kurang dari 10 kan? :)
Dan kesemua pos juga berkemungkinan tidak akan dapat sama price, ada bezqa beberapa pips.

saya cuba compile ye


.tq bro..
 
if (OrderSymbol()!=Symbol())

letak () lepas Symbol
code yang saya kasi tak akan jamin buka 10 pos. Kalau requote atau server bz, pos akan kurang drp 10.
Kalau nak pastikan 10 kena ubah sikit dan guna while.
Skrg susah nak tulis coding pasal guna henfon. Nanti malam free saya tunjukkan camne nak guna while
 

Latest Posts

Live Forex Chart

Currency
Rates
EUR / USD
1.14869
USD / JPY
157.137
GBP / USD
1.33624
USD / CHF
0.82348
USD / CAD
1.39828
EUR / JPY
180.500
AUD / USD
0.71233
Back
Top
Log in Register