Am I in the right place for Coding help?
1571im looking to have some fix my code. am i at the right place
Post the code so we can see if we can fix it.rnicholas wrote: Thu Apr 18, 2024 9:09 pm im looking to have some fix my code. am i at the right place
Code: Select all
void UpdateTrailingStop()
{
double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
double trailingStopPrice;
if(PositionSelect(_Symbol))
{
// Get the time when the current candle opened
datetime currentTime = TimeCurrent();
datetime positionOpenTime = (datetime)PositionGetInteger(POSITION_TIME);
int candleTimeframe = PeriodSeconds(_Period); // Timeframe of the chart in seconds
// Calculate how many candles have closed since the trade was opened
int candlesSinceOpen = (int)((currentTime - positionOpenTime) / candleTimeframe);
// Check if the required number of candles have passed
if(candlesSinceOpen >= CandlesBeforeTrailing)
{
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
{
double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
trailingStopPrice = NormalizeDouble(openPrice + TrailingStopPoints * point, _Digits);
// Move stop loss into profit
if(trailingStopPrice < SymbolInfoDouble(_Symbol, SYMBOL_BID) && trailingStopPrice > PositionGetDouble(POSITION_SL))
trade.PositionModify(_Symbol, trailingStopPrice, PositionGetDouble(POSITION_TP));
}
else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
{
double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
trailingStopPrice = NormalizeDouble(openPrice - TrailingStopPoints * point, _Digits);
// Move stop loss into profit
if(trailingStopPrice > SymbolInfoDouble(_Symbol, SYMBOL_ASK) && trailingStopPrice < PositionGetDouble(POSITION_SL))
trade.PositionModify(_Symbol, trailingStopPrice, PositionGetDouble(POSITION_TP));
}
}
}
}
DaffyTaffy wrote: Mon Apr 22, 2024 9:02 pm Hi everyone. I'm trying to create a function that moves the stoploss into a set number of profit after a certain number of candles have passed since the point of entry in mql5. It works for buys trades but never for sell trades for some reason. I've been staring at it for hours and can't figure out why.
Here's the function below. Much thanks in advance to anyone who can spot the problem.
Code: Select all
void UpdateTrailingStop() { double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT); double trailingStopPrice; if(PositionSelect(_Symbol)) { // Get the time when the current candle opened datetime currentTime = TimeCurrent(); datetime positionOpenTime = (datetime)PositionGetInteger(POSITION_TIME); int candleTimeframe = PeriodSeconds(_Period); // Timeframe of the chart in seconds // Calculate how many candles have closed since the trade was opened int candlesSinceOpen = (int)((currentTime - positionOpenTime) / candleTimeframe); // Check if the required number of candles have passed if(candlesSinceOpen >= CandlesBeforeTrailing) { if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) { double openPrice = PositionGetDouble(POSITION_PRICE_OPEN); trailingStopPrice = NormalizeDouble(openPrice + TrailingStopPoints * point, _Digits); // Move stop loss into profit if(trailingStopPrice < SymbolInfoDouble(_Symbol, SYMBOL_BID) && trailingStopPrice > PositionGetDouble(POSITION_SL)) trade.PositionModify(_Symbol, trailingStopPrice, PositionGetDouble(POSITION_TP)); } else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) { double openPrice = PositionGetDouble(POSITION_PRICE_OPEN); trailingStopPrice = NormalizeDouble(openPrice - TrailingStopPoints * point, _Digits); // Move stop loss into profit if(trailingStopPrice > SymbolInfoDouble(_Symbol, SYMBOL_ASK) && trailingStopPrice < PositionGetDouble(POSITION_SL)) trade.PositionModify(_Symbol, trailingStopPrice, PositionGetDouble(POSITION_TP)); } } } }
Code: Select all
void LockInProfit()
{
double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
double trailingStopPrice;
if(PositionSelect(_Symbol))
{
// Get the time when the current candle opened
datetime currentTime = TimeCurrent();
datetime positionOpenTime = (datetime)PositionGetInteger(POSITION_TIME);
int candleTimeframe = PeriodSeconds(_Period); // Timeframe of the chart in seconds
// Calculate how many candles have closed since the trade was opened
int candlesSinceOpen = (int)((currentTime - positionOpenTime) / candleTimeframe);
// Check if the required number of candles have passed
if(candlesSinceOpen >= CandlesBeforeTrailing)
{
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
{
double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
trailingStopPrice = NormalizeDouble(openPrice + TrailingStopPoints * point, _Digits);
// Move stop loss into profit
if(trailingStopPrice < SymbolInfoDouble(_Symbol, SYMBOL_BID) && trailingStopPrice > PositionGetDouble(POSITION_SL))
trade.PositionModify(_Symbol, trailingStopPrice, PositionGetDouble(POSITION_TP));
}
else
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
{
double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
trailingStopPrice = NormalizeDouble(openPrice - TrailingStopPoints * point, _Digits);
// Move stop loss into profit
// Ensure stop loss is set above the current ask price but does not need to be lower than the existing stop if the market moved favorably
if(trailingStopPrice > SymbolInfoDouble(_Symbol, SYMBOL_ASK) && trailingStopPrice > PositionGetDouble(POSITION_SL))
trade.PositionModify(_Symbol, trailingStopPrice, PositionGetDouble(POSITION_TP));
}
}
}
}
I suspect that your reference to PositionSelect is not returning the latest position data:DaffyTaffy wrote: Mon Apr 22, 2024 9:59 pm I feel kinda slow right now. Or maybe I've just been at my screen for too long. Fixed it buy changing making both conditions "&& trailingStopPrice > PositionGetDouble(POSITION_SL)" but I don't get how that's supposed to work. Shouldn't it be the other way around? I also changed the name of the function as my intention was to lock in a bit of profit and not trail the stops. Removing the "&& trailingStopPrice > PositionGetDouble(POSITION_SL)" part of the code entirely alos works.
Code: Select all
if(PositionsTotal() == 1)
{
for(int i = PositionsTotal() - 1; i >= 0; i--) // count all currency pair positions
{
// get the ticket number
ulong PositionTicket=PositionGetTicket(i);
ENUM_POSITION_TYPE type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
ENUM_POSITION_PROPERTY_DOUBLE openprice = (ENUM_POSITION_PROPERTY_DOUBLE)PositionGetDouble(POSITION_PRICE_OPEN);
if (type == POSITION_TYPE_BUY)
{
lastBuyStopPrice = POSITION_PRICE_OPEN;
}
if (type == POSITION_TYPE_SELL)
{
lastSellStopPrice = POSITION_PRICE_OPEN;
}
}
}
tankk is the one who coded this and has non repaint versions available
Try, seems it might be better.pin12 wrote: Tue Jun 25, 2024 2:10 am OrderBlocks Origin Indicator
Hello
This interesting indicator seems accurate, but sometimes it does not appear on the chart, it is as if it is erased or disappears.
Could an expert coder check it to see if there is anything wrong with it?
Thank you
Users browsing this forum: No registered users and 4 guests