avatarMahmud Ahsan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

6094

Abstract

nge(len(<span class="hljs-class"><span class="hljs-keyword">data</span>)):</span>

        try:
            
            <span class="hljs-class"><span class="hljs-keyword">data</span>[i, where] = (<span class="hljs-title">data</span>[<span class="hljs-title">i</span> - <span class="hljs-title">lookback</span> + 1:<span class="hljs-title">i</span> + 1, <span class="hljs-title">close</span>].<span class="hljs-title">mean</span>())</span>
        
        except <span class="hljs-type">IndexError</span>:
            
            pass
        
<span class="hljs-class"><span class="hljs-keyword">data</span> = jump(<span class="hljs-title">data</span>, <span class="hljs-title">lookback</span>)</span>

return <span class="hljs-class"><span class="hljs-keyword">data</span></span></pre></div><div id="0f03"><pre><span class="hljs-attribute">def</span> demarker(Data, lookback, high, low, where):

<span class="hljs-comment"># Calculating DeMAX</span>
<span class="hljs-attribute">for</span> i in range(len(Data)):
    
    <span class="hljs-attribute">if</span> Data[i, high] &gt; Data[i - <span class="hljs-number">1</span>, high]:
        <span class="hljs-attribute">Data</span>[i, where] = Data[i, high] - Data[i - <span class="hljs-number">1</span>, high]
    <span class="hljs-attribute">else</span>:
        <span class="hljs-attribute">Data</span>[i, where] = <span class="hljs-number">0</span>

<span class="hljs-comment"># Calculating the Moving Average on DeMAX</span>
<span class="hljs-attribute">Data</span> = ma(Data, lookback, where, where + <span class="hljs-number">1</span>)        
        
<span class="hljs-comment"># Calculating DeMIN</span>
<span class="hljs-attribute">for</span> i in range(len(Data)):
    
    <span class="hljs-attribute">if</span> Data[i - <span class="hljs-number">1</span>, low] &gt; Data[i, low]:
        <span class="hljs-attribute">Data</span>[i, where + <span class="hljs-number">2</span>] = Data[i - <span class="hljs-number">1</span>, low] - Data[i, low]
    <span class="hljs-attribute">else</span>:
        <span class="hljs-attribute">Data</span>[i, where + <span class="hljs-number">2</span>] = <span class="hljs-number">0</span>    

<span class="hljs-comment"># Calculating the Moving Average on DeMIN</span>
<span class="hljs-attribute">Data</span> = ma(Data, lookback, where + <span class="hljs-number">2</span>, where + <span class="hljs-number">3</span>)        


<span class="hljs-comment"># Calculating DeMarker</span>
<span class="hljs-attribute">for</span> i in range(len(Data)):
    
    <span class="hljs-attribute">Data</span>[i, where + <span class="hljs-number">4</span>] = Data[i, where + <span class="hljs-number">1</span>] / (Data[i, where + <span class="hljs-number">1</span>] + Data[i, where + <span class="hljs-number">3</span>]) 

<span class="hljs-comment"># Removing Excess Columns</span>
<span class="hljs-attribute">Data</span> = deleter(Data, where, <span class="hljs-number">4</span>)

