Let AI program your crappy strategies

Discussion in 'Automated Trading' started by hilmy83, Dec 30, 2022.

  1. hilmy83

    hilmy83

    Let AI do its thing

    https://openai.com/

    I don't know how to code but with chat gpt, you just need specific inquiries and boom, MT5 and NT8 codes for MA crossover just like that

    upload_2022-12-30_9-18-8.png


    Code:
    #include <Trade\Trade.mqh>
    #include <Trade\SymbolInfo.mqh>
    #include <Trade\PositionInfo.mqh>
    #include <Trade\AccountInfo.mqh>
    #include <Trade\TradeTransInfo.mqh>
    
    #define MA_PERIOD1 5
    #define MA_PERIOD2 15
    
    int handle;
    double ma1, ma2;
    
    void OnStart()
    {
       // Set up the moving averages
       handle = iMA(NULL, 0, MA_PERIOD1, 0, MODE_SMA, PRICE_CLOSE, 0);
       if (handle == INVALID_HANDLE)
       {
          Print("Failed to set up moving average 1");
          return;
       }
       handle = iMA(NULL, 0, MA_PERIOD2, 0, MODE_SMA, PRICE_CLOSE, 0);
       if (handle == INVALID_HANDLE)
       {
          Print("Failed to set up moving average 2");
          return;
       }
    }
    
    void OnTick()
    {
       // Get the current values of the moving averages
       ma1 = iMA(NULL, 0, MA_PERIOD1, 0, MODE_SMA, PRICE_CLOSE, 0);
       ma2 = iMA(NULL, 0, MA_PERIOD2, 0, MODE_SMA, PRICE_CLOSE, 0);
    
       // Check if the moving averages have crossed
       if (ma1 > ma2)
       {
          // Moving average 1 is above moving average 2, so we will enter a long position
          if (!PositionSelect(NULL))
          {
             // No open position, so we will open a new one
             TradeRecord tr;
             tr.Comment = "MA Crossover Long";
             if (OrderSend(Symbol(), OP_BUY, 1.0, Ask, 3, 0, 0, tr.Comment, 0, 0, CLR_NONE))
             {
                Print("Order sent successfully");
             }
             else
             {
                Print("Failed to send order");
             }
          }
          else
          {
             // There is already an open position, so we will do nothing
          }
       }
       else if (ma1 < ma2)
       {
          // Moving average 1 is below moving average 2, so we will enter a short position
          if (!PositionSelect(NULL))
          {
             // No open position, so we will open a new one
             TradeRecord tr;
             tr.Comment = "MA Crossover Short";
             if (OrderSend(Symbol(), OP_SELL, 1.0, Bid, 3, 0, 0, tr.Comment, 0, 0, CLR_NONE))
             {
                Print("Order sent successfully");
             }
             else
             {
                Print("Failed to send order");
             }
          }
          else
          {
             // There is already an open position, so we will do nothing
          }
       }
       else
       {
          // Moving averages are equal, so we will do nothing
       }
    }
    

    upload_2022-12-30_9-20-42.png

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Indicator;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Strategy;
    
    namespace MyNamespace
    {
        public class MyAlgorithm : Strategy
        {
            private SMA sma1;
            private SMA sma2;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description = "A moving average crossover strategy";
                    Name = "MyAlgorithm";
    
                    // Set the default values for the SMA indicators
                    sma1 = SMA(Close, 5);
                    sma2 = SMA(Close, 15);
                }
            }
    
            protected override void OnBarUpdate()
            {
                // Check if the moving averages have crossed
                if (sma1[0] > sma2[0])
                {
                    // Moving average 1 is above moving average 2, so we will enter a long position
                    if (Position.MarketPosition == MarketPosition.Flat)
                    {
                        EnterLong(1, "MA Crossover Long");
                    }
                }
                else if (sma1[0] < sma2[0])
                {
                    // Moving average 1 is below moving average 2, so we will enter a short position
                    if (Position.MarketPosition == MarketPosition.Flat)
                    {
                        EnterShort(1, "MA Crossover Short");
                    }
                }
                else
                {
                    // Moving averages are equal, so we will do nothing
                }
            }
        }
    }
    


    The cool thing is that it also explains the code generated. So you can technically learn how to code in segments with AI walking you through each code.

    It's not perfect, I had to do multiple inquiries with the right words to get the majority of the code down. But sure is interesting
     
    Onra likes this.
  2. VEGASDESERT

    VEGASDESERT

    that sure af saves a lot of headache and time for non coders.
     
    hilmy83 likes this.
  3. hilmy83

    hilmy83

    Tried to get a mean reversion to vwap. I think AI misunderstood lol.

    upload_2022-12-30_9-27-19.png

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Indicator;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Strategy;
    
    namespace MyNamespace
    {
        public class MyAlgorithm : Strategy
        {
            private VWMA vwma;
            private ATR atr;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description = "A mean reversion to VWMA indicator trading strategy";
                    Name = "MyAlgorithm";
    
                    // Set the default values for the VWMA and ATR indicators
                    vwma = VWMA(Close, Volume, 20);
                    atr = ATR(14);
    
                    // Set the default values for the strategy
                    EntryHandling = EntryHandling.UniqueEntries;
                    ExitHandling = ExitHandling.AllPositions;
                    Slippage = 0;
                    StopLoss = 0;
                    ProfitTarget = 0;
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (CurrentBars[0] < 20)
                {
                    return;
                }
    
                // Check if the price has crossed the VWMA indicator
                if (Close[0] > vwma[0] && Close[1] <= vwma[1])
                {
                    // The price has crossed above the VWMA, so we will enter a long position
                    EnterLong(1, "Mean Reversion Long");
                }
                else if (Close[0] < vwma[0] && Close[1] >= vwma[1])
                {
                    // The price has crossed below the VWMA, so we will enter a short position
                    EnterShort(1, "Mean Reversion Short");
                }
    
                // Set the stop loss for the position based on the ATR indicator
                if (Position.MarketPosition == MarketPosition.Long)
                {
                    SetStopLoss(CalculationMode.Ticks, EntryPrice - atr[0] * 2);
                }
                else if (Position.MarketPosition == MarketPosition.Short)
                {
                    SetStopLoss(CalculationMode.Ticks, EntryPrice + atr[0] * 2);
                }
            }
        }
    }
    
    upload_2022-12-30_9-27-57.png

    Based on the code, the AI misunderstood what mean reversion is. It's actually going long as price cross up VWAP.

    Again, give it couple of years with more user inquiries/ratings, soon you'll be coding like a entry level college grad dev
     
  4. hilmy83

    hilmy83

     
  5. M.W.

    M.W.

    This has nothing whatsoever to do with code gpt. Code gpt takes care of the semantics and linguistics but NOT the logic of code, whether strategy code generation or any other program code generation.

    The explanation of this is actually quite simple. The trained deep learning function generates output based on what it has trained on and seen. It does not take into account the logic and intent of code. It takes stock of the semantic and linguistic details of code which makes it very suitable for debugging and code issues itself but not the logic and intent of your code. That you need to prescribe outside chatgpt. I currently take care of logic through different flavors of reinforcement learning and the code generation is taken care of by templating and chat gpt. RL is governed by the environment and reward functions that represent to a large degree the intent of your algorithm logic.

     
    Last edited: Dec 30, 2022
  6. rajnivp

    rajnivp

    Chatgpt will just create basic code for given conditions, you can search it on google and find it on Github/Stack overflow anyway, For anything complex it's pretty dumb.
     
    guest_trader_1, Zwaen, ET180 and 2 others like this.
  7. ET180

    ET180

    OP literally gave an example of chatGPT converting simple strategies from description into functional code which includes logic. He never claimed that it could convert any strategy from description to code, only crappy strategies.
     
  8. Hello hilmy83,

    You can hire a coder to program your trading ideas if you struggling with coding
     
  9. M.W.

    M.W.

    you have no idea what you are talking about.

     
  10. hilmy83

    hilmy83

    It's a average Joe Google for code. You type what you want, then you learn how to piece it together. In some queries it even explains the logic of the code. It's the framework for learning the language
     
    #10     Dec 30, 2022
    Zwaen likes this.