Need help with coding using history values

 

Hello everyone,


I am working on a EA and I am stuck with one problem. The EA is using stoploss and take profit that is fixed once the EA starts trading but if I start with loosing trade l I have loss in history ( let's say 10 pips ),what I would like to do is : after I have a loss I would like to enlarge my take profit order for the same amount I just lost

Example:
StopLoss - 10 pips
Take profit- 10 pips


If stop loss is hit I want take profit to be 20 pips.


Anybody has any suggestions about this part of code?


thank you

 
atomi50:

Hello everyone,


I am working on a EA and I am stuck with one problem. The EA is using stoploss and take profit that is fixed once the EA starts trading but if I start with loosing trade l I have loss in history ( let's say 10 pips ),what I would like to do is : after I have a loss I would like to enlarge my take profit order for the same amount I just lost

Example:
StopLoss - 10 pips
Take profit- 10 pips


If stop loss is hit I want take profit to be 20 pips.


Anybody has any suggestions about this part of code?


thank you


I've written the following function for you. It will allow you to perform actions of your choice if one of your TODAY'S orders has been stopped by the broker due to SL or TP.


int fnHistoryCheck()

 {

  iOrders = OrdersHistoryTotal()-1;

  for (i = iOrders; i>=0; i--)

   {

    OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);

    if (OrderSymbol() == Symbol())

     {  

      if ((TimeDayOfYear(OrderOpenTime()) == DayOfYear()) && (TimeYear(OrderOpenTime()) == Year()))

       {

        if (OrderProfit() >= 0)

          sResult = "PROFIT";

        else

          sResult = "LOSS";

        ---Do stuff here dependent upon sResult---

       }

     }

   }

  return(0);

 }  

 
cloudbreaker:

I've written the following function for you. It will allow you to perform actions of your choice if one of your TODAY'S orders has been stopped by the broker due to SL or TP.


int fnHistoryCheck()

{

iOrders = OrdersHistoryTotal()-1;

for (i = iOrders; i>=0; i--)

{

OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);

if (OrderSymbol() == Symbol())

{

if ((TimeDayOfYear(OrderOpenTime()) == DayOfYear()) && (TimeYear(OrderOpenTime()) == Year()))

{

if (OrderProfit() >= 0)

sResult = "PROFIT";

else

sResult = "LOSS";

---Do stuff here dependent upon sResult---

}

}

}

return(0);

}

hey cloudbraker thanks a lot for this kind response,


I was wondering once I get the sResult = LOSS I then have a number of negative trades right?

How would I then sum them up and calculate how much is the loss for the day at that moment and add it to takeprofit ?


sResult= LOSS - number of negative trades

sum the trades--->add it to takeprofit---->set new takeprofit to chart once the order opens


I've read thru the book searching but couldnt find anything :(


thanks man

 
atomi50:

hey cloudbraker thanks a lot for this kind response,


I was wondering once I get the sResult = LOSS I then have a number of negative trades right?

How would I then sum them up and calculate how much is the loss for the day at that moment and add it to takeprofit ?


sResult= LOSS - number of negative trades

sum the trades--->add it to takeprofit---->set new takeprofit to chart once the order opens


I've read thru the book searching but couldnt find anything :(


thanks man

This is a loop. So on each occasion where a profit/loss is identified as you pass through the loop you'd need to accumulate the appropriate total using simple mathematics.

I'm not going to code this one for you as a) its a prime example of something simple enough to be useful for the learning process (and your confidence) and more importantly b) because you spelt my name incorrectly.


CB

 
cloudbreaker:

This is a loop. So on each occasion where a profit/loss is identified as you pass through the loop you'd need to accumulate the appropriate total using simple mathematics.

I'm not going to code this one for you as a) its a prime example of something simple enough to be useful for the learning process (and your confidence) and more importantly b) because you spelt my name incorrectly.


CB


my apologies cloudbreaker :)


I will do my best to solve this problem,I'm sure it will be useful .

tnx anwy
 
I used the code and I have the EA counting every profit/loss in my history but what to do if I want to count only losses of one currency? I tried changing the OrderSymbol() == "EURUSD" ( example) like I saw in some other thread but nothing happened of course. I can't figure it out how to count those losses then sum them up,any ideas?
 
atomi50:
I used the code and I have the EA counting every profit/loss in my history but what to do if I want to count only losses of one currency? I tried changing the OrderSymbol() == "EURUSD" ( example) like I saw in some other thread but nothing happened of course. I can't figure it out how to count those losses then sum them up,any ideas?

- OrderSymbol() returns the symbol of the order you have selected with OrderSelect().

- Symbol() returns the symbol of the chart to which the EA is attached.

In my example, everything within the code block directly following the "if (OrderSymbol() == Symbol())" statement will be executed only if the order you've selected is the same currency pair as the chart you attached the EA to. So this is the place to do your counting.


Try littering your code with Print() statements to aid in your debugging.


CB

 
cloudbreaker:

- OrderSymbol() returns the symbol of the order you have selected with OrderSelect().

- Symbol() returns the symbol of the chart to which the EA is attached.

In my example, everything within the code block directly following the "if (OrderSymbol() == Symbol())" statement will be executed only if the order you've selected is the same currency pair as the chart you attached the EA to. So this is the place to do your counting.


Try littering your code with Print() statements to aid in your debugging.


CB

int i2,hstTotal=OrderHistoryTotal();

for(i2=0;i2<hstTotal;i2++)
{

if(OrderSelect(i2,SELECT_BY_POS,MODE_HISTORY)){
if (OrderSymbol() == Symbol()){
if ((TimeDayOfYear(OrderOpenTime()) == DayOfYear()) && (TimeYear(OrderOpenTime()) == Year())){
if (OrderProfit() <= 0)
{
Print(" Loss ");
}
else{
Print (" Profit " );
}
}
}
}
}


this is the code I am using in my EA,with Print() orders,and I receive data from it . I get Profit and Loss statements for currency's in history and it all works just fine.But I am using the same EA on 4 different pairs and I don't know how to isolate one of them ( I don't understand your reply about that sry). how would I write code for EA so that he is printing just ONE pair ( and write couple of blocks for each currency/ adjust 4 diff EA's for each currency) so that if I have 3 closed orders for GBP USD (let's say 2 losses and 1 profit) the EA should print LOSS,LOSS,PROFIT in a loop !


I don't know if I explained good enough ?

 
atomi50:

int i2,hstTotal=OrderHistoryTotal();

for(i2=0;i2<hstTotal;i2++)
{

if(OrderSelect(i2,SELECT_BY_POS,MODE_HISTORY)){
if (OrderSymbol() == Symbol()){
if ((TimeDayOfYear(OrderOpenTime()) == DayOfYear()) && (TimeYear(OrderOpenTime()) == Year())){
if (OrderProfit() <= 0)
{
Print(" Loss ");
}
else{
Print (" Profit " );
}
}
}
}
}


this is the code I am using in my EA,with Print() orders,and I receive data from it . I get Profit and Loss statements for currency's in history and it all works just fine.But I am using the same EA on 4 different pairs and I don't know how to isolate one of them ( I don't understand your reply about that sry). how would I write code for EA so that he is printing just ONE pair ( and write couple of blocks for each currency/ adjust 4 diff EA's for each currency) so that if I have 3 closed orders for GBP USD (let's say 2 losses and 1 profit) the EA should print LOSS,LOSS,PROFIT in a loop !


I don't know if I explained good enough ?



Can you just humour me and explain your deployment model precisely, so we are clear and I can give you the right advice:

A. A single EX4 instance attached to a chart attempting to perform for 4 separate symbols, one of which may or may not be the symbol which is native to the chart

B. Four instances of the same EX4 code (symbol-agnostic), each one being attached to a separate chart/symbol

C. Four instances of mostly the same EX4 code (but each tailored for a particular symbol), each one being attached to a separate chart/symbol

 
cloudbreaker:

Can you just humour me and explain your deployment model precisely, so we are clear and I can give you the right advice:

A. A single EX4 instance attached to a chart attempting to perform for 4 separate symbols, one of which may or may not be the symbol which is native to the chart

B. Four instances of the same EX4 code (symbol-agnostic), each one being attached to a separate chart/symbol

C. Four instances of mostly the same EX4 code (but each tailored for a particular symbol), each one being attached to a separate chart/symbol

if I understood correctly you gave me 3 options ?! I would say B .( if you asked me to choose the options)


I have 4 charts opened and on each chart the same EA is attached.


Like I said I have an EA ( 1 version,1 EX4 ) that successfully places trades when attached to any chart.In my case 4 charts are opened.Everything works great except I can't find a way to make it do the following ( I will try and explain in detail )

When EA starts trading StopLoss is set to 2o pips lets say and take profit to 20 pips,if the first trade activates TP the EA should stop trading for a day. If SL is activated I want EA to look in history,take the ammount I just lost and adds it to take profit order.


Problem is at this moment EA checks history and Prints out losses for all the pairs traded and I don' t know how to isolate the wanted pair and count the losses that happend


When the TP is acctivated the EA is done for the day,and start trading at midnight the next day.


Did I explained any better this time or you need any more info?


basiclly what I want is

-EA losses
-checks history
-adds loss to take profit
-if takprofit is not reached but another loss accurs
-EA checks history again and adds the new loss to TP
-EA hits TP
-EA StopsTrading

 
atomi50:

if I understood correctly you gave me 3 options ?! I would say B .( if you asked me to choose the options)


I have 4 charts opened and on each chart the same EA is attached.


Like I said I have an EA ( 1 version,1 EX4 ) that successfully places trades when attached to any chart.In my case 4 charts are opened.Everything works great except I can't find a way to make it do the following ( I will try and explain in detail )

When EA starts trading StopLoss is set to 2o pips lets say and take profit to 20 pips,if the first trade activates TP the EA should stop trading for a day. If SL is activated I want EA to look in history,take the ammount I just lost and adds it to take profit order.


Problem is at this moment EA checks history and Prints out losses for all the pairs traded and I don' t know how to isolate the wanted pair and count the losses that happend


When the TP is acctivated the EA is done for the day,and start trading at midnight the next day.


Did I explained any better this time or you need any more info?


basiclly what I want is

-EA losses
-checks history
-adds loss to take profit
-if takprofit is not reached but another loss accurs
-EA checks history again and adds the new loss to TP
-EA hits TP
-EA StopsTrading





Was just checking that I understood. In that case, the code I gave you is what you need. To isolate the orders in history which relate to the symbol of the current chart just check OrderSymbol() against Symbol().

Reason: