I am trying to write a piece of code but struggling for days on a seemingly simple calculation. The EA does the following:
- Takes as user input the Profit_Target and Drawdown_Target, which are equity targets based in the same currency as the account. Default is USD, and $10,200 / $9,800, which means a target profit of $200 and loss of $200, assuming a starting equity of $10,000 (typical default for Empty4's strategy tester)
- If profit or drawdown target is reached, close all trades
- Calculates the price targets for TP and SL based on the number of lot positions and displays it in the comments
The goal is so that the user knows the new TP/SL price after adding or closing traded lots. The code assumes there is only one pair traded, and is trading long.
------------------
The EA automatically adds 0.01 lots long every 2 hours (up to the predefined limit) to allow testing, but the trading part is just used for testing the main code here, which is to calculate TP/SL is as follows:
Code: Select all
void pipCalc()
{
PipValue=(((MarketInfo(Symbol(),MODE_TICKVALUE)*MyPoint)/MarketInfo(Symbol(),MODE_TICKSIZE))*TotalLotsOnBook());
PointValue=PipValue/10;
if(PointValue==0) //no trades in account, avoid 0 division
{
TargetPrice = 0;
DrawdownPrice = 0;
}
else
{
TargetPrice = Ask + (Profit_Target-AccountEquity())/PointValue/1000;
DrawdownPrice = Ask + (Drawdown_Target-AccountEquity())/PointValue/1000;
}
}
Currently the TP/SL calculations seems a bit off, but I can't quite figure out why.
Would be glad to get some of your help, or if someone already has a similar method in their EA shell, please share or advise! Many thanks