PHP code example of michalbrauner / forex-calculator

1. Go to this page and download the library: Download michalbrauner/forex-calculator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

michalbrauner / forex-calculator example snippets


$currencyConverter = new \ForexCalculator\Services\CurrencyConverter(
  new \ForexCalculator\DataProviders\YahooDataProvider(new \GuzzleHttp\Client())
);

# Convert 100 eur to usd

$convertedValue = $currencyConverter->convertToCurrency('eur', 'usd', 100);

# Create calculator factory

$tradeAttributesCalculatorFactory = new \ForexCalculator\Services\TradeAttributesByTradeSizeCalculatorFactory(
    new \ForexCalculator\DataProviders\YahooDataProvider(new \GuzzleHttp\Client()),
    new \ForexCalculator\PrecisionProviders\MoneyPrecisionProvider(),
    new \ForexCalculator\PrecisionProviders\RiskRewardRatioPrecisionProvider()
);

# Calculator settings

$symbol = 'eurusd';
$outputCurrency = 'usd';
$extendedPoint = true;

$tradeAttributesCalculator = $tradeAttributesCalculatorFactory->create('eurusd', 'usd', $extendedPoint);

# Factory to create prices to trade

$priceNumberFactory = new \ForexCalculator\DataObjects\FloatNumberFactory(
    new \ForexCalculator\PrecisionProviders\PricePrecisionProvider($symbol, $extendedPoint)
);

$trade = new \ForexCalculator\DataObjects\Trade(
    $priceNumberFactory->create('1.03953'),
    $priceNumberFactory->create('1.03936'),
    $priceNumberFactory->create('1.04016')
);

# Loss and profit for trade and size 20000 units

$loss = $tradeAttributesCalculator->getLoss($trade, 20000);
$profit = $tradeAttributesCalculator->getProfit($trade, 20000);

$riskRewardRatio = $tradeAttributesCalculator->getRiskRewardRatio($trade);

...

$numberOfUnitsCalculator = new \ForexCalculator\Services\NumberOfUnitsByMaximalLossCalculator(
    $symbol,
    $outputCurrency,
    $extendedPoint,
    $tradeAttributesCalculatorFactory
);

$moneyNumberFactory = new \ForexCalculator\DataObjects\FloatNumberFactory(
    new \ForexCalculator\PrecisionProviders\MoneyPrecisionProvider()
);

# Number of units to trade to risk 180 usd per trade

$numberOfUnits = $numberOfUnitsCalculator->getNumberOfUnits($trade, $moneyNumberFactory->create('180'));