Trading is an emotional ordeal for the majority of those in the crypto space, especially considering the market is based so heavily on speculation rather than earnings reports, mergers, and more, like stocks. Algorithm trading lets you create a paper-proven strategy that runs like clockwork, letting you take time away from the screen and stop stressing about every button you click.
This is why stop losses are so important, as they let you rest, knowing that you wonât end up riding a trade down into oblivion. Stop losses set a certain price point at which you cut your losses and exit the trade, therefore limiting your downside to a safe and healthy level.
Firstly we need to connect to the exchange, in this case, Phemex, along with a config file that holds our API keys. Next, we will enable the rate limit and set it true, this lets us import our API keys. You donât need to import your keys from a file, though we do this simply to not expose the keys used in our tutorials.

Creating an Order
Next, we establish our market through the symbol command, entering âsymbol = âuBTCUSDââ. Obviously, you can use other markets but this is the one we will be using. Followed by size = 1 in the next line.

When creating an order with a stop loss, it has to be done differently compared to a regular order. When setting up parameters, we need to use the command sl_params. This is followed by a lot more code than used in regular orders which will be visible in the image to the left, rather than us typing it out here.
Where we insert the command TriggerType, We can use âByLastPriceâ which will use whatever the last price. Or we can use âByMarkPriceâ which is the spot price. Either works, though we generally recommend using ByLastPrice. Now we need to pull in our bid and ask price. At this stage in your algorithm trading journey, you will already know how to do this. If not you can check out our previous tutorials where we go over how to do this. To help keep this article short and sweet, we will pull in some examples for our code. Using an example bid of 36000, we can now get on to the next stage.

Triggering the stop loss
This is an important step in this tutorial as itâs a place where many people get stuck. The code you need to write is âstop_price = (bid * 10000) + 10000â Then âstop_trigger = stop_price + 20000â in the following line.The reason we need to do it like this is because we are using the command âpriceEpâ later on, which multiplies the relevant numbers by 10000. This means that the â+ 10000â is equivalent to adding 1 dollar to the price.

Creating a sell order
Now we are going to create our first order with our new code. Start with the standard code of âphemex.create_limit_sell_orderâ then pass in the data (symbol, size, ask). You also need to define the ask price right above, again you can view our other tutorials to see how to import this live from the market.

As youâll have seen in the images displayed, we are actually inserting the order before the stop loss which can be a bit hard to get your head around, but itâs just how itâs done with CCXT.
Furthermore, youâll see that when it comes to your âsideâ command, you will be writing âBuyâ, this is because you are exiting a short position, therefore committing a buy order.
Next, we just loop it through by making a custom order. We do this by putting in the code âstop = phemex.create_order(symbol, type=âlimitâ, side=âBuyâ, amount=size, price=stop_price, params=sl_params)
This is the Stop Loss itself so is placed right at the end of the code.

In Conclusion
Although Algo trading can be pretty confusing at first, once you learn the basics and get into the swing of things, it is much easier and less stressful in the long run. Fully removing the emotion and stress from trading lets you spend your time perfecting your strategies and profiting consistently. Additionally, once the code is created, itâs there forever, you just use the code you created however long ago.
Algorithm trading helps you push past competitors and provide you with the same strategies and methods as hedge funds. Learning to code is an essential skill to push through and provide a steady income stream from trading.
want to learn how to algo trade so you can remove all emotions from trading and automate it 100%? click below to join the free discord and then join the bootcamp to get started today.