Skip to content

Commit

Permalink
v3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
Lu-Yi-Hsun committed Sep 10, 2019
1 parent 907e145 commit f53a555
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 48 deletions.
58 changes: 48 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

[![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.me/iqoptionapi)

last Version:3.8.1
last Version:3.9

last update:2019/9/9
last update:2019/9/10

Version:3.9

fix duration time
[change buy api: remove force_buy&!!!fix duration time!!](#buy)
[fix buy_digital_spot() error problem](#buydigitalspot)

Version:3.8.2

[fix digital check win return&add sample code](#checkwindigitalv2)

Version:3.8.1

Expand Down Expand Up @@ -264,18 +273,16 @@ Money=1
ACTIVES="EURUSD"
ACTION="call"#or "put"
expirations_mode=1
force_buy= False#i suggest use False
I_want_money.buy(Money,ACTIVES,ACTION,expirations_mode,force_buy)

I_want_money.buy(Money,ACTIVES,ACTION,expirations_mode)
```

```python
I_want_money.buy(Money,ACTIVES,ACTION,expirations,force_buy)
I_want_money.buy(Money,ACTIVES,ACTION,expirations)
#Money:How many you want to buy type(int)
#ACTIVES:sample input "EURUSD" OR "EURGBP".... you can view by get_all_ACTIVES_OPCODE
#ACTION:"call"/"put" type(str)
#expirations:input minute,careful too large will false to buy(Closed market time)thank Darth-Carrotpie's code (int)https://github.com/Lu-Yi-Hsun/iqoptionapi/issues/6
#force_buy= True: if fail try buy untill sucess
#False:if fail break
#return:(None/id_number):if sucess return (id_number) esle return(None) 2.1.5 change this
```
#### <a id=buymulti>buy_multi</a>
Expand Down Expand Up @@ -326,9 +333,9 @@ Money=1
ACTIVES="EURUSD"
ACTION="call"#or "put"
expirations_mode=1
force_buy= False
id=I_want_money.buy(Money,ACTIVES,ACTION,expirations_mode,force_buy)
id2=I_want_money.buy(Money,ACTIVES,ACTION,expirations_mode,force_buy)

id=I_want_money.buy(Money,ACTIVES,ACTION,expirations_mode)
id2=I_want_money.buy(Money,ACTIVES,ACTION,expirations_mode)

time.sleep(5)
sell_all=[]
Expand Down Expand Up @@ -567,6 +574,37 @@ I_want_money.check_win_digital_v2(id)#get the id from I_want_money.buy_digital
#if trade not clode yet:False,None
```

sample code

```python
from iqoptionapi.stable_api import IQ_Option
import logging
import random
import time
import datetime
#logging.basicConfig(level=logging.DEBUG,format='%(asctime)s %(message)s')
I_want_money=IQ_Option("email","password")


ACTIVES="EURUSD"
duration=1#minute 1 or 5
amount=1
action="call"#put
id=(I_want_money.buy_digital_spot(ACTIVES,amount,action,duration))
print(id)
if id !="error":
while True:
check,win=I_want_money.check_win_digital_v2(id)
if check==True:
break
if win<0:
print("you loss "+str(win)+"$")
else:
print("you win "+str(win)+"$")
else:
print("please try again")
```


#### close digital
```python
Expand Down
79 changes: 79 additions & 0 deletions iqoptionapi/expiration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#python
import time
from datetime import datetime,timedelta



def get_expiration_time(timestamp,duration):
#
now_date = datetime.fromtimestamp(timestamp)
exp_date=now_date.replace(second=0,microsecond=0)
if (int((exp_date+timedelta(minutes=1)).strftime("%s"))-timestamp)>30:
exp_date= exp_date+timedelta(minutes=1)

else:
exp_date= exp_date+timedelta(minutes=2)
exp=[]
for _ in range(5):
exp.append(exp_date.strftime("%s"))
exp_date= exp_date+timedelta(minutes=1)



idx=50
index=0
now_date = datetime.fromtimestamp(timestamp)
exp_date=now_date.replace(second=0,microsecond=0)
while index<idx:
if int(exp_date.strftime("%M"))%15==0 and (int(exp_date.strftime("%s"))-int(timestamp))>60*5:
exp.append(exp_date.strftime("%s"))
index=index+1
exp_date= exp_date+timedelta(minutes=1)

remaning=[]

for t in exp:
remaning.append(int(t)-int(time.time()))

close = [abs(x-60*duration) for x in remaning]

return int(exp[close.index(min(close))]),int(close.index(min(close)))


def get_remaning_time(timestamp):
now_date = datetime.fromtimestamp(timestamp)
exp_date=now_date.replace(second=0,microsecond=0)
if (int((exp_date+timedelta(minutes=1)).strftime("%s"))-timestamp)>30:
exp_date= exp_date+timedelta(minutes=1)

else:
exp_date= exp_date+timedelta(minutes=2)
exp=[]
for _ in range(5):
exp.append(exp_date.strftime("%s"))
exp_date= exp_date+timedelta(minutes=1)
idx=11
index=0
now_date = datetime.fromtimestamp(timestamp)
exp_date=now_date.replace(second=0,microsecond=0)
while index<idx:
if int(exp_date.strftime("%M"))%15==0 and (int(exp_date.strftime("%s"))-int(timestamp))>60*5:
exp.append(exp_date.strftime("%s"))
index=index+1
exp_date= exp_date+timedelta(minutes=1)

remaning=[]

for idx, t in enumerate(exp):
if idx>=5:
dr=15*(idx-4)
else:
dr=idx+1
remaning.append((dr,int(t)-int(time.time())))

return remaning





56 changes: 26 additions & 30 deletions iqoptionapi/stable_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
import pytz
from collections import defaultdict
from interruptingcow import timeout
from iqoptionapi.expiration import get_expiration_time
from datetime import datetime,timedelta


def nested_dict(n, type):
if n == 1:
return defaultdict(type)
Expand All @@ -18,7 +21,7 @@ def nested_dict(n, type):


class IQ_Option:
__version__ = "3.8.1"
__version__ = "3.9"

def __init__(self, email, password):
self.size = [1, 5, 10, 15, 30, 60, 120, 300, 600, 900, 1800,
Expand Down Expand Up @@ -641,41 +644,32 @@ def buy_multi(self,price,ACTIVES,ACTION,expirations):
pass
buy_id=[]
for key in sorted(self.api.buy_multi_option.keys()):
value=self.api.buy_multi_option[key]
buy_id.append(value["id"])
try:
value=self.api.buy_multi_option[key]
buy_id.append(value["id"])
except:
buy_id.append(None)

return buy_id
else:
logging.error('buy_multi error please input all same len')



def buy(self, price, ACTIVES, ACTION, expirations, force_buy=False):
def buy(self, price, ACTIVES, ACTION, expirations):
self.api.buy_successful = None
self.api.buy_id = None
while True:
while True:
try:
self.api.buy(
price, OP_code.ACTIVES[ACTIVES], ACTION, expirations)
break
except:
logging.error('self.api.buy error')
if force_buy == False:
return (None)
self.connect()
self.api.buy(price, OP_code.ACTIVES[ACTIVES], ACTION, expirations)
try:
with timeout(30, exception=RuntimeError):
while self.api.buy_successful == None and self.api.buy_id == None:
pass
start = time.time()
while self.api.buy_successful == None or self.api.buy_id == None:
if time.time()-start > 60:
logging.error('check buy_successful time late 60sec')
break
if self.api.buy_successful:
return (self.api.buy_id)
else:
if force_buy == False:
return (None)
logging.error('**error** buy error...')
self.connect()
except RuntimeError:
logging.error('**warning** buy late 30 sec')


return self.api.buy_successful,self.api.buy_id


def sell_option(self, options_ids):
self.api.sell_option(options_ids)
Expand Down Expand Up @@ -769,8 +763,10 @@ def buy_digital_spot(self, active,amount, action, duration):
logging.error('buy_digital_spot active error')
return -1
#doEURUSD201907191250PT5MPSPT
UTC=datetime.datetime.utcnow()
dateFormated = str(UTC.strftime("%Y%m%d%H"))+str(int(UTC.strftime("%M"))+duration ).zfill(2)

exp,idx=get_expiration_time(int(self.api.timesync.server_timestamp),duration)

dateFormated = str(datetime.utcfromtimestamp(exp).strftime("%Y%m%d%H%M"))
instrument_id = "do" + active + dateFormated + "PT" + str(duration) + "M" + action + "SPT"
self.api.digital_option_placed_id=None

Expand Down Expand Up @@ -800,7 +796,7 @@ def check_win_digital_v2(self,buy_order_id):
order_data=self.get_async_order(buy_order_id)
if order_data!=None:
if order_data["status"]=="closed":
return True,order_data["close_effect_amount"]
return True,order_data["pnl_realized_enrolled"]
else:
return False,None
else:
Expand Down
18 changes: 14 additions & 4 deletions iqoptionapi/ws/chanels/buyv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import time
from iqoptionapi.ws.chanels.base import Base
import logging
from iqoptionapi.expiration import get_expiration_time
from datetime import datetime,timedelta

class Buyv2(Base):
"""Class for IQ option buy websocket chanel."""
Expand All @@ -19,11 +21,19 @@ def __call__(self, price, active, direction,duration):
"""
# thank Darth-Carrotpie's code
#https://github.com/Lu-Yi-Hsun/iqoptionapi/issues/6
exp, option = self.get_expiration_time(duration)

exp,idx=get_expiration_time(int(self.api.timesync.server_timestamp),duration)
if idx<=5:
option="turbo"
else:
option="binary"



data = {
"price": price,
"act": active,
"exp":exp,
"exp":int(exp),
"type": option,
"direction": direction.lower(),
"time": self.api.timesync.server_timestamp
Expand All @@ -33,7 +43,7 @@ def __call__(self, price, active, direction,duration):

# thank Darth-Carrotpie's code
#https://github.com/Lu-Yi-Hsun/iqoptionapi/issues/6
def get_expiration_time(self, duration):
""" def get_expiration_time(self, duration):
exp=int(self.api.timesync.server_timestamp)
if duration>=1 and duration<=5:
option="turbo"
Expand All @@ -60,4 +70,4 @@ def get_expiration_time(self, duration):
else:
logging.error("ERROR get_expiration_time DO NOT LESS 1")
exit(1)
return exp, option
return exp, option"""
11 changes: 8 additions & 3 deletions iqoptionapi/ws/chanels/buyv3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from iqoptionapi.ws.chanels.base import Base
import logging


from iqoptionapi.expiration import get_expiration_time
class Buyv3(Base):

name = "sendMessage"
Expand All @@ -12,7 +12,11 @@ def __call__(self, price, active, direction, duration,request_id):

# thank Darth-Carrotpie's code
# https://github.com/Lu-Yi-Hsun/iqoptionapi/issues/6
exp, option = self.get_expiration_time(duration)
exp,idx=get_expiration_time(int(self.api.timesync.server_timestamp),duration)
if idx<=5:
option = 3#"turbo"
else:
option = 1#"binary"
data = {
"body": {"price": price,
"active_id": active,
Expand All @@ -24,7 +28,7 @@ def __call__(self, price, active, direction, duration,request_id):
"version": "1.0"
}
self.send_websocket_request(self.name, data,str(request_id))

"""
# thank Darth-Carrotpie's code
# https://github.com/Lu-Yi-Hsun/iqoptionapi/issues/6
def get_expiration_time(self, duration):
Expand Down Expand Up @@ -55,3 +59,4 @@ def get_expiration_time(self, duration):
logging.error("ERROR get_expiration_time DO NOT LESS 1")
exit(1)
return exp, option
"""
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name="iqoptionapi",
version="3.8.1",
version="3.9",
packages=find_packages(),
install_requires=["pylint","requests","websocket-client==0.47","interruptingcow"],
include_package_data = True,
Expand Down

0 comments on commit f53a555

Please sign in to comment.