| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- //+------------------------------------------------------------------+
- //| Pulse_EA_project_MT5.mq5 |
- //| Copyright 2024, MQL Development |
- //| https://www.mqldevelopment.com/ |
- //+------------------------------------------------------------------+
- #property copyright "Copyright 2024, MQL Development"
- #property link "https://www.mqldevelopment.com/"
- #property version "1.00"
-
- #define MaxOrders 10000
-
- struct new_trade_store
- {
- int trade_ticket;
- int trade_type;
- int trade_magic_no;
- double trade_open_price;
- double trade_close_price;
- datetime trade_open_time;
- datetime trade_close_time;
- double trade_sl;
- double trade_tp;
- double trade_lot;
- double trade_profit;
-
- new_trade_store()
- {
- trade_ticket = -1;
- trade_type = -1;
- trade_magic_no = -1;
- trade_open_price = -1;
- trade_close_price = -1;
- trade_open_time = -1;
- trade_close_time = -1;
- trade_sl = -1;
- trade_tp = -1;
- trade_lot = -1;
- trade_profit = -1;
- }
-
- };
- new_trade_store od[MaxOrders];
-
- enum trade_typ
- {
- buy, // Buy
- reverse, // Reverse
- };
-
- input trade_typ ordTyp = buy;
- input double tpPips = 20;
- input double slPips = 20;
- input double lot = 0.1;
- input int magicNo = 123;
- input string fileName = "TradeDataFile.csv"; // File Name
-
- bool tpSlHit = false;
- int ticketAssigner = 1;
- //+------------------------------------------------------------------+
- //| Expert initialization function |
- //+------------------------------------------------------------------+
- int OnInit()
- {
- //---
- tpSlHit = false;
- ticketAssigner = 1;
- //---
- return(INIT_SUCCEEDED);
- }
- //+------------------------------------------------------------------+
- //| Expert deinitialization function |
- //+------------------------------------------------------------------+
- void OnDeinit(const int reason)
- {
- //---
-
- }
- //+------------------------------------------------------------------+
- //| Expert tick function |
- //+------------------------------------------------------------------+
- void OnTick()
- {
- //---
- placeTrade();
- checkTPSLHit();
-
- }
-
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void placeTrade()
- {
- if(tpSlHit == false)
- {
- if(ordTyp == buy)
- {
- placeBuyTrade();
- }
- if(ordTyp == reverse)
- {
- placeSellTrade();
- }
- tpSlHit = true;
- }
- }
-
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void placeBuyTrade()
- {
- double buySl = 0,buyTp=0;
- double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
-
- if(slPips != 0)
- {
- buySl = Ask - (slPips * Point() * 10);
- }
- if(tpPips != 0)
- {
- buyTp = Ask + (tpPips * Point() * 10);
- }
-
- AddToStructure(ticketAssigner,0,magicNo,Ask,0,TimeCurrent(),0,buySl,buyTp,lot,0);
-
- ticketAssigner++;
-
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void placeSellTrade()
- {
-
- double sellSl = 0, sellTp = 0;
- double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
-
- if(slPips != 0)
- {
- sellSl = Bid + (slPips * 10 * Point());
- }
- if(tpPips != 0)
- {
- sellTp = Bid - (tpPips * 10 * Point());
- }
-
- AddToStructure(ticketAssigner,1,magicNo,Bid,0,TimeCurrent(),0,sellSl,sellTp,lot,0);
-
- ticketAssigner++;
-
- }
-
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void AddToStructure(int ticket, int type, int magic, double open_price,
- double close_price, datetime open_time, datetime close_time,
- double sl, double tp, double lotSize, double profit)
- {
- for(int i = 0; i < MaxOrders; i++)
- {
- if(od[i].trade_ticket == -1)
- {
-
- od[i].trade_ticket = ticket;
- od[i].trade_type = type;
- od[i].trade_magic_no = magic;
- od[i].trade_open_price = open_price;
- od[i].trade_close_price = close_price;
- od[i].trade_open_time = open_time;
- od[i].trade_close_time = close_time;
- od[i].trade_sl = sl;
- od[i].trade_tp = tp;
- od[i].trade_lot = lotSize;
- od[i].trade_profit = profit;
-
- Print(" ========== Trade Placed of Ticket ========== ",ticket);
- Print("[AddToStructure] Added at index: ", i,
- " | Ticket = ", ticket,
- " | Type = ", type,
- " | Magic = ", magic,
- " | OpenPrice = ", open_price,
- " | ClosePrice = ", close_price,
- " | SL = ", sl,
- " | TP = ", tp,
- " | Lots = ", lotSize,
- " | Profit = ", profit);
-
- break;
- }
- }
- }
-
-
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void checkTPSLHit()
- {
-
- double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
- double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
-
- for(int i = 0; i < MaxOrders; i++)
- {
- if(od[i].trade_ticket != -1 && od[i].trade_close_price == 0)
- {
-
- int ticket = od[i].trade_ticket;
- int type = od[i].trade_type;
- double sl = od[i].trade_sl;
- double tp = od[i].trade_tp;
-
- double closePrice = 0;
-
- // ----------------------------------------------
- // BUY ORDER CHECK
- // ----------------------------------------------
- if(type == 0)
- {
- // TP hit → Bid >= TP
- if(bid >= tp && tp > 0)
- {
- closePrice = bid;
- Print(" ======== Buy Trade TP Hit ======= ",ticket);
- }
-
- // SL hit → Bid <= SL
- if(bid <= sl && sl > 0)
- {
- closePrice = bid;
- Print(" ======== Buy Trade SL Hit ======= ",ticket);
- }
- }
-
- // ----------------------------------------------
- // SELL ORDER CHECK
- // ----------------------------------------------
- if(type == 1)
- {
- // TP hit → Ask <= TP (price goes DOWN)
- if(ask <= tp && tp > 0)
- {
- closePrice = ask;
- Print(" ======== Sell Trade TP Hit ======= ",ticket);
- }
-
- // SL hit → Ask >= SL
- if(ask >= sl && sl > 0)
- {
- closePrice = ask;
- Print(" ======== Sell Trade SL Hit ======= ",ticket);
- }
- }
-
- // ----------------------------------------------
- // IF ANYTHING TRIGGERED → CLOSE TRADE
- // ----------------------------------------------
- if(closePrice > 0)
- {
-
- od[i].trade_close_price = closePrice;
- od[i].trade_close_time = TimeCurrent();
-
- double tpdifference = type == 1 ? (od[i].trade_open_price - od[i].trade_close_price) / Point() : (od[i].trade_close_price - od[i].trade_open_price) / Point();
-
- od[i].trade_profit += tpinDollar(od[i].trade_lot, tpdifference);
-
- Print("======= TRADE CLOSED of ticket =======",ticket);
- Print("Index: ", i,
- " | Ticket: ", ticket,
- " | Type: ", type,
- " | ClosePrice: ", closePrice,
- " | Time: ", od[i].trade_close_time,
- " | Profit: ", od[i].trade_profit);
-
- tpSlHit = false; // make these zero so new trade place's
-
-
- string line = MakeTradeString(
- od[i].trade_ticket,
- od[i].trade_type,
- od[i].trade_magic_no,
- od[i].trade_open_price,
- od[i].trade_close_price,
- od[i].trade_open_time,
- od[i].trade_close_time,
- od[i].trade_sl,
- od[i].trade_tp,
- od[i].trade_lot,
- od[i].trade_profit
- );
-
- writeDatainFile(line);
-
- }
- }
- }
- }
-
-
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- double tpinDollar(double lotSize, double distance)
- {
-
- double pipvalue = NormalizeDouble(((SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)/(SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE)/Point()))*10),2);
- pipvalue = pipvalue / 10;
- double dollar = lotSize * distance * pipvalue;
- return NormalizeDouble(dollar,2);
- }
-
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void writeDatainFile(string str)
- {
-
- int filehandle = FileOpen(fileName,FILE_WRITE|FILE_CSV|FILE_COMMON|FILE_END,",");
-
- if(filehandle != INVALID_HANDLE)
- {
-
- FileSeek(filehandle, 0, SEEK_END);
- FileWrite(filehandle,str);
- Print(str);
-
- FileClose(filehandle);
- }
-
-
- }
-
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- string MakeTradeString(int ticket, int type, int magic,
- double open_price, double close_price,
- datetime open_time, datetime close_time,
- double sl, double tp, double lotSize, double profit)
- {
- string str = StringFormat("%d,%d,%d,%.5f,%.5f,%s,%s,%.5f,%.5f,%.2f,%.2f",
- ticket,
- type,
- magic,
- open_price,
- close_price,
- TimeToString(open_time, TIME_DATE|TIME_SECONDS),
- TimeToString(close_time, TIME_DATE|TIME_SECONDS),
- sl,
- tp,
- lotSize,
- profit);
-
- return str;
- }
-
|