{"id":16330,"date":"2021-09-22T02:21:32","date_gmt":"2021-09-21T18:21:32","guid":{"rendered":"https:\/\/www.tejwin.com\/?post_type=insight&#038;p=16330"},"modified":"2023-08-10T14:27:18","modified_gmt":"2023-08-10T06:27:18","slug":"martingale-strategy","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insight\/martingale-strategy\/","title":{"rendered":"Martingale Strategy"},"content":{"rendered":"\n<p>The gambling trading strategy.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1_1wABPWOVg1mw6pX0Fc0QNrA.jpg\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Photo Creds:&nbsp;<a href=\"https:\/\/unsplash.com\/\" rel=\"noreferrer noopener\" target=\"_blank\">Unsplash<\/a><\/figcaption><\/figure>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_81 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69f42c4c5730e\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"ez-toc-cssicon\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69f42c4c5730e\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.tejwin.com\/en\/insight\/martingale-strategy\/#Highlights\" >Highlights<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.tejwin.com\/en\/insight\/martingale-strategy\/#Preface\" >Preface<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.tejwin.com\/en\/insight\/martingale-strategy\/#The_Editing_Environment_and_Modules_Required\" >The Editing Environment and Modules Required<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.tejwin.com\/en\/insight\/martingale-strategy\/#Database_Used\" >Database Used<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.tejwin.com\/en\/insight\/martingale-strategy\/#Foreign_Exchange_Margin_Profit\" >Foreign Exchange Margin &amp; Profit<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.tejwin.com\/en\/insight\/martingale-strategy\/#Strategy\" >Strategy<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.tejwin.com\/en\/insight\/martingale-strategy\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.tejwin.com\/en\/insight\/martingale-strategy\/#Source_code\" >Source code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.tejwin.com\/en\/insight\/martingale-strategy\/#Extended_Reading\" >Extended Reading<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.tejwin.com\/en\/insight\/martingale-strategy\/#Related_Link\" >Related Link<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"f3b6\"><span class=\"ez-toc-section\" id=\"Highlights\"><\/span><strong>Highlights<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Difficulty\uff1a\u2605\u2606\u2606\u2606\u2606<\/li>\n\n\n\n<li>Introduce commonly used trading strategy in the foreign exchange market.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"141e\"><span class=\"ez-toc-section\" id=\"Preface\"><\/span><strong>Preface<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"1fb5\">\u300cMartingale Strategy\u300dis a popular way of gambling since 18th century. The main method is to double the next bet when we lose this time. By this strategy, if we win the next bet, not only recover previous losses, but also get rewards for the first bet. It sound like a unbeatable strategy, but it has a tremendous risk behind.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"10c9\"><span class=\"ez-toc-section\" id=\"The_Editing_Environment_and_Modules_Required\"><\/span><strong>The Editing Environment and Modules Required<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"b49b\">Mac OS and Jupyter Notebook<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import tejapi<br>import pandas as pd<br>import numpy as np<\/pre>\n\n\n\n<p id=\"3679\"><em>Note: To install module tejapi :&nbsp;<\/em><code><em>pip install tejapi<\/em><\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">tejapi.ApiConfig.api_key = 'Your Key'<br>tejapi.ApiConfig.ignoretz = True<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"b5e2\"><span class=\"ez-toc-section\" id=\"Database_Used\"><\/span><strong>Database Used<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/api.tej.com.tw\/datatables.html?db=GLOBAL&amp;t=%E8%B7%A8%E5%9C%8B%E8%B3%87%E6%96%99%E5%BA%AB\" rel=\"noreferrer noopener\" target=\"_blank\">Multinational Database<\/a>: The code of database is \u2018GLOBAL\/GCURR\u2019. To obtain quotations in the New York and Asian markets exchange rate. The time range is from 2019 to 2020.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">fx = tejapi.get('GLOBAL\/GCURR',<br>                coid = 'EUR',<br>                mdate={'gte': '2019-01-01', 'lte':'2020-12-31'},<br>                opts = {'columns':['mdate','tuse2']},<br>                chinese_column_name = True,<br>                paginate = True)<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1_1-NJkk-7MvQD9IrC4R8Dhew.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"05e9\"><span class=\"ez-toc-section\" id=\"Foreign_Exchange_Margin_Profit\"><\/span><strong>Foreign Exchange Margin &amp; Profit<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Standard Hand : EUR\/USD is 100,000EUR ( If leverage ratio is 1:500, then the margin is 100000\/500 = 200EUR)<\/li>\n\n\n\n<li>Reference :&nbsp;<a href=\"https:\/\/www.investopedia.com\/terms\/f\/foreign-exchange.asp\" rel=\"noreferrer noopener\" target=\"_blank\">https:\/\/www.investopedia.com\/terms\/f\/foreign-exchange.asp<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2b62\"><span class=\"ez-toc-section\" id=\"Strategy\"><\/span><strong>Strategy<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"8af8\">When the profit is positive, execute liquidation and buy 0.01 lots; if it is negative, continue to increase (2 times the lot) to buy.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"b2b3\"><strong>Step 1.\u00a0Initial Value<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">account  = 100000   # 100k USD<br>lot = 0.01            <br>lev = 1\/500           <br>lot_record = [].      <br>account_record = [] <br>cum_profit = 0<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"bb15\"><strong>Step 2.\u00a0Execute Strategy<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">for i in range(len(fx)):<br>    <br>    # \u7b2c\u4e00\u7b46\u8cb7\u5165<br>    if i &lt; 1:<br>        cum_profit -= (100000*lot*lev*fx.loc[i, '\u539f\u5e63\u514c\u7f8e\u5143 (\u7f8e\u5143)'])  <br>        # \u521d\u59cb\u8cb70.01\u624b\u9700\u8981\u7684\u4fdd\u8b49\u91d1\u70ba\u6b50\u5143\uff0c\u6240\u4ee5\u6309\u7167\u7576\u6642\u532f\u7387\u63db\u6210\u7f8e\u5143<br>        account += cum_profit                                       <br>        account_record.append(account)<br>        lot_record.append(lot)<br>    else:<br>        # \u7d2f\u8a08\u640d\u76ca\uff0c\u4e58\u4ee5100000\u662f\u70ba\u4e86\u63db\u6210\u5e03\u6578<br>        cum_profit += (fx.loc[i, '\u539f\u5e63\u514c\u7f8e\u5143 (\u7f8e\u5143)'] - fx.loc[i-1, <br>        '\u539f\u5e63\u514c\u7f8e\u5143 (\u7f8e\u5143)'])*100000*lot    # \u5e33\u6236\u9918\u984d<br>        account += (fx.loc[i, '\u539f\u5e63\u514c\u7f8e\u5143 (\u7f8e\u5143)'] - fx.loc[i-1, '\u539f\u5e63<br>        \u514c\u7f8e\u5143 (\u7f8e\u5143)'])*100000*lot    # \u5982\u679c\u7d2f\u8a08\u640d\u76ca\u7372\u5229,\u5e73\u5009\u5f8c\u8cb7\u51650.01\u624b<br>        if cum_profit &gt;= 0:       <br>            #\u8cb7\u51650.01\u624b\uff0c\u6545\u91cd\u7f6elot &amp; cum_profit<br>            lot = 0.01<br>            cum_profit = -(100000*lot*lev*fx.loc[i, '\u539f\u5e63\u514c\u7f8e\u5143 (\u7f8e<br>            \u5143)']) <br>            account += cum_profit<br>            account_record.append(account)<br>            lot_record.append(lot)    # \u82e5\u7d2f\u8a08\u640d\u76ca\u70ba\u8ca0\uff0c\u5247\u52a0\u78bc\u8cb7\u9032<br>        else:<br>            lot = lot *2 <br>            cum_profit -= (100000*(lot-lot\/2)*lev*fx.loc[i, '\u539f\u5e63\u514c\u7f8e<br>            \u5143 (\u7f8e\u5143)'])  <br>            account -= (100000*(lot-lot\/2)*lev*fx.loc[i, '\u539f\u5e63\u514c\u7f8e\u5143 <br>            (\u7f8e\u5143)'])<br>            account_record.append(account)              <br>            lot_record.append(lot)<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"cfd2\"><strong>Step 3.\u00a0Review<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">fx['\u50f9\u503c'] = account_record<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1rI6qwm43O4httt3CAXFKdg.gif\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"78cf\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span><strong>Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"58d0\">Here we can see the big drawdown in the middle of our profit line. That is because when we face loss, we will increase our position. But if the price did not go up, we would double the loss. The biggest problem of this strategy is the risk could eat all the profit we earned. Therefore, the risk control is the Martingale users continually dedicate to improve.<\/p>\n\n\n\n<p id=\"508e\">If readers are interested in certain databases, welcome to&nbsp;<a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_intro\" rel=\"noreferrer noopener\" target=\"_blank\">E-shop<\/a>&nbsp;to choose the optimal plan for yourself !<\/p>\n\n\n\n<p id=\"b253\">The content of this webpage is not an investment device and does not constitute any offer or solicitation to offer or recommendation of any investment product. It is for learning purposes only and does not take into account your individual needs, investment objectives and specific financial circumstances. Investment involves risk. Past performance is not indicative of future performance. Readers are requested to use their personal independent thinking skills to make investment decisions on their own. If losses are incurred due to relevant suggestions, it will not be involved with author.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Source_code\"><\/span><strong>Source code<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/gist.github.com\/tej87681088\/855eac6900fbbbc7cd979d10d969b6db#file-tejapi_medium_-ipynb\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">Click here to go Github<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8456\"><span class=\"ez-toc-section\" id=\"Extended_Reading\"><\/span><strong>Extended Reading<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.tejwin.com\/en\/insight\/warren-e-buffetts-value-investing\/\" class=\"ek-link\">Warren E. Buffett\u2019s Value Investing<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.tejwin.com\/en\/insight\/efficient-frontier\/\" class=\"ek-link\">Efficient Frontier<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"47b9\"><span class=\"ez-toc-section\" id=\"Related_Link\"><\/span><strong>Related Link<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/api.tej.com.tw\/index.html\" rel=\"noreferrer noopener\" target=\"_blank\">TEJ API<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_intro\" rel=\"noreferrer noopener\" target=\"_blank\">TEJ E-Shop<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The gambling trading strategy. Highlights Preface \u300cMartingale Strategy\u300dis a popular way of gambling since 18th century. The main method is to double the next bet when we lose this time. By this strategy, if we win the next bet, not only recover previous losses, but also get rewards for the first bet. It sound like [&hellip;]<\/p>\n","protected":false},"featured_media":16339,"template":"","tags":[2371,3005],"insight-category":[690,50],"class_list":["post-16330","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-python","tag-tejapi-application","insight-category-data-analysis","insight-category-fintech"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/16330","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight"}],"about":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/types\/insight"}],"version-history":[{"count":0,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/16330\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media\/16339"}],"wp:attachment":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media?parent=16330"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=16330"},{"taxonomy":"insight-category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight-category?post=16330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}