Code to calculate TP/SL based on lot size?

Post Reply
aclyeh
Posts: 7
Joined: Mon Nov 30, 2015 12:25 pm

Code to calculate TP/SL based on lot size?

Post by aclyeh »

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
You do not have the required permissions to view the files attached to this post.
afeudale
Trader
Posts: 59
Joined: Mon Aug 08, 2016 1:23 pm

Code to calculate TP/SL based on lot size?

Post by afeudale »

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);
Post Reply

Return to “Coders Hangout”