// Importing necessary libraries// These libraries provide access to Position Information and Trading Functions.#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
// Define the size of a trading position// 'lotSize' represents the volume of an individual trade in the financial market.inputdouble lotSize = 0.01; // Set to 0.001 by default.// Variable to store the name of the symbol being traded// 'symbolName' is used to hold the name of a symbol in trading, like a currency pair or stock.string symbolName;
// Variables to store order identification numbers// 'sellTicket' and 'buyTicket' store the ticket numbers of sell and buy orders, initialized to 0.ulong sellTicket = 0;
ulong buyTicket = 0;
// Enumerated actions for trading// 'ENUM_TRADE_ACTIONS' defines actions: selling and buying assets.enum ENUM_TRADE_ACTIONS {
TRADE_ACTION_SELL = 0, // Identifier for selling an asset.
TRADE_ACTION_BUY = 1// Identifier for buying an asset.
};
CTrade m_trade; // Instance of the 'CTrade' class to access trading methods and functionalities.// CloseAllPositions Function// This function closes all open trading positions for the current symbol on the chart.void CloseAllPositions() {
// Loop through open positions to close themfor (int i = PositionsTotal() - 1; i >= 0; i--)
{
CPositionInfo m_position; // Object to hold position information// Select a position by its index for analysisif (m_position.SelectByIndex(i))
{
// Check if the selected position matches the symbol of the current chartif (m_position.Symbol() == Symbol())
{
// Close the selected position by its ticket number
m_trade.PositionClose(m_position.Ticket());
}
}
}
}
// OnTick Function// This function is executed on each tick of the chart.voidOnTick()
{
// Fetch current Ask and Bid pricesdouble currentAsk = SymbolInfoDouble(_Symbol, SYMBOL_ASK); // Fetches the current Ask price of the symbol.double currentBid = SymbolInfoDouble(_Symbol, SYMBOL_BID); // Fetches the current Bid price of the symbol.// Define stop loss in points for the US Tech 100double stopLossInPoints = 5000.0; // Stop loss in points for the US Tech 100.MqlTradeRequest request; // Request object for trade execution.MqlTradeResult result; // Result object to store trade execution results.// Find the latest upper and lower fractalsint upperFractal = iFractals(_Symbol, PERIOD_CURRENT); // Finds the latest upper fractal.int lowerFractal = iFractals(_Symbol, PERIOD_CURRENT); // Finds the latest lower fractal.double stopLossBuy = currentAsk - (5000 * _Point); // Calculate stop loss for a buy order.// Check for a sell signal (fractal above)if (upperFractal > 0)
{
// Set take profit and stop loss for a sell orderdouble takeProfit = 999999; // Set a very high value as "N/A" for Take Profit.double stopLossSell = currentBid - (5000 * _Point); // Calculate stop loss for a sell order.// Close all open positions
CloseAllPositions(); // Calls the function to close all open positions.// Open a new sell order
request.action = (ENUM_TRADE_REQUEST_ACTIONS)TRADE_ACTION_SELL; // Defines the action as 'Sell'.
request.symbol = _Symbol; // Specifies the symbol.
request.volume = lotSize; // Sets the trading volume.
request.price = currentBid; // Sets the price of the sell order.
request.sl = stopLossSell; // Sets the stop loss for the sell order.
request.tp = takeProfit; // Sets the take profit for the sell order.
request.comment = "Sell Order"; // Adds a comment for identification.if (m_trade.OrderSend(request, result))
{
sellTicket = result.order; // Save the ticket of the sell order.
}
}
// Check for a buy signal (fractal below)if (lowerFractal > 0)
{
// Set take profit and stop loss for a buy orderdouble takeProfit = currentAsk + (stopLossInPoints * _Point); // Calculate take profit for a buy order.// Close all open positions
CloseAllPositions(); // Calls the function to close all open positions.// Open a new buy order
request.action = (ENUM_TRADE_REQUEST_ACTIONS)TRADE_ACTION_BUY; // Defines the action as 'Buy'.
request.symbol = _Symbol; // Specifies the symbol.
request.volume = lotSize; // Sets the trading volume.
request.price = currentAsk; // Sets the price of the buy order.
request.sl = stopLossBuy; // Sets the stop loss for the buy order.
request.tp = takeProfit; // Sets the take profit for the buy order.
request.comment = "Buy Order"; // Adds a comment for identification.if (m_trade.OrderSend(request, result))
{
buyTicket = result.order; // Save the ticket of the buy order.
}
}
}
Hi i am new to coding and MQL5 i have successfully build a project with no errors when compiling but when i try to run it in strategy tester it doesn't work as intended.
the way its meant to work is use fractals, when a fractal open below it creates a buy position and then closes all positions and opens a new position when the opposite signal is shown (vice versa).
When i run the script it seems that every mili second i am receiving errors as follows:
2023.12.14 14:39:12.1502023.12.01 11:02:08 failed prices for US_TECH100 0.001 [Invalid request]
Trade Server Return Codes - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Hi i am new to coding and MQL5 i have successfully build a project with no errors when compiling but when i try to run it in strategy tester it doesn't work as intended.
the way its meant to work is use fractals, when a fractal open below it creates a buy position and then closes all positions and opens a new position when the opposite signal is shown (vice versa).
When i run the script it seems that every mili second i am receiving errors as follows:
2023.12.14 14:39:12.150 2023.12.01 11:02:08 failed prices for US_TECH100 0.001 [Invalid request]
2023.12.14 14:39:12.150 2023.12.01 11:02:08 CTrade::OrderSend: unknown action 0 [invalid request]
2023.12.14 14:39:12.150 2023.12.01 11:02:08 failed market unknown 0.001 US_TECH100 sl: 15918.50 tp: 16018.50 [Invalid request]
2023.12.14 14:39:12.150 2023.12.01 11:02:08 CTrade::OrderSend: market unknown order type 16 0.00 US_TECH100 sl: 15918.50 tp: 16018.50 [invalid request]
Any help greatly appreciated.