SL at the lowest (highest) of the previous x bars for buy (sell) order

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #68892 quote
    AlgoAlex
    Participant
    Master

    Hi,

    I’d like to code a simple SL function:

    when the conditions for buy order are triggered, I want the stop loss to be put below the lowest of the previous x bars.

     

    Same for sell orders, above the highest of the previous  bars.

    Thanks for any help

    #68901 quote
    Vonasi
    Moderator
    Master
    IF Not OnMarket and (your conditions) THEN
    BUY at Market
    SL = Lowest[100](Low)
    SELL AT SL STOP
    ENDIF
    
    IF OnMarket THEN
    SELL at SL STOP
    ENDIF

    This should work if I have understood right what you want. The stoploss price is set and an order set at the time of entry and an order placed every bar after that at that same stoploss price while you still on the market because the STOP orders need to be renewed at every new bar. Obviously change the [100] to whatever period you need for your lookback.

    Code the reverse with Highest[x](High and SellShort and ExitShort for short positions.

    AlgoAlex thanked this post
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

SL at the lowest (highest) of the previous x bars for buy (sell) order


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
AlgoAlex @alexf Participant
Summary

This topic contains 1 reply,
has 2 voices, and was last updated by Vonasi
7 years, 10 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 04/20/2018
Status: Active
Attachments: No files
Logo Logo
Loading...