Lot size based on risk and price difference between entry and stop

 

Hi,

This is something I'm really stuggling with. I know my entry price, I know where my stop needs to be base on price but I just can't seem to get the lot size to calculate correctly.


Are there any examples or tutorials that will help me get my head round what is needed?


Thanks

 
Gary Burth:

Hi,

This is something I'm really stuggling with. I know my entry price, I know where my stop needs to be base on price but I just can't seem to get the lot size to calculate correctly.


Are there any examples or tutorials that will help me get my head round what is needed?


Thanks

You should show your code if you need coding help. There are numerous threads on this forum about lot size calculation, did you search ?
 
Alain Verleyen #:
You should show your code if you need coding help. There are numerous threads on this forum about lot size calculation, did you search ?
Hi, yes sorry I'll attached the code below. I've made a little progress since posting earlier but I'm still struggling to adjust for the difference in USD & JPY pairs (3 or 5 dp)

The code is based on a tutorial I watched on YT. 

    double riskAmount = 500;
    double riskPoints = 230;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    Test(Symbol());

    return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+

void Test(string symbol)
{

    PrintFormat("Base currency is %s", AccountInfoString(ACCOUNT_CURRENCY));
    PrintFormat("Testing for symbol %s", symbol);

    double pointValue = PointValue(symbol);

    PrintFormat("ValuePerPoint for %s is %f", symbol, pointValue);

    double riskLots = riskAmount / (pointValue * riskPoints);
    PrintFormat("Risk lots for %s value %f and stop loss at %f points is %f",
                symbol, riskAmount, riskPoints, riskLots);
}

double PointValue(string symbol)
{

    double tickSize = MarketInfo(symbol, MODE_TICKSIZE);
    double tickValue = MarketInfo(symbol, MODE_TICKVALUE);
    double point = MarketInfo(symbol, MODE_POINT);
    double ticksPerPoint = tickSize / point;
    double pointValue = tickValue / ticksPerPoint;

    PrintFormat("tickSize=%f, tickValue=%f, point=%f, ticksPerPoint=%f, pointValue=%f",
                tickSize, tickValue, point, ticksPerPoint, pointValue);

    double entryPrice = 184.211;
    double stopLoss = 183.981;
    double takeProfit = 184.441;

    double pointsBetweenPrices = (entryPrice - stopLoss) / point;

    Print("Points between prices: ", pointsBetweenPrices);

    return (pointValue);
}