hi , i made this bot of the logic that when the price of crypto falls lower or equal to previous low price in candle stick it should buy and when the prices increases as high as previous high or just more than what it had bought , it should liquidate portfolio.
- I am trading with USDTUSD pair .
- Most of the time the price increases by 0.0001 and falls at roughly to 0.0001 in 5min bar.
- i just want suggestion on how can i make my code more efficient or better than it's present condition.
# --------------------------
CRYPTO = "USDTUSD"; BAR = 5;
# --------------------------
class CryptoConsolidator(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 4, 1)
self.SetEndDate(2022, 4, 20)
self.SetCash(1000)
self.crypto = self.AddCrypto(CRYPTO, Resolution.Minute,Market.FTX).Symbol
self.Consolidate(self.crypto, timedelta(minutes = BAR), self.BarHandler)
self.entryPrice = 0
self.nextEntryTime = self.Time
self.period = timedelta(minutes = BAR)
def BarHandler(self, consolidated):
self.Plot("USDTUSD", self.crypto, self.Securities[self.crypto].Price)
def OnData(self, data):
hist = self.History(self.crypto , timedelta(BAR), Resolution.Hour)#365
low = min(hist["low"])
high = max(hist["high"])
if self.IsWarmingUp or not (self.nextEntryTime <= self.Time): return
price = float(self.Securities[self.crypto].Close)
if not self.Portfolio[self.crypto].Invested:
if price < low :
self.SetHoldings(self.crypto, 1.00)
#self.entryPrice = price
elif self.Portfolio[self.crypto].Invested:
if price >= high :
self.Liquidate(self.crypto, "Take Profit")
#self.entryPrice = price
self.nextEntryTime = self.Time + self.period
Blackpanther
Fred Painchaud HI can u look into this code , i want to set this bot such that when the price is low or equal to the historic low price it will buy , but i also want to add that when people put a lot of sell order the bot should know that it's time to buy and sell when people are buying more.Can u tell me how can i do it ?
Fred Painchaud
Hi Blackpanther (✊🏾),
Sure. Please let me confirm your specs first, mind you?
1- “when the price is low or equal to the historic low price it will buy”: you want to use the lowest low over which period of time? It looks like it is over the last 5 days? Is that what you want? Or do you think/want that your algo looks over the last 5 hours?
2- “also want to add that when people put a lot of sell order the bot should know that it's time to buy and sell when people are buying more”: by putting a lot of sell orders, you probably mean that the volume increases but the price goes down, exact? If so, the fundamental issue with that is that you are trading a stablecoin which is not traded on a single market, or at least on one major market where a very very high % of the total world volume is to be found. So volume on FTX is going to be just that, volume on FTX. Volume can go up or down on FTX but in another direction at the same time on Binance, on Coinbase, etc, etc, etc. Moreover, even with traditional equities like AAPL:NASDAQ where volume is somewhat significative, “sell volume” and “buy volume” is a big approximation in lieu of having access to level 2 data with some deep penetration. But well, now that you know the limitations, you can still try it, but informed. So, are you saying you would like the bot to buy when volume is up and price is going down (selling more) and sell when volume is up and price is going up (buy more)?
Fred
Fred Painchaud
Oh and btw, you are targeting some major league here. USDTUSD is pure scalping. Backtest vs live can be very, very different.
Fred
Blackpanther
Fred Painchaud Hi , i thought i had set the historical price at 5min , and yes u are right usdt doesn't get traded on big volumes but that's what is good in it the coin falls and rises up again and again .
the price fluctuation is stable so what i wanted that when the price is low same as it was before 5min or more i will execute my orders. I know the market is different in outside world but the pattern is same buy low sell high and the usdt is stable most of the time , it is very much predictable than any other coins.
Fred Painchaud
Hi,
Ok. So 5 min period. Your current period is set to 5 days since the first parameter of timedelta is days so timedelta(5) is 5 days. So self.History(self.crypto , timedelta(BAR), Resolution.Hour) returns the hour bars over the last 5 days. I'll change all that.
I didn't say USDTUSD was traded on small volumes, I was saying it is not centrally traded, it is decentrally traded and as such, volume on one market does not mean much. USDTUSD daily trades at a volume > 70B, it's not small to me.
Yeah, buy low sell high applies, as it always does, the issue is to make it happen. Anyway…
You're writing something different now: “when the price is low same as it was before 5min”. Do you want to compare the current price to the price 5 min ago or do you want to compare it to the minimum price over the last 5 mins? I guess it is to the minimum price over the last 5 minutes but confirming since I will work on that in a couple of hours anyway so there's time.
Fred
Fred Painchaud
Hi,
Attached backtest is one version using the previous 1m close to gauge selling high volume and buying high volume.
I also illustrated with the sma of the close over the last 5m as a gauge. That other version is here:
Same performance, i.e., not very good - which is expected. You'll need much much more initial investment in infra and contacts to scalp thousandths of cents.
And another version where you buy when volume and price is up and sell when volume is up and price is down:
This is NOT financial advice.
Fred
Blackpanther
Fred Painchaud hi , i read your msg just now , sorry for the delay . what i want is that when the price hits the previous low or is near to it buy and when the price increases upto previous high or near to it then sell.
Fred Painchaud
Hello,
That's what I did on top of your volume signals discussed above.
“Previous low” is the low over the last 5 minutes. “Previous high” is the high over the last 5 minutes.
High volume is modelled as two standard deviations over the mean. Sell and Buy volume are modelled in different ways (see above).
Fred
Blackpanther
Fred Painchaud hi Fred thanks for your reply , i was thinking if candle stick pattern could help in this , If u think they could be useful then tell me ?
Fred Painchaud
Hi Blackpanther,
I'm really sorry to burst your bubble but I would recommend you to look elsewhere in terms of strategy. The issue you are facing is the necessary speed you need to have to both “buy low” and “sell high”. And the fact that the orderbook has already 8M waiting in queue to buy at 1.0000 and 19M to sell at 1.0001. And I'm not mentioning the eventual fees.
Fred
Blackpanther
Fred Painchaud i will search a strategy which will be perfect for my idea , thanks for all the help really appreciate it :)
Blackpanther
Fred Painchaud hi just last question after that I will close the discussion , how can i store price of last candle into variable which will update itself ?
Fred Painchaud
Hi Blackpanther,
Cool, good luck.
If you want the price of the last 1m candle (since you are on minute candles above), self.prev_price in my code above is already the price of the previous candle - and it updates itself every minute. However, your algo was trading only once every 5 minutes so it still only trades once every 5 minutes.
To keep only one previous price, you simply need a variable and you update it in OnData, after using it (of course, as if you update it before using it, you won't be using the previous price but the current one).
Fred
Blackpanther
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!