Need help personal project - calculating new variables based on $ inputs

Hi all,

I’ve been working on this contract but am stuck and would appreciate any help at all!

I’m trying to build a contract that will calculate the Risk/Reward of a trade setup.

What it does is take 3 inputs from a user:

  1. Entry Price, e.g. $1000
  2. Stop Loss, e.g. $900 for longs, $1100 for shorts
  3. Take Profit, e.g. $1200 for longs, $800 for shorts

Then calculates the:

  1. Profit Amount: e.g. $1200 - $1000 = $200 (for the long example)
  2. Loss Amount: e.g. $1000 - $900 = $100 (for the long example)
  3. “R” i.e. Reward:Risk ratio: e.g. $200 / $100 = 2.

I tried putting this together into a basic skeleton as per this first block of code below. However was not able to get it working properly.

Can anyone please give me some pointers? Greatly appreciated!!

// RiskCalculator.sol is a tool for traders to calculate the Risk/Reward of a trade setup.

/* for LONG: stopLoss < entry < takeProfit
 * 
 * Example LONG:
 * entry, stopLoss, takeProfit : 1000, 900, 1200
 * profitAmount = 1200 - 1000 = 200
 * lossAmount = 1000 - 900 = 100
 * rr = 200 / 100 = 2
 */

/* for SHORT: takeProfit < entry < stopLoss
 * 
 * Example SHORT:
 * entry, stopLoss, takeProfit : 1000, 1250, 500
 * profitAmount = 1000 - 500 = 500
 * lossAmount = 1250 - 1000 = 250
 * rr = 500 / 250 = 2
 */

pragma solidity 0.8.15;

contract RiskCalculator {

    // uint decimals;

    uint profitAmount;
    uint lossAmount;
    uint rr;

    function calculateProfitLoss(uint size, uint entry, uint stopLoss, uint takeProfit) public returns(uint, uint, uint) {
        
        if (takeProfit > entry) { // if long
            profitAmount = ( size * ((takeProfit / entry) - 1) ); // doesn't work -> can't divide?
            lossAmount = ( size * (1 - (stopLoss / entry)) ); // doesn't work
            rr = profitAmount / lossAmount;
            return(profitAmount, lossAmount, rr);
        } 
        else if (takeProfit < entry) { // if short
            profitAmount = ( size * (1 - (takeProfit / entry)) ); // doesn't work -> can't divide?
            lossAmount = ( size * ((stopLoss / entry) - 1) ); // doesn't work
            rr = profitAmount / lossAmount;
            return(profitAmount, lossAmount, rr);
        } 
        else { 
            return (0,0,0); 
        }        
    }

    // function calculateLong(uint size, uint entry, uint stopLoss, uint takeProfit) public returns(uint, uint, uint) {
        
    //     require(takeProfit > entry, "This is not a long");

    //     profitAmount = ( size * ((takeProfit / entry) - 1) );
    //     lossAmount = ( size * (1 - (stopLoss / entry)) ); 
    //     rr = profitAmount / lossAmount;

    //     return(profitAmount, lossAmount, rr);
    // }

} 

So I went backwards and tried this very simple contract, that works as intended.

However, if I try to add 3 or 4 variables in the same way, and use division in my calculations
e.g. int newVar = ( var1 * ( var2 / var3 - 1 ) ), things start to break. I’m guessing it could be something to do with not being able to show decimals properly?

Kinda stuck now. Help!

pragma solidity 0.8.15;

contract RiskCalculator {

    int entry;
    int stopLoss;

    function setLong(int _entry) public {
        entry = _entry;
    }

    function getLong() public view returns(int) {
        return entry;
    }

    function setSL(int _stopLoss) public {
        stopLoss = _stopLoss;
    }

    function getSL() public view returns(int) {
        return stopLoss;
    }

    function calculateEntryMinusStopLoss() public returns(int) {
        int stop = entry - stopLoss;
        return stop; // works fine
    }

}