Simple Take Profit and Stop Loss Example

Hi, I'm new here…is there any code example of TP and SL that we can use inside the blueshift IDE?Im pretty confuse of setting it with 'order_target'.

 

You can set a state variable to track the profit or loss on a particular position and put a condition to exit to implement a SL or TP monitors. One example is here. Here we have a state variable context.entry_levels to track the entry price for each positions. In the handle_exit function we fetch the current price and compute the move from entry price to check the profit on this position. If this is greater than the threshold, we exit. A similar approach can also implement stop-loss.

Thanx Sir, i'll study and update to you on my understandings…Thank you very much Sir. 

Hi Sir I cant seem to figure it out. These are the codes here. As you can see it is taken from Blueshift RSI strategy template. I'm pretty confused on what to put as entry and exit. Do we use 'context.signal[security]' as the threshhold or do we have to create other instances. The 'handle_exit' function I used seems to run but doesnt do as a TP or SL is intended.

context.signals[security]
context.signals[security]

Sorry to get back late on this. I have added a demo strategy that handles stoploss in our github repo. We modify the demo bollinger band strategy to add a couple of tracker containers (entry_price and entry_side) and define a couple of new functions (one to calculate entry prices and another to enforce stop loss). See the comments at the top of the strategy as well.

Thank you very much Sir…I will study the link…No worries…You replied it eventually Sir…Appreciate the help.

Greetings Sir,



I manage to put the code examples you provided. However I devised the codes to take profit function first. The notes said we should not use the target_position in live trading. So,


  1. Do we use data.current(context.securities, 'close') instead or any other options.
  2. Are there ways we could use 'pips' as target instead of order_target_percent,since we are using currencies as instrument.



    Thank you for your support,



    Munir
 

Hi Abdul, not very sure I understand your questions clearly. The method data.current is used to fetch last prices, and data.history for historical prices - see more on data fetching APIs here. For the second questions, do you mean to say a target for profit?



Note the word target in the ordering functions (like order_target_percent) is not a target on profit, but rather on positions. They try to achieve the specified target in your asset positions. If your existing position exceeds the target, they will sell. If existing position is below, they will buy. If existing position already matches the target, they will do absolutely nothing. On the other hand, for profit target, you need to track your price move. Which certainly can be done either by percentage or pips (absolute movement). You need to change the condition accordingly.

Hi,Prodipta



Once again, thank you for replying…and thank you for correcting my understandings on the data.current and data.history.



  Regarding the the 'target' ,when you say 'absolute movement', do we use order_target() instead of order_target_percent?

The point is: order_target or order_target_percent has nothing to do with profit or loss targetting. If you want profit target, see the code line here. This calculates percentage loss. For pips profit target, you need to calculate absolute profit (x - y instead of x/y-1, where x is current price and y is the entry price). That signals when you want to get out. The functions order_target and order_target_percent means how the number of units you want to trade is interpreted. Usually for getting out of a position, the target should be 0. And for order_target(asset, 0) and order_target_percent(asset, 0) are essentially same, it zeroes out the position. For non-zero position, the positions in the asset to target is interpreted differently. For order_target of a units, - if you have existing position a+b units, b units will be sold, if you have existing postion a-b units, b units will be bought, if you have exactly a units in current position, nothing will happen. For order_target_percent, it works the same way, except the target is interpreted as fraction of your portfolio value (instead of units itself) and will be multiplied by the current portfolio value to calculate the unit. So if your portfolio value is P, and P.a = b, the function calls order_target(asset, a) and order_target_percent(asset, b) are exactly the same thing. Hope this clears things up.

 

Thank you Sir for the explanation…It clears my understanding of the topic…Thank you Mr. Prodipta…