Página 1 de 1

problema 129 cerrando trade

Publicado: 11 Dic 2023 15:30
por especulador101
¿Alguien sabrá por qué, usando la función "Alert" en el siguiente código, me dice en el mensaje: "error cerrando corta: 129", siendo que el precio al cerrar es "Bid", el cual ya está normalizado? (lo he probado en dos cuentas demo, por si el problema estaba en la plataforma):


void OnTick()
{

double SMA7Previous, SMA9Previous, SL, TP, SalidaReal, OpenPrice;
int Period_Mayor, cnt, ticket, Total;
Period_Mayor=9;
SMA7Previous=iMA(NULL,PERIOD_M15,7,0,MODE_SMA,PRICE_CLOSE,1);
SMA9Previous=iMA(NULL,PERIOD_M15,Period_Mayor,0,MODE_SMA,PRICE_CLOSE,1);
SL = 0.00100;
TP = 0.00100;
SalidaReal=0.00060;
Total=OrdersTotal();
//---
if(Bars<50)
{
Print("bars less than 100");
return;
}



if(Total==0)
{
if(SMA7Previous>SMA9Previous)
{
RefreshRates();
ticket = OrderSend(Symbol(), OP_BUY, 0.01, Ask, 3, Ask-SL, Ask+TP, "FT_SMA_BUY", 12345, 0, Green);
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
{
OpenPrice=OrderOpenPrice();
Print("Posición larga abierta con éxito: ", ticket);
}
else
{
Alert("Error abriendo larga:", GetLastError());
}
}

if(SMA7Previous<SMA9Previous)
{
RefreshRates();
ticket = OrderSend(Symbol(), OP_SELL, 0.01, Bid, 3, Bid+SL, Bid-TP, "FT_SMA_SELL", 23456, 0, Red);
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
{
OpenPrice=OrderOpenPrice();
Print("Posición cortaabierta con éxito: ", ticket);
}
else
{
Alert("Error abriendo corta:", GetLastError());
}
}

}

for(cnt=0;cnt<Total;cnt++)
{
if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
{
continue;
}

if(OrderType()==OP_BUY && OrderSymbol()==Symbol())
{
double WinLargo= Ask-OpenPrice;
double LossLargo=OpenPrice-Ask;
if(SMA7Previous<SMA9Previous || WinLargo>SalidaReal || LossLargo>SalidaReal)
{
RefreshRates();
if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,CLR_NONE))
{
Alert("Error cerrando largo",GetLastError());
}
}
}
if(OrderType()==OP_SELL && OrderSymbol()==Symbol())
{
double WinCorto= OpenPrice-Bid;
double LossCorto=Bid-OpenPrice;
if(SMA7Previous>SMA9Previous || WinCorto>SalidaReal || LossCorto>SalidaReal)
{
RefreshRates();
if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,CLR_NONE))
{
Alert("Error cerrando corta",GetLastError());
}
}
}
}
}
//+------------------------------------------------------------------+

Re: problema 129 cerrando trade

Publicado: 11 Dic 2023 17:46
por X-Trader
El error 129 (ERR_INVALID_PRICE) indica que alguno de los precios introducidos no es válido. Revisando tu código intuyo que el problema viene del SL/TP. Para solucionarlo yo haría algo como esto:

Código: Seleccionar todo

// Al principio define SL y TP como enteros
int SL, TP;
SL = 100;
TP = 100;

// Y luego en el código que procesa la orden calcula antes los valores (el ejemplo es para una orden de compra):

double TakeProfitPrice = 0;
double StopLossPrice = 0;
double Price;
double point = SymbolInfoDouble(symbol, SYMBOL_POINT);
int digits = (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);

Price = OrderOpenPrice();
if (TakeProfit > 0)
    {
      TakeProfitPrice = NormalizeDouble(Price + TakeProfit * point, digits);
      TakeProfitPrice = NormalizeDouble(MathRound(TakeProfitPrice / tick_size) * tick_size, digits); 
        }

if (StopLoss > 0)
      {
        StopLossPrice = NormalizeDouble(Price - StopLoss * point, digits);
        StopLossPrice = NormalizeDouble(MathRound(StopLossPrice / tick_size) * tick_size, digits); 
            }
Con eso debería funcionarte, ya me cuentas ;).


Saludos,
X-Trader