Someone could help with a simple EA ?

 
I setted with AI a small routine 
Open 3 position at 0.01 same time
Open a pop up to insert SL Same for all positions
Tp1
Tp2
Tp3
Apply button
At tp1 first position automatically close and bring tp2 and tp3 at BE + or - offset 0.20 price of entrance
The problem is that this last part doesnt work SL remain the same for the last 2 positions and same at Tp1 SL
Thanks in advance



 
Tonybarba:

There are many forums where you everyone is welcome to make such requests, but this is not one of those.

This forum is mostly for coding help and reporting of bugs with mt4/5.

and requests mentioning AI are especially ignored.

if you want help with coding then, you need to upload your code via the code s feature in the comment editor so that we can all see it; but if it is done by AI it will be recognised as being done by a non human and you will be ignored or even get ridiculed.

 
Michael Charles Schefe #:

There are many forums where you everyone is welcome to make such requests, but this is not one of those.

This forum is mostly for coding help and reporting of bugs with mt4/5.

and requests mentioning AI are especially ignored.

if you want help with coding then, you need to upload your code via the code s feature in the comment editor so that we can all see it; but if it is done by AI it will be recognised as being done by a non human and you will be ignored or even get ridiculed.

Sorry but this is my first day here so no chance to be helped if AI is the author
Thanks anyway
 
Michael Charles Schefe #:

There are many forums where you everyone is welcome to make such requests, but this is not one of those.

This forum is mostly for coding help and reporting of bugs with mt4/5.

and requests mentioning AI are especially ignored.

if you want help with coding then, you need to upload your code via the code s feature in the comment editor so that we can all see it; but if it is done by AI it will be recognised as being done by a non human and you will be ignored or even get ridiculed.

Please don't talk in everyone's name. 

There is no problem to post such request on this forum. Everyone is free to have it's opinion and to not impose it to others.

 
Tonybarba #:
Sorry but this is my first day here so no chance to be helped if AI is the author
Thanks anyway
You requested for help with coding but didn't post any code ?
 
Alain Verleyen #:
You requested for help with coding but didn't post any code ?

sorry here's the code 


many thanks appreciate any suggestion


Tony

#include <Trade/Trade.mqh>

CTrade trade;

input double LotSize = 0.01;
input double BE_Offset_Value = 0.20; // Offset per Break Even

double SL_Price=0, TP1_Price=0, TP2_Price=0, TP3_Price=0;
bool popup_open=false;
bool break_even_done=false;

