python if语句:基于我的进入位置进行交易逻辑,然后根据操作设置变量



我正在努力从交易API中获取当前买卖价格,将其与我购买的职位进行比较,然后采取适当的措施 - 出售或购买

由于交易费用而有出售和购买价格

有5个组件:

  • 开始购买股票的价格
  • 股票的当前买价
  • 目前的股票卖出
  • 上一买价(我买了股票)
  • 以前的卖价(我在哪里出售股票)

我需要看看第一个价格变化是什么相对于我的起始位置,如果我可以买到较低,然后购买;如果我可以出售更多,请出售。但是我想重复很多次。

因此,如果我买了更多,请设置上一买价,然后如果当前的卖出价格上涨,那就出售。然后在购买方面相同

这是我需要帮助的逻辑:

  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!

我已经盯着这个问题了大约3个小时,试图在Python做到这一点。到目前为止,这是我的代码:

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()

我真的在这里挣扎,请有人可以帮助我将我的if我的if我的if转换为第一个缩进的文本块中的python代码,该代码可以多次运行

这四行是奇怪的:

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

您的前两个呼叫功能,但是参数似乎在任何地方都没有定义。您的第二个依赖于对前两个呼叫的响应都是字典,这在没有存在的论点的情况下很难。

您可能还想研究"循环"/",因为这些会有助于"很多次"。

我得到了它!

这是您给它以100英镑的价格购买的价格。然后,我将API称为寻找卖价。如果售价低于100英镑,请卖出并将以前的卖价设定为此。如果购买价格将属于以前的购买价格,请购买。然后移至下一个循环,仅在获得折扣或利润时仅执行买卖,并且只有在连续重复同一措施

的情况下,
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)

相关内容

最新更新