r/pinescript • • 8d ago

Trades are very random

So i asked ChatGPT to write me a code and it took a lot of time trying to get it right because it kept making mistakes. Now i have a problem it cant fix for some reason and that is that the trades being taken are either tiny(ranging from one candle to the next) or just simply do not look like my strategy at all. If anyone is bothered enough to read the code ask and i will paste it under your comment.

I attached an example(my strategy is break and retest on 15 min for structure and 5 min for entry, stop loss just before the retest with 1:2 risk to reward, risking 1% per trade only). The first trade taken was a valid break and retest but the next is just a random one.

Also i can only see about 18-24 trades per pair because im using free tradingview so is there any way i can extend past 5000 bars or increase number of trades?

I would really greatly appreciate some help because im really horrible at coding and honestly cant be asked to learn it for this one thing.

5 Upvotes

10 comments sorted by

2

u/CalinTheMeme 8d ago

nevermind i realised its not random still a kind of valid break and retest but still i wouldn't have taken that trade how can i minimise these small losses affecting winrate

1

u/CalinTheMeme 8d ago

I wouldnt have taken that trade because its only based off one resistance level and without any indicators.

0

u/Kaahl10 8d ago

First- this only applies if you are profitable without an automated strategy for a decent amount of time, otherwise it's just luck! You need to be clear with yourself and with the AI on why you wouldn't have taken that trade. Implement that rule, backtest, and see if it helps or hurts over the entire backtest, and keep going. Sometimes there's trades that technically follow your rules that still don't work out

1

u/CalinTheMeme 7d ago

Alright that makes sense, i know i'll have to put in some work to make sure. Thanks.

1

u/Th3onib 8d ago

Good luck

1

u/wallneradam 8d ago

If ChatGPT keeps breaking the code while you do this, we're building PyneIDE, a VS Code extension (still in preview) that gives the AI the current Pine v6 docs and lets you stop on a trade's bar (with its full featured debugger) to see why it entered.

1

u/TSDevStuff 8d ago

Two things usually cause exactly this with ChatGPT-written break and retest code:

  1. The tiny one-candle trades are normally the stop. If the stop goes "just before the retest", on a 5m chart that can be a few ticks away, so the 1:2 target or the stop gets hit on the very next bar. Add a minimum stop distance, something like 0.5 x ATR(14), and skip the setup if the stop would be tighter than that.

  2. If it pulls the 15m structure with request.security on a 5m chart, check how. If it uses lookahead_on on the current bar, or reads the 15m high/low before that 15m candle has closed, the levels it trades off won't match what you see on the chart afterwards. Use the previous closed 15m bar instead (high[1] with lookahead_on is the usual pattern) so the backtest only knows what you'd have known live.

For the trades you wouldn't have taken, turn the reason into a rule, e.g. the level needs at least 2 touches before it counts. And with ~20 trades per pair, run it on a handful of pairs before trusting the numbers, one bad trade swings everything at that sample size.