Created
April 11, 2017 08:15
-
-
Save robcarver17/f73abc0455f25d3bcbc90959204d2e86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from syslogdiag.ourlogging import logger | |
from math import copysign | |
def contracts_trade(total_trades, pos_priced, pos_forward, rolling=False): | |
""" | |
Given actual contracts decide what to trade | |
Note at this stage contracts are identified in as PRICE, FORWARD | |
Any rogue contracts not in that space would have been signalled / weeded out by now | |
Note this function doesn't produce 'proper' rolls these would only be on the back of a force roll | |
Returns tuple - trade_priced, trade_forward, cancel_priced, cancel_forward | |
""" | |
log=logger() | |
assert type(total_trades) is int | |
assert type(pos_priced) is int | |
assert type(pos_forward) is int | |
if not total_trades==0.0: | |
if rolling: | |
### Rolling so reduce in near, increase in far | |
## Simplified | |
reducing=False | |
if total_trades<0 and pos_priced>0: | |
reducing=True | |
if total_trades>0 and pos_priced<0: | |
reducing=True | |
if reducing: | |
## Old contract | |
if abs(total_trades)>abs(pos_priced): | |
## do the closing part in old and the new part in new | |
closing_bit= - pos_priced | |
trade_priced = closing_bit | |
trade_forward= total_trades - closing_bit | |
log.info("Doing closing part of trade %f in old contract, new part %f in new contract" % (trade_priced, trade_forward)) | |
else: | |
## Do all in current | |
log.info("Doing closing trade %f in old contract, nothing in new contract" % total_trades) | |
trade_priced=total_trades | |
trade_forward=0 | |
else: | |
## New contract | |
## | |
log.info("Doing opening trade %f in new contract, nothing in old contract" % total_trades) | |
trade_priced=0 | |
trade_forward=total_trades | |
else: | |
## Not rolling so will only trade in current contract | |
## Check haven't screwed up and already trading forward | |
if not pos_forward==0: | |
errormsg="Not rolling but have positions in forward contract!!! Suggest you turn rolling on as we won't trade eithier contract!!! " | |
log.critical(errormsg) | |
return (0,0) | |
log.info("Not rolling; all trade of %f in priced contract" % total_trades) | |
trade_priced=total_trades | |
trade_forward=0 | |
else: | |
log.info("Nothing to do") | |
trade_priced=0 | |
trade_forward=0 | |
return (trade_priced, trade_forward) | |
if __name__=="__main__": | |
assert contracts_trade(7.0, 0.0, 0.0, 0.0, 5.0, 1.0, rolling=False)==(-1.0, 0.0, 0.0, 0.0) | |
assert contracts_trade(3.0, 0.0, 0.0, 0.0, 5.0, 1.0, rolling=False)==(1.0, 0.0, 0.0, 0.0) | |
assert contracts_trade(4.0, 0.0, 0.0, 0.0, 5.2, 1.0, rolling=False)==(0.0, 0.0, 0.0, 0.0) | |
try: | |
assert contracts_trade(4.0, 1.0, 0.0, 0.0, 5.2, 1.0, rolling=False)==(0.0, 0.0, 0.0, 0.0) | |
raise Exception("should be broken") | |
except: | |
print "expected exception" | |
assert contracts_trade(4.0, 0.0, 0.0, 0.0, 5.2, 1.0, rolling=True)==(0.0, 0.0, 0.0, 0.0) | |
assert contracts_trade(4.0, 0.0, 0.0, 0.0, 6.2, 1.0, rolling=True)==(0.0, 1.0, 0.0, 0.0) | |
assert contracts_trade(-10.0, 0.0, 0.0, 0.0, -3.2, 1.0, rolling=True)==(6.0, 0.0, 0.0, 0.0) | |
assert contracts_trade(-10.0, 0.0, 0.0, 0.0, 3.2, 1.0, rolling=True)==(10.0, 2.0, 0.0, 0.0) | |
assert contracts_trade(10.0, 0.0, 0.0, 0.0, -3.2, 1.0, rolling=True)==(-10.0, -2.0, 0.0, 0.0) | |
assert contracts_trade(2.0, 2.0, 0.0, 0.0, 6.2, 1.0, rolling=True)==(0.0, 1.0, 0.0, 0.0) | |
assert contracts_trade(-8.0, -2.0, 0.0, 0.0, -3.2, 1.0, rolling=True)==(6.0, 0.0, 0.0, 0.0) | |
assert contracts_trade(-6.0, 2.0, 0.0, 0.0, -1.0, 1.0, rolling=True)==(2.0, 0.0, 0.0, 0.0) | |
assert contracts_trade(-2.0, -4.0, 0.0, 0.0, -1.0, 1.0, rolling=True)==(2.0, 2.0, 0.0, 0.0) | |
assert contracts_trade(-5.0, 3.0, 0.0, 0.0, 2.0, 1.0, rolling=True)==(3.0, 0.0, 0.0, 0.0) | |
assert contracts_trade(2.0, 2.0, 0.0, 0.0, -1.0, 1.0, rolling=True)==(-2.0, -2.0, 0.0, 0.0) | |
assert contracts_trade(0.0, 2.0, 0.0, 0.0, -1.0, 1.0, rolling=True)==(0.0, -2.0, 0.0, 0.0) | |
## These should never happen, but ... | |
assert contracts_trade(3.0, -3.0, 0.0, 0.0, -2.0, 1.0, rolling=True)==(-1.0, 0.0, 0.0, 0.0) | |
assert contracts_trade(-3.0, 3.0, 0.0, 0.0, -2.0, 1.0, rolling=True)==(0.0, -1.0, 0.0, 0.0) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment