EveryDayBetter
Pine Script Master
Pine Script Master
Posts: 27
Joined: February 16th, 2022

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

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?

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

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

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)

EveryDayBetter
Pine Script Master
Pine Script Master
Posts: 27
Joined: February 16th, 2022

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

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.?

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

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

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.

EveryDayBetter
Pine Script Master
Pine Script Master
Posts: 27
Joined: February 16th, 2022

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

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.

Return to “Pine Script Q&A”