def test_sell_long(trader: Trader): """ Test trader sell long functionality. :param trader: Trader object. """ trader.currentPeriod = {'date_utc': 'test_date'} trader.currentPrice = 10 transaction_fee = trader.startingBalance * trader.transactionFeePercentageDecimal coin = (trader.startingBalance - transaction_fee) / trader.currentPrice trader.buy_long("test_buy") trader.currentPrice = 15 previous_transaction_fee = transaction_fee transaction_fee = coin * trader.currentPrice * trader.transactionFeePercentageDecimal balance = coin * trader.currentPrice - transaction_fee trader.sell_long("test_sell", stopLossExit=True) assert trader.smartStopLossEnter is False assert trader.stopLossExit is True assert trader.balance == balance assert trader.currentPosition is None assert trader.previousPosition == LONG assert trader.buyLongPrice is None assert trader.longTrailingPrice is None assert trader.commissionsPaid == transaction_fee + previous_transaction_fee assert trader.coin == 0 assert trader.trades[-1] == { 'date': 'test_date', 'action': 'test_sell', 'net': round(trader.get_net(), trader.precision) }
def test_buy_short(trader: Trader): """ Test trader buy short functionality. :param trader: Trader object. """ trader.currentPeriod = {'date_utc': 'test_date'} trader.currentPrice = 10 transaction_fee = trader.startingBalance * trader.transactionFeePercentageDecimal coin_owed = trader.startingBalance / trader.currentPrice trader.sell_short("test_short", smartEnter=True) trader.currentPrice = 5 previous_transaction_fee = transaction_fee transaction_fee = coin_owed * trader.currentPrice * trader.transactionFeePercentageDecimal balance = trader.balance - coin_owed * trader.currentPrice - transaction_fee trader.buy_short("test_end_short", stopLossExit=True) assert trader.stopLossExit is True assert trader.smartStopLossEnter is False assert trader.currentPosition is None assert trader.previousPosition == SHORT assert trader.sellShortPrice is None assert trader.shortTrailingPrice is None assert trader.balance == balance assert trader.commissionsPaid == previous_transaction_fee + transaction_fee assert trader.coin == 0 assert trader.coinOwed == 0 assert trader.trades[-1] == { 'date': 'test_date', 'action': 'test_end_short', 'net': round(trader.get_net(), trader.precision) }
def test_sell_short(trader: Trader): """ Test trader sell short functionality. :param trader: Trader object. """ trader.currentPeriod = {'date_utc': 'test_date'} trader.currentPrice = 10 transaction_fee = trader.startingBalance * trader.transactionFeePercentageDecimal coin_owed = trader.startingBalance / trader.currentPrice trader.sell_short("test_short") assert trader.smartStopLossEnter is False assert trader.stopLossExit is False assert trader.balance == trader.startingBalance * 2 - transaction_fee assert trader.currentPosition == SHORT assert trader.sellShortPrice == trader.currentPrice assert trader.shortTrailingPrice == trader.currentPrice assert trader.coin == 0 assert trader.coinOwed == coin_owed assert trader.commissionsPaid == transaction_fee assert trader.trades[-1] == { 'date': 'test_date', 'action': 'test_short', 'net': round(trader.get_net(), trader.precision) }
def test_get_net(trader: Trader): """ Test trader get net. """ trader.currentPeriod = {'date_utc': 'test_date'} trader.currentPrice = 100 trader.buy_long("test") trader.currentPrice = 200 assert trader.get_net() == trader.currentPrice * trader.coin trader.currentPrice = 50 assert trader.get_net() == trader.currentPrice * trader.coin trader.sell_long("end_buy_net") trader.startingBalance = trader.balance = 1000 trader.commissionsPaid = 0 trader.currentPrice = 20 trader.sell_short("test_short") assert trader.get_net() == trader.startingBalance - trader.commissionsPaid trader.currentPrice = 10 trader.buy_short("test_end_short") assert round(trader.get_net(), 2) == 1498.50 trader.startingBalance = trader.balance = 1000 trader.commissionsPaid = 0 trader.currentPrice = 5 trader.sell_short("test") trader.buy_short("test") assert trader.get_net() == 998
def test_add_trade(trader: Trader): """ Test trader add trade functionality. :param trader: Trader object. """ trader.currentPeriod = {'date_utc': 'test_date'} trader.currentPrice = 10 trader.add_trade(message="test_trade", stopLossExit=True, smartEnter=True) assert trader.stopLossExit is True assert trader.smartStopLossEnter is True assert trader.trades[-1] == { 'date': 'test_date', 'action': 'test_trade', 'net': trader.startingBalance }
def test_get_stop_loss(trader: Trader): """ Test trader get stop loss functionality. :param trader: Trader object. """ trader.lossStrategy = STOP trader.lossPercentageDecimal = 0.1 trader.currentPrice = 5 trader.currentPosition = LONG trader.buyLongPrice = 10 assert trader.get_stop_loss() == 10 * (1 - trader.lossPercentageDecimal) trader.currentPosition = SHORT trader.sellShortPrice = 10 assert trader.get_stop_loss() == 10 * (1 + trader.lossPercentageDecimal) trader.currentPosition = None assert trader.get_stop_loss() is None