Skip to main content
All CollectionsCody AI - Trading Assistant
How to review the code generated by Cody AI
How to review the code generated by Cody AI
Quad Team avatar
Written by Quad Team
Updated over a year ago

Summary

Before running a strategy generated by Cody, always run a backtest and review the code to ensure it fits your desired strategy. In this article, we'll review how to go through this process.

Details

Before starting, please make sure to join our Discord to exchange with the team as well as other Cody AI beta testers. If you need any help, feel free to reach out to anyone in the Cody channel.

Step 1: Review the initialize() function

def initialize(context):
context.target_profit = 0.05
context.stop_loss = -0.2
context.investment_amount = context.portfolio.cash * 0.1
log.info("Strategy initialized")
  • Check that all necessary variables are defined and initialized appropriately

  • Confirm that the function sets up any initial state or configuration needed for the algorithm

  • Ensure that any internal variables or settings, such as context.symbol, are properly defined and set. Each of these settings will be used to run the strategy.

In the provided code, we can verify:

  • the target profit is set to 5%

  • the stop loss to -20%

  • The order amount is set to 10% of the allocated capital

Step 2: Review the handle_data() function

# Handling the data for the strategy
def handle_data(context, data):
# Retrieving the current price of MATIC
price = data.current(context.symbol, "price")
# Retrieving the RSI value for the last 30 days
rsi = talib.RSI(data.history(context.symbol, "price", bar_count=31, frequency="1D"), timeperiod=context.rsi_period)[-1]
# Checking if there is an open order
open_orders = get_open_orders()
if open_orders:
log.info(f'Open orders: {open_orders}')
# Checking if the RSI is below the oversold level and there is no open order
if rsi < context.rsi_oversold and not context.order:
# Calculating the amount to buy in base currency
amount = context.order_amount / price
# Buying the amount of MATIC
context.order = order(context.symbol, amount)
log.info(f'Buying {amount} MATIC at {price}')
# Checking if the RSI is above the overbought level and there is an open order
elif rsi > context.rsi_overbought and context.order:
# Selling all the MATIC
order(context.symbol, -context.portfolio.positions[context.symbol].amount)
log.info(f'Selling {context.portfolio.positions[context.symbol].amount} MATIC at {price}')
# Resetting the order variable
context.order = None
  • Make sure the timeframe is correct both for the indicators as well as for the execution timeframe.

  • Check that the function properly implements the trading logic of the algorithm

  • Verify that any external data sources are correctly integrated into the algorithm

  • Ensure that the function properly handles orders and logs

In the provided code, we can verify:

  • The RSI value are on the daily chart

    • bar_count=31, frequency="1D"

  • The triggers are correct, in this case

    • if rsi < context.rsi_oversold and not context.order

      • this is the buy condition

    • elif rsi > context.rsi_overbought and context.order:

      • this is the sell condition

  • Logs are correctly set:

    • log.info(f'Buying {amount} MATIC at {price}')

    • log.info(f'Selling {context.portfolio.positions[context.symbol].amount} MATIC at {price}')

Quick Links

Did this answer your question?