//+------------------------------------------------------------------+
int OnInit()
{
    CreateMainButton("BUY_BTN",20,40,"BUY",clrGreen);
    CreateMainButton("SELL_BTN",100,40,"SELL",clrRed);
    return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
void OnTick()
{
    MonitorBreakEven();
    AutoCloseTP();
}
//+------------------------------------------------------------------+
//| Creazione pulsanti principali                                    |
//+------------------------------------------------------------------+
void CreateMainButton(string name,int x,int y,string text,color bg)
{
    ObjectCreate(0,name,OBJ_BUTTON,0,0,0);
    ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);
    ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y);
    ObjectSetInteger(0,name,OBJPROP_XSIZE,70);
    ObjectSetInteger(0,name,OBJPROP_YSIZE,25);
    ObjectSetInteger(0,name,OBJPROP_BGCOLOR,bg);
    ObjectSetString(0,name,OBJPROP_TEXT,text);
}
//+------------------------------------------------------------------+
//| Apertura 3 posizioni                                             |
//+------------------------------------------------------------------+
void OpenTrade(bool buy)
{
    break_even_done=false;
    double price = buy ? SymbolInfoDouble(_Symbol,SYMBOL_ASK)
                       : SymbolInfoDouble(_Symbol,SYMBOL_BID);

    for(int i=0;i<3;i++)
    {
        bool result = buy ? trade.Buy(LotSize,_Symbol,price,0,0)
                          : trade.Sell(LotSize,_Symbol,price,0,0);
        if(!result)
        {
            Print("Errore apertura posizione ",i," Retcode:",trade.ResultRetcode());
            return;
        }
    }

    CreatePopup();
}
//+------------------------------------------------------------------+
//| Applicazione SL e TP                                             |
//+------------------------------------------------------------------+
void ApplySLTP()
{
    struct PosData
    {
        ulong ticket;
        datetime time;
        ENUM_POSITION_TYPE type;
    };

    PosData positions[10];
    int count=0;

    for(int i=0;i<PositionsTotal();i++)
    {
        ulong ticket=PositionGetTicket(i);
        if(ticket==0) continue;
        if(PositionSelectByTicket(ticket))
        {
            if(PositionGetString(POSITION_SYMBOL)==_Symbol)
            {
                positions[count].ticket=ticket;
                positions[count].time=(datetime)PositionGetInteger(POSITION_TIME);
                positions[count].type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
                count++;
            }
        }
    }

    if(count<3)
    {
        Print("Non trovate 3 posizioni.");
        return;
    }

    // Ordina per apertura
    for(int i=0;i<count-1;i++)
        for(int j=i+1;j<count;j++)
            if(positions[i].time>positions[j].time)
            {
                PosData tmp=positions[i];
                positions[i]=positions[j];
                positions[j]=tmp;
            }

    double tps[3]={TP1_Price,TP2_Price,TP3_Price};

    for(int i=0;i<3;i++)
    {
        if(!trade.PositionModify(positions[i].ticket,SL_Price,tps[i]))
        {
            Print("Errore modifica ticket ",positions[i].ticket," Ret:",trade.ResultRetcode());
            return;
        }
    }

    Print("SL e TP applicati correttamente.");
}
//+------------------------------------------------------------------+
//| Break Even TP2 e TP3 dopo TP1                                    |
//+------------------------------------------------------------------+
void MonitorBreakEven()
{
    if(break_even_done) return;

    for(int idx=0; idx<PositionsTotal(); idx++)
    {
        ulong ticket = PositionGetTicket(idx);
        if(ticket==0) continue;
        if(!PositionSelectByTicket(ticket)) continue;
        if(PositionGetString(POSITION_SYMBOL)!=_Symbol) continue;

        ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
        double current=(type==POSITION_TYPE_BUY)?SymbolInfoDouble(_Symbol,SYMBOL_BID)
                                               :SymbolInfoDouble(_Symbol,SYMBOL_ASK);

        bool tp1_hit = (type==POSITION_TYPE_BUY && current>=TP1_Price) ||
                       (type==POSITION_TYPE_SELL && current<=TP1_Price);

        if(tp1_hit)
        {
            int pos_count=0;
            for(int j=0;j<PositionsTotal();j++)
            {
                ulong t = PositionGetTicket(j);
                if(t==0) continue;
                if(!PositionSelectByTicket(t)) continue;
                if(PositionGetString(POSITION_SYMBOL)!=_Symbol) continue;

                pos_count++;
                if(pos_count>=2) // TP2 e TP3
                {
                    double curr_tp=PositionGetDouble(POSITION_TP);
                    double open_price=PositionGetDouble(POSITION_PRICE_OPEN);
                    double newSL=(type==POSITION_TYPE_BUY)?open_price+BE_Offset_Value
                                                        :open_price-BE_Offset_Value;
                    newSL=NormalizeDouble(newSL,_Digits);

                    if(!trade.PositionModify(t,newSL,curr_tp))
                        Print("Errore BE TP2/TP3 ticket ",t," Ret:",trade.ResultRetcode());
                    else
                        Print("BreakEven applicato TP2/TP3, ticket ",t);
                }
            }

            break_even_done=true;
            Print("BREAK EVEN TP2 e TP3 ATTIVATO DOPO TP1");
            return;
        }
    }
}
//+------------------------------------------------------------------+
//| Chiusura automatica posizioni raggiunto TP                        |
//+------------------------------------------------------------------+
void AutoCloseTP()
{
    for(int i=0;i<PositionsTotal();i++)
    {
        ulong ticket = PositionGetTicket(i);
        if(ticket==0) continue;
        if(!PositionSelectByTicket(ticket)) continue;
        if(PositionGetString(POSITION_SYMBOL)!=_Symbol) continue;

        ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
        double current=(type==POSITION_TYPE_BUY)?SymbolInfoDouble(_Symbol,SYMBOL_BID)
                                               :SymbolInfoDouble(_Symbol,SYMBOL_ASK);

        double tp=PositionGetDouble(POSITION_TP);
        if(tp==0) continue;

        bool tp_hit=(type==POSITION_TYPE_BUY && current>=tp) ||
                    (type==POSITION_TYPE_SELL && current<=tp);

        if(tp_hit)
        {
            if(!trade.PositionClose(ticket))
                Print("Errore chiusura TP ticket ",ticket," Ret:",trade.ResultRetcode());
            else
                Print("Posizione chiusa automaticamente al TP, ticket ",ticket);
        }
    }
}
//+------------------------------------------------------------------+
//| Gestione eventi popup                                            |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
{
    if(id==CHARTEVENT_OBJECT_CLICK)
    {
        if(sparam=="BUY_BTN") OpenTrade(true);
        if(sparam=="SELL_BTN") OpenTrade(false);

        if(sparam=="APPLY_BTN")
        {
            SL_Price  = StringToDouble(ObjectGetString(0,"SL_EDIT",OBJPROP_TEXT));
            TP1_Price = StringToDouble(ObjectGetString(0,"TP1_EDIT",OBJPROP_TEXT));
            TP2_Price = StringToDouble(ObjectGetString(0,"TP2_EDIT",OBJPROP_TEXT));
            TP3_Price = StringToDouble(ObjectGetString(0,"TP3_EDIT",OBJPROP_TEXT));

            ApplySLTP();
            ClosePopup();
        }
    }
}
//+------------------------------------------------------------------+
//| Creazione popup minimal                                          |
//+------------------------------------------------------------------+
void CreatePopup()
{
    if(popup_open) return;

    int cx=(int)(ChartGetInteger(0,CHART_WIDTH_IN_PIXELS)/2-150);
    int cy=(int)(ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS)/2-120);

    ObjectCreate(0,"POP_BG",OBJ_RECTANGLE_LABEL,0,0,0);
    ObjectSetInteger(0,"POP_BG",OBJPROP_XDISTANCE,cx);
    ObjectSetInteger(0,"POP_BG",OBJPROP_YDISTANCE,cy);
    ObjectSetInteger(0,"POP_BG",OBJPROP_XSIZE,300);
    ObjectSetInteger(0,"POP_BG",OBJPROP_YSIZE,240);
    ObjectSetInteger(0,"POP_BG",OBJPROP_BGCOLOR,clrBlack);

    CreateEdit("SL_EDIT",cx+100,cy+30);
    CreateEdit("TP1_EDIT",cx+100,cy+70);
    CreateEdit("TP2_EDIT",cx+100,cy+110);
    CreateEdit("TP3_EDIT",cx+100,cy+150);

    CreateMainButton("APPLY_BTN",cx+90,cy+190,"APPLY",clrDodgerBlue);

    popup_open=true;
}
//+------------------------------------------------------------------+
void CreateEdit(string name,int x,int y)
{
    ObjectCreate(0,name,OBJ_EDIT,0,0,0);
    ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);
    ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y);
    ObjectSetInteger(0,name,OBJPROP_XSIZE,120);
    ObjectSetInteger(0,name,OBJPROP_YSIZE,22);
    ObjectSetInteger(0,name,OBJPROP_BGCOLOR,clrWhite);
}
//+------------------------------------------------------------------+
void ClosePopup()
{
    string objs[]={"POP_BG","SL_EDIT","TP1_EDIT","TP2_EDIT","TP3_EDIT","APPLY_BTN"};
    for(int i=0;i<ArraySize(objs);i++)
        ObjectDelete(0,objs[i]);

    popup_open=false;
}
 
Hi guys i think i fixed it