Help with Looping and OrderProfit()

 

Can anyone point me to information that will help me..

I'm trying to loop thru all the open orders and get the Profit for all the Buy Orders only.... and the profit for all the Sell orders only. I want to use the info to close Buys and or Close Sells at some point....I also want to show them with "Comment"...but I'm having trouble looping and adding all the sells or all the buys using OrderProfit()

Should be easy but....I must be missing something obvious.

 
n8937g:

Can anyone point me to information that will help me..

I'm trying to loop thru all the open orders and get the Profit for all the Buy Orders only.... and the profit for all the Sell orders only. I want to use the info to close Buys and or Close Sells at some point....I also want to show them with "Comment"...but I'm having trouble looping and adding all the sells or all the buys using OrderProfit()

Should be easy but....I must be missing something obvious.

Show your loop and we'll fix.


CB

 

Hi

try something like this

double Calcul_Profit_Achat()
{
double Total_Profit_Achat=0;
int total=OrdersTotal();

for(int pos=0;pos<total;pos++)
{

RefreshRates();

if(OrderSelect(pos,SELECT_BY_POS)==false) continue;

if(OrderType()==OP_BUY && OrderSymbol()==Symbol()) // type Achat
{
Total_Profit_Achat = Total_Profit_Achat+OrderProfit();
continue;
}

} // for(pos=0;pos<total;pos++)


return(NormalizeDouble(Total_Profit_Achat,2));

} // double Calcul_Profit()

 

Here's what I have...how can it be improved? When I have no, either Buy or Sell Orders..., BuyProfit or SellProfit still remain at their last value, not zero. They don't go to zero...even though there is no profit(or loss)... When I try to allow for the condition no sell or buy order present ...zero profit condition the whole thing goes up in smoke....

double xx= 0.00;
double yy=0.00;
int x=0;
int y=0;
double BuyProfit=0.00;
double SellProfit=0.00;

//=============================

if (1==1)
{
for(x=OrdersTotal()-1;x>=0;x--)
{
OrderSelect(x,SELECT_BY_POS);
{
if(OrderType()==OP_BUY)
{
xx=OrderProfit();
BuyProfit= xx+OrderProfit();
xx=0;
}
if(OrderType()==OP_SELL)
{
yy=OrderProfit();
SellProfit= yy+OrderProfit();
yy=0;
}
}
}
}

 
xx=OrderProfit();
BuyProfit= xx+OrderProfit();
xx=0;
// that code makes no sense, it is equal to
BuyProfit= OrderProfit()*2;
// it should be something like this
BuyProfit= BuyProfit+OrderProfit();

dont know what you want to do...



if (1==1) makes no sense too..

if(true) is the same

 
n8937g:

i use something like

if (true)

{

}

regularly for debugging, but i have had never the idea to use

if ((50*20*30/3)==(50*20*30/3)) {}

for this purpose...

;-)

 

You can ignore the if(1==1)..LOL I just use that when I'm trouble shooting something....and want to not have any other qualifiying conditions for a piece of code to run...The problem is not there....

Re.

BuyProfit=BuyProfit+OrderProfit();

It looks right but when I run it that way it just keeps adding and adding.... when I use the xx or yy it just acts like a buffer that gets reset to zero every loop cycle.

I'm trying to get the total profit(or loss) from only all the open Buy orders...and the total profit (or loss) from only all the open sell orders.

ie

Comment ("\nBuyProfit ",BuyProfit,

"\nSellProfit ",SellProfit)

What/how should I reset BuyProfit or SellProfit to zero if there are no open either buy or sell orders...as BuyProfit and SellProfit will retain their last value even if there are no orders . I'd like to do this without another loop...if possible. Thanks

 

define BuyProfit global outside start and init (after the extern etc)

in start, write BuyProfit=0; immidiatly after the start funtion starts then start your loop

this should work

 
meikel wrote >>

define BuyProfit global outside start and init (after the extern etc)

in start, write BuyProfit=0; immidiatly after the start funtion starts then start your loop

this should work

WOW That worked GREAT...I was looking for a complicated solution...sometimes somethings are so obvious, but you can't see them....LOL

Thank you soooo much!!

 
n8937g:

WOW That worked GREAT...I was looking for a complicated solution...sometimes somethings are so obvious, but you can't see them....LOL

Thank you soooo much!!

thanks for the thanks - nice to read this from time to time LOL

i think the issue is, that if you define a variable in a function and in the same line give this variable a value, the compiler runs this line only once, and therefore the value is only given to the variable at the first run.

nice that it is working now.

have a nice weekend...

Reason: