//+------------------------------------------------------------------+
//| ThreePipTrader.mq5 |
//| Created by OpenAI Assistant |
//+------------------------------------------------------------------+
#property copyright "OpenAI"
#property link "https://openai.com/"
#property version "1.00"
#property strict
// Inputs
input double Lots = 0.1; // Lot size
input int PipDistance = 3; // Distance in pips to trigger orders
input int ReversePips = 6; // Pips reversal to open opposite trade
input int TakeProfitPips = 3; // Take Profit in pips
// Check if there are open orders
BuyOrderOpen = IsOrderOpen(ORDER_TYPE_BUY);
SellOrderOpen = IsOrderOpen(ORDER_TYPE_SELL);
// If no orders are open, check for new trade conditions
if(!BuyOrderOpen && !SellOrderOpen)
{
// Check for buy condition
if(bid >= BuyTriggerPrice)
{
OpenOrder(ORDER_TYPE_BUY);
// Set new trigger prices
BuyTriggerPrice = bid + (ReversePips * _Point);
SellTriggerPrice = bid - (ReversePips * _Point);
}
// Check for sell condition
else if(bid <= SellTriggerPrice)
{
OpenOrder(ORDER_TYPE_SELL);
// Set new trigger prices
BuyTriggerPrice = bid + (ReversePips * _Point);
SellTriggerPrice = bid - (ReversePips * _Point);
}
}
else
{
// If a buy order is open and price reverses by ReversePips, open a sell trade with TP
if(BuyOrderOpen && bid <= (BuyTriggerPrice - (ReversePips * _Point)))
{
OpenOrder(ORDER_TYPE_SELL, TakeProfitPips);
BuyOrderOpen = false; // Close the buy order status
}
// If a sell order is open and price reverses by ReversePips, open a buy trade with TP
else if(SellOrderOpen && bid >= (SellTriggerPrice + (ReversePips * _Point)))
{
OpenOrder(ORDER_TYPE_BUY, TakeProfitPips);
SellOrderOpen = false; // Close the sell order status
}
}
}
//+------------------------------------------------------------------+
//| Function to check if there is an open order of a specific type |
//+------------------------------------------------------------------+
bool IsOrderOpen(ENUM_ORDER_TYPE type)
{
ulong ticket = PositionGetTicket(0);
if(ticket != 0)
{
if(PositionGetInteger(POSITION_TYPE) == type)
return true;
}
return false;
}
Assistant
//+------------------------------------------------------------------+
//| Function to open an order |
//+------------------------------------------------------------------+
void OpenOrder(ENUM_ORDER_TYPE type, int tp_pips=0)
{
MqlTradeRequest request;
MqlTradeResult result;
ZeroMemory(request);
ZeroMemory(result);
//+------------------------------------------------------------------+
it is not executing any order can you fix it
Based on the code you've provided, there are a few issues that might be preventing the Expert Advisor (EA) from executing any orders. I'll address these issues and provide fixes step by step.
1. Incorrect Use of IsOrderOpen Function:
The IsOrderOpen function is not correctly checking for open positions. In MQL5, positions should be checked using the PositionSelect() function, and you should use ENUM_POSITION_TYPE instead of ENUM_ORDER_TYPE when dealing with positions.
Fix:
Update the IsOrderOpen function to correctly check for open positions of a specific type.
In your OnTick function, you're using ORDER_TYPE_BUY and ORDER_TYPE_SELL where you should be using POSITION_TYPE_BUY and POSITION_TYPE_SELL when checking positions.
Fix:
Update the variables BuyOrderOpen and SellOrderOpen as follows:
The calculations for price levels using _Point may not correctly represent pips, especially with symbols that have different digit settings (like 3, 5 digits). It's better to calculate pip values based on the symbol's tick size.
Fix:
Create a function to get the pip value for the symbol and use it in your calculations.
mq5
// Function to get the pip value for the symbol
double GetPipValue()
{
// For most currency pairs, 1 pip = 0.0001 or 0.01 for JPY pairs
double pip_value = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS);
if(digits == 3 || digits == 5)
pip_value *= 10;
return pip_value;
}
Define a global variable to hold the pip value:
mq5
double PipSize; // Global variable to hold the pip size
Update all calculations that use _Point to use PipSize instead. For example:
mq5
// In OnTick(), when setting new trigger prices after opening a trade
BuyTriggerPrice = bid + (ReversePips * PipSize);
SellTriggerPrice = bid - (ReversePips * PipSize);
4. Not Closing Existing Positions When Reversing:
Your EA opens new positions when the price reverses but doesn't close existing positions. This can lead to multiple positions being open simultaneously, which might not be your intention.
Fix:
Add code to close existing positions when opening a new one in the opposite direction. Create a function to close positions:
Update your OnTick function to close existing positions when reversing:
mq5
if(BuyOrderOpen && bid <= (BuyTriggerPrice - (ReversePips * PipSize)))
{
// Close existing buy position
ClosePosition(POSITION_TYPE_BUY);
// Open new sell order with Take Profit
OpenOrder(ORDER_TYPE_SELL, TakeProfitPips);
BuyOrderOpen = false; // Reset the buy order status
}
else if(SellOrderOpen && bid >= (SellTriggerPrice + (ReversePips * PipSize)))
{
// Close existing sell position
ClosePosition(POSITION_TYPE_SELL);
// Open new buy order with Take Profit
OpenOrder(ORDER_TYPE_BUY, TakeProfitPips);
SellOrderOpen = false; // Reset the sell order status
}
5. Normalize Prices When Sending Orders:
Prices should be normalized to the correct number of digits to avoid execution errors.
Fix:
In your OpenOrder function, normalize the price and take profit levels:
Make sure that automated trading is enabled in the platform and that your EA is allowed to place orders.
Fix:
Check the following:
Ensure that the "Algo Trading" button is pressed in MetaTrader 5.
In the EA settings, under the "Common" tab, make sure "Allow Algo Trading" is checked.
Ensure that there are no errors in the "Experts" or "Journal" tabs.
Final Revised Code Snippet:
Here is the revised EA incorporating the fixes:
mq5
Additional Tips:
Testing: Before running the EA on a live account, test it thoroughly in the strategy tester to ensure it behaves as expected.
Logging: Monitor the "Experts" and "Journal" tabs for any error messages that can provide further clues.
Broker Constraints: Be aware that some brokers have limitations on minimum lot sizes, step sizes, and allowed order types. Ensure your EA complies with these constraints.
Risk Management: Always incorporate proper risk management in your EAs to protect your trading capital.
By applying these fixes, your EA should start executing orders as expected. If you encounter further issues, check the logs for errors and adjust the code accordingly.
Not yet included in a published release.
//+------------------------------------------------------------------+
//| ThreePipTrader.mq5 |
//| Created by OpenAI Assistant |
//+------------------------------------------------------------------+
#property copyright "OpenAI"
#property link "https://openai.com/"
#property version "1.00"
#property strict
// Inputs
input double Lots = 0.1; // Lot size
input int PipDistance = 3; // Distance in pips to trigger orders
input int ReversePips = 6; // Pips reversal to open opposite trade
input int TakeProfitPips = 3; // Take Profit in pips
input int Slippage = 20; // Price slippage in points
// Global variables
double BuyTriggerPrice;
double SellTriggerPrice;
bool BuyOrderOpen = false;
bool SellOrderOpen = false;
double PipSize; // Pip value
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Initialize PipSize
PipSize = GetPipValue();
// Initialize trigger prices to current Bid price
double price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
BuyTriggerPrice = price + (PipDistance * PipSize);
SellTriggerPrice = price - (PipDistance * PipSize);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Function to get pip value |
//+------------------------------------------------------------------+
double GetPipValue()
{
// For most currency pairs, 1 pip = 0.0001 or 0.01 for JPY pairs
double pip_value = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS);
if(digits == 3 || digits == 5)
pip_value *= 10;
return pip_value;
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
// Check if there are open orders
BuyOrderOpen = IsOrderOpen(POSITION_TYPE_BUY);
SellOrderOpen = IsOrderOpen(POSITION_TYPE_SELL);
// If no orders are open, check for new trade conditions
if(!BuyOrderOpen && !SellOrderOpen)
{
// Check for buy condition
if(bid >= BuyTriggerPrice)
{
OpenOrder(ORDER_TYPE_BUY);
// Set new trigger prices
BuyTriggerPrice = bid + (ReversePips * PipSize);
SellTriggerPrice = bid - (ReversePips * PipSize);
}
// Check for sell condition
else if(bid <= SellTriggerPrice)
{
OpenOrder(ORDER_TYPE_SELL);
// Set new trigger prices
BuyTriggerPrice = bid + (ReversePips * PipSize);
SellTriggerPrice = bid - (ReversePips * PipSize);
}
}
else
{
// If a buy order is open and price reverses by ReversePips, open a sell trade with TP
if(BuyOrderOpen && bid <= (BuyTriggerPrice - (ReversePips * PipSize)))
{
// Close existing buy position
ClosePosition(POSITION_TYPE_BUY);
// Open new sell order with Take Profit
OpenOrder(ORDER_TYPE_SELL, TakeProfitPips);
BuyOrderOpen = false; // Reset the buy order status
}
// If a sell order is open and price reverses by ReversePips, open a buy trade with TP
else if(SellOrderOpen && bid >= (SellTriggerPrice + (ReversePips * PipSize)))
{
// Close existing sell position
ClosePosition(POSITION_TYPE_SELL);
// Open new buy order with Take Profit
OpenOrder(ORDER_TYPE_BUY, TakeProfitPips);
SellOrderOpen = false; // Reset the sell order status
}
}
}
//+------------------------------------------------------------------+
//| Function to check if there is an open position of a specific type|
//+------------------------------------------------------------------+
bool IsOrderOpen(ENUM_POSITION_TYPE type)
{
if(PositionSelect(_Symbol))
{
if(PositionGetInteger(POSITION_TYPE) == type)
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| Function to open an order |
//+------------------------------------------------------------------+
void OpenOrder(ENUM_ORDER_TYPE type, int tp_pips=0)
{
MqlTradeRequest request;
MqlTradeResult result;
ZeroMemory(request);
ZeroMemory(result);
request.action = TRADE_ACTION_DEAL;
request.symbol = _Symbol;
request.volume = Lots;
request.type = type;
request.price = (type == ORDER_TYPE_BUY) ? NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits)
: NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits);
request.deviation = Slippage;
// Set Take Profit if specified
if(tp_pips > 0)
{
double tp_price = (type == ORDER_TYPE_BUY) ? request.price + (tp_pips * PipSize)
: request.price - (tp_pips * PipSize);
request.tp = NormalizeDouble(tp_price, _Digits);
}
// Send the order
if(!OrderSend(request, result))
{
Print("OrderSend failed: ", GetLastError());
}
else
{
if(result.retcode != TRADE_RETCODE_DONE)
{
PrintFormat("OrderSend failed, retcode=%d, comment=%s", result.retcode, result.comment);
}
else
{
PrintFormat("OrderSend successful, ticket=%d", result.order);
}
}
}
//+------------------------------------------------------------------+
//| Function to close a position |
//+------------------------------------------------------------------+
void ClosePosition(ENUM_POSITION_TYPE type)
{
if(PositionSelect(_Symbol))
{
if(PositionGetInteger(POSITION_TYPE) == type)
{
ulong ticket = PositionGetInteger(POSITION_TICKET);
MqlTradeRequest request;
MqlTradeResult result;
ZeroMemory(request);
ZeroMemory(result);
request.action = TRADE_ACTION_DEAL;
request.position = ticket;
request.symbol = _Symbol;
request.volume = PositionGetDouble(POSITION_VOLUME);
request.type = (type == POSITION_TYPE_BUY) ? ORDER_TYPE_SELL : ORDER_TYPE_BUY;
request.price = (type == POSITION_TYPE_BUY) ? NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits)
: NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);
request.deviation = Slippage;
if(!OrderSend(request, result))
{
Print("ClosePosition failed: ", GetLastError());
}
else
{
if(result.retcode != TRADE_RETCODE_DONE)
{
PrintFormat("ClosePosition failed, retcode=%d, comment=%s", result.retcode, result.comment);
}
else
{
PrintFormat("Position closed successfully, ticket=%d", result.order);
}
}
}
}
}