Skip to content

Instantly share code, notes, and snippets.

@bbennett36
Last active July 29, 2019 16:29
Show Gist options
  • Save bbennett36/ce0b99a2f4ef5ed9e63f1efd7ad082f3 to your computer and use it in GitHub Desktop.
Save bbennett36/ce0b99a2f4ef5ed9e63f1efd7ad082f3 to your computer and use it in GitHub Desktop.
Take Profit/Stop Loss Profitability Calculator for Trading Crypto
import pandas as pd
import numpy as np
def test_cutoffs(start_amount, start_price, buy_fee, sell_fee, end_price):
bought_fee = start_amount*buy_fee
bought_amt = (start_amount-bought_fee) / start_price
sold = bought_amt*end_price
sold_fee = sold*sell_fee
sold = sold-sold_fee
pl = sold-start_amount
return {'start_price': start_price, 'end_price': end_price, 'bought_fee':bought_fee, 'bought_amt': bought_amt, 'sold': sold, 'sold_fee': sold_fee, 'pl':pl}
# under_df would be your stop loss
# over_df would be your take profit
under_df = pd.DataFrame()
over_df = pd.DataFrame()
val_list = list()
# The amount you're willing to risk on the trade
invest = 10000
# don't change this
counter = 1
# the current price or price level you want to buy at
start_price = 310
# diff determines the min,max range to check. I make it a percentage of the starting price to keep it dynamic.
# 0.1 will be a smaller range. 0.5 would be appopriate if you need to look at larger ranges.
diff = start_price*0.1
# this determines the iterator value
iterator = 0.1
for test_val in np.arange( start_price-diff,start_price, iterator):
temp = pd.DataFrame()
result = test_cutoffs(invest,start_price, 0.0025, 0.0025, test_val)
temp['val'] = [test_val-start_price]
temp['invest'] = invest
temp['start_price'] = result['start_price']
temp['end_price'] = result['end_price']
temp['bought_fee'] = result['bought_fee']
temp['bought_amt'] = result['bought_amt']
temp['sold'] = result['sold']
temp['sold_fee'] = result['sold_fee']
temp['pl'] = result['pl']
if counter == 1:
under_df = temp
counter +=1
else:
under_df = under_df.append(temp)
counter = 1
for test_val in np.arange(start_price, start_price+diff, iterator):
temp = pd.DataFrame()
result = test_cutoffs(invest, start_price, 0.0025, 0.0025, test_val)
temp['val'] = [test_val-start_price]
temp['invest'] = invest
temp['start_price'] = result['start_price']
temp['end_price'] = result['end_price']
temp['bought_fee'] = result['bought_fee']
temp['bought_amt'] = result['bought_amt']
temp['sold'] = result['sold']
temp['sold_fee'] = result['sold_fee']
temp['pl'] = result['pl']
if counter == 1:
over_df = temp
counter +=1
else:
over_df = over_df.append(temp)
under_df = under_df.reset_index(drop=True)
over_df = over_df.reset_index(drop=True)
print(under_df.shape)
print(over_df.shape)
# displaying stop loss levels
under_df.tail()
# displaying only profitable TP levels
over_df[over_df['pl'] > 5].head()
@bbennett36
Copy link
Author

Image of the results - https://imgur.com/Tl4JF7B

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment