Unlock the Power of ChatGPT to Create a Winning Trading Strategy.

SmartLever
4 min readJan 26, 2023

ChatGPT is a window to an unanticipated future where people who learn to put the pieces of the jigsaw together will be infinitely more productive and efficient thanks to the use of AI as a lever.

Smartbots is a comprehensive platform that bridges the gap between idea generation and real-time implementation. Basktesting alone is not enough, as we explain here.

By using ChatGPT as a recent Harvard graduate with great Python knowledge, as our Quant assistant, we can explore this peek into the future and ask him to produce code from text. Then we can immediately do a basktesting using this code and SmartBots, and if the numbers we obtain are satisfactory, why not, put them into real time working.

The secret is in the dough: prompt engineering.

90% of us aren’t utilizing ChatGPT’s full potential despite the fact that it is quite powerful. A concise and organized prompt is essential.

  • Simulate a person.
  • The request.
  • Guidelines to follow
  • Context
  • Output format.

Trading Strategy Prompt:

YOUR REQUEST COULD BE: “I want a strategy that applies the Relative Strength Index (RSI). If the RSI is greater than 80, it should send a buy order, and if the RSI is less than 20, it should send a sell order.”

You are a specialized Python coding assistant for quantitative trading with trading systems and strategies. Using the following 18 guidelines, 
create a new class that inherits from the Abstract_Strategy class and implement the next request:
""" YOUR REQUEST """

1. Import the Abstract_Strategy class from **`src.domain.abstractions.abstract_strategy`**
2. Create a new class that inherits from Abstract_Strategy.
3. Use methods and functions to encapsulate the calculation of any indicator and algorithm from the request.
4. Override the add_event method and implement the algorithms and indicators from the request.
5. Avoid using third-party libraries for indicators, instead develop your own methods.
6. Do not use callbacks in the add_event method.
7. Include detailed comments in the code to explain the methods, functions and classes.
8. Use the self.ticket, price, self.quantity, action, type, and datetime arguments when calling the send_order method.
9. Event received in the add_event method is a dataclass type, with attributes for close, open, high, low, volume, and ticker.
10. Provide a dictionary of inputs for any indicator you create from the request in the `parameters` argument from init method.
11. When conditions for buying and selling are met, use the send_order method with keyword arguments.
12. When conditions for buying and selling are met, only send one order until the strategy change.
13. Use event.event_type checking, it can be ‘bar’ and ‘tick’ type.
14. Use self.position and do not change value of self.position in add_event method. send_order method calculate self.number_of_contracts variable that you have to use in send_order implementation.
15. Preallocate memory to avoid storing more data than necessary with variables.
16. Refer to past events to calculate variable if the request is not expressed in another way.
17. Use _add_event_example as an example for implement add_event.
18. Create an answer within one code block.

Context: """ Copy Code from https://github.com/SmartLever/SmartBots/blob/main/src/domain/abstractions/abstract_strategy.py """

Note: for launching your own request:

  • Edit your request in the text below and it will generate your trading strategy.
  • Copy code from URL in context. For keep it simple I do not copy it here.

ChatGPT Code Output:

class RSI_Strategy(Abstract_Strategy):
def __init__(self, parameters: dict = None, id_strategy: int = None, callback: callable = None, set_basic: bool = True):
super().__init__(parameters, id_strategy, callback, set_basic)
self.previous_close = None
self.rsi_period = 14
if 'rsi_period' in self.parameters:
self.rsi_period = self.parameters['rsi_period']
self.gain = 0
self.loss = 0
self.average_gain = 0
self.average_loss = 0
self.previous_change = 0
self.rsi = None

def calculate_rsi(self, event):
close = event.close
if self.previous_close is None:
self.previous_close = close
return
change = close - self.previous_close
self.previous_close = close
if self.n_events <= self.rsi_period:
if change > 0:
self.gain += change
else:
self.loss -= change
if self.n_events == self.rsi_period:
self.average_gain = self.gain / self.rsi_period
self.average_loss = self.loss / self.rsi_period
else:
if change > 0:
self.average_gain = ((self.average_gain * (self.rsi_period - 1)) + change) / self.rsi_period
self.average_loss = ((self.average_loss * (self.rsi_period - 1)) + 0) / self.rsi_period
else:
self.average_gain = ((self.average_gain * (self.rsi_period - 1)) + 0) / self.rsi_period
self.average_loss = ((self.average_loss * (self.rsi_period - 1)) - change) / self.rsi_period
if self.average_loss != 0:
self.rsi = 100 - (100 / (1 + (self.average_gain / self.average_loss)))

def _add_event(self, event):
if event.event_type == 'bar':
self.n_events += 1
self.calculate_rsi(event)
if self.rsi > 80 and self.position <= 0:
self.send_order(ticker=self.ticker_broker, price=close, quantity=self.quantity, action='buy', type='market', datetime=event.datetime)
self.position += self.quantity
elif self.rsi < 20 and self.position >= 0:
self.send_order(ticker=self.ticker_broker, price=close, quantity=self.quantity, action='sell', type='market', datetime=event.datetime)
self.position -= self.quantity

There are some logical errors that can be solved with a Debug and it did not take me more than 5 minutes to solve it. I’m sure that with a finer Prompt and with new updates of GPT 4, these errors will be fewer and we will be able to include more complex contexts very soon.

The future appears promising.

Here is the RSI Strategy with the corrections.

A good starting point: The basktesting with SmartBots.

If you want to perform the basktesting on your own, here is a guidance.

Subscribe to all our news here:

The train is in the station and you can’t miss it, so join us on this journey. We are an open community and if you want to collaborate with us, please send us an email: andres@smartlever.tech

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

SmartLever
SmartLever

Written by SmartLever

Leverage your options with technology.

Responses (6)

Write a response

The part about prompt engineering is very enlightening

It works great for PineScript too which is perhaps a bit easier to automate if you want a strategy that you can automate on a simple service like Tickerly