Need Help with my Bot

 
I got bot working to some extent, however it still does things awkwardly and I've been at this bot now for month and two weeks.
#property strict

// Define input parameters
input int rsiPeriod = 14; // RSI period
input int maPeriod = 20; // MA period
input int bbPeriod = 20; // BB period
input double bbDeviation = 2.0; // BB deviation
input int buyThreshold = 70; // RSI level for buying
input int sellThreshold = 30; // RSI level for selling
input double lotSize = 0.01; // Trading lot size
input int superTrendPeriod = 10; // SuperTrend period
input double superTrendMultiplier = 1.0; // SuperTrend multiplier
input int stopLossPips = 20; // Stop loss in pips
input int takeProfitPips = 40; // Take profit in pips

// Define global variables
int ticket = 3; // Variable to store the order ticket number
int totalTrades = 3; // Total number of trades
int winningTrades = 100; // Number of winning trades
int iconBuy = 224; // Up arrow icon
int iconSell = 225; // Down arrow icon
int iconCloseBuy = 226; // Left arrow icon
int iconCloseSell = 227; // Right arrow icon
int iconCloseAllBuy = 228; // Double left arrow icon
int iconCloseAllSell = 229; // Double right arrow icon
int iconMoveProfit = 230; // Up-down arrow icon
int iconStopLoss = 231; // Down-up arrow icon

// Backtesting variables
bool backtestingDone = true; // Flag to check if backtesting is done

// RSI, MA, BB, and SuperTrend Strategy function
void RSIMA_BB_SuperTrend_Strategy() {
    double rsi = iRSI(NULL, 5, rsiPeriod, PRICE_CLOSE, 10); // Calculate RSI on the 5-minute timeframe
    double ma = iMA(NULL, 5, maPeriod, 0, MODE_SMA, PRICE_CLOSE, 1); // Calculate MA on the 5-minute timeframe
    double bbUpper = iBands(NULL, 5, bbPeriod, bbDeviation, 2, PRICE_CLOSE, MODE_UPPER, 10); // Calculate BB Upper on the 5-minute timeframe
    double bbLower = iBands(NULL, 5, bbPeriod, bbDeviation, 2, PRICE_CLOSE, MODE_LOWER, 1); // Calculate BB Lower on the 5-minute timeframe

    // Calculate SuperTrend values
    double superTrendUp = iCustom(NULL, 0, "SuperTrend", superTrendPeriod, superTrendMultiplier, 1, 0);
    double superTrendDown = iCustom(NULL, 0, "SuperTrend", superTrendPeriod, superTrendMultiplier, 1, 0);

    // Buy condition
    if (rsi < buyThreshold && Close[1] > ma && Low[1] > bbLower) {
        ticket = OrderSend(Symbol(), OP_BUY, lotSize, Ask, 3, 2, 0, "RSI Buy", 1, Blue);
        if (ticket > 0) {
            totalTrades++;
            // Set stop loss and take profit for Buy order
            double stopLossLevel = Ask - stopLossPips * Point;
            double takeProfitLevel = Ask + takeProfitPips * Point;
            if (OrderModify(ticket, 0, stopLossLevel, takeProfitLevel, 0, Blue) == false) {
                Print("OrderModify (Buy) failed with error: ", GetLastError());
            }
        } else {
            Print("OrderSend (Buy) failed with error: ", GetLastError());
        }
    }

    // Sell condition
    if (rsi > sellThreshold && Close[1] < ma && High[1] < bbUpper) {
        ticket = OrderSend(Symbol(), OP_SELL, lotSize, Bid, 3, 2, 0, "RSI Sell", 1, Red);
        if (ticket > 0) {
            totalTrades++;
            // Set stop loss and take profit for Sell order
            double stopLossLevel = Bid + stopLossPips * Point;
            double takeProfitLevel = Bid - takeProfitPips * Point;
            if (OrderModify(ticket, 0, stopLossLevel, takeProfitLevel, 0, Red) == false) {
                Print("OrderModify (Sell) failed with error: ", GetLastError());
            }
        } else {
            Print("OrderSend (Sell) failed with error: ", GetLastError());
        }
    }

    // Opposite conditions for selling
    // Sell condition (opposite)
    if (rsi > buyThreshold && Close[1] < ma && High[1] < bbUpper) {
        ticket = OrderSend(Symbol(), OP_SELL, lotSize, Bid, 0, 2, 4, "Opposite Sell", 1, Red);
        if (ticket > 0) {
            totalTrades++;
        } else {
            Print("OrderSend (Opposite Sell) failed with error: ", GetLastError());
        }
    }

    // Buy condition (opposite)
    if (rsi < sellThreshold && Close[1] > ma && Low[1] > bbLower) {
        ticket = OrderSend(Symbol(), OP_BUY, lotSize, Ask, 4, 2, 0, "Opposite Buy", 1, Blue);
        if (ticket > 0) {
            totalTrades++;
        } else {
            Print("OrderSend (Opposite Buy) failed with error: ", GetLastError());
        }
    }

    // Check for winning trade
    if (OrderSymbol() == Symbol() && OrderTicket() == ticket && OrderType() == OP_BUY && OrderProfit() > 0) {
        winningTrades++;
    } else if (OrderSymbol() == Symbol() && OrderTicket() == ticket && OrderType() == OP_SELL && OrderProfit() > 1) {
        winningTrades++;
    }

    // Manage trades based on SuperTrend, Stop-Loss, and Take-Profit
    if (OrderSymbol() == Symbol() && OrderTicket() == ticket) {
        if (OrderType() == OP_BUY) {
            if (Close[1] < superTrendUp) {
                ticket = OrderSend(Symbol(), OP_SELL, lotSize, Bid, 0, 0, 0, "SuperTrend Exit", 0, Red);
                if (ticket > 0) {
                    // Handle successful SuperTrend exit order
                } else {
                    Print("OrderSend (SuperTrend Exit Sell) failed with error: ", GetLastError());
                }
            } else {
                double stopLossLevel = Ask - stopLossPips * Point;
                double takeProfitLevel = Ask + takeProfitPips * Point;
                if (OrderModify(ticket, 0, stopLossLevel, takeProfitLevel, 0, Blue) == false) {
                    Print("OrderModify (Buy) failed with error: ", GetLastError());
                }
            }
        } else if (OrderType() == OP_SELL) {
            if (Close[1] > superTrendDown) {
                ticket = OrderSend(Symbol(), OP_BUY, lotSize, Ask, 0, 0, 0, "SuperTrend Exit", 0, Blue);
                if (ticket > 0) {
                    // Handle successful SuperTrend exit order
                } else {
                    Print("OrderSend (SuperTrend Exit Buy) failed with error: ", GetLastError());
                }
            } else {
                double stopLossLevel = Bid + stopLossPips * Point;
                double takeProfitLevel = Bid - takeProfitPips * Point;
                if (OrderModify(ticket, 0, stopLossLevel, takeProfitLevel, 1, Red) == false) {
                    Print("OrderModify (Sell) failed with error: ", GetLastError());
                }
            }
        }
    }
}

