Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # IF YOU FOUND THIS USEFUL, Please Donate some Bitcoin ....
- # 1FWt366i5PdrxCC6ydyhD8iywUHQ2C7BWC
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- ####################################################
- # Scikit's LinearRegression model
- # from sklearn.linear_model import LinearRegression
- ####################################################
- import numpy
- import pandas
- import matplotlib.pyplot as plt
- import matplotlib.dates as mdates
- from scipy import stats
- import math
- import requests
- import json
- import time
- import random
- from time import localtime, strftime
- import datetime
- import calendar
- from random import shuffle
- import holidays
- import pytz
- # Frankfurt
- # Germany
- # EUR
- # print("Frankfurt Market Status:...." + str(is_between(str(now_time), ("06:00", "14:00"))))
- # London
- # Great Britain
- # GBP
- # print("London Market Status:...." + str(is_between(str(now_time), ("07:00", "15:00"))))
- # New York
- # United States
- # USD
- # print("New York Market Status:...." + str(is_between(str(now_time), ("12:00", "20:30"))))
- # Sydney
- # Austrailia
- # AUD
- # print("Sydney Market Status:...." + str(is_between(str(now_time), ("22:00", "06:30"))))
- # Tokyo
- # Japan
- # JPY
- # print("Tokyo Market Status:...." + str(is_between(str(now_time),
- # ("23:00", "07:30"))))
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
- # NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE
- # DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY,
- # WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- # SOFTWARE.
- print("########################################################################################")
- print("########################################################################################")
- print("########################################################################################")
- print("THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.")
- print("########################################################################################")
- print("########################################################################################")
- print("########################################################################################")
- # ALL EPICS
- main_epic_ids = [
- 'CS.D.AUDUSD.TODAY.IP',
- 'CS.D.EURCHF.TODAY.IP',
- 'CS.D.EURGBP.TODAY.IP',
- 'CS.D.EURJPY.TODAY.IP',
- 'CS.D.EURUSD.TODAY.IP',
- 'CS.D.GBPEUR.TODAY.IP',
- 'CS.D.GBPUSD.TODAY.IP',
- 'CS.D.USDCAD.TODAY.IP',
- 'CS.D.USDCHF.TODAY.IP',
- 'CS.D.USDJPY.TODAY.IP',
- 'CS.D.CADCHF.TODAY.IP',
- 'CS.D.CADJPY.TODAY.IP',
- 'CS.D.CHFJPY.TODAY.IP',
- 'CS.D.EURCAD.TODAY.IP',
- 'CS.D.EURSGD.TODAY.IP',
- 'CS.D.EURZAR.TODAY.IP',
- 'CS.D.GBPCAD.TODAY.IP',
- 'CS.D.GBPCHF.TODAY.IP',
- 'CS.D.GBPJPY.TODAY.IP',
- 'CS.D.GBPSGD.TODAY.IP',
- 'CS.D.GBPZAR.TODAY.IP',
- 'CS.D.MXNJPY.TODAY.IP',
- 'CS.D.NOKJPY.TODAY.IP',
- 'CS.D.PLNJPY.TODAY.IP',
- 'CS.D.SEKJPY.TODAY.IP',
- 'CS.D.SGDJPY.TODAY.IP',
- 'CS.D.USDSGD.TODAY.IP',
- 'CS.D.USDZAR.TODAY.IP',
- 'CS.D.AUDCAD.TODAY.IP',
- 'CS.D.AUDCHF.TODAY.IP',
- 'CS.D.AUDEUR.TODAY.IP',
- 'CS.D.AUDGBP.TODAY.IP',
- 'CS.D.AUDJPY.TODAY.IP',
- 'CS.D.AUDNZD.TODAY.IP',
- 'CS.D.AUDSGD.TODAY.IP',
- 'CS.D.EURAUD.TODAY.IP',
- 'CS.D.EURNZD.TODAY.IP',
- 'CS.D.GBPAUD.TODAY.IP',
- 'CS.D.GBPNZD.TODAY.IP',
- 'CS.D.NZDAUD.TODAY.IP',
- 'CS.D.NZDCAD.TODAY.IP',
- 'CS.D.NZDCHF.TODAY.IP',
- 'CS.D.NZDEUR.TODAY.IP',
- 'CS.D.NZDGBP.TODAY.IP',
- 'CS.D.NZDJPY.TODAY.IP',
- 'CS.D.NZDUSD.TODAY.IP',
- 'CS.D.CHFHUF.TODAY.IP',
- 'CS.D.EURCZK.TODAY.IP',
- 'CS.D.EURHUF.TODAY.IP',
- 'CS.D.EURILS.TODAY.IP',
- 'CS.D.EURMXN.TODAY.IP',
- 'CS.D.EURPLN.TODAY.IP',
- 'CS.D.EURTRY.TODAY.IP',
- 'CS.D.GBPCZK.TODAY.IP',
- 'CS.D.GBPHUF.TODAY.IP',
- 'CS.D.GBPILS.TODAY.IP',
- 'CS.D.GBPMXN.TODAY.IP',
- 'CS.D.GBPPLN.TODAY.IP',
- 'CS.D.GBPTRY.TODAY.IP',
- 'CS.D.TRYJPY.TODAY.IP',
- 'CS.D.USDCZK.TODAY.IP',
- 'CS.D.USDHUF.TODAY.IP',
- 'CS.D.USDILS.TODAY.IP',
- 'CS.D.USDMXN.TODAY.IP',
- 'CS.D.USDPLN.TODAY.IP',
- 'CS.D.USDTRY.TODAY.IP',
- 'CS.D.CADNOK.TODAY.IP',
- 'CS.D.CHFNOK.TODAY.IP',
- 'CS.D.EURDKK.TODAY.IP',
- 'CS.D.EURNOK.TODAY.IP',
- 'CS.D.EURSEK.TODAY.IP',
- 'CS.D.GBPDKK.TODAY.IP',
- 'CS.D.GBPNOK.TODAY.IP',
- 'CS.D.GBPSEK.TODAY.IP',
- 'CS.D.NOKSEK.TODAY.IP',
- 'CS.D.USDDKK.TODAY.IP',
- 'CS.D.USDNOK.TODAY.IP',
- 'CS.D.USDSEK.TODAY.IP',
- 'CS.D.AUDCNH.TODAY.IP',
- 'CS.D.CADCNH.TODAY.IP',
- 'CS.D.CNHJPY.TODAY.IP',
- 'CS.D.BRLJPY.TODAY.IP',
- 'CS.D.GBPINR.TODAY.IP',
- 'CS.D.USDBRL.TODAY.IP',
- 'CS.D.USDIDR.TODAY.IP',
- 'CS.D.USDINR.TODAY.IP',
- 'CS.D.USDKRW.TODAY.IP',
- 'CS.D.USDMYR.TODAY.IP',
- 'CS.D.USDPHP.TODAY.IP',
- 'CS.D.USDTWD.TODAY.IP',
- 'CS.D.EURCNH.TODAY.IP',
- 'CS.D.sp_EURRUB.TODAY.IP',
- 'CS.D.GBPCNH.TODAY.IP',
- 'CS.D.NZDCNH.TODAY.IP',
- 'CS.D.USDCNH.TODAY.IP',
- 'CS.D.sp_USDRUB.TODAY.IP']
- # ALL EPICS
- # TESTINGs
- # main_epic_ids = [
- # 'CS.D.AUDUSD.TODAY.IP',
- # 'CS.D.EURGBP.TODAY.IP',
- # 'CS.D.EURJPY.TODAY.IP',
- # 'CS.D.EURUSD.TODAY.IP',
- # 'CS.D.GBPUSD.TODAY.IP',
- # 'CS.D.USDCAD.TODAY.IP',
- # 'CS.D.USDCHF.TODAY.IP',
- # 'CS.D.USDJPY.TODAY.IP',
- # 'CS.D.AUDEUR.TODAY.IP',
- # 'CS.D.AUDGBP.TODAY.IP',
- # 'CS.D.AUDJPY.TODAY.IP',
- # 'CS.D.EURAUD.TODAY.IP',
- # 'CS.D.GBPAUD.TODAY.IP']
- # TESTING
- # TIME_FRAMES = ["MINUTE/1", "MINUTE_2/1", "MINUTE_3/1", "MINUTE_5/1", "MINUTE_10/1",
- # "MINUTE_15/1", "MINUTE_30/1", "HOUR/1", "HOUR_2/1", "HOUR_3/1", "HOUR_4/1",
- # "DAY/1", "WEEK/1", "MONTH/1"]
- b_REAL = True
- if b_REAL:
- REAL_OR_NO_REAL = 'https://api.ig.com/gateway/deal'
- API_ENDPOINT = "https://api.ig.com/gateway/deal/session"
- API_KEY = 'API KEY'
- data = {"identifier": "USERNAME", "password": "PASSWORD"}
- else:
- REAL_OR_NO_REAL = 'https://demo-api.ig.com/gateway/deal'
- API_ENDPOINT = "https://demo-api.ig.com/gateway/deal/session"
- API_KEY = 'API KEY'
- data = {"identifier": "USERNAME", "password": "PASSWORD"}
- headers = {'Content-Type': 'application/json; charset=utf-8',
- 'Accept': 'application/json; charset=utf-8',
- 'X-IG-API-KEY': API_KEY,
- 'Version': '2'
- }
- r = requests.post(API_ENDPOINT, data=json.dumps(data), headers=headers)
- headers_json = dict(r.headers)
- CST_token = headers_json["CST"]
- print(R"CST : " + CST_token)
- x_sec_token = headers_json["X-SECURITY-TOKEN"]
- print(R"X-SECURITY-TOKEN : " + x_sec_token)
- # GET ACCOUNTS
- base_url = REAL_OR_NO_REAL + '/accounts'
- authenticated_headers = {'Content-Type': 'application/json; charset=utf-8',
- 'Accept': 'application/json; charset=utf-8',
- 'X-IG-API-KEY': API_KEY,
- 'CST': CST_token,
- 'X-SECURITY-TOKEN': x_sec_token}
- auth_r = requests.get(base_url, headers=authenticated_headers)
- d = json.loads(auth_r.text)
- # print ("-----------------DEBUG-----------------")
- # print ("#################DEBUG#################")
- # print(auth_r.status_code)
- # print(auth_r.reason)
- # print (auth_r.text)
- # print ("-----------------DEBUG-----------------")
- # print ("#################DEBUG#################")
- for i in d['accounts']:
- if str(i['accountType']) == "SPREADBET":
- print("Spreadbet Account ID is : " + str(i['accountId']))
- spreadbet_acc_id = str(i['accountId'])
- # SET SPREAD BET ACCOUNT AS DEFAULT
- base_url = REAL_OR_NO_REAL + '/session'
- data = {"accountId": spreadbet_acc_id, "defaultAccount": "True"}
- auth_r = requests.put(
- base_url,
- data=json.dumps(data),
- headers=authenticated_headers)
- # print ("-----------------DEBUG-----------------")
- # print ("#################DEBUG#################")
- # print(auth_r.status_code)
- # print(auth_r.reason)
- # print (auth_r.text)
- # print ("-----------------DEBUG-----------------")
- # print ("#################DEBUG#################")
- # ERROR about account ID been the same, Ignore!
- ##########################################################################
- ##########################END OF LOGIN CODE###############################
- ##########################END OF LOGIN CODE###############################
- ##########################END OF LOGIN CODE###############################
- ##########################END OF LOGIN CODE###############################
- ##########################################################################
- # PROGRAMMABLE VALUES
- # SET INITIAL VARIABLES, Hacky for now!
- orderType_value = "MARKET"
- size_value = "1"
- expiry_value = "DFB"
- guaranteedStop_value = True
- currencyCode_value = "GBP"
- forceOpen_value = True
- stopDistance_value = "15" # Make this a global variable for ease!
- SL_MULTIPLIER = 3.24
- LOW_SL_WATERMARK = 20
- HIGH_SL_WATERMARK = 70
- STR_CHECK = ""
- set(pytz.all_timezones_set)
- YEAR_var = 2018
- def is_between(time, time_range):
- if time_range[1] < time_range[0]:
- return time >= time_range[0] or time <= time_range[1]
- return time_range[0] <= time <= time_range[1]
- def midpoint(p1, p2):
- return (p1 + p2) / 2
- def distance(a, b):
- if (a == b):
- return 0
- elif (a < 0) and (b < 0) or (a > 0) and (b > 0):
- if (a < b):
- return (abs(abs(a) - abs(b)))
- else:
- return -(abs(abs(a) - abs(b)))
- else:
- return math.copysign((abs(a) + abs(b)), b)
- def percentage(part, whole):
- return 100 * float(part) / float(whole)
- def humanize_time(secs):
- mins, secs = divmod(secs, 60)
- hours, mins = divmod(mins, 60)
- return '%02d:%02d:%02d' % (hours, mins, secs)
- def find_good_epics():
- spreads_and_epics = []
- i_count = 0
- pick_from_epics = []
- full_hol_list = []
- ###################################################################
- tz = pytz.timezone('Europe/Berlin')
- todays_date = str(datetime.datetime.now(tz=tz).strftime('%Y-%m-%d'))
- ger_today = str(str("GER_" + str(todays_date)))
- print("Europe/Berlin :- Today's Date is ..." + str(todays_date))
- ###################################################################
- tz = pytz.timezone('Europe/London')
- todays_date = str(datetime.datetime.now(tz=tz).strftime('%Y-%m-%d'))
- gb_today = str(str("GB_" + str(todays_date)))
- print("Europe/London :- Today's Date is ..." + str(todays_date))
- ###################################################################
- tz = pytz.timezone('America/New_York')
- todays_date = str(datetime.datetime.now(tz=tz).strftime('%Y-%m-%d'))
- us_today = str(str("US_" + str(todays_date)))
- print("America/New_York :- Today's Date is ..." + str(todays_date))
- ###################################################################
- tz = pytz.timezone('Australia/Sydney')
- todays_date = str(datetime.datetime.now(tz=tz).strftime('%Y-%m-%d'))
- aus_today = str(str("AUS_" + str(todays_date)))
- print("Australia/Sydney :- Today's Date is ..." + str(todays_date))
- ###################################################################
- tz = pytz.timezone('Asia/Tokyo')
- todays_date = str(datetime.datetime.now(tz=tz).strftime('%Y-%m-%d'))
- jp_today = str(str("JP_" + str(todays_date)))
- print("Asia/Tokyo :- Today's Date is ..." + str(todays_date))
- ###################################################################
- b_ger_hol = False
- b_uk_hol = False
- b_us_hol = False
- b_aus_hol = False
- b_jp_hol = False
- for date, name in sorted(holidays.DE(years=YEAR_var).items()):
- full_hol_list.append(str("GER_" + str(date)))
- # print("###########################")
- for date, name in sorted(holidays.UK(years=YEAR_var).items()):
- full_hol_list.append(str("GB_" + str(date)))
- # print("###########################")
- for date, name in sorted(holidays.US(years=YEAR_var).items()):
- full_hol_list.append(str("US_" + str(date)))
- # print("###########################")
- for date, name in sorted(holidays.AU(years=YEAR_var).items()):
- full_hol_list.append(str("AUS_" + str(date)))
- # print("###########################")
- for date, name in sorted(holidays.JP(years=YEAR_var).items()):
- full_hol_list.append(str("JP_" + str(date)))
- # print("###########################")
- full_hol_list = sorted(full_hol_list)
- for d in full_hol_list:
- # print (d)
- if str(d) == ger_today:
- b_ger_hol = True
- if str(d) == gb_today:
- b_uk_hol = True
- if str(d) == us_today:
- b_us_hol = True
- if str(d) == aus_today:
- b_aus_hol = True
- if str(d) == jp_today:
- b_jp_hol = True
- for epic_id in main_epic_ids:
- tmp_lst = []
- base_url = REAL_OR_NO_REAL + '/markets/' + epic_id
- auth_r = requests.get(base_url, headers=authenticated_headers)
- d = json.loads(auth_r.text)
- try:
- i_count = i_count + 1
- if epic_id.find('MXN') != -1:
- # print("!!DEBUG!!...skipping, FOUND MXN in..." + str(epic_id))
- time.sleep(1)
- elif epic_id.find('SEK') != -1:
- # print("!!DEBUG!!...skipping, FOUND SEK in..." + str(epic_id))
- time.sleep(1)
- elif epic_id.find('NOK') != -1:
- # print("!!DEBUG!!...skipping, FOUND NOK in..." + str(epic_id))
- time.sleep(1)
- elif epic_id.find('CNH') != -1:
- # print("!!DEBUG!!...skipping, FOUND CNH in..." + str(epic_id))
- time.sleep(1)
- else:
- b_TRADE_OK = False
- while True:
- ###################EUROPE############################
- ###################EUROPE############################
- ###################EUROPE############################
- tz = pytz.timezone('Europe/Berlin')
- now_time = datetime.datetime.now(tz=tz).strftime('%H:%M')
- # print ("!!DEBUG!! Europe/Berlin:" + str(now_time))
- if is_between(str(now_time), ("08:00", "16:00")):
- # print("!!DEBUG!!...FRANKFURT MARKET OPEN!!")
- STR_CHECK = "EUR"
- if STR_CHECK in epic_id and b_ger_hol == False:
- time.sleep(2)
- b_TRADE_OK = True
- break
- ###################LONDON############################
- ###################LONDON############################
- ###################LONDON############################
- tz = pytz.timezone('Europe/London')
- now_time = datetime.datetime.now(tz=tz).strftime('%H:%M')
- while True:
- if is_between(str(now_time), ("22:00", "22:59")):
- time.sleep(1) # Sleeping for the tally up hour
- print("!!DEBUG!! Tally Up hour:" + str(now_time))
- now_time = datetime.datetime.now(
- tz=tz).strftime('%H:%M')
- else:
- break
- # print ("!!DEBUG!! Europe/London:" + str(now_time))
- if is_between(str(now_time), ("08:00", "16:00")):
- # print("!!DEBUG!!...LONDON MARKET OPEN!!")
- STR_CHECK = "GBP"
- if STR_CHECK in epic_id and b_uk_hol == False:
- time.sleep(2)
- b_TRADE_OK = True
- break
- ###################NY############################
- ###################NY############################
- ###################NY############################
- tz = pytz.timezone('America/New_York')
- now_time = datetime.datetime.now(tz=tz).strftime('%H:%M')
- # print ("!!DEBUG!! America/New_York:" + str(now_time))
- if is_between(str(now_time), ("08:00", "16:00")):
- # print("!!DEBUG!!...NEW YORK MARKET OPEN!!")
- STR_CHECK = "USD"
- if STR_CHECK in epic_id and b_us_hol == False:
- time.sleep(2)
- b_TRADE_OK = True
- break
- ###################AUS############################
- ###################AUS############################
- ###################AUS############################
- tz = pytz.timezone('Australia/Sydney')
- now_time = datetime.datetime.now(tz=tz).strftime('%H:%M')
- # print ("!!DEBUG!! Australia/Sydney:" + str(now_time))
- if is_between(str(now_time), ("08:00", "16:00")):
- # print("!!DEBUG!!...SYDNEY MARKET OPEN!!")
- STR_CHECK = "AUD"
- if STR_CHECK in epic_id and b_aus_hol == False:
- time.sleep(2)
- b_TRADE_OK = True
- break
- ###################TOKYO############################
- ###################TOKYO############################
- ###################TOKYO############################
- tz = pytz.timezone('Asia/Tokyo')
- now_time = datetime.datetime.now(tz=tz).strftime('%H:%M')
- # print ("!!DEBUG!! Asia/Tokyo:" + str(now_time))
- if is_between(str(now_time), ("08:00", "16:00")):
- # print("!!DEBUG!!...TOKYO MARKET OPEN!!")
- STR_CHECK = "JPY"
- if STR_CHECK in epic_id and b_jp_hol == False:
- time.sleep(2)
- b_TRADE_OK = True
- break
- break
- if b_TRADE_OK:
- current_bid = d['snapshot']['bid']
- ask_price = d['snapshot']['offer']
- spread = float(current_bid) - float(ask_price)
- if float(spread) >= -2:
- pick_from_epics.append(epic_id)
- print("!!DEBUG!!...FOUND GOOD EPIC..." +
- str(i_count) + "/" + str(len(main_epic_ids)))
- time.sleep(1)
- else:
- print(
- "!!DEBUG!!...skipping, NO GOOD EPIC....Checking next epic spreads..." +
- str(i_count) +
- "/" +
- str(
- len(main_epic_ids)))
- time.sleep(1)
- continue
- except Exception as e:
- print(e)
- pass
- # sorted_list = sorted(spreads_and_epics, key=operator.itemgetter(1))
- # for i in range(len(sorted_list)):
- # print(sorted_list[i])
- # print (max(sorted_list, key=lambda x: x[1]))
- # lowest_epic = max(sorted_list, key=lambda x: x[1])
- # return (str(lowest_epic[0]))
- return (pick_from_epics)
- def are_we_going_to_trade(epic_id, TRADE_DIRECTION, limit):
- if TRADE_DIRECTION == "NONE":
- return None
- limitDistance_value = str(limit)
- ##########################################################################
- print(
- "Order will be a " +
- str(TRADE_DIRECTION) +
- " Order, With a limit of: " +
- str(limitDistance_value))
- print(
- "stopDistance_value for " +
- str(epic_id) +
- " will bet set at " +
- str(stopDistance_value))
- ##########################################################################
- # MAKE AN ORDER
- base_url = REAL_OR_NO_REAL + '/positions/otc'
- data = {
- "direction": TRADE_DIRECTION,
- "epic": epic_id,
- "limitDistance": limitDistance_value,
- "orderType": orderType_value,
- "size": size_value,
- "expiry": expiry_value,
- "guaranteedStop": guaranteedStop_value,
- "currencyCode": currencyCode_value,
- "forceOpen": forceOpen_value,
- "stopDistance": stopDistance_value}
- r = requests.post(
- base_url,
- data=json.dumps(data),
- headers=authenticated_headers)
- print("-----------------DEBUG-----------------")
- print("#################DEBUG#################")
- print(r.status_code)
- print(r.reason)
- print(r.text)
- print("-----------------DEBUG-----------------")
- print("#################DEBUG#################")
- d = json.loads(r.text)
- deal_ref = d['dealReference']
- time.sleep(1)
- # CONFIRM MARKET ORDER
- base_url = REAL_OR_NO_REAL + '/confirms/' + deal_ref
- auth_r = requests.get(base_url, headers=authenticated_headers)
- d = json.loads(auth_r.text)
- DEAL_ID = d['dealId']
- print("DEAL ID : " + str(d['dealId']))
- print(d['dealStatus'])
- print(d['reason'])
- if str(d['reason']) != "SUCCESS":
- print("!!DEBUG!!...!!some thing occurred ERROR!!")
- time.sleep(10)
- print("-----------------DEBUG-----------------")
- print("#################DEBUG#################")
- print("!!DEBUG!! Resuming...")
- print("-----------------DEBUG-----------------")
- print("#################DEBUG#################")
- else:
- print("-----------------DEBUG-----------------")
- print("#################DEBUG#################")
- print("!!DEBUG!!...Yay, ORDER OPEN")
- time.sleep(10)
- print("-----------------DEBUG-----------------")
- print("#################DEBUG#################")
- def Chandelier_Exit_formula(TRADE_DIR, ATR, Price):
- # Chandelier Exit (long) = 22-day High - ATR(22) x 3
- # Chandelier Exit (short) = 22-day Low + ATR(22) x 3
- if TRADE_DIR == "BUY":
- return float(Price) - float(ATR) * 3
- elif TRADE_DIR == "SELL":
- return float(Price) + float(ATR) * 3
- def calculate_stop_loss(d):
- price_ranges = []
- closing_prices = []
- first_time_round_loop = True
- TR_prices = []
- price_compare = "bid"
- for i in d['prices']:
- if first_time_round_loop:
- ###########################################
- # First time round loop cannot get previous
- ###########################################
- closePrice = i['closePrice'][price_compare]
- closing_prices.append(closePrice)
- high_price = i['highPrice'][price_compare]
- low_price = i['lowPrice'][price_compare]
- price_range = float(high_price - closePrice)
- price_ranges.append(price_range)
- first_time_round_loop = False
- else:
- prev_close = closing_prices[-1]
- ###########################################
- closePrice = i['closePrice'][price_compare]
- closing_prices.append(closePrice)
- high_price = i['highPrice'][price_compare]
- low_price = i['lowPrice'][price_compare]
- price_range = float(high_price - closePrice)
- price_ranges.append(price_range)
- TR = max(high_price - low_price,
- abs(high_price - prev_close),
- abs(low_price - prev_close))
- TR_prices.append(TR)
- return str(int(float(max(TR_prices))))
- if __name__ == '__main__':
- while True:
- try:
- base_url = REAL_OR_NO_REAL + "/accounts"
- auth_r = requests.get(base_url, headers=authenticated_headers)
- d = json.loads(auth_r.text)
- print("-----------------DEBUG-----------------")
- print("#################DEBUG#################")
- print(auth_r.status_code)
- print(auth_r.reason)
- print(auth_r.text)
- print("-----------------DEBUG-----------------")
- print("#################DEBUG#################")
- for i in d['accounts']:
- if str(i['accountType']) == "SPREADBET":
- balance = i['balance']['balance']
- deposit = i['balance']['deposit']
- percent_used = percentage(deposit, balance)
- print("-----------------DEBUG-----------------")
- print("#################DEBUG#################")
- print(
- "!!DEBUG!!...Percent of account used ..." +
- str(percent_used))
- print("-----------------DEBUG-----------------")
- print("#################DEBUG#################")
- if float(percent_used) > 70:
- print("!!DEBUG!!...Don't trade, Too much margin used up already")
- time.sleep(60)
- continue
- else:
- print("!!DEBUG!!...OK to trade, Account balance OK!!")
- tradeable_epic_ids = find_good_epics()
- shuffle(tradeable_epic_ids)
- except Exception as e:
- print(e)
- print("!!DEBUG!!...No Suitable Epics...Yet!!, Try again!!")
- continue
- for epic_id in tradeable_epic_ids:
- base_url = REAL_OR_NO_REAL + "/prices/" + epic_id + "/MINUTE_15/16"
- # Price resolution (MINUTE, MINUTE_2, MINUTE_3, MINUTE_5,
- # MINUTE_10, MINUTE_15, MINUTE_30, HOUR, HOUR_2, HOUR_3,
- # HOUR_4, DAY, WEEK, MONTH)
- auth_r = requests.get(base_url, headers=authenticated_headers)
- d = json.loads(auth_r.text)
- # print("-----------------DEBUG-----------------")
- # print("#################DEBUG#################")
- # print(auth_r.status_code)
- # print(auth_r.reason)
- # print(auth_r.text)
- # print("-----------------DEBUG-----------------")
- # print("#################DEBUG#################")
- high_prices = []
- low_prices = []
- mid_prices = []
- close_prices = []
- ATR = calculate_stop_loss(d)
- remaining_allowance = d['allowance']['remainingAllowance']
- reset_time = humanize_time(int(d['allowance']['allowanceExpiry']))
- print("-----------------DEBUG-----------------")
- print("#################DEBUG#################")
- print("Remaining API Calls left: " + str(remaining_allowance))
- print("Time to API Key reset: " + str(reset_time))
- print("-----------------DEBUG-----------------")
- print("#################DEBUG#################")
- for i in d['prices']:
- if i['highPrice']['bid'] is not None:
- highPrice = i['highPrice']['bid']
- high_prices.append(highPrice)
- ########################################
- if i['lowPrice']['bid'] is not None:
- lowPrice = i['lowPrice']['bid']
- low_prices.append(lowPrice)
- ########################################
- if i['closePrice']['bid'] is not None:
- closePrice = i['lowPrice']['bid']
- close_prices.append(closePrice)
- ########################################
- mid_prices.append(float(midpoint(highPrice, lowPrice)))
- close_prices = numpy.asarray(close_prices)
- low_prices = numpy.asarray(low_prices)
- high_prices = numpy.asarray(high_prices)
- mid_prices = numpy.asarray(mid_prices)
- xi = numpy.arange(0, len(low_prices))
- # close_prices_slope, close_prices_intercept, close_prices_r_value, close_prices_p_value, close_prices_std_err = stats.linregress(
- # xi, close_prices)
- # low_prices_slope, low_prices_intercept, low_prices_r_value, low_prices_p_value, low_prices_std_err = stats.linregress(
- # xi, low_prices)
- # high_prices_slope, high_prices_intercept, high_prices_r_value, high_prices_p_value, high_prices_std_err = stats.linregress(
- # xi, high_prices)
- # mid_prices_slope, mid_prices_intercept, mid_prices_r_value, mid_prices_p_value, mid_prices_std_err = stats.linregress(
- # xi, mid_prices)
- close_prices_slope, close_prices_intercept, close_prices_lo_slope, close_prices_hi_slope = stats.theilslopes(
- close_prices, xi)
- low_prices_slope, low_prices_intercept, low_prices_lo_slope, low_prices_hi_slope = stats.theilslopes(
- low_prices, xi)
- high_prices_slope, high_prices_intercept, high_prices_lo_slope, high_prices_hi_slope = stats.theilslopes(
- high_prices, xi)
- mid_prices_slope, mid_prices_intercept, mid_prices_lo_slope, mid_prices_hi_slope = stats.theilslopes(
- mid_prices, xi)
- # print ("#####################################################")
- # print ("#####################################################")
- # print ("#####################################################")
- # print ("Please uncomment this to enable graphs for debugging")
- # print ("#####################################################")
- # print ("#####################################################")
- # print ("#####################################################")
- # close_prices_line = close_prices_slope * xi + close_prices_intercept
- # low_prices_line = low_prices_slope * xi + low_prices_intercept
- # high_prices_line = high_prices_slope * xi + high_prices_intercept
- # mid_prices_line = mid_prices_slope * xi + mid_prices_intercept
- # plt.plot(
- # xi,
- # low_prices_line,
- # xi,
- # high_prices_line,
- # xi,
- # close_prices,
- # 'g--',
- # xi,
- # mid_prices_line)
- # plt.fill_between(
- # xi,
- # low_prices_line,
- # high_prices_line,
- # facecolor='green',
- # alpha=0.5)
- # plt.xlabel('X Axis')
- # plt.ylabel('Y Axis')
- # plt.show()
- # plt.clf()
- # print ("#####################################################")
- # print ("#####################################################")
- # print ("#####################################################")
- # print ("Please uncomment this to enable graphs for debugging")
- # print ("#####################################################")
- # print ("#####################################################")
- # print ("#####################################################")
- base_url = REAL_OR_NO_REAL + '/markets/' + epic_id
- auth_r = requests.get(
- base_url, headers=authenticated_headers)
- d = json.loads(auth_r.text)
- # print("-----------------DEBUG-----------------")
- # print("#################DEBUG#################")
- # print(auth_r.status_code)
- # print(auth_r.reason)
- # print(auth_r.text)
- # print("-----------------DEBUG-----------------")
- # print("#################DEBUG#################")
- current_bid = d['snapshot']['bid']
- # 2 Standard Deviation
- std_dev_BUY = float(mid_prices_intercept +
- (numpy.std(mid_prices) * 2))
- # 2 Standard Deviation
- std_dev_SELL = float(mid_prices_intercept -
- (numpy.std(mid_prices) * 2))
- if float(current_bid) > float(mid_prices_intercept) and float(
- current_bid) > float(std_dev_BUY):
- TRADE_DIRECTION = "BUY"
- pip_limit = int(abs(float(max(high_prices)) -
- float(current_bid)) / SL_MULTIPLIER)
- print("-----------------DEBUG-----------------")
- print("#################DEBUG#################")
- print("!!DEBUG!!...BUY!!")
- print(str(epic_id))
- print(
- "!!DEBUG!!...Take Profit@...." +
- str(pip_limit) +
- " pips")
- print("-----------------DEBUG-----------------")
- print("#################DEBUG#################")
- ce_stop = Chandelier_Exit_formula(
- TRADE_DIRECTION, ATR, min(low_prices))
- tmp_stop = int(abs(float(current_bid) - (ce_stop)))
- elif float(current_bid) < float(mid_prices_intercept) and float(current_bid) < float(std_dev_SELL):
- TRADE_DIRECTION = "SELL"
- pip_limit = int(abs(float(min(low_prices)) -
- float(current_bid)) / SL_MULTIPLIER)
- print("-----------------DEBUG-----------------")
- print("#################DEBUG#################")
- print("!!DEBUG!!...SELL!!")
- print(str(epic_id))
- print(
- "!!DEBUG!!...Take Profit@...." +
- str(pip_limit) +
- " pips")
- print("-----------------DEBUG-----------------")
- print("#################DEBUG#################")
- ce_stop = Chandelier_Exit_formula(
- TRADE_DIRECTION, ATR, max(high_prices))
- tmp_stop = int(abs(float(current_bid) - (ce_stop)))
- else:
- pip_limit = 9999999
- tmp_stop = "999999"
- TRADE_DIRECTION = "NONE"
- print("!!DEBUG!!...Within trading range...Leave!!")
- if int(pip_limit) <= 1:
- print("!!DEBUG!!...No Trade!!, Pip Limit Under 1")
- TRADE_DIRECTION = "NONE"
- stopDistance_value = str(tmp_stop)
- if int(stopDistance_value) <= LOW_SL_WATERMARK or int(
- stopDistance_value) >= HIGH_SL_WATERMARK:
- TRADE_DIRECTION = "NONE"
- try:
- are_we_going_to_trade(epic_id, TRADE_DIRECTION, pip_limit)
- except Exception as e:
- print(e)
- print("Something fucked up!!, Try again!!")
- continue
Advertisement
Add Comment
Please, Sign In to add comment