Positions Handling
Positions in HaasScript
Intro
When orders are executed and completed the engine of HaasScript will maintain internal bot positions(s). The positions will increase or decrease depending on the order direction executed. The engine will keep track of fee costs, unrealized and realized profits as well as the profit loss ratio. Once the position is exited, it will store them and create a new one when an enter order has been completed.
HaasScript also offers the ability to separate positions based on an indentifier. This funtionallity is only available when trading with the unmanaged commands.
Position InformationManaged Trading
In a script that uses managed trading commands, the bot is restricted to 1 open position at all times. When the script is want to buy and the position reached the trade amount it will stop. Once a sell signal comes in it will start selling until the position is closed.
Commands like the ones in the easy insurance & safeties category and position information all take an optional positionId parameter. This can be left empty when trading managed. The bot will always have a single position so the id is redundant.
Partial Filled Orders
When a bot has completed a partially filled order, the filled amount is added or subtracts this to the position size. If the order was a buy, the position raises with the filled amount, if an order was a sell, the position would decrease with the filled amount. When the bot receives its next signal, it will either buy partially or sell the bought amount. If the bots trade amount is set on 1 BTC and the order was filled for 0.3 BTC, it would place a buy order of 0.7 BTC or sell order of 0.3 BTC.
Unmanaged Trading
By default unmanaged trading script will also have a single position per market which will in- or decrease depending on the order direction but you have also the ability to create multiple separate positions within a single script. This is very useful in for example a market making script. By separating the positions we can easily keep track of profit, size, calculate the stop loss or take profit price and much more.
Separating Positions
Creating separate positions can be done with the positionId parameter in the place order commands. We need to save and pass on a unique value for each position.
Requesting Information
Once our orders have been filled we can request information on those positions. Even though we have 1 position on the exchange with an average price of 9750, we have 2 separate positions internally. One with an enter price of 10.000 and one of 9.500.
Easy Safeties/Insurances
We can utilize the build-in safeties and insurances for each of the positions. With this we can, for example, keep track of 2 different TakeProfit levels.
Closing the Position
We can gracefully exit the position without known the exact amount. Using the PlaceExitPositionOrder in combination with the positionId allows you to easily close each position separately. This command supports both spot and margin/leverage trading.
Shorting Spot Markets
A script is running in a unmanaged trading enviroment can go into a short position on spot markets. When the bot received a finished sell order and it has no bought position (anymore), it will create a short one. Profits on the short position are realized when the bot buys back again. The amount the bot was in a short position, will not be used in the long position. An example on how the bot will manage the positions. Let's say we have 1 BTC & 1000 USD in our wallet the current price of Bitcoin is 1000 USD. Our wallet value is 2000 USD.
The bot receives a finished buy order of 1 BTC @ 1000 USD. A long position is created of 1 BTC with an enter price of 1000 USD.
Next, a finished sell orders is processed. The order was for 2 BTC on 2000 USD. The bot will close the long position of 1 BTC and open a short position of 1 BTC. The realized profits on the long position is 1000 USD.
After a while the bot was executed and finished a buy order of 2 BTC on 1000 USD. The bot closes the short position of 1 BTC again and creates a new long position of 1 BTC. The realized profits of the short position is 1000 USD.
After the last order we have 1 BTC is our wallet and 3000 USD. The current price of Bitcoin is 1000 USD. Our wallet value is 4000 USD and our profit is 2000 USD.
When a script is utilizing the managed trading signals, shorting spot markets is not possible.
Last updated