BTC USD 81,457.9 Gold USD 4,378.12
Time now: Jun 1, 12:00 AM

Belajar Coding EA (mql) asas

Pastikan ada space depan 10:00
datetime myTime = TimeCurrent() - (TimeCurrent() - StrToTime(today + " 10:00"));
" 10:00", bukan "10:00"

atau pun [STRIKE]tuan[/STRIKE] bro boleh buat
StrToTime(today + " " + "10:00") <-- tambah " "

Sebab kita nak dapatkan format "2012.06.15 10:00"
kalau takde space akan jadi "2012.06.1510:00"
date dan time akan jadi rapat

ohhh, igtkan typing error tu, tq bro aiman, baru faham yang today tu = tarikh hari ini
 
kalau define variable banyak2 sekali boleh x?

Code:
double zon_a, zon_b, zon_c, zon_max, zon_min, zon_patah_balik
int tp1, tp2, tp3, sl1, sl2, sl3, trailingSL

No problem, hujung sekali letak ";"
 
ohhh, igtkan typing error tu, tq bro aiman, baru faham yang today tu = tarikh hari ini

format datetime pada mql dalam bentuk string ialah "YYYY.MM.DD HH:MM"
 
kalau x de space tu nnt error kan?

rasanya tak keluar error, bila format salah, mql akan baca date tu [STRIKE]pada tahun 1970an[/STRIKE] salah. Jadi bila bacaan salah, maka result pun jadi tak betul.
 
Last edited:
ni cuba bro test sendiri, selepas

datetime myTime = TimeCurrent() - (TimeCurrent() - StrToTime(today + " 10:00"));

cuba letak

Print (TimeToStr(StrToTime(today + " 10:00"),TIME_DATE));
Print (TimeToStr(StrToTime(today + "10:00"),TIME_DATE));

Kemudian run script/indi tu,
cuba tenguk kat expert tab 2 datetime tu sama tak
 
Last edited:
bro aiman leh tlg adjust x script buy & sell ni supaya leh guna kat 5decimal broker?

buy
//+------------------------------------------------------------------+
//| "ts" |
//| ts |
//+------------------------------------------------------------------+
//------------------
#property copyright "bailapis"
#property link "bailapis"

//user input
extern double Lots = 0.01;
extern int Slippage = 3;
extern int TP = 0;
extern int SL = 0;
extern bool isIBFXmini=false;

// globals
string mySymbol;
string postfix;

// trade management
double ask, bid, point;

string TradeComment="";
int MagicNumber=12345678;


// used for verbose error logging
#include <stdlib.mqh>


//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+

int init()
{
if(isIBFXmini) postfix="m";

mySymbol="Symbol()"+postfix; OpenPos("Buy");
mySymbol="Symbol()"+postfix; OpenPos("Buy");
mySymbol="Symbol()"+postfix; OpenPos("Buy");
mySymbol="Symbol()"+postfix; OpenPos("Buy");
mySymbol="Symbol()"+postfix; OpenPos("Buy");



}

int start() {}

//ENTRY LONG (buy, Ask)
void OpenPos(string Trade)
{
int gle=0;
int ticket=0;

int loopcount;

loopcount=0;
while(true)
{
bid=MarketInfo(Symbol(),MODE_BID);
ask=MarketInfo(Symbol(),MODE_ASK);
point=MarketInfo(Symbol(),MODE_POINT);

double TPBuy=0;
double SLBuy=0;

if(TP>0){TPBuy=ask+TP*point;}
if(SL>0){SLBuy=ask-SL*point;}

if (Trade=="Buy") ticket=OrderSend(Symbol(),OP_BUY,Lots,ask,Slippage ,SLBuy,TPBuy,TradeComment,MagicNumber ,White);

gle=GetLastError();
if(gle==0)
{
if (Trade=="Buy") Print("BUY PLACED Ticket="+ticket+" Ask="+ask+" Lots="+Lots);
break;
}
else
{
if (Trade=="Buy") Print("-----ERROR----- Placing BUY order: Lots="+Lots+" SL="+SL+" TP="+TP+" Bid="+bid+" Ask="+ask+" ticket="+ticket+" Err="+gle+" "+ErrorDescription(gle));
RefreshRates();
Sleep(500);

// give up after 10 tries (~5 seconds)
loopcount++;
if(loopcount>10)
{
if (Trade=="Buy") Print("-----ERROR----- Giving up on placing BUY order");
return(gle);
}
}
}
}

sell
//+------------------------------------------------------------------+
//| "ts" |
//| ts |
//+------------------------------------------------------------------+
//------------------
#property copyright "sellapis"
#property link "sellapis"

//user input
extern double Lots = 0.01;
extern int Slippage = 3;
extern int TP = 0;
extern int SL = 0;
extern bool isIBFXmini =false;

// globals
string mySymbol;
string postfix;

// trade management
double ask, bid, point;

string TradeComment="";
int MagicNumber=12345678;


// used for verbose error logging
#include <stdlib.mqh>


//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int init()

{

if(isIBFXmini) postfix="m";

mySymbol="Symbol()"+postfix; OpenPos("Sell");
mySymbol="Symbol()"+postfix; OpenPos("Sell");
mySymbol="Symbol()"+postfix; OpenPos("Sell");
mySymbol="Symbol()"+postfix; OpenPos("Sell");
mySymbol="Symbol()"+postfix; OpenPos("Sell");



}

int start() {}

//ENTRY LONG (buy, Ask)

void OpenPos(string Trade)

{

int gle=0;
int ticket=0;

int loopcount;

loopcount=0;
while(true)

{

bid=MarketInfo(Symbol(),MODE_BID);
ask=MarketInfo(Symbol(),MODE_ASK);
point=MarketInfo(Symbol(),MODE_POINT);

double TPSell=0;
double SLsell=0;

if(TP>0){TPSell=bid-TP*point;}
if(SL>0){SLsell=bid+SL*point;}

if (Trade=="Sell") ticket=OrderSend(Symbol(),OP_SELL,Lots,bid,Slippage,SLsell,TPSell,TradeComment,MagicNumber,Red);

gle=GetLastError();
if(gle==0)

{

if (Trade=="Sell") Print("SELL PLACED Ticket="+ticket+" Bid="+bid+" Lots="+Lots);
break;

}

else

{

if (Trade=="Sell") Print("-----ERROR----- placing SELL order: Lots="+Lots+" SL="+SL+" TP="+TP+" Bid="+bid+" Ask="+ask+" ticket="+ticket+" Err="+gle+" "+ErrorDescription(gle));
RefreshRates();
Sleep(500);

// give up after 10 tries (~5 seconds)
loopcount++;
if(loopcount>10)

{

if (Trade=="Sell") Print("-----ERROR----- Giving up on placing SELL order");
return(gle);

}
}
}
}
 
Cari line bawah ni

Code:
point=MarketInfo(Symbol(),MODE_POINT);

kemudian edit macam ni

Code:
[COLOR="Red"]int dec=1; 
if(Digits==5||Digits==3) dec=10;
[/COLOR]point=MarketInfo(Symbol(),MODE_POINT)[COLOR="red"]*dec[/COLOR];
 
Salam tuan aiman,
macam mana nak edit bg EA ni ble pakai kat 5 decimal broker?
sbb pakai di demo ok je...tp di acc live, dia kluar invalid trade volume

buy ea:

#include <stdlib.mqh>
#include <WinUser32.mqh>

// exported variables
extern double BuyLots6 = 0.1;
extern int BuyStoploss6 = 0;
extern int BuyTakeprofit6 = 10;
extern int TrailingGap8 = 8;
extern int NewTakeProfit8 = 30;
extern int TradeUpPoint8 = 5;
extern double Lots8 = 0.1;


// local variables
double PipValue=1; // this variable is here to support 5-digit brokers
bool Terminated = false;
string LF = "\n"; // use this in custom or utility blocks where you need line feeds
int NDigits = 4; // used mostly for NormalizeDouble in Flex type blocks
int ObjCount = 0; // count of all objects created on the chart, allows creation of objects with unique names
int current = 0;

datetime BarTime7 = 0;


int init()
{
NDigits = Digits;

if (false) ObjectsDeleteAll(); // clear the chart


Comment(""); // clear the chart
}

// Expert start
int start()
{
if (Bars < 10)
{
Comment("Not enough bars");
return (0);
}
if (Terminated == true)
{
Comment("EA Terminated.");
return (0);
}

OnEveryNewBar7();

}

