Michael Wu

The Life of a Cloud Engineer

  • Home
  • Resume
  • Blog
  • Contact

Posted on 12.07.17

Trading Calculator for risk management and reward risk ratio by PowerShell

Even though I know that risk management is the most important thing in trading, I don’t always follow the rule. Lack of discipline leads to blow up my hard working profit in only two days.

After the painful days, I commit myself to obey the 2% risk management (the maximum 2% risk per trade of my total fund). I calculate the shares I can buy based on my total fund, the entry price and the stop price by a calculator on my phone and a paper.

The slow and repeatable procedure leads to an automation idea. Therefore I decided to create a trading calculator by myself.

The Code

The PowerShell plugin of WordPress messes up the WPF <Window x:Class…> in the following code. Please Do NOT copy the code on this page. You can check and copy the full script on the GitHub.

Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms

# Main function
function calculate {
    [CmdletBinding()]
    param
    (
        [double]$total2,
        [double]$entry2,
        [int]$shares2,
        [double]$target2,
        [double]$stop2,
        [double]$risk2,
        [double]$ratio2
    )
   
    # Calculate Risk%
    if(($entry2 -ne &quot;&quot;) -and ($shares2 -ne &quot;&quot;) -and ($Stop2 -ne &quot;&quot;) -and ($risk2 -eq &quot;&quot;)){
        if ($entry2 -lt $stop2) {
            [System.Windows.Forms.MessageBox]::Show(&quot;Entry Price must be greater than the Stop Price&quot;, &quot;Invalid Values&quot;, 0, 48)
        }
        $entryCost = $entry2*$shares2
        $stoploss = $stop2*$shares2
        $riskper = ($entryCost-$stoploss)/$total2*100
        $risk.text= [math]::Round($riskper,3)
    }
    
    # Calculate Reward Risk Ratio
    elseif (($entry2 -ne &quot;&quot;) -and ($target2 -ne &quot;&quot;) -and ($stop2 -ne &quot;&quot;) -and ($ratio2 -eq &quot;&quot;)) {
        if ($entry2 -lt $stop2) {
            [System.Windows.Forms.MessageBox]::Show(&quot;Entry Price must be greater than the Stop Price&quot;, &quot;Invalid Values&quot;, 0, 48)
        }
        if ($entry2 -gt $target2) {
            [System.Windows.Forms.MessageBox]::Show(&quot;Target Price must be greater than the Entry Price&quot;, &quot;Invalid Values&quot;, 0, 48)
        }
        $riskCount = $entry2-$stop2
        $rewardCount = $target2-$entry2
        $ratioResult = $rewardCount/$riskCount
        $ratio.text = [math]::Round($ratioResult,3)
    }

    # Calculate Stop Price
    elseif (($entry2 -ne &quot;&quot;) -and ($shares2 -ne &quot;&quot;) -and ($risk2 -ne &quot;&quot;) -and ($stop2 -eq &quot;&quot;)) {
        $stopPrice = $entry2-(($total2*($risk2/100)/$shares2))
        $stop.text =  [math]::Round($stopPrice,3)
    }

    # Calculate Shares by the risk%
    elseif (($shares2 -eq &quot;&quot;) -and ($entry2 -ne &quot;&quot;) -and ($stop2 -ne &quot;&quot;) -and ($risk2 -ne &quot;&quot;) -and ($total2 -ne &quot;&quot;)) {
        if ($entry2 -lt $stop2) {
            [System.Windows.Forms.MessageBox]::Show(&quot;Entry Price must be greater than the Stop Price&quot;, &quot;Invalid Values&quot;, 0, 48)
        }
        $shares2 = ($total2*($risk2/100))/($entry2-$stop2)
        $shares.text = [math]::Round($shares2,0)
    }
    else {
        [System.Windows.Forms.MessageBox]::Show(&quot;Please Enter the required inputs.&quot;, &quot;Missing Values&quot;, 0, 48)
    }
}

# Reset the amount except total fund
function reset-text () {
    $entry.clear()
    $shares.clear()
    $target.clear()
    $stop.clear()
    $risk.clear()
    $ratio.Clear()
    $total.clear()
}


# WPF GUI Windows
$inputXAML = @&quot;
&lt;Window x:Class=&quot;WpfApp1.MainWindow&quot; xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot; xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot; xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot; xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot; xmlns:local=&quot;clr-namespace:WpfApp1&quot; mc:Ignorable=&quot;d&quot; Title=&quot;Trading Calculator&quot; Height=&quot;430.328&quot; Width=&quot;378.688&quot; ResizeMode=&quot;CanMinimize&quot;&gt;&lt;span data-mce-type=&quot;bookmark&quot; style=&quot;display: inline-block; width: 0px; overflow: hidden; line-height: 0;&quot; class=&quot;mce_SELRES_start&quot;&gt;&lt;/span&gt;
&lt;Grid UseLayoutRounding=&quot;False&quot;&gt;

&lt;Border BorderBrush=&quot;Black&quot; BorderThickness=&quot;1&quot; HorizontalAlignment=&quot;Left&quot; Height=&quot;357&quot; Margin=&quot;10,10,0,0&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;344&quot; Background=&quot;#FFD1D1D1&quot;/&gt;

&lt;Label Content=&quot;Total Fund : &quot; HorizontalAlignment=&quot;Left&quot; Height=&quot;30&quot; Margin=&quot;18,28,0,0&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;112&quot; FontSize=&quot;18&quot; FontWeight=&quot;Bold&quot;/&gt;
&lt;Label Content=&quot;Entry Price : &quot; HorizontalAlignment=&quot;Left&quot; Height=&quot;36&quot; Margin=&quot;18,111,0,0&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;112&quot; FontSize=&quot;18&quot; FontWeight=&quot;Bold&quot;/&gt;
&lt;Label Content=&quot;Stop Price : &quot; HorizontalAlignment=&quot;Left&quot; Height=&quot;40&quot; Margin=&quot;17,197,0,0&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;112&quot; FontSize=&quot;18&quot; FontWeight=&quot;Bold&quot;/&gt;
&lt;Label Content=&quot;Risk % : &quot; HorizontalAlignment=&quot;Left&quot; Height=&quot;30&quot; Margin=&quot;18,238,0,0&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;112&quot; FontSize=&quot;18&quot; FontWeight=&quot;Bold&quot;/&gt;
&lt;Label Content=&quot;Target Price : &quot; HorizontalAlignment=&quot;Left&quot; Height=&quot;37&quot; Margin=&quot;18,152,0,0&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;129&quot; FontSize=&quot;18&quot; FontWeight=&quot;Bold&quot;/&gt;
&lt;Label Content=&quot;http://mikewu.org&quot; HorizontalAlignment=&quot;Left&quot; Height=&quot;24&quot; Margin=&quot;10,367,0,0&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;101&quot; FontSize=&quot;11&quot; FontStyle=&quot;Italic&quot;/&gt;
&lt;Label Content=&quot;Reward Risk ratio : &quot; HorizontalAlignment=&quot;Left&quot; Height=&quot;30&quot; Margin=&quot;18,279,0,0&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;184&quot; FontSize=&quot;18&quot; FontWeight=&quot;Bold&quot;/&gt;
&lt;Button x:Name=&quot;run&quot; Content=&quot;Run&quot; HorizontalAlignment=&quot;Left&quot; Margin=&quot;140,325,0,0&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;92&quot; Height=&quot;31&quot; FontWeight=&quot;Bold&quot; FontSize=&quot;18&quot; Foreground=&quot;White&quot; Background=&quot;#FF0E349B&quot;/&gt;
&lt;Button x:Name=&quot;reset&quot; Content=&quot;Reset&quot; HorizontalAlignment=&quot;Left&quot; Margin=&quot;248,325,0,0&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;92&quot; Height=&quot;31&quot; FontWeight=&quot;Bold&quot; FontSize=&quot;18&quot; Foreground=&quot;White&quot; Background=&quot;#FFA03A14&quot;/&gt;
&lt;TextBox x:Name=&quot;total&quot; HorizontalAlignment=&quot;Left&quot; Height=&quot;24&quot; Margin=&quot;140,34,0,0&quot; TextWrapping=&quot;Wrap&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;180&quot; FontSize=&quot;16&quot; FontWeight=&quot;Bold&quot; TextAlignment=&quot;Center&quot;/&gt;
&lt;TextBox x:Name=&quot;entry&quot; HorizontalAlignment=&quot;Left&quot; Height=&quot;24&quot; Margin=&quot;140,117,0,0&quot; TextWrapping=&quot;Wrap&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;180&quot; FontSize=&quot;16&quot; FontWeight=&quot;Bold&quot; TextAlignment=&quot;Center&quot;/&gt;
&lt;TextBox x:Name=&quot;target&quot; HorizontalAlignment=&quot;Left&quot; Height=&quot;24&quot; Margin=&quot;139,158,0,0&quot; TextWrapping=&quot;Wrap&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;180&quot; FontSize=&quot;16&quot; FontWeight=&quot;Bold&quot; TextAlignment=&quot;Center&quot;/&gt;
&lt;TextBox x:Name=&quot;stop&quot; HorizontalAlignment=&quot;Left&quot; Height=&quot;24&quot; Margin=&quot;139,203,0,0&quot; TextWrapping=&quot;Wrap&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;180&quot; FontSize=&quot;16&quot; FontWeight=&quot;Bold&quot; TextAlignment=&quot;Center&quot;/&gt;
&lt;TextBox x:Name=&quot;risk&quot; HorizontalAlignment=&quot;Left&quot; Height=&quot;24&quot; Margin=&quot;140,244,0,0&quot; TextWrapping=&quot;Wrap&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;180&quot; FontSize=&quot;16&quot; FontWeight=&quot;Bold&quot; TextAlignment=&quot;Center&quot;/&gt;
&lt;TextBox x:Name=&quot;ratio&quot; HorizontalAlignment=&quot;Left&quot; Height=&quot;24&quot; Margin=&quot;199,285,0,0&quot; TextWrapping=&quot;Wrap&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;120&quot; FontSize=&quot;16&quot; FontWeight=&quot;Bold&quot; TextAlignment=&quot;Center&quot;/&gt;
&lt;TextBox x:Name=&quot;shares&quot; HorizontalAlignment=&quot;Left&quot; Height=&quot;24&quot; Margin=&quot;140,74,0,0&quot; TextWrapping=&quot;Wrap&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;180&quot; FontSize=&quot;16&quot; FontWeight=&quot;Bold&quot; TextAlignment=&quot;Center&quot;/&gt;
&lt;Label Content=&quot;Shares : &quot; HorizontalAlignment=&quot;Left&quot; Height=&quot;30&quot; Margin=&quot;18,69,0,0&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;112&quot; FontSize=&quot;18&quot; FontWeight=&quot;Bold&quot;/&gt;

&lt;/Grid&gt;
&lt;/Window&gt;

&quot;@

[xml]$Form = $inputXAML -replace 'mc:Ignorable=&quot;d&quot;', '' -replace &quot;x:N&quot;, 'N' -replace '^&lt;Win.*', '&lt;Window'

$NR = (New-Object System.Xml.XmlNodeReader $Form)
$Win = [Windows.Markup.XamlReader]::Load($NR)

# Define variables
$total = $win.FindName(&quot;total&quot;)
$entry = $win.FindName(&quot;entry&quot;)
$target = $win.FindName(&quot;target&quot;)
$stop = $win.FindName(&quot;stop&quot;)
$risk = $win.FindName(&quot;risk&quot;)
$ratio = $win.FindName(&quot;ratio&quot;)
$shares = $win.FindName(&quot;shares&quot;)
$run = $win.FindName(&quot;run&quot;)
$reset = $win.FindName(&quot;reset&quot;)

# click the Run Button
$run.add_click( {
    calculate -total2 $total.text -entry2 $entry.text -target2 $target.text -stop2 $stop.text -risk2 $risk.text -shares2 $shares.text -ratio2 $ratio.text
})

$reset.add_click({
    reset-text
})


$Win.showdialog()

Features

The tool can calculate the following needs according to the input.

  • Risk %: Input the Shares, Entry Price, Target Price, and Stop Price. It will calculate the Risk % for the trade against the Total Fund.
  • Reward Risk Ratio: Input the Entry, Target, and Stop Price. It will calculate the RRR (Reward Risk Ratio).
  • Stop Price: Input the Total Fund, Shares, Entry Price, Stop Price, and Risk %. It will calculate the Stop Price to match the risk.
  • Shares: This is the most useful feature for me. Input the Total Fund, Entry Price, Stop Price and Risk %. It will tell you how many shares should buy.

How to use it

You can download the PowerShell script from the GitHub.

The calculator tool can be executed by PowerShell on a Windows machine.

I create a shortcut on my desktop so I can run it quickly.

Right the shortcut and Run with PowerShell. Click Yes if you see the Script Execution Policy warning.

Let me know if you have any issues or suggestions to improve the tool.

Categories:PowerShell

Recent Posts

  • Automating Resource Restriction in Azure subscriptions with Budget Alert, Automation Account, and Azure Policy
  • How to configure the Security Center via Azure Resource Manager (ARM) template
  • How to restrict Account Operators to see the password in LAPS
  • How to join a VM to a domain by PowerShell in Azure
  • How to push an existing repository from VS Code to Azure DevOps by PowerShell

Search

Categories

  • Azure
  • IIS
  • PowerShell
  • SCCM
  • VMware
  • Windows Server

Contact

Email: [email protected] | Powered By the 太初網路

Copyright © 2024 mikewu.org Disclaimer

Connect

FacebookTwitterGoogle +Linkedin