Hi,
I have this code before,
https://www.prorealcode.com/topic/rsi-automated-buy-and-sell-request/
But would like to develop a stop loss function with this. I want it to buy when it buys, but I want that if I lose more than 0.3% then I want to sell, just as security, otherwise I want to sell at the highest similar to what the code shows, in this case: if onmarket and myrsi > 70 then sell at market
defparamcumulate orders = false
myrsi = rsi[14]
if myrsi < 30 then
buy 1 contract at market
endif
if onmarket and myrsi > 70 then
sell at market
endif
k3rex – Your topic has been moved to the ProOrder forums which are for automated strategy topics. You posted in the ProBuilder forum which is for indicator topics. Please try to ensure that you post in the correct forum with future topics.
Just add:
set stop %loss 0.3
to the bottom of the strategy and it will sell when price hits 0.3% below your buy price.
Alternatively you can add:
if onmarket and close <= positionprice * 0.997 then
sell at market
endif
and it will sell at the close of any candle that closes 0.3% or more lower than your buy price.
You should check that 0.3% is not too close to price as your broker will have a minimum stop distance and reject any order that is too close and possibly stop your strategy. You can use use MAX to try to ensure that no order is sent that is too close although sometimes brokers can add a big spread and a big minimum stop distance during high volatility so this is not totally foolproof.
For example if the minimum stop distance is 5 then the following will ensure that the 0.3% order is never closer than 5.
minstopdistance = 5
minstopperc = (minstopdistance/close)*100
mystop = max(minstopperc, 0.3)
set stop %loss mystop
defparamcumulate orders = false
myrsi = rsi[14]
if myrsi < 30 then
buy 1 contract at market
endif
if onmarket and myrsi > 70 then
sellshort 1 contract at market
endif
set stop %loss 0.3