<span class="hljs-attribute">return</span> Data</pre></div><figure id="a207"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Hzlb4TnTDcvH3sxeXwjTUQ.png"><figcaption><b>EURUSD in the first panel with the 21-period Demarker in the second panel.</b></figcaption></figure><p id="9cfd">Check out my weekly market sentiment report to understand the current positioning and to estimate the future direction of several major markets through complex and simple models working side by side. Find out more about the report through this link:</p><div id="7415" class="link-block">
      <a href="https://coalescence.substack.com/">
        <div>
          <div>
            <h2>Coalescence</h2>
            <div><h3>A Weekly Report Covering FX &amp; Equities Market Positioning Using Complex Models. Let me read it first This site requires…</h3></div>
            <div><p>coalescence.substack.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*7zo8Jv6sfnts5WCL)"></div>
          </div>
        </div>
      </a>
    </div><h1 id="8210">Using the Demarker</h1><p id="0df8">I am not a strong believer in the 0.30/0.70 overbought/oversold levels as I prefer to widen them a little bit to at least 0.20/0.80. Therefore, when we see the signals on the chart following the 0.20/0.80 rule, we can get something like the below plot.</p><ul><li><b>A long (Buy) signal is generated whenever the Demarker reaches 0.20 with the previous value above 0.20.</b></li><li><b>A short (Sell) signal is generated whenever the Demarker reaches 0.80 with the previous value below 0.80.</b></li></ul><div id="ff6e"><pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">signal</span>(<span class="hljs-params"><span class="hljs-title class_">Data</span>, what, buy, sell</span>)<span class="hljs-symbol">:</span></pre></div><div id="0e12"><pre><span class="hljs-type">Data</span> = adder(<span class="hljs-type">Data</span>, <span class="hljs-number">10</span>)</pre></div><div id="4a8f"><pre><span class="hljs-symbol">for</span> i in range(len(<span class="hljs-meta">Data</span>)):
        
    <span class="hljs-meta">if</span> <span class="hljs-meta">Data</span>[i, what] &lt; lower_barrier <span class="hljs-keyword">and</span> <span class="hljs-meta">Data</span>[i - <span class="hljs-number">1</span>, what] &gt; lower_barrier:
        <span class="hljs-meta">Data</span>[i + <span class="hljs-number">1</span>, buy] = <span class="hljs-number">1</span>
        
    <span class="hljs-meta">if</span> <span class="hljs-meta">Data</span>[i, what] &gt; upper_barrier <span class="hljs-keyword">and</span> <span class="hljs-meta">Data</span>[i - <span class="hljs-number">1</span>, what] &lt; upper_barrier:
        <span class="hljs-meta">Data</span>[i + <

Options

span class="hljs-number">1</span>, sell] = -<span class="hljs-number">1</span>

return <span class="hljs-meta">Data</span></pre></div><figure id="7ee7"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Vwt17ycpypw7FqWrOQ2urQ.png"><figcaption><b>Signal chart.</b></figcaption></figure><p id="251c">Another strategy can be created which states that only the exit of the extreme zones will yield a trade:</p><ul><li><b>A long (Buy) signal is generated whenever the Demarker exits 0.20 with the previous value below 0.20.</b></li><li><b>A short (Sell) signal is generated whenever the Demarker exits 0.80 with the previous value above 0.80.</b></li></ul><div id="47c0"><pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">signal</span>(<span class="hljs-params"><span class="hljs-title class_">Data</span>, what, buy, sell</span>)<span class="hljs-symbol">:</span></pre></div><div id="2fa6"><pre><span class="hljs-type">Data</span> = adder(<span class="hljs-type">Data</span>, <span class="hljs-number">10</span>)</pre></div><div id="f24b"><pre><span class="hljs-symbol">for</span> i in range(len(<span class="hljs-meta">Data</span>)):
        
    <span class="hljs-meta">if</span> <span class="hljs-meta">Data</span>[i, what] &gt; lower_barrier <span class="hljs-keyword">and</span> <span class="hljs-meta">Data</span>[i - <span class="hljs-number">1</span>, what] &lt; lower_barrier:
        <span class="hljs-meta">Data</span>[i + <span class="hljs-number">1</span>, buy] = <span class="hljs-number">1</span>
        
    <span class="hljs-meta">if</span> <span class="hljs-meta">Data</span>[i, what] &lt; upper_barrier <span class="hljs-keyword">and</span> <span class="hljs-meta">Data</span>[i - <span class="hljs-number">1</span>, what] &gt; upper_barrier:
        <span class="hljs-meta">Data</span>[i + <span class="hljs-number">1</span>, sell] = -<span class="hljs-number">1</span>
        
