|
|
@@ -0,0 +1,452 @@
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+//| CribMarketEA.mq5 |
|
|
|
+//| Copyright 2025, MQL Development |
|
|
|
+//| https://www.mqldevelopment.com/ |
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+#property copyright "Copyright 2025, MQL Development"
|
|
|
+#property link "https://www.mqldevelopment.com/"
|
|
|
+#property version "1.00"
|
|
|
+
|
|
|
+#include <Trade\Trade.mqh>
|
|
|
+CTrade trade;
|
|
|
+
|
|
|
+#define MaxOrders 10000
|
|
|
+
|
|
|
+struct new_trade_store
|
|
|
+ {
|
|
|
+ ulong buyTicket; // Buy Ticket
|
|
|
+ ulong sellTicket; // Sell Ticket
|
|
|
+ string buySymbol; // Buy Symbol Name
|
|
|
+ string sellSymbol; // Sell Symbol Name
|
|
|
+ new_trade_store()
|
|
|
+ {
|
|
|
+ buyTicket = -1;
|
|
|
+ sellTicket = -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ };
|
|
|
+new_trade_store newTradeStore[MaxOrders];
|
|
|
+
|
|
|
+
|
|
|
+input string Settings = " ------------- General Settings ------------- "; //_
|
|
|
+input int magicNo = 333; // Magic no
|
|
|
+input double lotSize = 0.01; // Lot Size
|
|
|
+input bool enableBasketTP = true; // Enable Basket TP
|
|
|
+input double basketTakeProfit = 1.0; // Basket Take Profit
|
|
|
+input int maxOpenPositions = 3; // Maximum number of open positions
|
|
|
+
|
|
|
+
|
|
|
+string goldPairs[];
|
|
|
+int totalGoldPairs = 0;
|
|
|
+
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+//| Expert initialization function |
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+int OnInit()
|
|
|
+ {
|
|
|
+//---
|
|
|
+
|
|
|
+ trade.SetExpertMagicNumber(magicNo);
|
|
|
+ trade.SetDeviationInPoints(10);
|
|
|
+ trade.SetTypeFilling(ORDER_FILLING_IOC);
|
|
|
+ trade.LogLevel(LOG_LEVEL_ALL);
|
|
|
+ trade.SetAsyncMode(false);
|
|
|
+
|
|
|
+ getSymbolsFromMarketWatch();
|
|
|
+
|
|
|
+
|
|
|
+// if(enableBasketTP == true)
|
|
|
+// {
|
|
|
+// checkBasketTakeProfit();
|
|
|
+// }
|
|
|
+//
|
|
|
+// string symbolToBuy = getSymbolWithLowestAsk();
|
|
|
+// string symbolToSell = getSymbolWithHighestBid();
|
|
|
+////Print(" Symbol to Buy is: ", symbolToBuy, " Symbol to Sell: ", symbolToSell);
|
|
|
+//
|
|
|
+// if(noOfActiveOrdersOfType(POSITION_TYPE_BUY) < maxOpenPositions)
|
|
|
+// {
|
|
|
+// if(symbolToBuy != NULL && symbolToBuy != "")
|
|
|
+// {
|
|
|
+// placeBuyTrade(symbolToBuy);
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// if(noOfActiveOrdersOfType(POSITION_TYPE_SELL) < maxOpenPositions)
|
|
|
+// {
|
|
|
+// if(symbolToSell != NULL && symbolToSell != "")
|
|
|
+// {
|
|
|
+// placeSellTrade(symbolToSell);
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+//--- create timer
|
|
|
+ EventSetMillisecondTimer(1000);
|
|
|
+
|
|
|
+//---
|
|
|
+ return(INIT_SUCCEEDED);
|
|
|
+ }
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+//| Expert deinitialization function |
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+void OnDeinit(const int reason)
|
|
|
+ {
|
|
|
+//---
|
|
|
+ EventKillTimer();
|
|
|
+
|
|
|
+ }
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+//| Expert tick function |
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+void OnTick()
|
|
|
+ {
|
|
|
+//---
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+//| |
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+void OnTimer()
|
|
|
+ {
|
|
|
+//---
|
|
|
+
|
|
|
+ removeFromStruct();
|
|
|
+
|
|
|
+ if(enableBasketTP == true)
|
|
|
+ {
|
|
|
+ checkBasketTakeProfit();
|
|
|
+ }
|
|
|
+
|
|
|
+ string symbolToBuy = getSymbolWithLowestAsk();
|
|
|
+ string symbolToSell = getSymbolWithHighestBid();
|
|
|
+//Print(" Symbol to Buy is: ", symbolToBuy, " Symbol to Sell: ", symbolToSell);
|
|
|
+
|
|
|
+ if(noOfActiveOrdersOfType(POSITION_TYPE_BUY) < maxOpenPositions)
|
|
|
+ {
|
|
|
+ if(symbolToBuy != NULL && symbolToBuy != "")
|
|
|
+ {
|
|
|
+ placeBuyTrade(symbolToBuy);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(noOfActiveOrdersOfType(POSITION_TYPE_SELL) < maxOpenPositions)
|
|
|
+ {
|
|
|
+ if(symbolToSell != NULL && symbolToSell != "")
|
|
|
+ {
|
|
|
+ placeSellTrade(symbolToSell);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+//| |
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+bool newBar()
|
|
|
+ {
|
|
|
+ static datetime lastbar;
|
|
|
+ datetime curbar = iTime(Symbol(), PERIOD_CURRENT, 0);
|
|
|
+ if(lastbar != curbar)
|
|
|
+ {
|
|
|
+ lastbar = curbar;
|
|
|
+ Print(" ---------------------- New Bar :: ---------------------- ",lastbar);
|
|
|
+ return (true);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return (false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+//| |
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+void placeBuyTrade(string symbol)
|
|
|
+ {
|
|
|
+
|
|
|
+ double ask = SymbolInfoDouble(symbol, SYMBOL_ASK);
|
|
|
+ double buySL = 0, buyTP = 0;
|
|
|
+
|
|
|
+ if(trade.PositionOpen(symbol, ORDER_TYPE_BUY, lotSize, ask, buySL, buyTP, "Buy Trade Placed"))
|
|
|
+ {
|
|
|
+ Print("Buy Trade Placed on ", symbol, ": ", trade.ResultOrder());
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Print("Error in placing Buy on ", symbol, ": ", GetLastError());
|
|
|
+ }
|
|
|
+ }
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+//| |
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+void placeSellTrade(string symbol)
|
|
|
+ {
|
|
|
+
|
|
|
+ double bid = SymbolInfoDouble(symbol, SYMBOL_BID);
|
|
|
+ double sellSL = 0, sellTP = 0;
|
|
|
+
|
|
|
+ if(trade.PositionOpen(symbol, ORDER_TYPE_SELL, lotSize, bid, sellSL, sellTP, "Sell Trade Placed"))
|
|
|
+ {
|
|
|
+ Print("Sell Trade Placed on ", symbol, ": ", trade.ResultOrder());
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Print("Error in placing Sell on ", symbol, ": ", GetLastError());
|
|
|
+ }
|
|
|
+ }
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+//| |
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+void getSymbolsFromMarketWatch()
|
|
|
+ {
|
|
|
+ int totalSymbols = SymbolsTotal(true);
|
|
|
+ ArrayResize(goldPairs, 0);
|
|
|
+ totalGoldPairs = 0;
|
|
|
+
|
|
|
+ for(int i = 0; i < totalSymbols; i++)
|
|
|
+ {
|
|
|
+ string symbolName = SymbolName(i, true);
|
|
|
+ //if(StringFind(symbolName, "GOLD") != -1 || StringFind(symbolName, "XAU") != -1)
|
|
|
+ {
|
|
|
+ ArrayResize(goldPairs, totalGoldPairs + 1);
|
|
|
+ if(totalGoldPairs < ArraySize(goldPairs))
|
|
|
+ {
|
|
|
+ goldPairs[totalGoldPairs] = symbolName;
|
|
|
+ totalGoldPairs++;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Print("Error: Array resize failed for symbol ", symbolName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Print("Found ", totalGoldPairs, " Symbol pairs in Market Watch");
|
|
|
+ }
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+//| |
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+string getSymbolWithLowestAsk()
|
|
|
+ {
|
|
|
+
|
|
|
+ if(totalGoldPairs == 0)
|
|
|
+ return NULL;
|
|
|
+
|
|
|
+ string lowestSymbol = "";
|
|
|
+//double lowestAsk = INT_MAX; // SymbolInfoDouble(lowestSymbol, SYMBOL_ASK);
|
|
|
+ double lowestAsk = DBL_MAX;
|
|
|
+
|
|
|
+ for(int i = 0; i < totalGoldPairs; i++)
|
|
|
+ {
|
|
|
+ double currentAsk = SymbolInfoDouble(goldPairs[i], SYMBOL_ASK);
|
|
|
+ Print("Pair:",goldPairs[i]," Price Ask:",currentAsk);
|
|
|
+
|
|
|
+ if(currentAsk < lowestAsk)
|
|
|
+ {
|
|
|
+ lowestAsk = currentAsk;
|
|
|
+ lowestSymbol = goldPairs[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Print("Lowest Ask Pair: ", lowestSymbol, " Lowest Ask: ", lowestAsk);
|
|
|
+ return lowestSymbol;
|
|
|
+ }
|
|
|
+
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+//| |
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+string getSymbolWithHighestBid()
|
|
|
+ {
|
|
|
+
|
|
|
+ if(totalGoldPairs == 0)
|
|
|
+ return NULL;
|
|
|
+
|
|
|
+ string highestSymbol = "";
|
|
|
+//double highestBid = INT_MIN; // SymbolInfoDouble(highestSymbol, SYMBOL_BID);
|
|
|
+ double highestBid = 0;
|
|
|
+
|
|
|
+ for(int i = 0; i < totalGoldPairs; i++)
|
|
|
+ {
|
|
|
+ double currentBid = SymbolInfoDouble(goldPairs[i], SYMBOL_BID);
|
|
|
+ Print("Pair:",goldPairs[i]," Price Bid:",currentBid);
|
|
|
+ if(currentBid > highestBid)
|
|
|
+ {
|
|
|
+ highestBid = currentBid;
|
|
|
+ highestSymbol = goldPairs[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Print("Highest Bid Pair: ", highestSymbol, " Highest Bid: ", highestBid);
|
|
|
+ return highestSymbol;
|
|
|
+ }
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+//| |
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+int noOfActiveOrdersOfType(ENUM_POSITION_TYPE type)
|
|
|
+ {
|
|
|
+ int count = 0;
|
|
|
+
|
|
|
+ for(int i= PositionsTotal()-1; i>=0; i--)
|
|
|
+ {
|
|
|
+ ulong ticket = PositionGetTicket(i);
|
|
|
+ if(PositionSelectByTicket(ticket))
|
|
|
+ {
|
|
|
+ if(PositionGetInteger(POSITION_MAGIC) == magicNo
|
|
|
+ && (PositionGetInteger(POSITION_TYPE) == type)
|
|
|
+ && isGoldPair(PositionGetString(POSITION_SYMBOL)))
|
|
|
+ {
|
|
|
+ count++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return count;
|
|
|
+ }
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+//| |
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+void checkBasketTakeProfit()
|
|
|
+ {
|
|
|
+ double netProfit = 0;
|
|
|
+
|
|
|
+ for(int i = PositionsTotal() - 1; i >= 0; i--)
|
|
|
+ {
|
|
|
+ ulong ticket = PositionGetTicket(i);
|
|
|
+ if(PositionSelectByTicket(ticket))
|
|
|
+ {
|
|
|
+ if(isGoldPair(PositionGetString(POSITION_SYMBOL)) &&
|
|
|
+ PositionGetInteger(POSITION_MAGIC) == magicNo)
|
|
|
+ {
|
|
|
+ netProfit += PositionGetDouble(POSITION_PROFIT)+PositionGetDouble(POSITION_SWAP);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(netProfit >= basketTakeProfit)
|
|
|
+ {
|
|
|
+ Print("Basket TP hit: Closing all trades. Profit = ", netProfit);
|
|
|
+ closeAllActiveOrders();
|
|
|
+ }
|
|
|
+ }
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+//| |
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+void closeAllActiveOrders()
|
|
|
+ {
|
|
|
+ for(int i=PositionsTotal()-1; i >=0 ; i--)
|
|
|
+ {
|
|
|
+ ulong ticket = PositionGetTicket(i);
|
|
|
+ if(PositionSelectByTicket(ticket))
|
|
|
+ {
|
|
|
+ if(PositionGetInteger(POSITION_MAGIC) == magicNo &&
|
|
|
+ isGoldPair(PositionGetString(POSITION_SYMBOL)))
|
|
|
+ {
|
|
|
+ if(trade.PositionClose(ticket))
|
|
|
+ {
|
|
|
+ Print("Position closed ", ticket);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Print("Cannot close order: ",GetLastError());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+//| |
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+bool isGoldPair(string symbol)
|
|
|
+ {
|
|
|
+ for(int i = 0; i < totalGoldPairs; i++)
|
|
|
+ {
|
|
|
+ //Print("Total Gold pairs: ", totalGoldPairs, " Array: ", goldPairs[i], " Symbol: ", symbol);
|
|
|
+ if(goldPairs[i] == symbol)
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+//| |
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+void addToStructure(ulong r_buyTicket,ulong r_sellTicket, string r_buySymbol, string r_sellSymbol)
|
|
|
+ {
|
|
|
+ for(int i=0; i<MaxOrders; i++)
|
|
|
+ {
|
|
|
+ if(newTradeStore[i].buyTicket==-1 && newTradeStore[i].sellTicket==-1)
|
|
|
+ {
|
|
|
+ newTradeStore[i].buyTicket = r_buyTicket;
|
|
|
+ newTradeStore[i].sellTicket = r_sellTicket;
|
|
|
+ newTradeStore[i].buySymbol = r_buySymbol;
|
|
|
+ newTradeStore[i].sellSymbol = r_sellSymbol;
|
|
|
+
|
|
|
+ Print("Stored new ticket in structure. Buy Ticket: ", newTradeStore[i].buyTicket, " Sell Ticket: ", newTradeStore[i].sellTicket,
|
|
|
+ " Buy Symbol: ", newTradeStore[i].buySymbol, " Sell Symbol: ", newTradeStore[i].sellSymbol);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+//| |
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+void removeFromStruct()
|
|
|
+ {
|
|
|
+ for(int i = 0 ; i < MaxOrders ; i++)
|
|
|
+ {
|
|
|
+ bool buyPresent = false;
|
|
|
+ bool sellPresent = false;
|
|
|
+
|
|
|
+ if(newTradeStore[i].buyTicket !=-1 || newTradeStore[i].sellTicket !=-1)
|
|
|
+ {
|
|
|
+ for(int j = PositionsTotal()-1; j>=0; j--)
|
|
|
+ {
|
|
|
+ ulong ticket = PositionGetTicket(j);
|
|
|
+ if(PositionSelectByTicket(ticket))
|
|
|
+ {
|
|
|
+ if(ticket == newTradeStore[i].buyTicket)
|
|
|
+ {
|
|
|
+ buyPresent = true;
|
|
|
+ }
|
|
|
+ if(ticket == newTradeStore[i].sellTicket)
|
|
|
+ {
|
|
|
+ sellPresent = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for(int j = OrdersTotal()-1; j>=0; j--)
|
|
|
+ {
|
|
|
+ ulong ticket = OrderGetTicket(j);
|
|
|
+ if(OrderSelect(ticket))
|
|
|
+ {
|
|
|
+ if(ticket == newTradeStore[i].buyTicket)
|
|
|
+ {
|
|
|
+ buyPresent = true;
|
|
|
+ }
|
|
|
+ if(ticket == newTradeStore[i].sellTicket)
|
|
|
+ {
|
|
|
+ sellPresent = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!buyPresent)
|
|
|
+ {
|
|
|
+ Print("Buy ticket closed: ", newTradeStore[i].buyTicket);
|
|
|
+ newTradeStore[i].buyTicket = -1;
|
|
|
+ newTradeStore[i].buySymbol = "";
|
|
|
+ }
|
|
|
+ if(!sellPresent)
|
|
|
+ {
|
|
|
+ Print("Sell ticket closed: ", newTradeStore[i].sellTicket);
|
|
|
+ newTradeStore[i].sellTicket = -1;
|
|
|
+ newTradeStore[i].sellSymbol = "";
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+//| |
|
|
|
+//+------------------------------------------------------------------+
|
|
|
+
|
|
|
+//+------------------------------------------------------------------+
|