ow to get pofit of variable number of closed trades

 

Hi @ all,



i spent a lot of time in programming an EA, which is lukily working very well most of time.

But during trying to make it better i found the following problem for e.g.:



The EA has 3 Open Buy Orders with MagicNumber 1

then it is opening an position in opposite direction with MN 2

Next it will open a buy order again (MN 1) and close immedeatly after this the order with MN 2

then it is opening an position in opposite direction with MN 2

Next it will open a buy order again (MN 1) and close immedeatly after this the order with MN 2


So in this Moment i have 5 opend Buy positions with MN 1 and during this opening those positions i closed 2 Orders with MN 2.


Calculating the profit of the two last closed Orders is no problem, for sure!



But how can i calculate the profit of the closed orders MN 2 if i dont know how many of them where closed in one cycle MN 1?



THANKS IN ADVANCE

 

"But how can i calculate the profit of the closed orders MN 2 if i dont know how many of them where closed in one cycle MN 1?"

You can access closed orders using OrdersHistoryTotal() and OrderSelect() with MODE_HISTORY.

I don't really understand your question.

 
phy:

"But how can i calculate the profit of the closed orders MN 2 if i dont know how many of them where closed in one cycle MN 1?"

You can access closed orders using OrdersHistoryTotal() and OrderSelect() with MODE_HISTORY.

I don't really understand your question.

Thx phy for your reply.



I´m sure that my bad english caused, that u didn´t understand my question.

My German is better cause i life there, but i think that this won´t help.

So i try it with other words:

Well what i mean is that the EA is starting trading in on direction. in my example first is long.

But if price now is moving down it will pick up the next chance to make another long position.

in my first example this happend twice, an so i have now three open long positions.


Well if now price is moving up i have no problem an save my profits by trailing a stop or take profit.


But in my example above the price moved again down before i could take profit and so i sell the same number of

lots that i have bougt in all the open positions together. if price is now moving up or down it doesnt matter because i have the

same bougt and sold lots.


in example above price is falling again, the ea generates a new enter for long and so i will open my fourth long position and now

close the sell order that i opend before. So now i have made profit with my sell order. and i´m still on negative profit with my buy orders.

if price is again moving against me i will also again open a sell order with the exact lots like the buy orders together have.

Now Price again down --> next buy generated, and the last sell order closed with profit.


So if price is now moving up, i want to close them, when the profit of buys plus the profit of the two closed sell orders has reached a fixed amount.

To calculate the profit of fixed to last trades is easy i know.


But i don´t know how often such sell orders are opend and closed until i close all of the buy orders.

Maybe there are two, or four or none or what ever. And i want to know how many profit was made with sell orders since i started

the first long position.



I hope this is wrote in a way that can tell u what i mean :-) If not pls give me an english course haha! :-)



Tanks in advance

 
double MN1_Profits;
double MN2_Profits;
int MN1_Start_Time;
// find earliest open time and profits for currently open BUY
for(i = OrdersTotal()-1; i >= 0; i--){
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    if(OrderSymbol() == Symbol()){
        if(OrderType() == OP_BUY){
            MN1_Profits += OrderProfit();
            if(OrderOpenTime() < MN1_Start_Time || MN1_Start_Time == 0){
                MN1_Start_Time = OrderOpenTime();                
            }
        }
    }
}

// find profits for SELL orders opened after the first BUY
for(i = OrdersTotal()-1; i >= 0; i--){
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    if(OrderSymbol() == Symbol()){
        if(OrderType() == OP_SELL){
            if(OrderOpenTime() >= MN1_Start_Time){
                MN2_Profits += OrderProfit();
            }
        }
    }
}
// find profits for closed SELL orders opened after the first BUY
for(i = OrdersHistoryTotal()-1; i >= 0; i--){
    OrderSelect(i, SELECT_BY_TICKET, MODE_HISTORY);
    if(OrderSymbol() == Symbol()){
        if(OrderType() == OP_SELL){
            if(OrderOpenTime() >= MN1_Start_Time){
                MN2_Profits += OrderProfit();
            }
        }
    }
}

double position_Profits = MN1_Profits+MN2_Profits;
 
phy:

Phy thank u VERY MUCH the code works exactly the way that i wanted it to work --> Ur one of the most helpful users here thanks