Luandre Ezra:
Hi,
I tried to calculate profit from long position and short position. When I print it I notice there's difference in profit between print statement and trade tab on strategy tester.
Here's the screenshot on trade tab,
and here's the print statement.
From the result above the overall profit is -2.57 and the profit from short position is -0.42. But on the print statement, while the profit from short position is correct, the result from the long position print statement is the same with overall profit.
Here's the code that I used to calculate profit for long and short position.
Anyone knows where the problem is?
What is the result when you change this:
if(position.PositionType() == POSITION_TYPE_SELL)
to this:
else if(position.PositionType() == POSITION_TYPE_SELL)
And could you print out each value on each iteration, maybe.
printf("profit: %f; swap: %f; commission: %f;", profit, swap, commission);
You are accumulating the wrong values!
This is the corrected code:
void ProfitInfo (int SymbolLoop, ulong MagicNumber, double& PositionProfitBuy, double& PositionProfitSell) { string CurrentSymbol = SymbolArray[SymbolLoop]; double profit = 0, swap = 0, commission = 0; //--- Reset the returned profit values on new calls PositionProfitBuy = PositionProfitSell = 0; for(int i = PositionsTotal() - 1; i >= 0; i--) { if(position.SelectByIndex(i)) { if(position.Symbol() == CurrentSymbol && position.Magic() == MagicNumber) { if(position.PositionType() == POSITION_TYPE_BUY) { profit = position.Profit(); swap = position.Swap(); commission = position.Commission(); PositionProfitBuy += profit + swap + commission; } else if(position.PositionType() == POSITION_TYPE_SELL) { profit = position.Profit(); swap = position.Swap(); commission = position.Commission(); PositionProfitSell += profit + swap + commission; } } } } Print("profit buy: ", PositionProfitBuy, ", profit sell: ", PositionProfitSell, ", Total profit: ", PositionProfitBuy + PositionProfitSell); }
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi,
I tried to calculate profit from long position and short position. When I print it I notice there's difference in profit between print statement and trade tab on strategy tester.
Here's the screenshot on trade tab,
and here's the print statement.
From the result above the overall profit is -2.57 and the profit from short position is -0.42. But on the print statement, while the profit from short position is correct, the result from the long position print statement is the same with overall profit.
Here's the code that I used to calculate profit for long and short position.
Anyone knows where the problem is?