This tutorial will create a scalper bot. The bot will enter a position based on a simple trend calculation and exit on either a stop loss or take profit.
First, we need to check what current direction is of our position. We only want to do something when the bot does not have an open position. Add the GetPositionDirection command. Right-click on the output dot and add both Equals and NoPosition. Connect both GetPositionDirection and NoPosition to Equals.
Next step is adding the trend detection. We are going to do this by looking if the price is dropping or raising with help of the IsFalling command. Add both a ClosePrices and IsFalling command. Connect the ClosePrices to the array parameter of IsFalling. Right-click on the dot next to lookback and add an Input. We are going to make the lookback an input field so we can change it later without updating the whole script. Give the Input a name and a default value
Remeber, we want only want to continue executing if there is no position. Right-click on the IsFalling block. A context menu will show and we are going to select Toggle connector. This will give the command an additional parameter called execute. When the result of the Equals command is false the execution will stop here. When the value is true, IsFalling and other attached commands are executed.
IsFalling will return true of the price is dropping and false when the price is raising. We are going to split the execution flow here with the Branch command. Right-click on the IsFalling output and add it. The Branch command has 2 outputs. isTrue and isFalse. When the result of IsFalling is true (price is dropping) we are going to open a long position. Right-click on isTrue and add DoLong. Conversely, when IsFalling is false (price is raising) we are going to open a short position. Right-click on isFalse and add the DoShort command.
We have now completed the entry logic.
The exit logic is a lot easier. We are going to make use of the build-in EasySafeties and add a StopLoss and TakeProfit. Both commands require a percentage input. Right-click on both dots and add an Input. Give them a name and default value.
Both commands will return true when their price has been breached or false when there is no position or when the price didn't breach the price yet. If one of the 2 outputs is true we are going to exit the position. Right-click on the output of TakeProfit and add an Or command. Connect the StopLoss output to the same input. Or will return true when any of the values is true.
The last command we need to add is the DoExitPosition. Right-click on the Or output and select DoExitPosition. This is the final step for the scalper script.
The script is now finished and we can run a few backtests to find the best parameters for the bot.