I would like to trade a fixed percent of my Equity on each order, say 1%. So, according to the Stop Loss price (distance), I would like to get the Lot Size (shares/units) to trade.
I came up with this formula but I don't think it's working well. Can you please take a look at it or let me know if there is a better way to do it?
Thank you!
Code: Select all
//Example of how to use it:
// UnitsToTrade = SharesByPercentRisk(1, 200, "Long");
// If ... buy UnitsToTrade shares next bar at market
Inputs:
PercentageRisk(NumericSimple), // The percentage I want to trade ie: 1 or 2
SLdistance(NumericSimple), // Distance from Market price
Direction(StringSimple); // Long or Short
Variables:
AccountEquity(0), // will hold the account equity at the moment
MarketPrice(0), // MarketPrice at the moment: Bid price for shorts and Ask price for Longs
SLPrice(0), // Price where the SL will be set according to the distance input
Risk(0), // Receives the integer and turns it into a percentage
NumberOfShares(0); // final result of formula
Risk = PercentageRisk/100;
AccountEquity= GetRTAccountEquity(GetAccount(1));
if Direction= "Long" then
begin
MarketPrice = CurrentAsk;
SLPrice = MarketPrice - SLdistance * TickSize;
NumberOfShares = AccountEquity* Risk / ((MarketPrice - SLPrice)*TickSize);
End
Else if Direction= "Short" then
Begin
MarketPrice = CurrentBid;
SLPrice = MarketPrice + SLdistance * TickSize;
NumberOfShares = AccountEquity* Risk / ((SLPrice - MarketPrice)*TickSize);
End;
SharesByPercentRisk = Round(NumberOfShares,0);