Need help with this one I'm learning still getting lost at some part of coding like, writing the full logic. It works, as it is, however it loses.

 
//+------------------------------------------------------------------+
//|                                                                  |
//|            Comprehensive MT4 Bot Code                           |
//|                                                                  |
//+------------------------------------------------------------------+

// Input parameters
input int MaxTrades = 5;                      // Maximum number of trades
input double DDReductionMin = 10;             // Minimum drawdown reduction percentage
input double DDReductionMax = 50;             // Maximum drawdown reduction percentage
input bool ExpertMode = true;                 // Expert mode
input int Slippage = 3;                       // Slippage
input bool TradeOnNewCandle = true;           // Trade on new candle
input bool OpenBuy = true;                    // Open buy trades
input bool OpenSell = true;                   // Open sell trades
input double SetTarget = 100;                 // Set target in points
input double LockProfitPercent = 50;          // Lock profit in percentage
input double LockProfitMoney = 100;           // Lock profit in money
input double TrailingStop = 30;               // Trailing stop
input bool TrailingOnlyProfit = true;         // Trailing only profit
input double TrailingProfitPercent = 30;      // Trailing profit in percentage
input double TrailingProfitMoney = 50;        // Trailing profit in money
input int MaxSpread = 5;                      // Max spread
input int MaxOpenBuyOrders = 3;               // Maximum open buy orders
input int MaxOpenSellOrders = 3;              // Maximum open sell orders
input bool DoubleOrders = true;               // Double orders
input double Lots = 0.1;                      // Lots size
input bool AutomaticLot = false;              // Automatic lot size
input double LotsBuy = 0.1;                   // Lots size for buy orders
input double LotsSell = 0.1;                  // Lots size for sell orders
input double MaxLot = 1.0;                    // Maximum lot size

// Global variables
int totalBuyOrders = 0;
int totalSellOrders = 0;
double currentStopLoss;  // Declare global variable for current stop loss
double newStopLoss;      // Declare global variable for new stop loss
bool modifyResult;       // Declare global variable for modification result
double stopLoss;        // Declare global variable for stop loss
double takeProfit;      // Declare global variable for take profit

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    // Initialization code here
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    // Deinitialization code here
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    // Check for new candle and other conditions to place trades
    if (TradeOnNewCandle)
    {
        // Check if conditions are met to open buy trades
        if (OpenBuy && totalBuyOrders < MaxOpenBuyOrders)
        {
            PlaceBuyTrade(); // Call the function to place buy trades
        }

        // Check if conditions are met to open sell trades
        if (OpenSell && totalSellOrders < MaxOpenSellOrders)
        {
            PlaceSellTrade(); // Call the function to place sell trades
        }
    }

    // Check for trailing stop and other management tasks
    ManageTrades();
}

//+------------------------------------------------------------------+
//| Function to place buy trades                                     |
//+------------------------------------------------------------------+
void PlaceBuyTrade()
{
    // Add logic to place buy trades
    double price = Ask; // Get current ask price
    stopLoss = price - TrailingStop * Point; // Calculate stop loss
    takeProfit = price + SetTarget * Point; // Calculate take profit
    int ticket = OrderSend(Symbol(), OP_BUY, LotsBuy, price, Slippage, stopLoss, takeProfit, "Buy Order", 0, 0, Green); // Send buy order
    if(ticket > 0) // If order is placed successfully
    {
        totalBuyOrders++; // Increment total buy orders
    }
}

//+------------------------------------------------------------------+
//| Function to place sell trades                                    |
//+------------------------------------------------------------------+
void PlaceSellTrade()
{
    // Add logic to place sell trades
    double price = Bid; // Get current bid price
    stopLoss = price + TrailingStop * Point; // Calculate stop loss
    takeProfit = price - SetTarget * Point; // Calculate take profit
    int ticket = OrderSend(Symbol(), OP_SELL, LotsSell, price, Slippage, stopLoss, takeProfit, "Sell Order", 0, 0, Red); // Send sell order
    if(ticket > 0) // If order is placed successfully
    {
        totalSellOrders++; // Increment total sell orders
    }
}

//+------------------------------------------------------------------+
//| Function to manage trades                                         |
//+------------------------------------------------------------------+
void ManageTrades()
{
    for(int i = OrdersTotal() - 1; i >= 0; i--) // Loop through all orders
    {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) // Select order by position
        {
            // Check if order is a buy order
            if(OrderType() == OP_BUY)
            {
                // Check if trailing stop is enabled
                if(TrailingStop > 0)
                {
                    currentStopLoss = OrderStopLoss(); // Get current stop loss
                    newStopLoss = Bid - TrailingStop * Point; // Calculate new stop loss
                    // Check if new stop loss is less than current stop loss
                    if(newStopLoss > currentStopLoss)
                    {
                        modifyResult = OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, Green); // Modify order
                        if(modifyResult)
                        {
                            Print("Buy order modified successfully");
                        }
                    }
                }
            }
            // Check if order is a sell order
            else if(OrderType() == OP_SELL)
            {
                // Check if trailing stop is enabled
                if(TrailingStop > 0)
                {
                    currentStopLoss = OrderStopLoss(); // Get current stop loss
                    newStopLoss = Ask + TrailingStop * Point; // Calculate new stop loss
                    // Check if new stop loss is greater than current stop loss
                    if(newStopLoss < currentStopLoss)
                    {
                        modifyResult = OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, Red); // Modify order
                        if(modifyResult)
                        {
                            Print("Sell order modified successfully");
                        }
                    }
                }
            }
        }
    }
}
 
Aldan Parris: It works, as it is, however it loses.

Then your strategy or its implementation is bad.

 

I agree with William Roeder,

And I will also add to his comment and say that the code snippet that you've shared doesn't actually have any strategy.

It just opens new buy and sell orders simultaneously, at new candle, if the open buy/sell positions is less than a specified limit (3, by default).


I would say that coding a strategy is the extremely easy part.

Finding a profitable strategy is the hard part.

 
William Roeder #:

Then your strategy or its implementation is bad.

thank will take into account this and look to think of one that meet my needs

 
AMI289 #:

I agree with William Roeder,

And I will also add to his comment and say that the code snippet that you've shared doesn't actually have any strategy.

It just opens new buy and sell orders simultaneously, at new candle, if the open buy/sell positions is less than a specified limit (3, by default).


I would say that coding a strategy is the extremely easy part.

Finding a profitable strategy is the hard part.

understood, I'm still new to this, so bear with me...

 
Aldan Parris #:

understood, I'm still new to this, so bear with me...

can someone help me build a strategy for this then however please DM, the version with a working one cause i'm still trying to learn that part of building these EA's...

 
Aldan Parris #:

can someone help me build a strategy for this then however please DM, the version with a working one cause i'm still trying to learn that part of building these EA's...

see the Freelance page. Buy a coder or go to codebase to find free stuff. learn by reading and copying from others.

 
    double price = Ask; // Get current ask price
    stopLoss = price - TrailingStop * Point; // Calculate stop loss
    takeProfit = price + SetTarget * Point; // Calculate take profit

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

  1. 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?

  2. 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

  3. 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)