darwinex / dwx-zeromq-connector

Wrapper library for algorithmic trading in Python 3, providing DMA/STP access to Darwinex liquidity via a ZeroMQ-enabled MetaTrader Bridge EA.
https://blog.darwinex.com/zeromq-interface-python-r-metatrader4/
BSD 3-Clause "New" or "Revised" License
342 stars 227 forks source link

IDEA "TELEGRAM + MT4 + dwx-zeromq-connector" #54

Closed TheChapus closed 4 years ago

TheChapus commented 4 years ago

Hi,

Sorry for my English, I used the google translator

While studying FX, I found several channels / groups on telegram that offer signal services.

The problem is that the "signals" arrive between 00: 00-07: 00 AM (Local Time) where I am almost always sleeping.

As I had previously developed BOT for telegram, the idea of joining Telegram with this project was born.

Happy to receive ideas and collaborations in this thread.

SO = Ubuntu 18.04 RAM = 8GB

First I am testing with 2 signal channels, one sends the signal as text and another sends the signal with an image (pair) and text, which motivated me more to learn from Artificial Vision + Python> 3.

EUR/CAD

SELL 1.4591

TP 1.4571
TP 1.4541
TP 1.4491
SL 1.4619
|------------------------------|
|         IMG(PAIR)            |
|------------------------------|

SELL 0.6320

TP 0.6300
TP 0.6270
TP 0.6220
SL 0.6370

After going through several iterations I have reached this, which is not working correctly

Now the entertaining part, the code

# -*- coding: utf-8 -*-

from DWX_ZeroMQ_Connector_v2_0_1_RC8 import *
import time
from telethon import TelegramClient, sync
from telethon import TelegramClient, events
from telethon import utils,functions, types
import sys
import zmq
import threading
import cv2
import os
import pytesseract

#*********************************************
api_id = 
api_hash =
phone = 
client = TelegramClient('phone.session', api_id, api_hash)
#**********************************************

def img_txt(img):
    try:
        image = cv2.imread(img)
        image = cv2.resize(image,(500,500))
        #xy1 xy2        Y           X
        crop_img = image[300:450, 60:450]
        text = pytesseract.image_to_string(crop_img)
        large = len(text)
        txt = text.split()
        #print(txt,len(txt))
        if(len(txt)==3):
            if(txt[0]=='GOLD'):
                txt[0]='XAU'
            os.remove(img)
            return(txt[0]+txt[2])
        else:
            print("Problem to convert imgtxt: ",txt)
    except Exception as e:
        print("ERROR : ",e)

# Lectura de msg telegram
@client.on(events.NewMessage())
async def orden_(event):

    # Order
    par   =''
    orde  =''
    n_orde=0
    be    =''
    slf   =''
    tp1   =''
    tp2   =''
    tp3   =''
    lt    =''
    msg   = ''

    ## MetaTrader response (JSON):{'_action': 'EXECUTION', '_magic': 123456, '_ticket': 85051741, '_open_price': 1.14414, '_sl': 250, '_tp': 750}
    ordentst = {'_action': 'OPEN','_type': 0,'_symbol': 'EURUSD','_price': 0.0,'_SL': 0,'_TP': 0,'_comment': "Python-Bot",'_lots': 0,'_magic': 0,'_ticket': 0}

    try:

        #Channel ESP
        if(event.message.to_id.channel_id == IDCHANELSIGNAL):

            a  = event.raw_text.split()
            if(len(a)==11):

                par = a[0].replace("/","")
                orde  = a[1]
                be  = a[2]
                sl  = a[10]
                tp1 = a[4]
                tp2 = a[6]
                tp3 = a[8]
                lt1 = 0.03
                lt2 = 0.03
                lt3 = 0.02

                if(orde=="SELL"):
                    n_orden = 1
                    ordentst['_type']=n_orden

                ordentst['_symbol']=str(par)
                ordentst['_SL']=str(sl)

                for x in range(0,2):

                    if x == 0:
                        ordentst['_lots']=str(lt1)
                        ordentst['_TP']=str(tp1)

                    elif x ==1:
                        ordentst['_lots']=str(lt2)
                        ordentst['_TP']=str(tp3)

                    #abrimos orden
                    print("open order: ", ordentst)
                    _zmq._DWX_MTX_NEW_TRADE_(_order=ordentst)
                    time.sleep(2)
                    rps = _zmq._thread_data_output#Capturamos rps
                    print("RSP:",rps)
                    time.sleep(3)

                    try:

                        if(rps==None):
                            msg = "Error Order : "+str(a)
                            print("No se envia msg")

                        else:
                            msg = "Orden Ok Ubuntu : "+str(par)+" ,"+str(orde)+" ,"+str(rps['_ticket'])
                            print("Enviando msg")

                    except Exception as e:
                        print("Error:",e)

            else:
                pass

    except Exception as e:
        msg = "Error Orden :\n{}\n{}".format(str(event.raw_text),str(e))

#*********************************************
# main
if __name__ == '__main__':
    print("Init code")

    try:

        # Declarar el programador para consultar alertas
        print("Init Telegram")
        client.start()

        print("Init connector Python-MQL4")
        _zmq = DWX_ZeroMQ_Connector()
        time.sleep(0.5)

        print("Comienza el codigo")
        ################################################
        client.run_until_disconnected()

    except KeyboardInterrupt:
        print("Adios")
        #client.disconnect()

    except Exception as e:
        print("Error ",e)
        client.disconnect()

Example:

MSG:  GBP/CHF

BUY 1.2708

TP 1.2728
TP 1.2758
TP 1.2808
SL 1.2672
Open Order:  {'_action': 'OPEN', '_type': 0, '_symbol': 'GBPCHF', '_price': 0.0, '_SL': '1.2672', '_TP': '1.2728', '_comment': 'Python-Bot', '_lots': '0.03', '_magic': 0, '_ticket': 0}
('unexpected EOF while parsing', ('<string>', 1, 124, "{'_action': 'EXECUTION', '_magic': 0, '_ticket': 150955783, '_open_time': '2019.11.14 17:17:42', '_open_price': 1.27102000}}"))
pwerspire commented 4 years ago

Hi,

Did you change something on DWX_ZeroMQ_Server_v2.0.1_RC8 side? because the order supported is with SL and TP in point and the groups sends the value to close the trade.

I also tried to do this, but couldn't change the TL and SL in MT4 side

integracore2 commented 4 years ago

Hi @TheChapus,

Thank you for your interest in the project 🙂

We're unable to offer support for individual projects of this nature, but from the example output you've pasted, it appears that @pwerspire is right (i.e. was any MQL modified?)

p.s. There's an important question here though:

Why sell signals when you can list a DARWIN and charge 20% performance fees on investor profits?

Here are a few videos (in Spanish if you prefer) that explain the difference between DARWINs and underlying trading strategies:

1) https://www.youtube.com/watch?v=e-sUJ4YXWKI

2) https://www.youtube.com/watch?v=UUEiGO8MIMQ

I highly recommend you watch them - all the best!