stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Code to calculate TP/SL based on lot size?
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=4991
Page 1 of 1
Author:  aclyeh [ Fri Nov 18, 2016 2:02 am ]
Post subject:  Code to calculate TP/SL based on lot size?

Hi all,

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
Author:  afeudale [ Sat Dec 03, 2016 6:30 pm ]
Post subject:  Code to calculate TP/SL based on lot size?

What the hell...I just typed out my response and then went to preview and it deleted it...

Oh well, this is my first time posting my code, so it's a bit messy, but hopefully you can use the relevant parts.

My code calculates lot size based on SL, account free margin, open lots, etc.

It's a grid system, so L1UP, etc are grid levels. TopLevel and BottomLevel are the SL values being used.

Code: Select all

MiddlePrice=NormalizeDouble((Ask+Bid)/2,Digits);

PercentLoss = AccountBalance()*Percentage*0.01;
MarginLots=NormalizeDouble(AccountFreeMargin()/MarketInfo(Symbol(),MODE_MARGINREQUIRED),2);
MaxLots=NormalizeDouble(AccountBalance()/MarketInfo(Symbol(),MODE_MARGINREQUIRED),2);
OpenLots=NormalizeDouble(OpenValue/MarketInfo(Symbol(),MODE_MARGINREQUIRED),2);

L1UP=MiddlePrice+NormalizeDouble(((TopLevelMid-MiddlePrice)/5*1),Digits);
L2UP=MiddlePrice+NormalizeDouble(((TopLevelMid-MiddlePrice)/5*2),Digits);
L3UP=MiddlePrice+NormalizeDouble(((TopLevelMid-MiddlePrice)/5*3),Digits);
L4UP=MiddlePrice+NormalizeDouble(((TopLevelMid-MiddlePrice)/5*4),Digits);
 
L1DOWN=MiddlePrice-NormalizeDouble(((MiddlePrice-BottomLevelMid)/5*1),Digits);
L2DOWN=MiddlePrice-NormalizeDouble(((MiddlePrice-BottomLevelMid)/5*2),Digits);
L3DOWN=MiddlePrice-NormalizeDouble(((MiddlePrice-BottomLevelMid)/5*3),Digits);
L4DOWN=MiddlePrice-NormalizeDouble(((MiddlePrice-BottomLevelMid)/5*4),Digits);

LotSize=NormalizeDouble(MathMin(MathMin(PercentLoss/(((4*(L4UP-L1DOWN))+(8*Spread))*TickValue/Point),MarginLots/(6*NumberofGrids)),(MaxLots-OpenLots)/6),2);
All times are UTC Page 1 of 1