Matthew Lam

Step-by-Step: Utilizing the Pattern Recognizer for Better Trading Outcomes

Quantitative Model


Are you ready to elevate your trading game? Unlock the potential of your strategies with my innovative Pattern Recognizer library! Designed for traders of all levels, this powerful tool can help you identify key technical patterns and generate actionable entry signals. Dive in and discover how you can make smarter trading decisions with ease!


poster


Step 1. Get your API key

Following the instruction from this post to generate your API key.


Step 2. Import library

Import my library under "def start(...)" in line #12.

Also remember to replace line #11 with your own user ID and API key.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from AlgoAPI import AlgoAPIUtil, AlgoAPI_Backtest
from datetime import datetime, timedelta

class AlgoEvent:
    def __init__(self):
        self.timer = datetime(1970,1,1)

    def start(self, mEvt):
        self.evt = AlgoAPI_Backtest.AlgoEvtHandler(self, mEvt)

        headers = {"user":"xxx", "apikey":"xxx"}
        self.app1 = self.evt.app_init("https://algogene.com/rest/v1/bot/76/pattern_recognition.signals", headers=headers)

        self.evt.start()

Step 3. Call library and Get Result

Use "self.evt.app_call(...)" to call my library and get signal indicator results (i.e. line #5-11).

In below example, we will

  • open a buy order if double bottom pattern appears
  • open a sell order if double top pattern appears
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    def on_marketdatafeed(self, md, ab):
        if md.timestamp >= self.timer+timedelta(hours=24):
            self.timer = md.timestamp

            params = {
                "instrument":md.instrument, 
                "timestamp":md.timestamp,
                "interval":"D", 
                "count":90
            }
            res = self.evt.app_call(self.app1, params)
    
            # print result
            self.evt.consoleLog(res)
            
            # open order
            if res['double_bottom_pattern']==1:
                self.openorder(md.instrument, 1)
            elif res['double_top_pattern']==1:
                self.openorder(md.instrument, -1)
    
    def openorder(self, instrument, buysell):
        order = AlgoAPIUtil.OrderObject(
            instrument=instrument,
            buysell=1,
            volume=1,
            openclose='open',
            ordertype=0
        )
        self.evt.sendOrder(order)

Step 4. Final Backtest

Here is the full backtest script. we will apply this script to 1-day candle of XAUUSD for 2020.01.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from AlgoAPI import AlgoAPIUtil, AlgoAPI_Backtest
from datetime import datetime, timedelta

class AlgoEvent:
    def __init__(self):
        self.timer = datetime(1970,1,1)

    def start(self, mEvt):
        self.evt = AlgoAPI_Backtest.AlgoEvtHandler(self, mEvt)

        headers = {"user":"xxx", "apikey":"xxx"}
        self.app1 = self.evt.app_init("https://algogene.com/rest/v1/bot/76/pattern_recognition.signals", headers=headers)

        self.evt.start()

    def on_marketdatafeed(self, md, ab):
        if md.timestamp >= self.timer+timedelta(hours=24):
            self.timer = md.timestamp

            params = {
                "instrument":md.instrument, 
                "timestamp":md.timestamp,
                "interval":"D", 
                "count":90
            }
            res = self.evt.app_call(self.app1, params)
    
            # print result
            self.evt.consoleLog(res)
            
            # open order
            if res['double_bottom_pattern']==1:
                self.openorder(md.instrument, 1)
            elif res['double_top_pattern']==1:
                self.openorder(md.instrument, -1)
    
    def openorder(self, instrument, buysell):
        order = AlgoAPIUtil.OrderObject(
            instrument=instrument,
            buysell=1,
            volume=1,
            openclose='open',
            ordertype=0
        )
        self.evt.sendOrder(order)

sample


Remark: you may encounter API error if your wallet has insufficient balance. In case you encounter such error, simply top-up your wallet balance before running the script above.



Conclusion

Don't miss out on the opportunity to enhance your trading strategy. My Pattern Recognizer library is your key to mastering market movements and gaining an edge over the competition. Like this post and start your journey toward more informed trading today!