I'm working on getting the current buy and sell price from a Trading API, compare it to the position I bought in at, then take the appropriate action - sell or buy
There are sell and buy prices due to transaction fees
There are 5 components:
- Start buy price where I got the stock
- Current buy price for the stock
- Current sell price for the stock
- Previous buy price (where I bought the stock)
- Previous sell price (where I sold the stock at)
I need to see what the first price change is relative to my start position, if I can buy lower, then buy; if I can sell it for more, sell. But I want to repeat lots and lots of times.
So if I bought more, set the previous buy price, then if current sell price rises above that, sell. Then the same on the buy side
This is the logic I need help with putting into if statements:
Start_buy_price = 100
Start scenario 1:
# buy price from start price drops to 90
buy_price = 90:
buy_price < start_buy_price:
buy!
set previous_buy_price = buy_price
# price to sell then rises to 95
sell_price = 95
sell_price > previous_buy_price:
Sell!
set previous_sell_price = sell_price
Start scenario 2:
# price to sell rises to 105
sell_price = 105
sell_price > start_buy_price:
Sell!
set previous_sell_price = sell_price
# price to buy then falls to 100
buy_price = 100
buy_price < previous_sell_price:
Buy!
set previous_buy_price = buy_price
----------------------------------------------------------------------
Following run through of the code:
Following scenario 1:
# price to buy is lower than previous sell price
buy_price < previous_sell_price:
Buy!
set previous_buy_price = buy_price
# price to sell is higher than previous buy price
sell_price < previous_buy_price:
Sell!
set previous_sell_price = sell_price
repeat!
I've been staring at this for about 3 hours now, trying to do it in Python. Here is my code so far:
def run_this_code():
start_buy_price = 100 # enter what I bought in at
get_buy_price = client.get_buy_price(buy_price)
get_sell_price = client.get_sell_price(sell_price)
buy_price = float(get_buy_price['amount'])
sell_price = float(get_sell_price['amount'])
if buy_price < start_buy_price:
action = 'Buy!'
previous_buy_price = buy_price
if sell_price > start_buy_price:
action = 'Sell!'
previous_sell_price = sell_price
if previous_buy_price !=None:
if sell_price > previous_buy_price:
action = 'Sell!'
previous_sell_price = sell_price
if previous_sell_price !=None:
if buy_price < previous_sell_price:
action = 'Buy!'
previous_buy_price = buy_price
print(action,'start_buy:', start_buy_price,'buy:', buy_price,'sell:', sell_price)
pass
run_this_code()
I'm really struggling here, please can someone help convert my if statements in the first indented text block, into python code that can be run multiple times