Skip to content

Commit

Permalink
But fixes, optimizations.
Browse files Browse the repository at this point in the history
Changes to be committed:
	modified:   Base/Conditional.ccxt
	modified:   Base/Conditional.mimic
	modified:   Base/Conditional.oanda
	modified:   Base/JackrabbitOliverTwist
	modified:   Base/Library/JRRccxt.py
	modified:   Base/Library/JRRmimic.py
	modified:   Base/Library/JRRoanda.py
	modified:   Base/Library/JackrabbitRelay.py
	modified:   Extras/otcWatch
	modified:   StartJackrabbit

Experimental (DO NOT USE)
	new file:   Base/JROT.ALPHA
	new file:   Base/JROT.SAFE
	new file:   Base/Library/CONDccxt.py
	new file:   Base/Library/CONDmimic.py
	new file:   Base/Library/CONDoanda.py
	new file:   Base/Library/ORPHANccxt.py
	new file:   Base/Library/ORPHANoanda.py
  • Loading branch information
rapmd73 committed Mar 13, 2024
1 parent 9a4ff5f commit 6447e62
Show file tree
Hide file tree
Showing 18 changed files with 2,057 additions and 115 deletions.
4 changes: 1 addition & 3 deletions Base/Conditional.ccxt
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,7 @@ def main():
FinishOrphan(Orphan['Key'],Orphan['lID'],Orphan['mID'],Orphan['Status'])
except Exception as e:
# Something went wrong
relay.JRLog.Write(f"{Orphan['Key']}: Code Error - {str(e)}",stdOut=False)
if 'Diagnostics' in relay.Active:
relay.JRLog.Write(f"{Orphan['Key']}: {data}",stdOut=False)
relay.JRLog.Write(f"{Orphan['Key']}: Code Error - {sys.exc_info()[-1].tb_lineno}/{str(e)}",stdOut=False)
FinishOrphan(Orphan['Key'],Orphan['lID'],Orphan['mID'],Orphan['Status'])

if __name__ == '__main__':
Expand Down
146 changes: 55 additions & 91 deletions Base/Conditional.mimic
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ def FinishOrphan(Key,lID,mID,State):
OliverTwistLock=JRRsupport.Locker("OliverTwist",ID=lID)
Memory=JRRsupport.Locker(Key,ID=mID)

OliverTwistLock.Lock()

State=State.lower()

if State!='delete':
# Return this order to a waiting state
Memory.Put(OliverTwistTimeout*100,"Waiting")
elif State=='delete':
# This order has been processed and needs to be removed from the system.
Memory.Put(OliverTwistTimeout*100,"Delete")
NewState='Waiting'
else:
NewState='Delete'

OliverTwistLock.Lock()
Memory.Put(OliverTwistTimeout*100,NewState)
OliverTwistLock.Unlock()

# We're done. This child has completed its task
print(Key,NewState)
sys.stdout.flush()
sys.exit(0)

# Get the order ID. If there isn't an ID, the order FAILED.
Expand Down Expand Up @@ -110,6 +110,8 @@ def main():
data=sys.stdin.read().strip()
Orphan=json.loads(data)

# JRRsupport.WriteFile(f"/tmp/{Orphan['ID']}.txt",json.dumps(Orphan)+'\n')

# Use Relay to process and validate the order, must be a string
if type(Orphan['Order']) is dict:
order=json.dumps(Orphan['Order'])
Expand All @@ -119,6 +121,10 @@ def main():
relay=JRR.JackrabbitRelay(framework=Orphan['Framework'],payload=order,NoIdentityVerification=True)
relay.JRLog.SetBaseName('OliverTwist')

###--->
relay.JRLog.Write(f"{Orphan['Key']}: {Orphan['lID']} {Orphan['mID']}",stdOut=False)
relay.JRLog.Write(f"{data}",stdOut=False)

try:
# Check to see if order is still open and return current state
# Handle OANDa's weird order id sequencing
Expand All @@ -134,8 +140,8 @@ def main():

# Process the position

# We need to check TakeProfit and StopLoss. If one of them is hit, we need to build and order and backfeed it in
# to Relay. It will place a new order.
# We need to check TakeProfit and StopLoss. If one of them is hit, we need to build and order and
# backfeed it in to Relay. It will place a new order.

# Get the direction of the trade, long/short
dir=relay.Order['Direction'].lower()
Expand All @@ -147,59 +153,13 @@ def main():
base=relay.Markets[relay.Order['Asset']]['base'].upper()
bal=relay.GetBalance(Base=base)

# if ticker is below price and spread, purge and re-purchase
"""
if abs(amount)>bal:
if "ConditionalRepurchase" in relay.Active:
relay.JRLog.Write(f"Original: {json.dumps(relay.Order)}",stdOut=False)
# Fsilsafe, in the WORST way possible. Do NOT leave a take profit out of the order. At this stage, the
# whole thing is an absolute nightmare to fix. The is a very brutal way of dealing with poor user
# choices.

# figure out direction and build replacement order
makeNewOrder=False
if dir=='long':
makeNewOrder=ticker['Bid']<(price-ticker['Spread'])
else:
makeNewOrder=ticker['Ask']>(price+ticker['Spread'])
if makeNewOrder==True:
# Copy original order
newOrder=relay.Order
newOrder['OliverTwist']='Repurchase'
"""
newOrder['Exchange']=relay.Order['Exchange']
newOrder['Account']=relay.Order['Account']
newOrder['Market']=relay.Order['Market']
newOrder['Asset']=relay.Order['Asset']
newOrder['Action']=relay.Order['SellAction']
newOrder['Price']=str(strikePrice)
newOrder['Base']=str(amount)
"""
if 'OrderType' in relay.Order:
newOrder['OrderType']=relay.Order['OrderType']
else:
newOrder['OrderType']='market'
relay.JRLog.Write(f"New: {json.dumps(newOrder)}",stdOut=False)
newOrder['Identity']=relay.Identity['Identity']
# Feed the new order to Relay
result=relay.SendWebhook(newOrder)
oid=GetOrderID(result)
if oid!=None:
# Repurchase was successful, remove this order
FinishOrphan(Orphan['Key'],Orphan['lID'],Orphan['mID'],'Delete')
else:
# Something went wrong, leave it and try again later
FinishOrphan(Orphan['Key'],Orphan['lID'],Orphan['mID'],Orphan['Status'])
else:
# Not time to make a new purchase, leave it
FinishOrphan(Orphan['Key'],Orphan['lID'],Orphan['mID'],'Delete')
else:
relay.JRLog.Write(f"{id}: Amount {amount:.8f} > Balance {bal:.8f} {base}, purge",stdOut=False)
FinishOrphan(Orphan['Key'],Orphan['lID'],Orphan['mID'],'Delete')
"""
# Fsilsafe, in the WORST way possible. Do NOT leave a take profit out of the order. At this stage, the whole thing is
# an absolute nightmare to fix. The is a very brutal way of dealing with poor user choices.
if 'TakeProfit' not in relay.Order:
relay.Order['TakeProfit']='2%'

# Calculate Take Profit
tp=round(CalculatePriceExit(relay.Order,'TakeProfit',dir,price),5)

Expand Down Expand Up @@ -241,39 +201,43 @@ def main():
strikePrice=ticker['Ask']
StrikeHappened=True

if StrikeHappened==True and abs(amount)>bal:
# Build "strike" order. TakeProfit or StopLoss has been triggered
newOrder={}
newOrder['OliverTwist']='Conditional'
newOrder['Exchange']=relay.Order['Exchange']
newOrder['Account']=relay.Order['Account']
newOrder['Market']=relay.Order['Market']
newOrder['Asset']=relay.Order['Asset']
newOrder['Action']=relay.Order['SellAction']
newOrder['Price']=str(strikePrice)
newOrder['Base']=str(amount)
if 'OrderType' in relay.Order:
newOrder['OrderType']=relay.Order['OrderType']
else:
newOrder['OrderType']='market'

# relay.JRLog.Write(f"{id}: {json.dumps(newOrder)}",stdOut=False)

newOrder['Identity']=relay.Active['Identity']

# Feed the new order to Relay
result=relay.SendWebhook(newOrder)
oid=GetOrderID(result)
if oid!=None:
relay.JRLog.Write(LogMSG,stdOut=False)
resp=relay.GetOrderDetails(id=oid,symbol=relay.Order['Asset'])
# Order must be closed as it succedded
newOrder['ID']=oid
relay.WriteLedger(Order=newOrder,Response=resp)
FinishOrphan(Orphan['Key'],Orphan['lID'],Orphan['mID'],'Delete')
if StrikeHappened==True:
if abs(amount)>bal:
# Build "strike" order. TakeProfit or StopLoss has been triggered
newOrder={}
newOrder['OliverTwist']='Conditional'
newOrder['Exchange']=relay.Order['Exchange']
newOrder['Account']=relay.Order['Account']
newOrder['Market']=relay.Order['Market']
newOrder['Asset']=relay.Order['Asset']
newOrder['Action']=relay.Order['SellAction']
newOrder['Price']=str(strikePrice)
newOrder['Base']=str(amount)
if 'OrderType' in relay.Order:
newOrder['OrderType']=relay.Order['OrderType']
else:
newOrder['OrderType']='market'

# relay.JRLog.Write(f"{id}: {json.dumps(newOrder)}",stdOut=False)

newOrder['Identity']=relay.Active['Identity']

# Feed the new order to Relay
result=relay.SendWebhook(newOrder)
oid=GetOrderID(result)
if oid!=None:
relay.JRLog.Write(LogMSG,stdOut=False)
resp=relay.GetOrderDetails(id=oid,symbol=relay.Order['Asset'])
# Order must be closed as it succedded
newOrder['ID']=oid
relay.WriteLedger(Order=newOrder,Response=resp)
FinishOrphan(Orphan['Key'],Orphan['lID'],Orphan['mID'],'Delete')
else:
# Give OliverTwist a response
relay.JRLog.Write(f"{id}: Order failed",stdOut=False)
FinishOrphan(Orphan['Key'],Orphan['lID'],Orphan['mID'],Orphan['Status'])
else:
# Give OliverTwist a response
relay.JRLog.Write(f"{id}: Order failed",stdOut=False)
# Amount < Balance
FinishOrphan(Orphan['Key'],Orphan['lID'],Orphan['mID'],Orphan['Status'])
else:
# Strike did not happen
Expand Down
5 changes: 1 addition & 4 deletions Base/Conditional.oanda
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import time
import JRRsupport
import JackrabbitRelay as JRR

import traceback

# Timeout before Locker auto-deletes this order result

OliverTwistTimeout=(15*60)
Expand Down Expand Up @@ -377,8 +375,7 @@ def main():
FinishOrphan(Orphan['Key'],Orphan['lID'],Orphan['mID'],'Delete')
except Exception as e:
# Something broke or went horrible wrong
ln=traceback.extract_tb(sys.exc_info()[2])[-1][1]
relay.JRLog.Write(f"{Orphan['Key']}: Code Error {ln} - {str(e)}",stdOut=False)
relay.JRLog.Write(f"{Orphan['Key']}: Code Error - {sys.exc_info()[-1].tb_lineno}/{str(e)}",stdOut=False)
FinishOrphan(Orphan['Key'],Orphan['lID'],Orphan['mID'],Orphan['Status'])

if __name__ == '__main__':
Expand Down
Loading

0 comments on commit 6447e62

Please sign in to comment.