Page 1 of 1

How is it possible to let PineScript calculate the optimal Stop Loss for a profitable strategy?

Posted: Fri Feb 18, 2022 8:46 am
by EveryDayBetter
Maybe like the Excel solver add-in?

My idea is to define for instance the net profit or the Percent Profitable and let PineScript calculate the necessary variables for
- Stop Loss
- Take Profit
- (and maybe the amount of days for a timestop)

How could look the code for this idea?

Re: How is it possible to let PineScript calculate the optimal Stop Loss for a profitable strategy?

Posted: Fri Feb 18, 2022 9:09 am
by processingclouds
Well pinescript is not AI, but a programming language. So you need to provide logic to how you would like the stop loss to be hit at or logic for any other variables that you want it to evaluate.

So your idea needs to be a complete idea , and not just i need profit of 100% and boom you have pinescript do the rest by itself.


I am attaching a script snippet by "theCrypster 2020" , in this you define the inputs for stop loss % and take profit and than it uses simple ema crosses to get the long buy.

Code: Select all

// © theCrypster 2020
//@version=4
strategy("Fixed Percent Stop Loss & Take Profit %", overlay=true)
eg1 = ema(close, 5)
eg2 = ema(close, 32)
long = crossover(eg1, eg2)
short = crossunder(eg1, eg2)
strategy.entry("LONG", strategy.long, when=long)
strategy.entry("SHORT", strategy.short, when=short)
stopPer = input(5.0, title='Stop Loss %', type=input.float) / 100
takePer = input(10.0, title='Take Profit %', type=input.float) / 100

longStop = strategy.position_avg_price * (1 - stopPer)
shortStop = strategy.position_avg_price * (1 + stopPer)
shortTake = strategy.position_avg_price * (1 - takePer)
longTake = strategy.position_avg_price * (1 + takePer)

if strategy.position_size > 0 
    strategy.exit(id="Close Long", stop=longStop, limit=longTake)
if strategy.position_size < 0 
    strategy.exit(id="Close Short", stop=shortStop, limit=shortTake)

Re: How is it possible to let PineScript calculate the optimal Stop Loss for a profitable strategy?

Posted: Fri Feb 18, 2022 10:55 am
by EveryDayBetter
Thank you for your help. Do you have any idea how I can write a strategy, which entries every Monday at 4 p.m. and exist on Tuesdays at 3 p.m.?

Re: How is it possible to let PineScript calculate the optimal Stop Loss for a profitable strategy?

Posted: Fri Feb 18, 2022 3:05 pm
by processingclouds
Hey,

Please have a look at https://www.tradingview.com/script/dT5b ... ke-Profit/

This enters monday near close time, and exits tuesday close time. It will also exit on take profit or stop loss which can be changed in the settings.

Re: How is it possible to let PineScript calculate the optimal Stop Loss for a profitable strategy?

Posted: Fri Feb 18, 2022 4:33 pm
by EveryDayBetter
processingclouds wrote:
Fri Feb 18, 2022 3:05 pm
Hey,

Please have a look at https://www.tradingview.com/script/dT5b ... ke-Profit/

This enters monday near close time, and exits tuesday close time. It will also exit on take profit or stop loss which can be changed in the settings.
Thank you very much.