I need some help with this EA don't add sl and tp

 

I need some help with this EA don't add SL and TP on live trading but when i use it on strategy tester it works normal

sorry if the code is missy I'm just using chatgpt for this 

the mean EA concept is to open buy and sell position at every new candle Open.

Thanks

//+------------------------------------------------------------------+
//|                                                      Your_EA.mq5 |
//|                        Copyright 2024, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+

#define   version       "1.00"
#property strict

#include <Trade/Trade.mqh>
#include <Trade/PositionInfo.mqh>

input double TakeProfitAmount = 100.0; // Take profit amount in points
input double StopLossAmount = 50.0;    // Stop loss amount in points
input double LotSize = 0.01;
input group             "Time control"
input bool                 InpTimeControl          = true;           // Use time control
input uchar                InpStartHour            = 10;             // Start Hour
input uchar                InpStartMinute          = 01;             // Start Minute
input uchar                InpEndHour              = 15;             // End Hour
input uchar                InpEndMinute            = 02;             // End Minute
input long   Magic = 1;

CTrade         trade;
CPositionInfo  position;

int OnInit() {
   trade.SetExpertMagicNumber(Magic);
   if (!trade.SetTypeFillingBySymbol(_Symbol)) {
      trade.SetTypeFilling(ORDER_FILLING_RETURN);
   }
   return INIT_SUCCEEDED;
}


//+------------------------------------------------------------------+
//| TimeControl                                                      |
//+------------------------------------------------------------------+
bool TimeControlHourMinute(void)
  {
   if(!InpTimeControl)
      return(true);
   MqlDateTime STimeCurrent;
   datetime time_current=TimeCurrent();
   if(time_current==D'1970.01.01 00:00')
      return(false);
   TimeToStruct(time_current,STimeCurrent);
   if((InpStartHour*60*60+InpStartMinute*60)<(InpEndHour*60*60+InpEndMinute*60)) // intraday time interval
     {
      /*
      Example:
      input uchar    InpStartHour      = 5;        // Start hour
      input uchar    InpEndHour        = 10;       // End hour
      0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22 23 0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15
      _  _  _  _  _  +  +  +  +  +  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  +  +  +  +  +  _  _  _  _  _  _
      */
      if((STimeCurrent.hour*60*60+STimeCurrent.min*60>=InpStartHour*60*60+InpStartMinute*60) &&
         (STimeCurrent.hour*60*60+STimeCurrent.min*60<InpEndHour*60*60+InpEndMinute*60))
         return(true);
     }
   else
      if((InpStartHour*60*60+InpStartMinute*60)>(InpEndHour*60*60+InpEndMinute*60)) // time interval with the transition in a day
        {
         /*
         Example:
         input uchar    InpStartHour      = 10;       // Start hour
         input uchar    InpEndHour        = 5;        // End hour
         0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22 23 0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15
         _  _  _  _  _  _  _  _  _  _  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  _  _  _  _  _  +  +  +  +  +  +
         */
         if(STimeCurrent.hour*60*60+STimeCurrent.min*60>=InpStartHour*60*60+InpStartMinute*60 ||
            STimeCurrent.hour*60*60+STimeCurrent.min*60<InpEndHour*60*60+InpEndMinute*60)
            return(true);
        }
      else
         return(false);
//---
   return(false);
  }



void OnTick() {
   static datetime lastBarTime = 0;
   datetime barTime = iTime(_Symbol, PERIOD_CURRENT, 0);
   if (barTime != lastBarTime) {
      if (lastBarTime != 0) OnBar();
      lastBarTime = barTime;
   }
}

void OnBar() {
   double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);

   // Sell logic
   if (TimeControlHourMinute() && trade.Sell(LotSize, _Symbol, bid, 0.0, 0.0)) {
      if (trade.ResultRetcode() == TRADE_RETCODE_DONE ||
          trade.ResultRetcode() == TRADE_RETCODE_DONE_PARTIAL) {
         ulong ticket = trade.ResultDeal();
         if (position.SelectByTicket(ticket)) {
            double valuePerLot = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE) 
                                    / SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
            double takeProfit = position.PriceOpen() - TakeProfitAmount / valuePerLot;
            double stopLoss = position.PriceOpen() + StopLossAmount / valuePerLot;
            trade.PositionModify(ticket, stopLoss, takeProfit);
         }
      }
   }

   // Buy logic
   if (TimeControlHourMinute() && trade.Buy(LotSize, _Symbol, ask, 0.0, 0.0)) {
      if (trade.ResultRetcode() == TRADE_RETCODE_DONE ||
          trade.ResultRetcode() == TRADE_RETCODE_DONE_PARTIAL) {
         ulong ticket = trade.ResultDeal();
         if (position.SelectByTicket(ticket)) {
            double valuePerLot = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE) 
                                    / SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
            double takeProfit = position.PriceOpen() + TakeProfitAmount / valuePerLot;
            double stopLoss = position.PriceOpen() - StopLossAmount / valuePerLot;
            trade.PositionModify(ticket, stopLoss, takeProfit);
         }
      }
   }
}
 

Hi

Probably the list of trades is not yet updated after opening - try to add SL and TP prices directly with the open order request.

I see you use somelbrary here for trades - but assume those 0.0s n the Sell/Buy functions may be sl/tp levels. So calculate them first:

double takeProfit = ask- TakeProfitAmount*_Point;

double stopLoss = ask + StopLossAmount *_Point;

and then:
trade.Buy(LotSize, _Symbol, ask, stopLoss, takeProfit)

If you have problems with coding on your own, you probably get better results when you find some freelancer to code the strategy for you.

Best Regards

 
ulong ticket = trade.ResultDeal();
ResultDeal returns the ticket very late... Use this instead:
ulong ticket = PositionGetTicket(PositionsTotal()-1);