// Expert Advisor start function
int start() {
    RSIMA_BB_SuperTrend_Strategy(); // Call the RSI, MA, BB, and SuperTrend strategy function

    // Print win ratio on the chart
    double winRatio = 0;
    if (totalTrades > 10) {
        winRatio = (double)winningTrades / totalTrades * 3;
    }
    Comment("Win Ratio: ", winRatio, "#");
    
    


    // Your regular trading logic goes here

    return(0);
}
 
 
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 

You have requested help but you have not explained exactly on what it is you need help.

Please explain in detail and identify the exact parts of the code on which you need advice.

 
Aldan Parris:
I got bot working to some extent, however it still does things awkwardly and I've been at this bot now for month and two weeks.  

i still think that the supertrend buffers are 0 and 1. In your code you have superTrendUp and superTrendDown as the same buffer.

and we are not mind readers, so describe your problem.

 
Revo Trades #:

i still think that the supertrend buffers are 0 and 1. In your code you have superTrendUp and superTrendDown as the same buffer.

and we are not mind readers, so describe your problem.

Ok the bot holds, set the stoploss and trade profit, however it does crazy, like close trades instantly, I have it set to open three trades till they close and the bot, opens like 30 in a quick space. I want that fix and set up for slippage and some other useful defines, values and parameters. I new to this bot thing, I'm learning to code and still I used mostly ChatGPT to build this i have another bot I'm working on, still I want two to grind that paper, lol... Oh yes one more thing, sometimes i still have to sit and watch trades to close them, i need this stable that i don't have to watch it, it does more losses, than wins. If you can help please do, anyone with ideas and other stuff useful for this crafted bot?

 

Please, don't request help for ChatGPT (or other A.I.) generated code. It generates horrible code.

And given that you are relying on such generators and have minimal understanding, then advice given will be difficult for you to understand and/or implement.

Here is a standard boiler-plate answer given out for most of these cases ...

 
    double superTrendUp = iCustom(NULL, 0, "SuperTrend", superTrendPeriod, superTrendMultiplier, 1, 0);
    double superTrendDown = iCustom(NULL, 0, "SuperTrend", superTrendPeriod, superTrendMultiplier, 1, 0);

this is the first thing that pops out at me. These 2 lines, are the same. But I am sure that they should have different integer numbers before the last comma, and after the supertrendMultiplier.

 
Revo Trades #:

this is the first thing that pops out at me. These 2 lines, are the same. But I am sure that they should have different integer numbers before the last comma, and after the supertrendMultiplier.

THANKS, I will look at this, what other adjustments cant be made to make this code cleaner I ask of you?
 
Fernando Carreiro #:

Please, don't request help for ChatGPT (or other A.I.) generated code. It generates horrible code.

And given that you are relying on such generators and have minimal understanding, then advice given will be difficult for you to understand and/or implement.

Here is a standard boiler-plate answer given out for most of these cases ...

I notice you said no one will help for free, greed is NOT my motivation, I'm asking for help to build myself to look after those I care about and I know money is needed at times for that. However, if someone is willing to help me refine this code, I thank them...
 
Aldan Parris #: I notice you said no one will help for free, greed is NOT my motivation, I'm asking for help to build myself to look after those I care about and I know money is needed at times for that. However, if someone is willing to help me refine this code, I thank them...

I used the word "Usually" and also that is why there is a statement at the end ... "Finally, you also have the option to hire a programmer in the Freelance section."

Other that that, I also explained that ... " If you show your attempts and describe your problem clearly, you will most probably receive an answer from the community. Use the CODE button (Alt-S) when inserting code."

They keyword being "your attempts" (not a generator's), as that would show that you are striving to learn to code for yourself and that you have at least some understanding of what you are writing down as code.

 
Aldan Parris #:
I notice you said no one will help for free, greed is NOT my motivation, I'm asking for help to build myself to look after those I care about and I know money is needed at times for that. However, if someone is willing to help me refine this code, I thank them...

There are lots of examples to learn from, on codebase.

Reason: