{"id":11394,"date":"2017-07-07T15:51:19","date_gmt":"2017-07-07T07:51:19","guid":{"rendered":"http:\/\/www.vbtutor.net\/?page_id=11394"},"modified":"2017-07-08T19:24:32","modified_gmt":"2017-07-08T11:24:32","slug":"stock-trading-simulation-program","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/","title":{"rendered":"Stock Trading Simulation Program"},"content":{"rendered":"<p>You can create a stock trading simulation program easily in Visual Basic. We will create such program using Visual Basic 6. Of course, you can also create the program using other versions of Visual Basic.<\/p>\n<p>For stock trading, we need to consider the following data:<\/p>\n<ul>\n<li>Asking Price- Price offered by the sellers<\/li>\n<li>Bidding Price- Price offered by the buyers<\/li>\n<li>Selling Quantity-Total number of shares available for sale on the stock market<\/li>\n<li>Buying Quantity- Total number of shares the buyers bid on the stock market<\/li>\n<li>Last Done Price- The price for the last transaction<\/li>\n<li>Order Price &#8211; Price bid by the\u00a0trader to buy or to sell<\/li>\n<li>Order Quantity- Number of Shares ordered by the trader<\/li>\n<li>Average Price- Average share price<\/li>\n<li>Buy Value &#8211; Average value of shares paid by the \u00a0trader<\/li>\n<li>Gross Market Value- The current market of shares owned by the trader<\/li>\n<li>The total number of shares in the hand of the trader.<\/li>\n<li>Profit or Loss<\/li>\n<\/ul>\n<p>We shall use the following variables to represent the aforementioned data:<\/p>\n<p>AP- Asking Price<br \/>\nBD- Bidding Price<br \/>\nSQ- Selling Quantity<br \/>\nBQ- Buying Quantity<br \/>\nLP- Last Done Price<br \/>\nOP- Order Price<br \/>\nOQ- Order Quantity<br \/>\nAVP- Average Price<br \/>\nBV- Buy Value<br \/>\nMV- Gross Market Value<br \/>\nTQ- Total Number of Shares in Hand<\/p>\n<p>In this program, we need to insert a timer to generate random values for the Asking Price, the Bidding Price, the Selling Quantity, the Buying Quantity and the Last Done Price. You can set the timer to any interval you think is suitable, here we set it to 15 seconds so that the values changes every 15 seconds to simulate the real stock market. The code is as follows:<\/p>\n<p>ranNum1 = Rnd() * 2 + 3<br \/>\nranNum2 = Rnd() * 2 + 3<br \/>\nranNum3 = Rnd() * 2 + 3<br \/>\nAP = Round(ranNum1, 3) * 5<br \/>\nSQ = Int(Round(Rnd(), 2) * 10000) + 1000<br \/>\nBD = Round(ranNum2, 3) * 5<br \/>\nBQ = Int(Round(Rnd(), 2) * 10000) + 1000<br \/>\nLP = Round(ranNum3, 3) * 5 &#8216;Last Done Price<br \/>\nLVOL = Int(Round(Rnd(), 2) * 10000) + 1000 &#8216;Last Done Volume<br \/>\nPL- Profit or Loss<\/p>\n<p>Besides that, we need to write code to calculate the Average Price, the Buy Value, the Gross Market Value and the total number of shares in hand, as follows:<\/p>\n<p>AVP = (AVP * TQ + OP * OQ) \/ (TQ + OQ)<br \/>\nTQ = TQ + OQ<br \/>\nMV = LP * TQ<br \/>\nBV = TQ * AVP<\/p>\n<h3>The Design Interface<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-11416\" src=\"http:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2017\/07\/stock_trade1-1.jpg\" alt=\"\" width=\"644\" height=\"448\" srcset=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2017\/07\/stock_trade1-1.jpg 644w, https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2017\/07\/stock_trade1-1-300x209.jpg 300w, https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2017\/07\/stock_trade1-1-624x434.jpg 624w\" sizes=\"auto, (max-width: 644px) 100vw, 644px\" \/><br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<!-- vb Sample Responsive --><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block;\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"5060655505\" data-ad-format=\"auto\"><\/ins><br \/>\n<script>\n(adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h3><strong>The Code<\/strong><\/h3>\n<pre>Dim ranNum1, ranNum2, randNum3, ranNum4 As Double\r\nDim AP As Single\r\nDim SQ As Integer\r\nDim BD As Single\r\nDim BQ As Integer\r\nDim OQ As Long\r\nDim OP As Single\r\nDim LP As Single\r\nDim LVOL As Integer\r\nDim AVP As Single\r\nDim TQ As Long\r\nDim BV As Single\r\nDim MV As Single\r\nDim PL As Single\r\n\r\n\r\nPrivate Sub Cmd_Submit_Click()\r\nOQ = Val(Txt_Quantity.Text)\r\nOP = Val(Txt_Price.Text)\r\nLP = Lbl_LP.Caption\r\nIf Opt_Buy.Value = True And BV &gt;= 1000 Then\r\nAVP = (AVP * TQ + OP * OQ) \/ (TQ + OQ)\r\nTQ = TQ + OQ\r\nLbl_TQ.Caption = TQ\r\nMV = LP * TQ\r\nBV = TQ * AVP\r\nPL = MV - BV\r\nLbl_LP.Caption = Format(LP, \"#,##.00\")\r\nLbl_MV.Caption = Format(MV, \"#,##.00\")\r\nLbl_BV.Caption = Format(BV, \"#,##.00\")\r\nLbl_PL.Caption = Format(PL, \"#,##.00\")\r\nLbl_PL.Caption = Format(PL, \"#,##.00\")\r\nLbl_AVP.Caption = Format(AVP, \"#,##.00\")\r\nElseIf Opt_Buy.Value = True And BV &lt; 1000 Then MsgBox (\"You don't have enough fund to buy, reduce order\") End If If Opt_Sell.Value = True And TQ &gt;= OQ Then\r\nAVP = (AVP * TQ + OP * OQ) \/ (TQ + OQ)\r\nTQ = TQ - OQ\r\nLbl_TQ.Caption = TQ\r\nMV = LP * TQ\r\nBV = TQ * AVP\r\nPL = MV - BV\r\nLbl_MV.Caption = Format(MV, \"#,##.00\")\r\nLbl_BV.Caption = Format(BV, \"#,##.00\")\r\nLbl_PL.Caption = Format(PL, \"#,##.00\")\r\nLbl_AVP.Caption = Format(AVP, \"#,##.00\")\r\n\r\nElseIf Opt_Sell.Value = True And TQ &lt; OQ Then\r\nMsgBox (\"Not enough shares, reduce order\")\r\nEnd If\r\n\r\nEnd Sub\r\n\r\nPrivate Sub Form_Load()\r\n\r\nRandomize\r\nranNum1 = Rnd() * 2 + 3\r\nranNum2 = Rnd() * 2 + 3\r\nranNum3 = Rnd() * 2 + 3\r\nranNum4 = Rnd() * 2 + 3\r\nAVP = Round(ranNum1, 3) * 5 ' Average Price\r\nTQ = Int(Round(Rnd(), 2) * 10000) + 1000 'Total Shares at Hand\r\nBV = AVP * TQ 'Buy Value\r\nAP = Round(ranNum2, 3) * 5 'Asking Price\r\nSQ = Int(Round(Rnd(), 2) * 10000) + 1000\r\nBD = Round(ranNum2, 3) * 5 'Bidding Price\r\nBQ = Int(Round(Rnd(), 2) * 10000) + 1000\r\nLP = Round(ranNum2, 3) * 5 'Last Done Price\r\nMV = LP * TQ\r\nPL = MV - BV\r\nLVOL = Int(Round(Rnd(), 2) * 10000) + 1000 'Last Done Volume\r\nLbl_AP.Caption = AP\r\nLbl_SQ.Caption = SQ\r\nLbl_BD.Caption = BD\r\nLbl_BQ.Caption = BQ\r\nLbl_LP.Caption = OP\r\nLbl_AVP.Caption = AVP\r\nLbl_TQ.Caption = TQ\r\nLbl_MV.Caption = Format(MV, \"#,##.00\")\r\nLbl_PL.Caption = Format(PL, \"#,##.00\")\r\nLbl_LP.Caption = Format(LP, \"#,##.00\")\r\nLbl_BV.Caption = Format(BV, \"#,##.00\")\r\nLbl_Vol.Caption = LVOL\r\n\r\nEnd Sub\r\n\r\n\r\nPrivate Sub Timer1_Timer()\r\nRandomize\r\nranNum1 = Rnd() * 2 + 3\r\nranNum2 = Rnd() * 2 + 3\r\nranNum3 = Rnd() * 2 + 3\r\nAP = Round(ranNum1, 3) * 5\r\nSQ = Int(Round(Rnd(), 2) * 10000) + 1000\r\nBD = Round(ranNum2, 3) * 5\r\nBQ = Int(Round(Rnd(), 2) * 10000) + 1000\r\nLP = Round(ranNum3, 3) * 5 'Last Done Price\r\nMV = LP * TQ ' Market Value\r\nPL = MV - BV\r\nLVOL = Int(Round(Rnd(), 2) * 10000) + 1000 'Last Done Volume\r\nLbl_AP.Caption = AP\r\nLbl_SQ.Caption = SQ\r\nLbl_BD.Caption = BD\r\nLbl_BQ.Caption = BQ\r\nLbl_LP.Caption = LP\r\nLbl_Vol.Caption = LVOL\r\nLbl_MV.Caption = Format(MV, \"#,##.00\")\r\nLbl_PL.Caption = Format(PL, \"#,##.00\")\r\nEnd Sub\r\n\r\n<\/pre>\n<h3>The Runtime Interface<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-11417\" src=\"http:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2017\/07\/stock_trade-1.jpg\" alt=\"\" width=\"630\" height=\"451\" srcset=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2017\/07\/stock_trade-1.jpg 630w, https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2017\/07\/stock_trade-1-300x215.jpg 300w, https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2017\/07\/stock_trade-1-624x447.jpg 624w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<!-- vb Sample Responsive --><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block;\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"5060655505\" data-ad-format=\"auto\"><\/ins><br \/>\n<script>\n(adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can create a stock trading simulation program easily in Visual Basic. We will create such program using Visual Basic 6. Of course, you can also create the program using other versions of Visual Basic. For stock trading, we need to consider the following data: Asking Price- Price offered by the sellers Bidding Price- Price &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Stock Trading Simulation Program<\/span><\/a><\/p>\n","protected":false},"author":23013,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-11394","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Stock Trading Simulation Program - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB<\/title>\n<meta name=\"description\" content=\"Visual Basic Tutorial\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Stock Trading Simulation Program - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"og:description\" content=\"Visual Basic Tutorial\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Vbtutor\" \/>\n<meta property=\"article:modified_time\" content=\"2017-07-08T11:24:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2017\/07\/stock_trade1-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"644\" \/>\n\t<meta property=\"og:image:height\" content=\"448\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@liewvk\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/\",\"url\":\"https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/\",\"name\":\"Stock Trading Simulation Program - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"isPartOf\":{\"@id\":\"https:\/\/www.vbtutor.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2017\/07\/stock_trade1-1.jpg\",\"datePublished\":\"2017-07-07T07:51:19+00:00\",\"dateModified\":\"2017-07-08T11:24:32+00:00\",\"description\":\"Visual Basic Tutorial\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/#primaryimage\",\"url\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2017\/07\/stock_trade1-1.jpg\",\"contentUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2017\/07\/stock_trade1-1.jpg\",\"width\":644,\"height\":448},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Stock Trading Simulation Program\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vbtutor.net\/#website\",\"url\":\"https:\/\/www.vbtutor.net\/\",\"name\":\"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"description\":\"Start learning Visual Basic from beginner to advanced. Includes VB.NET, VBA, and classic VB tutorials for students and professionals.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Stock Trading Simulation Program - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"Visual Basic Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/","og_locale":"en_US","og_type":"article","og_title":"Stock Trading Simulation Program - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"Visual Basic Tutorial","og_url":"https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/","og_site_name":"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","article_publisher":"https:\/\/www.facebook.com\/Vbtutor","article_modified_time":"2017-07-08T11:24:32+00:00","og_image":[{"width":644,"height":448,"url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2017\/07\/stock_trade1-1.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_site":"@liewvk","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/","url":"https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/","name":"Stock Trading Simulation Program - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","isPartOf":{"@id":"https:\/\/www.vbtutor.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/#primaryimage"},"image":{"@id":"https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/#primaryimage"},"thumbnailUrl":"http:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2017\/07\/stock_trade1-1.jpg","datePublished":"2017-07-07T07:51:19+00:00","dateModified":"2017-07-08T11:24:32+00:00","description":"Visual Basic Tutorial","breadcrumb":{"@id":"https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/#primaryimage","url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2017\/07\/stock_trade1-1.jpg","contentUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2017\/07\/stock_trade1-1.jpg","width":644,"height":448},{"@type":"BreadcrumbList","@id":"https:\/\/www.vbtutor.net\/index.php\/stock-trading-simulation-program\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Stock Trading Simulation Program"}]},{"@type":"WebSite","@id":"https:\/\/www.vbtutor.net\/#website","url":"https:\/\/www.vbtutor.net\/","name":"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"Start learning Visual Basic from beginner to advanced. Includes VB.NET, VBA, and classic VB tutorials for students and professionals.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/11394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/users\/23013"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/comments?post=11394"}],"version-history":[{"count":18,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/11394\/revisions"}],"predecessor-version":[{"id":11418,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/11394\/revisions\/11418"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=11394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=11394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=11394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}