'SELECT_BY_POS' - undeclared identifier
'OrderGetTicket' - wrong parameters count
Hi, Check this code:
// Input parameters
input double Range = 200.0; // Range in points
input double TakeProfit = 400.0; // Take Profit in points
input double StopLoss = 200.0; // Stop Loss in points
input double LotSize = 0.1; // Lot size
input int TrailingStop = 200; // Trailing Stop in points
input string OrderComment = "MySetup"; // Order comment
input int TimerOption = 0; // Timer option (0: Off, 1: On)
input datetime TimerTargetTime = D'2024.08.29 18:00:00'; // Target time
input int DeletePOTimerOption = 0; // Delete PO Timer option (0: Off, 1: On)
input datetime DeletePOTargetTime = D'2024.08.29 18:00:05'; // Time to delete pending orders
// Global variables
double BuyStopPrice, SellStopPrice;
double StopLossBuy, TakeProfitBuy, StopLossSell, TakeProfitSell;
CTrade trade;
bool tradeOpened = false; // Flag to track if a trade has been opened
// Function declarations
void SetOrders();
void ManageTrailingStop();
void DeletePendingOrders();
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Print a message on the log
Print("MyEA has started.");
// Return initialization result
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Print a message on the log
Print("MyEA has stopped.");
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Check if a trade has already been opened
if (!tradeOpened)
{
// If the timer is on and the current time is greater or equal to the target time
if (TimerOption == 1)
{
if (TimeCurrent() >= TimerTargetTime)
{
// Calculate the prices and set the orders
SetOrders();
}
}
else
{
// If timer is off, immediately set the orders
SetOrders();
}
}
else
{
// Manage trailing stop for open orders
ManageTrailingStop();
}
// Check if the Delete PO Timer is on and the current time is greater or equal to the target time
if (DeletePOTimerOption == 1 && TimeCurrent() >= DeletePOTargetTime)
{
DeletePendingOrders(); // Delete all pending orders at the specified time
}
}
//+------------------------------------------------------------------+
//| Calculate the prices and set the orders |
//+------------------------------------------------------------------+
void SetOrders()
{
double price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
// Calculate the buy stop and sell stop prices
BuyStopPrice = price + Range * _Point;
SellStopPrice = price - Range * _Point;
// Calculate the stop loss and take profit for buy stop
StopLossBuy = BuyStopPrice - StopLoss * _Point;
TakeProfitBuy = BuyStopPrice + TakeProfit * _Point;
// Calculate the stop loss and take profit for sell stop
StopLossSell = SellStopPrice + StopLoss * _Point;
TakeProfitSell = SellStopPrice - TakeProfit * _Point;
// Place buy stop order
if (!trade.BuyStop(LotSize, BuyStopPrice, _Symbol, StopLossBuy, TakeProfitBuy, ORDER_TIME_GTC, 0, OrderComment))
{
Print("Error placing buy stop order: ", GetLastError());
}
else
{
tradeOpened = true;
Print("Buy stop order placed successfully.");
}
// Place sell stop order
if (!trade.SellStop(LotSize, SellStopPrice, _Symbol, StopLossSell, TakeProfitSell, ORDER_TIME_GTC, 0, OrderComment))
{
Print("Error placing sell stop order: ", GetLastError());
}
else
{
tradeOpened = true;
Print("Sell stop order placed successfully.");
}
}
//+------------------------------------------------------------------+
//| Manage trailing stop for open orders |
//+------------------------------------------------------------------+
void ManageTrailingStop()
{
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
if (PositionSelectByIndex(i))
{
ulong ticket = PositionGetTicket(i);
string symbol = PositionGetString(POSITION_SYMBOL);
if (symbol != _Symbol) continue;
double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double stopLoss = PositionGetDouble(POSITION_SL);
double newStopLoss;
if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
{
newStopLoss = currentPrice - TrailingStop * _Point;
if (newStopLoss > stopLoss)
{
if (!trade.PositionModify(ticket, newStopLoss, PositionGetDouble(POSITION_TP)))
{
Print("Error modifying position: ", GetLastError());
}
}
}
else if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
{
newStopLoss = currentPrice + TrailingStop * _Point;
if (newStopLoss < stopLoss)
{
if (!trade.PositionModify(ticket, newStopLoss, PositionGetDouble(POSITION_TP)))
{
Print("Error modifying position: ", GetLastError());
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Delete all pending orders at the specified time |
//+------------------------------------------------------------------+
void DeletePendingOrders()
{
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS))
{
int orderType = (int)OrderGetInteger(ORDER_TYPE);
if (orderType == ORDER_TYPE_BUY_STOP || orderType == ORDER_TYPE_SELL_STOP)
{
ulong ticket = OrderGetTicket();
if (!trade.OrderDelete(ticket))
{
Print("Error deleting pending order: ", GetLastError());
}
else
{
Print("Pending order deleted: ", ticket);
}
}
}
}
}
//+------------------------------------------------------------------+
double price = SymbolInfoDouble(_Symbol, SYMBOL_BID); // Calculate the buy stop and sell stop prices BuyStopPrice = price + Range * _Point; SellStopPrice = price - Range * _Point; // Calculate the stop loss and take profit for buy stop StopLossBuy = BuyStopPrice - StopLoss * _Point; TakeProfitBuy = BuyStopPrice + TakeProfit * _Point;
You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.
-
Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?
-
Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25 -
Prices (open, SL, and TP) must be a multiple of ticksize. Using Point means code breaks on 4 digit brokers (if any still exists), exotics (e.g. USDZAR where spread is over 500 points), and metals. Compute what a logical PIP is and use that, not points.
How to manage JPY pairs with parameters? - MQL4 programming forum (2017)
Slippage defined in index points - Expert Advisors and Automated Trading - MQL5 programming forum (2018) -
The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
My GBPJPY shows average spread = 26 points, average maximum spread = 134.
My EURCHF shows average spread = 18 points, average maximum spread = 106.
(your broker will be similar).
Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Error on
Delete all pending orders at the specified time'SELECT_BY_POS' - undeclared identifier'OrderGetTicket' - wrong parameters count