Python: while loop/ control statements not achieving desired result, how can i fix this? - loops

cost = int(input("Enter the cost:\n"))
payment = int(input("Deposit a coin or note:\n"))
if payment >= cost:
change = payment - cost
print (change)
elif payment < cost:
while payment < cost:
payment = int(input("Deposit a coin or note:\n"))
change = cost - payment
print (change)
break
I basically want the value of "change".
The above was wrong because it wasn't 'storing' the value of the first payment if it was less than cost, realized my bad mistake.
cost = int(input("Enter the cost:\n"))
payment = 0
while payment < cost:
firstpayment = int(input("Deposit a coin or note:\n"))
payment = payment + firstpayment
if payment > cost:
change = payment - cost
print ("Your change is: $",change)

The loop condition says:
while payment< cost:
So, the loop automatically exists when payment becomes more than cost. So an easier approach will be,
while payment<cost:
#do something
change = payment-cost
Coz, when the loop ends, we know that payment should be >= cost...

Related

Bybit API - How do I calculate qty in USDT Perpetual with leverage

I'm using bybit-api to create a conditional order but don't know how do I calculate quantity. Is it based on leveraged amount or original?
for example
I have balance of 50 USDT and want to use 100% per trade with following conditions.
BTC at price 44,089.50 with 50x leverage.
SHIB at price 0.030810 with 50x leverage.
How do I calculate the qty parameter?
https://bybit-exchange.github.io/docs/linear/#t-placecond
I trade Bitcoin through USDT Perpetuals (BTCUSDT). I've setup my own python API and created my own function to calculate quantity for cross margin:
def order_quantity(self, price:float, currency:str='USDT', leverage:float=50.0):
margin = self.get_wallet_balance(currency)
instrument = Instrument(self.query_instrument()[0], 'bybit')
if not price: # Market orders
last_trade = self.ws_get_last_trade() # private function to get last trade
lastprice = float(last_trade[-1]['price'])
else: # Limit orders
lastprice = price
totalbtc = float(margin[currency]['available_balance']) * (1 - instrument.maker_fee * leverage)
rawbtc = totalbtc / lastprice
btc = math.floor(rawbtc / instrument.lot_size) * instrument.lot_size
return min(btc,instrument.max_lot_size)
It is based on the leverage amount.
Your quantity should be :
qty = 50 USDT * 50 (leverage) / 44089 (BTC price) = 0.0567 BTC

How to make it roll multiple times

I am trying to be able to have a person specify how many sets of dice they want to roll. at present if they want to roll three sets of a d100 they have to enter the command 3 times. I want to be able to have them enter an amount like !3d100 and have it roll it 3 times?
#client.command(name='d100', help='Rolls a d100 sice')
async def dice(context):
diceEmbed = discord.Embed(title="Rolling for " + str(context.message.author.display_name), color=0xCC5500)
roll = (random.randint(1, 100))
url = "http://157.230.225.61/images/dice/d100/d100_{:03}.png"
url = url.format(roll)
diceEmbed.set_image(url=url)
diceEmbed.add_field(name="d100", value=roll, inline=True)
if roll == 1:
diceEmbed.set_footer(text="FUMBLE")
await context.message.channel.send(embed=diceEmbed)
First of all consider using f string. You will have to add a input to your command !dice 3 This will give you 3 rolls if no amount given only 1.
Keep in mind in embed you can only place one image so i took the highest according to you.
#bot.command()
async def dice(ctx, amount: int = 1):
diceEmbed = discord.Embed(title=f"Rolling for {ctx.message.author.display_name}", color=0xCC5500)
max_roll = 0
for i in range(amount):
roll = (random.randint(1, 100))
if roll > max_roll:
url = f"http://157.230.225.61/images/dice/d100/d100_{roll:03d}.png"
max_roll = roll
diceEmbed.add_field(
name=f"Role number {i+1}", value=roll, inline=False)
if roll == 1:
diceEmbed.set_footer(text="FUMBLE")
# You can have only one image which is the highest
diceEmbed.set_image(url=url)
await ctx.message.channel.send(embed=diceEmbed)

Is there a way in Amibroker backtest to specify buy price?

Is there a way to place buy order at a certain value?
I am trying to buy at the break of previous day high on a 5 min tf:
DayH = TimeFrameGetPrice("H", inDaily, -1); // yesterdays high
DayL = TimeFrameGetPrice("L", inDaily, -1); // low
DayC = TimeFrameGetPrice("C", inDaily, -1); // close
DayO = TimeFrameGetPrice("O", inDaily); // current day open
Buy = H >= DayH AND DayO < DayH;
BuyPrice = DayH;
But I am not able to get the buy price similar to yesterday's high, its always something more or less
Use SetTradeDelays()
Buy = High
SetTradeDelays(-1,-1,-1,-1);

Amibroker AFL first 15min candle High

I want to find high of the first 15 min candle.
I am using the below code.
bi = BarIndex();
arrayitem = SelectedValue(bi) -bi[0];
firstbarHigh = High[arrayitem ];
This code is giving me the CLOSE price for the 1st candle. I want High price of the first 15min candle.
Plz help me.
Check this out
newday = Day() != Ref(Day(),-1); //check if new day or not
starttime = ValueWhen(newday,TimeNum());
IBendtime = starttime+1500;
minh = ValueWhen(newday,TimeFrameGetPrice("H",in5Minute*3));
minl = ValueWhen(newday,TimeFrameGetPrice("L",in5Minute*3));

How to do a "while quantity is greater than zero loop"

So I am using a gift certificate module with satchmo store and in order to send multiple gift certificate codes equal to the number of items purchased I need to add a loop doing
"while quantity is greater than zero loop"
Here is the code, the loop is being added to right before "price=order_item.unit_price"
def order_success(self, order,
order_item):
log.debug("Order success called, creating gift certs on order:
%s", order)
message = ""
email = ""
for detl in order_item.orderitemdetail_set.all():
if detl.name == "email":
email = detl.value
elif detl.name == "message":
message = detl.value
price=order_item.unit_price
log.debug("Creating gc for %s", price)
gc = GiftCertificate(
order = order,
start_balance= price,
purchased_by = order.contact,
valid=True,
message=message,
recipient_email=email
)
gc.save()
I am not sure I understand the question, but maybe something like
for ix in range(0, order_item.quantity):
... do stuff
might do the trick. You don't have to use the ix anywhere inside the loop, it is just (arguably) the standard way to do something n times in Python.

Resources