return <span class="hljs-meta">Data</span></pre></div><figure id="2c88"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*_4OQFQtIqhz1AfcsJcXosg.png"><figcaption><b>Signal chart.</b></figcaption></figure><p id="d761">The second technique of waiting before initiating the position showed slightly better results but not statistically different. In anyways, both are losing strategies with the second one losing less (0.94 profit factor compared to 0.96).</p><p id="3618">I have recently partnered with Lumiwealth, and if you want to see how to create all sorts of algorithms yourself, feel free to check out the below link. From algorithmic trading to blockchain and machine learning, they have hands-on detailed courses that I highly recommend.</p><div id="fead" class="link-block">
      <a href="https://www.lumiwealth.com/algorithmic-trading-landing-page/?utm_source=influence&amp;utm_medium=medium&amp;utm_campaign=sofien">
        <div>
          <div>
            <h2>Learn Algorithmic Trading with Python Lumiwealth</h2>
            <div><h3>undefined</h3></div>
            <div><p>undefined</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*FbCz_ARtbOdywMlC)"></div>
          </div>
        </div>
      </a>
    </div><h1 id="e420">Summary</h1><p id="05a4">To sum up, what I am trying to do is to simply contribute to the world of objective technical analysis which is promoting more transparent techniques and strategies that need to be back-tested before being implemented. This way, technical analysis will get rid of the bad reputation of being subjective and scientifically unfounded.</p><p id="570c">Medium is a hub to interesting reads. I read a lot of articles before I decided to start writing. Consider joining Medium using my referral link (at <b>NO </b>additional cost to you).</p><div id="de8c" class="link-block">
      <a href="https://kaabar-sofien.medium.com/membership">
        <div>
          <div>
            <h2>Join Medium with my referral link — Sofien Kaabar</h2>
            <div><h3>As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…</h3></div>
            <div><p>kaabar-sofien.medium.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*XBqK-oHjNF_4ydU8)"></div>
          </div>
        </div>
      </a>
    </div><p id="5d41">I recommend you always follow the the below steps whenever you come across a trading technique or strategy:</p><ul><li>Have a critical mindset and get rid of any emotions.</li><li>Back-test it using real life simulation and conditions.</li><li>If you find potential, try optimizing it and running a forward test.</li><li>Always include transaction costs and any slippage simulation in your tests.</li><li>Always include risk management and position sizing in your tests.</li></ul><p id="beb3">Finally, even after making sure of the above, stay careful and monitor the strategy because market dynamics may shift and make the strategy unprofitable.</p><p id="4f75">For the paperback link of the book, you may use the following link:</p><div id="355f" class="link-block">
      <a href="https://www.amazon.com/dp/B09VG3SH2P?&amp;linkCode=sl1&amp;tag=sofien-20&amp;linkId=f0daa140733c5f6b08c1e744bd1b98b5&amp;language=en_US&amp;ref_=as_li_ss_tl">
        <div>
          <div>
            <h2>Contrarian Trading Strategies in Python</h2>
            <div><h3>Amazon.com: Contrarian Trading Strategies in Python: 9798434008075: Kaabar, Sofien: Books</h3></div>
            <div><p>www.amazon.co</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*C7k3f3kawsSsQCVn)"></div>
          </div>
        </div>
      </a>
    </div></article></body>

Flutter

How to Use Provider in Flutter

The easiest way to manage the global state in a flutter app

By author

Whether you develop a native app or hybrid app, state management is a common issue to address. To manage a global state in a flutter app, Provider is one of the most popular libraries.

In this tutorial, I will guide you, how to use the provider in a flutter app. We will build the following application with 3 screens:

  1. Home
  2. About
  3. Settings

From the settings screen, a user can change the font size, which will affect the home and about the screen. And we will implement this feature using the Provider state management library.

Flutter App With Provider

I assume you have a basic understanding of the Flutter framework.

Source Code:

1. Global state concept

If you already use a stateful widget, you know what is state and how to use local state within a Flutter app. But we want to use a global state so that any screen or widget can access the data.

So visually it will look like this.

Visual diagram by Author
  1. MyApp is the main widget or root widget where we will bind the App state or you can say a global state.
  2. All other screens are children of this MyApp widget
  3. But as we have a global state, we can access it from anywhere easily.

2. Create the app

I use android studio to create flutter apps. But it is optional. You can use visual studio code editor as well.

2.1 Create a new Flutter Project in Android Studio

Select New Flutter Project from the File — New menu.

Screen captured by author

You will see the following screen. Now select Flutter Application and click Next.

Screen captured by author

Now write a project name, select the Flutter SDK path, set the project location, and click Next.

Screen captured by author

Now write a package name if you want another name, and click Finish.

Screen captured by author

Now you will see the following screen:

Screen captured by author

2.2 Create a few files and a directory

Create a directory name model within the lib directory. Now create 5 files with the .dart extension.

  1. model/ui.dart
  2. about.dart
  3. drawer_menu.dart
  4. home.dart
  5. settings.dart
Screen captured by author

3. Install provider

Now open the pubspec.yaml file and type flutter_lorem: ^1.1.0 and provider: ^4.3.2+2 just under cupertino_icons: ^1.0.0 and click Pub get to install the libraries.

We need flutter_lorem to show random text.

Screen captured by author

4. Important concept

Before digging further, let's know about some concepts. To use a global state by Provider we have to understand 3 classes:

  1. ChangeNotifier
  2. ChangeNotifierProvider
  3. Consumer

de>ChangeNotifier is a built-in class in the Flutter framework. Its job is to notify its listener, simple.

de>ChangeNotifierProvider class comes from the Provider library. It listens to a de>ChangeNotifier and when it listens something new by ChangeNotifier.notifyListeners is called, it rebuilds its dependents.

The de>Consumer basically a widget by the provider library. It just calls Provider.of in a new widget, and delegates its build implementation to builder.

4. Creating a model

Now open the model/ui.dart file, and write the following code:

Here we created a class UI which implemented the ChangeNotifier. We defined a private variable fontSize and some methods to access or modify the value.

Screen captured by author

Within the set fontSize(newValue) method, you see there is a notifyListerns() method at the end. If the fontSize value is changed, it notifies its listeners. If you don’t use this method, nothing will work, so it is very important.

Another thing is, the slider value for font size is ranged from 0.0 to 1.0. But we want a human-readable font size. That’s why we multiply 30 with the _fontSize value.

double get fontSize => _fontSize * 30;

4. Main.dart modification

Open themain.dart file and remove all the code. Now paste the following code there.

Here we import our own custom widgets and most importantly provider package by import ‘package:provider/provider.dart’;

MyApp is the main widget and using the main() function we create an instance of it.

Within MyApp we create a MultiProvider widget, where in the providers list we pass ChangeNotifierProvider(create: (_) => UI()) . UI is our data model class, and using ChangeNotifierProvider we create an instance of it.

In the child section, we use the MaterialApp widget with a configured routing.

In your app, if you want to use many providers, just add additional ChangeNotifierProvider(create: (_) => YOUR_DATA_MODEL()) within the providers list.

5. Create a drawer menu

We need 3 menu items. Home, About, and Settings. Using the drawer menu system, we can easily do that.

Screen captured by author

Open the drawer_menu.dart file and paste the following code.

The code is self-explanatory. Basically, it creates the menus, and if the user taps a menu item, it will show that widget as a screen.

6. Home screen

Now we have to create the home screen where we will show some random text.

Screen captured by author

So open the home.dart file, copy the following code, and paste on there.

The code is easy to understand. Only take note that we use a Consumer<UI> widget at the line number 23. It means, we want to access the UI data model here, which is bound at the top-level root widget MyApp via the provider.

Within it at line number 24, we pass a builder function which receives 3 arguments, and we can access them via the names of: context, ui, and child. ui is the instance of UI class which is technically instantiated and binded with our main widget MyApp.

Now in the builder function, we put the widget where we want to access the font data.

If in your application you use multiple data model, just mention the model name within the Consumer widget

So if the fontSize is updated, it will use that updated data.

7. About screen

Similar to the home screen, open the about.dart file and copy-paste the following code.

8. Settings screen

This is the screen where the user will see a slider and can change the font size.

Screen captured by author

Now open the settings.dart file and copy-paste the following code

This portion is similar to home.dart. But the only difference is, this widget can both access and update the font data. Within the Slider widget at line number 27, you will see at line number 30–32, we update the font size via ui.fontSize = newValue. So all other screens when will appear, will show the updated font size.

Screen captured by author

That’s it. Now you have a global state in your app.

Conclusion

I like the Provider library for its simplicity to use. Unless your team prefers other state management library, just use it.

If you want to see more features or you have any questions, let me know in the comments.

Source Code: 🚀

Check out my other Flutter Tutorials:

Flutter
iOS
Android
Dart
Software Engineering
Recommended from ReadMedium