//+------------------------------------------------------------------+
//| 23 Dasar-Dasar MQL.mq4 |
//| Copyright 2017, SoeHoe.net |
//| https://SoeHoe.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Putra Kuningan Group"
#property link "https://putrakuningan.net"
#property version "1.0"
#property strict
//Bagian Input Data
input int TakeProfit = 16;
input int StopLoss = 126;
input double Lots = 0.01;
input bool Trailling = true;
input int Magic = 1989;
input string Notes = "Keterangan EA";
//Bagian Input Data
//Telegram
#import "Telegram4Mql.dll"
string TelegramSendTextAsync(string ApiKey, string ChatId, string ChatText);
#import
string apikey = "XXXXXXXXXXXXXXXXXXXXXXXXX";
string chatid = "XXXXXXXXXXXXXXXXXXXXXXXXXX";
int totalord,totalpnd,totalopn;
string mySigalname = "===EA Putra Kuningan 1.0===";
string msg, msgbuy, msgsell, msgclos;
bool SendCloseOrdersMassage = true;
//Akhir Telegram
double pt;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//Tes Order
NewOrder(1);
//Akhir Tes Order
//Telegram
Print(TelegramSendTextAsync(apikey, chatid, "Welcome Back!\nEA Putra Kuningan V.1.0"));
//Akhir Telegram
//Mecari Jumlah Order
if (jumlahorder(OP_SELL))Alert(jumlahorder(OP_SELL),"SELL");
if(jumlahorder(OP_BUY)) Alert(jumlahorder(OP_BUY),"BUY");
//Akhir jumlah order
Alert("===========");
//Membuat trailing stop
bool _Trailing_StopBuy = false;
bool _Trailing_StopSell = false;
_Trailing_StopBuy = Trailing_Stop (Magic,0,100,1);
_Trailing_StopSell = Trailing_Stop (Magic,0,100,1);
//Akhir trailing stop
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
return;
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
/*
if(1==1){
NewOrder(0);
}
if(1==2){
NewOrder(1);
};
dtrailing();
*/
//Telegram Close
CloseOrder ();
//Akhir Telegram
jumlahorder(0);
return;
}
//+------------------------------------------------------------------+
//=========================================== CHECK LIMIT ORDER ===================================
int jumlahorder( int tipe)
{
int total=0;
for(int i=0; i<OrdersTotal(); i++)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))continue;
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic || OrderType()!=tipe) continue;
total++;
}
return(total) ;
}
//=========================================== MEMBUAT ORDER ===================================
//NewOrder();
void NewOrder(int CMD,string myNotes=""){
double pTP = 0, pSL=0, pMarket=0;
//ini merupakan jumlah buy
if(CMD==OP_BUY){
pMarket = Ask;
pTP = pMarket+TakeProfit*Point();
pSL = pMarket-StopLoss*Point();
}
//ini merupakan jumlah sell
if(CMD==OP_SELL){
pMarket = Bid;
pTP = pMarket-TakeProfit*Point();
pSL = pMarket+StopLoss*Point();
}
pTP = NormalizeDouble(pTP,Digits);
pSL = NormalizeDouble(pSL,Digits);
if(TakeProfit<=0) pTP = 0;
if(StopLoss<=0) pSL = 0;
int ticket = OrderSend(Symbol(),CMD,Lots,pMarket,0,pSL,pTP,myNotes,Magic);
//Telegram Open Order
msgsell=StringFormat("%s\n===%s: SELL===\nPrice: %s\nLots: %s\nTakeProfit: %s\nStopLoss: %s \nTime: %s",
mySigalname,OrderSymbol(),
DoubleToString(OrderOpenPrice(),_Digits),
DoubleToString(OrderLots(), 2),DoubleToString(OrderTakeProfit(), Digits),DoubleToString(OrderStopLoss(), Digits), TimeToString(OrderOpenTime()));
msgbuy =StringFormat(" %s\n==* %s: BUY*== \nPrice: %s\nLots: %s\nTakeProfit: %s\nStopLoss: %s \nTime :%s",mySigalname,
OrderSymbol(),
DoubleToString(OrderOpenPrice(),_Digits),
DoubleToString(OrderLots(), 2),DoubleToString(OrderTakeProfit(), Digits),
DoubleToString(OrderStopLoss(), Digits), TimeToString(OrderOpenTime()));
if ((CMD==OP_BUY) ){
Print(TelegramSendTextAsync(apikey, chatid, msgbuy));
}
if((CMD==OP_SELL)) {
Print(TelegramSendTextAsync(apikey, chatid, msgsell));
}
}
//Telegram Close Order
void CloseOrder (){
if(SendCloseOrdersMassage == true) {
if(GlobalVariableGet("_OrdersHistoryTotal") != OrdersHistoryTotal() && OrdersHistoryTotal() > 1) {
for(int i=OrdersHistoryTotal();i>=1;i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) {
if(TimeCurrent()-OrderCloseTime() < 1*60 && OrderType() < 2) {
double profit = (OrderType() == OP_BUY ? OrderClosePrice()-OrderOpenPrice() : OrderOpenPrice()-OrderClosePrice());
string Message = mySigalname
+ "\n Closing " + (OrderType() == OP_BUY ? "BUY" : "SELL") + " Order"
+ "\n Symbol: " + OrderSymbol()
+ "\n Order Ticket #" + IntegerToString(OrderTicket())
+ "\n Opened by price: " + DoubleToString(OrderOpenPrice(),(int)MarketInfo(OrderSymbol(),MODE_DIGITS))
+ "\n Closed by price: " + DoubleToString(OrderClosePrice(),(int)MarketInfo(OrderSymbol(),MODE_DIGITS))
+ (profit >= 0 ? "\n | Profit: " : "\n | Loss: ") + DoubleToString((profit/MarketInfo(OrderSymbol(),MODE_POINT)),0) + " pips";
Print(TelegramSendTextAsync(apikey, chatid, Message));
GlobalVariableSet("_OrdersHistoryTotal",OrdersHistoryTotal());
GlobalVariableSet("_OrdersTotal",OrdersTotal());
break;
}
}
}
}
}
}
//Akhir Telegram
//}
//Telegram
string TypeMnem(int type) {
switch (type) {
case OP_BUY: return("buy");
case OP_SELL: return("sell");
default: return("???");
}
}
//Telegram
//=========================================== INI CODE TRAILING STOP ===================================
bool Trailing_Stop(int MagicIndex, int WaitForProfit, int TrailingStopPoints, int MinAdjustmentPoints)
{
double pnlPoints=0;
double price, sl, tp;
double point = MarketInfo(Symbol(),MODE_POINT);
int stopLevel = int(MarketInfo(Symbol(),MODE_STOPLEVEL) + MarketInfo(Symbol(),MODE_SPREAD));
int cmd;
bool result = true;
double newSl;
int total = OrdersTotal();
for(int i=total-1;i>=0;i--){
if (!OrderSelect(i, SELECT_BY_POS)) continue;
if(OrderMagicNumber() != MagicIndex || OrderSymbol() != Symbol()) continue;
cmd = OrderType();
sl = NormalizeDouble(OrderStopLoss(),Digits);
tp = OrderTakeProfit();
if (OrderType() == OP_BUY)
{
price = MarketInfo(Symbol(),MODE_BID);
newSl = NormalizeDouble(price - TrailingStopPoints * point, Digits);
if(((tp - price)/point) < stopLevel && tp != 0) continue;
if(((price - newSl)/point) < stopLevel)continue;
if(WaitForProfit == 0)
{
pnlPoints = (price - OrderOpenPrice())/point;
if (pnlPoints < TrailingStopPoints ) continue;
}
if (sl + MinAdjustmentPoints*point>= newSl) continue;
if(!OrderModify(OrderTicket(), OrderOpenPrice(), newSl, tp, 0))
{
printf("Error: Failed to modify trade. Ticket #%i, error code: %i", OrderTicket(), GetLastError());
result = false;
}
}
else if (OrderType() == OP_SELL)
{
price = MarketInfo(Symbol(),MODE_ASK);
newSl = NormalizeDouble(price+ TrailingStopPoints * point, Digits);
if(((price - tp)/point) < stopLevel) continue;
if(((newSl - price)/point) < stopLevel) continue;
if(WaitForProfit == 0)
{
pnlPoints = (OrderOpenPrice() - price)/point;
if (pnlPoints < TrailingStopPoints) continue;
}
if (sl - MinAdjustmentPoints*point <= newSl && sl != 0) continue;
if(!OrderModify(OrderTicket(), OrderOpenPrice(), newSl, tp, 0))
{
printf("Error: Failed to modify trade. Ticket #%i, error code: %i", OrderTicket(), GetLastError());
result = false;
}
}
}
return(result);
}
/*
void dtrailing()
{
int mod=0;
for(int i=0; i<OrdersTotal(); i++){
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic ) continue;
if(OrderType()==OP_BUY) {
if(Bid-OrderOpenPrice()>pt*TrailingStop) {
if((OrderStopLoss()<Bid-pt*TrailingStop) || (OrderStopLoss()==0)) {
mod= OrderModify(OrderTicket(),OrderOpenPrice(),Bid-pt*TrailingStop,OrderTakeProfit(),0,Green);
}
}
}
if(OrderType()==OP_SELL) {
if((OrderOpenPrice()-Ask)>(pt*TrailingStop)){
if(OrderStopLoss()>(Ask+pt*TrailingStop) || (OrderStopLoss()==0)){
mod=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+pt*TrailingStop,OrderTakeProfit(),0,Red);
}
}
}
}
}
*/
//=========================================== SELESAI CODE TRAILING STOP ===================================
void Komen(){
Comment("\nNomor Akun: ",Symbol(),
"\nNama : ",AccountName(),
"\nNama : ",AccountName(),
"\nNama : ",AccountName(),
"\nNama : ",AccountName(),
"\nNama : ",AccountName(),
"\nBalance : ",AccountBalance());
}
//=========================================== SELESAI CODE Untuk Komen ===================================