My brain can't take it anymore As a Novice

 
I need help with the code below. Apparently it's not as functional as I want. After all I did it can only open a trade before stopping, I just want it to double the Tp and SL when the trade hit Sl and back to normal when it hits Takeprofit
//---------------------------------+
#include <Trade\Trade.mqh>
CTrade trade;

input double StopLoss = 60;     // Initial Stop Loss in points
input double TakeProfit = 60;   // Initial Take Profit in points
input double LotSize = 0.01;    // Lot size for trading
input int MaxTrades = 1;        // Maximum number of trades to open
int totalTrades = 0;            // Counter for total trades opened
input int MagicNumber = 12345;  // Magic number to identify trades

//+------------------------------------------------------------------+
bool Last_Trade_Closed_In_TP(int Type) {
    for(int i=HistoryDealsTotal()-1; i>=0; i--) {
        ulong ticket = HistoryDealGetTicket(i);
        if(HistoryDealSelect(ticket)) {
            if(HistoryDealGetInteger(ticket, DEAL_TYPE) == Type) {
                double profit = HistoryDealGetDouble(ticket, DEAL_PROFIT);
                if(profit >= 0) { // Closed in profit (take profit)
                    return true;
                }
            }
        }
    }
    return false;
}

bool Last_Trade_Closed_In_SL(int Type) {
    for(int i=HistoryDealsTotal()-1; i>=0; i--) {
        ulong ticket = HistoryDealGetTicket(i);
        if(HistoryDealSelect(ticket)) {
            if(HistoryDealGetInteger(ticket, DEAL_TYPE) == Type) {
                double profit = HistoryDealGetDouble(ticket, DEAL_PROFIT);
                double stopLoss = HistoryDealGetDouble(ticket, DEAL_SL);
                if(stopLoss != 0 && profit < 0) { // Closed due to stop loss
                    return true;
                }
            }
        }
    }
    return false;
}

//+------------------------------------------------------------------+
int OnInit() {
    return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason) {
    // Perform cleanup tasks if needed
}

void OnTick() {
    int slippage = 3;
    double Price = SymbolInfoDouble(Symbol(), SYMBOL_BID);
    double Stoploss = Price + StopLoss * Point();      // Stop Loss for new trade
    double Takeprofit = Price - TakeProfit * Point();   // Take Profit for new trade
    
    if(PositionsTotal() < 1) {
        if(Last_Trade_Closed_In_SL(POSITION_TYPE_SELL)) {
            double SL = Stoploss * 2;    // Double the Stop Loss for new trade
            double TP = Takeprofit * 2;   // Double the Take Profit for new trade
            int ticket = Order_Send(Symbol(), 1, LotSize, Price, slippage, SL, TP, "Sell Order", MagicNumber, 0);
        }
      
        if(Last_Trade_Closed_In_TP(POSITION_TYPE_SELL) || totalTrades == 0) {
            totalTrades++;
            int ticket = Order_Send(Symbol(), 1, LotSize, Price, slippage, Stoploss, Takeprofit, "Sell Order", MagicNumber, 0);
        }
    }
}

bool Order_Send(string symbol, int type, double volume, double price, int Slippage, double stoploss,
                double takeprofit, string comment, int magicnumber, datetime expiration) {
    trade.SetExpertMagicNumber(magicnumber);
    trade.SetDeviationInPoints(Slippage);
    
    if(type == 0 && trade.Buy(volume,  symbol, price, stoploss, takeprofit, comment)) {
        return true;
    } 
    if(type == 1 && trade.Sell(volume,  symbol, price, stoploss, takeprofit, comment)) {
        return true;
    } 
    
    return false;
}
 
Help anyone please, I will appreciate

 

Hello

The last deal of a position is the closing deal and its of the opposite type . So a sell will close as a buy .

What you needed to do (for hedging mode accounts) was get the position id of the last deal that closed 

Get it's position id

Select all deals with that position id

then the first deal of the new selection is the opening deal

and the last deal of the new selection is the closing deal

also , you were doubling the entire price not just the sl + tp 

this code also needs further filtration to ensure you are working with your ea's positions 

//---------------------------------+
#include <Trade\Trade.mqh>
CTrade trade;

input double StopLoss = 60;     // Initial Stop Loss in points
input double TakeProfit = 60;   // Initial Take Profit in points
input double LotSize = 0.01;    // Lot size for trading
input int MaxTrades = 1;        // Maximum number of trades to open
int totalTrades = 0;            // Counter for total trades opened
input int MagicNumber = 12345;  // Magic number to identify trades

void Last_Trade_Check(bool &was_tp,bool &was_sl){
was_sl=false;
was_tp=false;
if(HistorySelect(0,TimeCurrent())){
//get the ticket of the last deal
ulong ticket = HistoryDealGetTicket(HistoryDealsTotal()-1);
//now get the id of the position of that deal
long position_id=HistoryDealGetInteger(ticket,DEAL_POSITION_ID);
//now select all deals for that position ID 
if(HistorySelectByPosition(position_id)){
//the first deal is the open 
  ticket=HistoryDealGetTicket(0);
  double op=HistoryDealGetDouble(ticket,DEAL_PRICE);
  double sl=HistoryDealGetDouble(ticket,DEAL_SL);
  double tp=HistoryDealGetDouble(ticket,DEAL_TP);
//the last deal is the close 
  ticket=HistoryDealGetTicket(HistoryDealsTotal()-1);
  double cp=HistoryDealGetDouble(ticket,DEAL_PRICE);
  double prft=HistoryDealGetDouble(ticket,DEAL_PROFIT);
  //tp
    if(tp>0.0&&prft>0.0){was_tp=true;}
  //sl
    if(sl>0.0&&prft<0.0){was_sl=true;}
}   
}
}
//+------------------------------------------------------------------+
int OnInit() {
    return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason) {
    // Perform cleanup tasks if needed
}

void OnTick() {
    bool wasTP=false,wasSL=false;
    Last_Trade_Check(wasTP,wasSL);
    
    if(PositionsTotal() < 1) {
        if(wasSL) {
          int slippage = 1000;
          double Price = SymbolInfoDouble(Symbol(), SYMBOL_BID);
          double SL = Price + StopLoss * Point() * 2.0;      // Stop Loss for new trade
          double TP = Price - TakeProfit * Point() * 2.0;   // Take Profit for new trade
          int ticket = Order_Send(Symbol(), 1, LotSize, Price, slippage, SL, TP, "FromLastSL", MagicNumber, 0);
        }
      
        if(wasTP || totalTrades == 0) {
            totalTrades++;
          int slippage = 1000;
          double Price = SymbolInfoDouble(Symbol(), SYMBOL_BID);
          double SL = Price + StopLoss * Point();      // Stop Loss for new trade
          double TP = Price - TakeProfit * Point();   // Take Profit for new trade
          int ticket = Order_Send(Symbol(), 1, LotSize, Price, slippage, SL, TP, "FromLastTP", MagicNumber, 0);
        }
    }
}

bool Order_Send(string symbol, int type, double volume, double price, int Slippage, double stoploss,
                double takeprofit, string comment, int magicnumber, datetime expiration) {
    trade.SetExpertMagicNumber(magicnumber);
    trade.SetDeviationInPoints(Slippage);
    
    if(type == 0 && trade.Buy(volume,  symbol, price, stoploss, takeprofit, comment)) {
        return true;
    } 
    if(type == 1 && trade.Sell(volume,  symbol, price, stoploss, takeprofit, comment)) {
        return true;
    } 
    
    return false;
}


 

Reason: