EA Code Query

 

I tried the code below to close half the open position once my unrealized profits exceed $200. 
This did not happen to my trades.

Can anyone enlighted why so how what went wrong?

I get the error message Error Opening order 129. 

How can i resolve this?


THanks! 


//+----------------------------------------------------------------+

//|                                            50% Take Profit.mq4 |
//|                        Copyright 2023, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern int fast_ma_period = 10; // fast MA period
extern int slow_ma_period = 20; // slow MA period
extern int magic_number = 1234; // unique identifier for this EA

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    //---

    //---
    return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    //---

}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    double unrealized_profit = AccountInfoDouble(ACCOUNT_PROFIT);
    double open_profit = 0;
    int total_positions = OrdersTotal();

    // Calculate open profit
    for (int i = 0; i < total_positions; i++)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            open_profit += OrderProfit();
        }
    }

    // Check if unrealized profit is greater than $200
    if (unrealized_profit >= 200)
    {
        // Calculate position size for selling half of positions
        double lot_size = NormalizeDouble(AccountBalance() * 0.5 / 20000, 2);

        // Sell half of positions
        for (int i = 0; i < total_positions; i++)
        {
            if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
            {
                if (OrderType() == OP_BUY && OrderProfit() > 0)
                {
                    int ticket = OrderTicket();
                    double volume = OrderLots() * lot_size;
                    if (OrderSend(Symbol(), OP_SELL, volume, Ask, 3, 0, 0, "Close half", magic_number, 0, Red) == -1)
                    {
                        Print("Error opening order: ", GetLastError());
                    }
                }
                else if (OrderType() == OP_SELL && OrderProfit() > 0)
                {
                    int ticket = OrderTicket();
                    double volume = OrderLots() * lot_size;
                    if (OrderSend(Symbol(), OP_BUY, volume, Bid, 3, 0, 0, "Close half", magic_number, 0, Green) == -1)
                    {
                        Print("Error opening order: ", GetLastError());
                    }
                }
            }
        }
    }
    
}

 

If your program does not do what it should the debugger will tell you where went what wrong:

Code debugging:  https://www.metatrader5.com/en/metaeditor/help/development/debug
Error Handling and Logging in MQL5:  https://www.mql5.com/en/articles/2041
Tracing, Debugging and Structural Analysis of Source Code, scroll down to: "Launching and Debuggin": https://www.mql5.com/en/articles/272

It's faster..

Code debugging - Developing programs - MetaEditor Help
  • www.metatrader5.com
MetaEditor has a built-in debugger allowing you to check a program execution step by step (by individual functions). Place breakpoints in the code...
 
How do you accept data. 
one direction retracement entry's, both direction reverse entry tp. if the most previous V2 had no tp so all 200 positions held entry 1 direction.

1 position reverse entry tp, lot increase manual or auto

200 simultaneous pending trades tp x2 lot manual

200 simultaneous pending trades tp reverse 200 simultaneous pending trades tp x2 account/lot size manual.

BOTH DIRECTIONS - simultaneous pending trades start 1 position x2x2x2, increasing/decreasing the simultaneous pending trades limit 25%/50% each time. adjusting based on ration of buy to sell entry's+ distance/shapes/patterns.

 
Fruit B:

I tried the code below to close half the open position once my unrealized profits exceed $200. 
This did not happen to my trades.

Can anyone enlighted why so how what went wrong?

I get the error message Error Opening order 129. 

How can i resolve this?


THanks! 



Because you are trying to sell at Ask. We Buy at Ask and Sell on Bid. 
 
Thank you. :)
Reason: