Python if 语句:交易逻辑基于我的入场位置,然后基于操作设置变量
Python if statements: Trading logic based on my entry position, then setting variables based on action
我正在努力从交易 API 中获取当前买卖价格,将其与我买入的头寸进行比较,然后采取适当的行动 - 卖出或买入
由于交易费用,有卖出价和买入价
有 5 个组成部分:
- 我得到股票的起始买入价
- 股票的当前买入价
- 股票的当前卖出价
- 之前的买入价(我买入股票的位置)
- 之前的卖出价(我卖出股票的价格)
我需要看看相对于我的起始位置的第一个价格变化是多少,如果我可以买低,然后买;如果我能卖得更多,卖掉。但我想重复很多很多次。
因此,如果我买入更多,设置之前的买入价,然后如果当前卖出价高于该价,则卖出。那么买方也一样
这是我需要帮助放入 if 语句的逻辑:
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 语句转换为 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'])
你的前两个调用函数,但参数似乎没有在任何地方定义。您的后两个依赖于对前两个调用的响应都是字典,如果不存在参数,这将很困难。
您可能还想查看 "While"/"For" 循环,因为它们有助于 "lots and lots of times"。
我明白了!
这是你给它买的价格,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)
我正在努力从交易 API 中获取当前买卖价格,将其与我买入的头寸进行比较,然后采取适当的行动 - 卖出或买入
由于交易费用,有卖出价和买入价
有 5 个组成部分:
- 我得到股票的起始买入价
- 股票的当前买入价
- 股票的当前卖出价
- 之前的买入价(我买入股票的位置)
- 之前的卖出价(我卖出股票的价格)
我需要看看相对于我的起始位置的第一个价格变化是多少,如果我可以买低,然后买;如果我能卖得更多,卖掉。但我想重复很多很多次。
因此,如果我买入更多,设置之前的买入价,然后如果当前卖出价高于该价,则卖出。那么买方也一样
这是我需要帮助放入 if 语句的逻辑:
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 语句转换为 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'])
你的前两个调用函数,但参数似乎没有在任何地方定义。您的后两个依赖于对前两个调用的响应都是字典,如果不存在参数,这将很困难。
您可能还想查看 "While"/"For" 循环,因为它们有助于 "lots and lots of times"。
我明白了!
这是你给它买的价格,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)