Set stoploss with percentage

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #116338 quote
    k3rex
    Participant
    Junior

    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
    midel thanked this post
    #116342 quote
    Vonasi
    Moderator
    Master

    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.

    #116343 quote
    Vonasi
    Moderator
    Master

    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
    
    k3rex thanked this post
    #116539 quote
    k3rex
    Participant
    Junior
    if onmarket and myrsi > 70 then
    sell at market 
    and then make an short position? So when the short position hit :
    if myrsi < 30 then
    buy 1 contract at market
    endif
    I mean the RSI 30-level and then sell it? Like it goes around like a circle.
    So what I mean is, when you reach the 70 level, then it becomes a short position and it sold when you reach the 30 level, and at the 30 level you buy and sell at the 70 level, and of course 0.3% stop loss function is inserted so you do not lose any capital.
    #116563 quote
    Vonasi
    Moderator
    Master
     
    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
    Nicolas thanked this post
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.

Set stoploss with percentage


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
k3rex @k3rex Participant
Summary

This topic contains 4 replies,
has 2 voices, and was last updated by Vonasi
6 years, 1 month ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 01/09/2020
Status: Active
Attachments: No files
Logo Logo
Loading...