EA doesn't work with cryptocurrency but work on Forex

 

Hi Everyone, 


Im trying to make a simple EA, working on bollinger band.

After after several attempts, I alaways have this error message :

"Tester: exchange rate cannot be calculated";

"Tester: margin exchange rate cannot be calculated" 


But when I use it on forex, it seems to work without error.

Can i help please ??


input int bollingerPeriod = 20;
input double bollingerDeviation = 2;
input int priceRangeFactor = 200; // Facteur pour le range de prix
input int magicNumber = 12345; // Un numéro unique pour identifier les ordres de cet EA
input double lotSize = 0.1; // Taille de lot fixe pour les ordres
input int slippage = 3; // Slippage en points


double lowerBand, upperBand, middleBand;
double assetValue, priceRange, high, low;

int OnInit() {
    // Initialisation des bandes de Bollinger
    // ...
    return(INIT_SUCCEEDED);
}

void OnTick() {
    // Calcul de la valeur de l'actif et du range de prix
    assetValue = MarketInfo(Symbol(), MODE_BID); // Obtenir la valeur actuelle de l'actif
    priceRange = assetValue / priceRangeFactor;

    // Vérification des conditions sur les 5 bougies précédentes
    for (int i = 1; i <= 5; i++) {
        high = iHigh(NULL, 0, i);
        low = iLow(NULL, 0, i);
        if (high - low > priceRange) return; // Si le range est dépassé, arrêter
    }

    // Calcul des bandes de Bollinger pour la bougie précédente
    lowerBand = iBands(NULL, 0, bollingerPeriod, bollingerDeviation, 0, PRICE_CLOSE, MODE_LOWER, 1);
    middleBand = iBands(NULL, 0, bollingerPeriod, bollingerDeviation, 0, PRICE_CLOSE, MODE_MAIN, 1);
    upperBand = iBands(NULL, 0, bollingerPeriod, bollingerDeviation, 0, PRICE_CLOSE, MODE_UPPER, 1);

    // Vérification des conditions des bandes de Bollinger
    double closePrice = Close[1]; // Prix de clôture de la bougie précédente
    double lowPrice = Low[1]; // Prix le plus bas de la bougie précédente

    // Condition 1: vérifie si la bougie précédente a franchi la bande basse mais est revenue à l'intérieur
    bool condition1 = lowPrice < lowerBand && closePrice > lowerBand;

    // Condition 2: vérifie si la bougie précédente a clôturé plus proche de la bande basse que de la médiane
    bool condition2 = closePrice < middleBand;

    // Si toutes les conditions sont remplies
    if (condition1 && condition2) {
        // Exécuter un ordre d'achat
        int ticket = OrderSend(Symbol(), OP_BUY, lotSize, Ask, slippage, 0, 0, "Bollinger Band Buy Order", magicNumber, 0, Blue);

        // Si l'ordre est exécuté avec succès, définir le TP au niveau de la bande haute
        if (ticket > 0) {
            bool modifyResult = OrderModify(ticket, OrderOpenPrice(), OrderStopLoss(), upperBand, 0, Blue);
            if (!modifyResult) {
                Print("Erreur lors de la modification de l'ordre : ", GetLastError());
            }
        } else {
            Print("Erreur lors de l'envoi de l'ordre : ", GetLastError());
        }
    }
}

// Autres fonctions utiles (gestion des risques, etc.)
 

I am not good at reading other people's code, so my attempt may not be good.

I see you are not testing for errors when you place the order, only when you modify it. Modifying orders can be a little tricky. It's easy to overlook something.

I see this line:

bool modifyResult = OrderModify(ticket, OrderOpenPrice(), OrderStopLoss(), upperBand, 0, Blue);

But where did you get OrderOpenPrice()? I think you need this first:

if    (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES) == true)    {EnterPrice  = OrderOpenPrice();}

Then you have EnterPrice. Without that, I think you have nothing the EA can use.

I also see this line:

int ticket = OrderSend(Symbol(), OP_BUY, lotSize, Ask, slippage, 0, 0, "Bollinger Band Buy Order", magicNumber, 0, Blue);
Never use Ask for OP_BUY orders. Use Bid.

Also, instead of modifying the order later, why not just set Stoploss and Takeprofit right out of the bat?

int ticket = OrderSend(Symbol(), OP_BUY, lotSize, Ask, slippage, OrderStopLoss(), upperBand, "Bollinger Band Buy Order", magicNumber, 0, Blue);
And where is OrderStopLoss() anyway? Not in the code you submitted. You need to check if it is calculating the Stoploss price correctly.
Oh, I see. It's the internal function. Again, I think you need to call OrderSelect() first. Either way, we know it's 0 because that is what you specified in the OrderSend command. Why not just set it to 0 then.

I hope this helps somehow.
 
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
Reason: