-1

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

2 Answers2

1

These four lines are odd:

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'])

Your first two call functions, but the arguments don't seem to be defined anywhere. Your second two rely on the responses to the first two calls both being dictionaries, which is difficult without the arguments existing.

You might also want to look into "While"/"For" loops, as these would help with the "lots and lots of times".

Peritract
  • 761
  • 5
  • 13
  • They come from connecting to the API, each time you run you'll get the latest buy or sell price. So what I want to do is constantly query them, then compare to what I bought in at, then if the current price is low, buy more; if the sell price is higher than what I bought in at, sell. THEN I have to do this not from the start buy price, but from the previous action - so whether I bought or sold – SuperSecretAndNotSafeFromWork Dec 11 '17 at 01:12
  • The for loop situation doesn't help me here (I looked into it), since we're just getting the very latest buy and sell prices (it's comes as JSON, i'm selecting purely the price and not any of the other elements) – SuperSecretAndNotSafeFromWork Dec 11 '17 at 01:13
0

I got it!

What this does is you give it the price you bought in at, £100. Then I call the API looking for the sell price. If the sell price goes under £100, sell and set previous sell price to that. If the buy price then falls under that previous, buy. Then move to the next loop and only execute a buy or sell if I get a discount or profit, and only if the same action isn't repeated the same time in a row

timeout = 15.0 

start_buy_price = 100

profit = 0

client = Client
get_buy_price = client.get_buy_price
get_sell_price = client.get_sell_price
buy_price = float(get_buy_price) 
sell_price = float(get_sell_price)

while (sell_price < start_buy_price):
    client = Client
    get_buy_price = client.get_buy_price
    get_sell_price = client.get_sell_price
    buy_price = float(get_buy_price)
    sell_price = float(get_sell_price)

action = 'Start Sell!'
previous_action = 'Sell'
previous_sell_price = sell_price
profit = sell_price-start_buy_price
pass

while action == 'Start Sell!':
    client = Client
    get_buy_price = client.get_buy_price
    get_sell_price = client.get_sell_price
    buy_price = get_buy_price['amount']
    sell_price = get_sell_price['amount']
    time.sleep(timeout)

    if float(buy_price) < float(previous_sell_price):
        action = 'Buy!'
        previous_buy_price = buy_price
        time.sleep(timeout)

while start_buy_price > 0:
    client = Client()
    get_buy_price = client.get_buy_price
    get_sell_price = client.get_sell_price
    buy_price = get_buy_price['amount']
    sell_price = get_sell_price['amount']

    if float(buy_price) < float(previous_sell_price) and action == 'Sell!':
        action = 'Buy!'
        previous_buy_price = buy_price
        time.sleep(timeout)

    if float(sell_price) > float(previous_buy_price) and action == 'Buy!':
        action = 'Sell!'
        previous_sell_price = sell_price
        profit = profit + float(sell_price)-float(previous_buy_price)
        time.sleep(timeout)