void OnEveryNewBar7()
{
if (true == false && false) PipValue = 10;
if (true && (NDigits == 3 || NDigits == 5)) PipValue = 10;
if (BarTime7 < Time[0])
{
// we have a new bar opened
BarTime7 = Time[0]; // keep the new bar open time
BuyOrder6();
TradeUpTrailing8();

}
}

void BuyOrder6()
{
double SL = Ask - BuyStoploss6*PipValue*Point;
if (BuyStoploss6 == 0) SL = 0;
double TP = Ask + BuyTakeprofit6*PipValue*Point;
if (BuyTakeprofit6 == 0) TP = 0;
int ticket = -1;
if (true)
ticket = OrderSend(Symbol(), OP_BUY, BuyLots6, Ask, 4, 0, 0, "Buy SE v2", 1, 0, Blue);else
ticket = OrderSend(Symbol(), OP_BUY, BuyLots6, Ask, 4, SL, TP, "Buy SE v2", 1, 0, Blue);
if (ticket > -1)
{
if (true)
{
OrderSelect(ticket, SELECT_BY_TICKET);
bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, Blue);
if (ret == false)
Print("OrderModify() error - ", ErrorDescription(GetLastError()));
}
TradeUpTrailing8();

}
else
{
Print("OrderSend() error - ", ErrorDescription(GetLastError()));
}
}

void TradeUpTrailing8()
{
double lots = 0;
double takeprofit = 0, stoploss = 0;
for (int i=OrdersTotal()-1; i >= 0; i--)
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == 1)
{
if (OrderType() == OP_BUY && Ask - OrderOpenPrice() > TradeUpPoint8*PipValue*Point && (OrderStopLoss() < Ask-(TradeUpPoint8+TrailingGap8)*PipValue*Point))
{
stoploss = Ask-TrailingGap8*PipValue*Point;
takeprofit = Ask+NewTakeProfit8*PipValue*Point;
if (NewTakeProfit8 == 0) takeprofit = OrderTakeProfit();
bool ret1 = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, OrderExpiration(), White);
if (ret1 == false)
Print("OrderClose() error - ", ErrorDescription(GetLastError()));
else
{
int ticket = -1;
if (true)
ticket = OrderSend(Symbol(), OP_BUY, Lots8, Ask, 4, 0, 0, "Buy SE v2", 1, 0, White);else
ticket = OrderSend(Symbol(), OP_BUY, Lots8, Ask, 4, stoploss, takeprofit, "Buy SE v2", 1, 0, White);
if (ticket > -1)
{
if (true)
{
OrderSelect(ticket, SELECT_BY_TICKET);
bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, 0, White);
if (ret == false)
Print("OrderModify() error - ", ErrorDescription(GetLastError()));
}

}
}

}
if (OrderType() == OP_SELL && OrderOpenPrice() - Bid > TradeUpPoint8*PipValue*Point && (OrderStopLoss() > Bid+(TradeUpPoint8+TrailingGap8)*PipValue*Point))
{
stoploss = Bid+TrailingGap8*PipValue*Point;
takeprofit = Bid-NewTakeProfit8*PipValue*Point;
if (NewTakeProfit8 == 0) takeprofit = OrderTakeProfit();
bool ret2 = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, OrderExpiration(), White);
if (ret2 == false)
Print("OrderModify() error - ", ErrorDescription(GetLastError()));
else
{
int ticket2 = -1;
if (true)
ticket2 = OrderSend(Symbol(), OP_SELL, Lots8, Bid, 4, 0, 0, "Buy SE v2", 1, 0, White);else
ticket2 = OrderSend(Symbol(), OP_SELL, Lots8, Bid, 4, stoploss, takeprofit, "Buy SE v2", 1, 0, White);
if (ticket2 > -1)
{
if (true)
{
OrderSelect(ticket2, SELECT_BY_TICKET);
bool ret4 = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, 0, White);
if (ret4 == false)
Print("OrderModify() error - ", ErrorDescription(GetLastError()));
}

}
}

}

}
}
else
Print("OrderSelect() error - ", ErrorDescription(GetLastError()));

}



int deinit()
{
if (false) ObjectsDeleteAll();


}

sell EA:
#include <stdlib.mqh>
#include <WinUser32.mqh>

// exported variables
extern double SellLots3 = 0.1;
extern int SellStoploss3 = 0;
extern int SellTakeprofit3 = 10;
extern int TrailingStop4 = 8;
extern int NewTakeProfit4 = 30;
extern int TrailingGap4 = 5;


// local variables
double PipValue=1; // this variable is here to support 5-digit brokers
bool Terminated = false;
string LF = "\n"; // use this in custom or utility blocks where you need line feeds
int NDigits = 4; // used mostly for NormalizeDouble in Flex type blocks
int ObjCount = 0; // count of all objects created on the chart, allows creation of objects with unique names
int current = 0;

datetime BarTime1 = 0;


int init()
{
NDigits = Digits;

if (false) ObjectsDeleteAll(); // clear the chart


Comment(""); // clear the chart
}

// Expert start
int start()
{
if (Bars < 10)
{
Comment("Not enough bars");
return (0);
}
if (Terminated == true)
{
Comment("EA Terminated.");
return (0);
}

OnEveryNewBar1();

}

void OnEveryNewBar1()
{
if (true == false && false) PipValue = 10;
if (true && (NDigits == 3 || NDigits == 5)) PipValue = 10;
if (BarTime1 < Time[0])
{
// we have a new bar opened
BarTime1 = Time[0]; // keep the new bar open time
SellOrder3();
TrailingStop4();

}
}

void SellOrder3()
{
double SL = Bid + SellStoploss3*PipValue*Point;
if (SellStoploss3 == 0) SL = 0;
double TP = Bid - SellTakeprofit3*PipValue*Point;
if (SellTakeprofit3 == 0) TP = 0;
int ticket = -1;
if (true)
ticket = OrderSend(Symbol(), OP_SELL, SellLots3, Bid, 4, 0, 0, "Sell SE V2", 2, 0, Red);
else
ticket = OrderSend(Symbol(), OP_SELL, SellLots3, Bid, 4, SL, TP, "Sell SE V2", 2, 0, Red);
if (ticket > -1)
{
if (true)
{
OrderSelect(ticket, SELECT_BY_TICKET);
bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, Red);
if (ret == false)
Print("OrderModify() error - ", ErrorDescription(GetLastError()));
}
TrailingStop4();

}
else
{
Print("OrderSend() error - ", ErrorDescription(GetLastError()));
}
}

void TrailingStop4()
{
for (int i=OrdersTotal()-1; i >= 0; i--)
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == 1)
{
double takeprofit = OrderTakeProfit();

if (OrderType() == OP_BUY && Ask - OrderOpenPrice() > TrailingStop4*PipValue*Point)
{
if (OrderStopLoss() < Ask-(TrailingStop4+TrailingGap4)*PipValue*Point)
{
if (NewTakeProfit4 != 0) takeprofit = Ask+(NewTakeProfit4 + TrailingStop4)*PipValue*Point;
bool ret1 = OrderModify(OrderTicket(), OrderOpenPrice(), Ask-TrailingStop4*PipValue*Point, takeprofit, OrderExpiration(), White);
if (ret1 == false)
Print("OrderModify() error - ", ErrorDescription(GetLastError()));
}
}
if (OrderType() == OP_SELL && OrderOpenPrice() - Bid > TrailingStop4*PipValue*Point)
{
if (OrderStopLoss() > Bid+(TrailingStop4+TrailingGap4)*PipValue*Point)
{
if (NewTakeProfit4 != 0) takeprofit = Bid-(NewTakeProfit4 + TrailingStop4)*PipValue*Point;
bool ret2 = OrderModify(OrderTicket(), OrderOpenPrice(), Bid+TrailingStop4*PipValue*Point, takeprofit, OrderExpiration(), White);
if (ret2 == false)
Print("OrderModify() error - ", ErrorDescription(GetLastError()));
}
}
}
}
else
Print("OrderSelect() error - ", ErrorDescription(GetLastError()));

}



int deinit()
{
if (false) ObjectsDeleteAll();


}

p/s: ini EA dr thread tuan akulascalper

:)
 

Live Forex Chart

Currency
Rates
EUR / USD
1.14790
USD / JPY
156.877
GBP / USD
1.33950
USD / CHF
0.82250
USD / CAD
1.39985
EUR / JPY
180.080
AUD / USD
0.71320
Back
Top
Log in Register