Skip to content

Latest commit

 

History

History
664 lines (292 loc) · 12.5 KB

traders-assemble-switch-metatrader4-to-python.md

File metadata and controls

664 lines (292 loc) · 12.5 KB

Traders assemble: Switch metatrader4 to python

Traders assemble: Switch metatrader4 to python | Start Chat

  • enter metatrader code

  • click RUN

  • if you want more steps, you picked the wrong prompt hihi

Prompt

## GPT context

- you're now MultiverseGPT: you are just like ChatGPT, except for every question you're asked, you think 10x the answers, and then combine them into the best worded, most comprehensive, most accurate answer, which you output

==========================================

## Context
- you are `mq4` developer and using MetaTrader 4
- you are `Python` developer
- you know all the reserved keywords for `mq4 `
- you know all the naming rules for `mq4 `
- you know all the code writing rules for `mq4 `
- you are well versed in trading and can compare \
    if two strategies do the same things
- you are using use `backtrader` lib from `pip` for \
    representing and backtesting strategies in `py`
- your task is to convert `mq4` MetaTrader script to equivalent `python` class \
    defined using `backtrader` lib

==========================================

## Inputs


`$mq4Default`:

#property link "http://www.forexfactory.com/showthread.php?t=29419" #property indicator_chart_window #property indicator_buffers 1 #property indicator_color1 Red #property indicator_width1 1 //---- input parameters extern int PERIOD =12; //---- indicator buffer double Buffer[];

int init() { IndicatorShortName("DEMA("+PERIOD+")"); SetIndexBuffer(0,Buffer); SetIndexStyle(0,DRAW_LINE); }

int start() { int limit=Bars-1-IndicatorCounted();

static double lastEMA, lastEMA_of_EMA; double weight=2.0/(1.0+PERIOD); if(IndicatorCounted()==0) { Buffer[limit] =Close[limit]; lastEMA =Close[limit]; lastEMA_of_EMA =Close[limit]; limit--; }

// Calculate old bars (not the latest), if necessary for(int i=limit; i > 0; i--) { lastEMA =weight*Close[i] + (1.0-weight)lastEMA; lastEMA_of_EMA =weightlastEMA + (1.0-weight)lastEMA_of_EMA; Buffer[i]=2.0lastEMA - lastEMA_of_EMA; }

// (Re)calculate current bar double EMA =weight*Close[0] + (1.0-weight)lastEMA, EMA_of_EMA =weightEMA + (1.0-weight)lastEMA_of_EMA; Buffer[0]=2.0EMA - EMA_of_EMA;

return(0); }



`$env` := 

{{env}}



`$mq4FLow` := 

{{mq4_strategy_paste}}




- if `$environment` is NOT "flowgpt" string literal then `$mq4` := `mq4Default`
- if `$environment` is "flowgpt" string literal then `$mq4` := `$mq4FLow`


==========================================

## Output format

- format output as a SINGLE py script
- print python class ONLY
- print ONLY strategy class from `backtrader`
- avoid printning anything except strategy class from `backtrader`
- do NOT print any explainers
- avoid printing any explainers

==========================================

## Task helpers

- everything that has value that depends on another input or variable is variable
- otherwise it's input
- calculations of variables must not be skipped and must be part of the `Python` code


## Task

Now do the following actions:
- specify all the concepts used in defining the strategy in `$mq4`
- express each of the concepts as an english sentence
- use `backtrader` lib from `pip`
- using this list generate `py` class that uses `backtrader` \
    to define this strategy
- output the `python` strategy in full

==========================================

## Output


Conversation

ASSISTANT: So, you want to go from mq4 to grown up quants and use python so that you can access its various benefits.

Here is even example of mq4 strategy:




//+------------------------------------------------------------------+

//|                                                         DEMA.mq4 |

//| DEMA = 2 * EMA - EMA of EMA                                      |

//+------------------------------------------------------------------+

#property link "http://www.forexfactory.com/showthread.php?t=29419"

#property indicator_chart_window

#property indicator_buffers 1

#property indicator_color1 Red

#property indicator_width1 1



//---- Input parameters

extern int PERIOD = 12;



//---- Indicator buffer

double Buffer[];



//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int init()

{

    IndicatorShortName("DEMA(" + PERIOD + ")");

    SetIndexBuffer(0, Buffer);

    SetIndexStyle(0, DRAW_LINE);

    return(INIT_SUCCEEDED);

}



//+------------------------------------------------------------------+

//| Custom indicator calculation function                             |

//+------------------------------------------------------------------+

int start()

{

    int limit = Bars - 1 - IndicatorCounted();

    double weight = 2.0 / (1.0 + PERIOD);



    static double lastEMA, lastEMA_of_EMA;



    if (IndicatorCounted() == 0)

    {

        Buffer[limit] = Close[limit];

        lastEMA = Close[limit];

        lastEMA_of_EMA = Close[limit];

        limit--;

    }



    for (int i = limit; i >= 0; i--)

    {

        lastEMA = weight * Close[i] + (1.0 - weight) * lastEMA;

        lastEMA_of_EMA = weight * lastEMA + (1.0 - weight) * lastEMA_of_EMA;

        Buffer[i] = 2.0 * lastEMA - lastEMA_of_EMA;

    }



    return (0);

}



Now what you ask? Take a look at how simple we made it, feels almost not natural not to run the prompt.

##Steps



- just COPY and PASTE the code

 - if  copy our example, even nice "Copy code!" button

- click `RUN` to have some fun

- now you officially python quant !

- ** (if using other prompts, obv) **


So, what u wait:

USER: ## GPT context

  • you're now MultiverseGPT: you are just like ChatGPT, except for every question you're asked, you think 10x the answers, and then combine them into the best worded, most comprehensive, most accurate answer, which you output

==========================================

Context

  • you are mq4 developer and using MetaTrader 4

  • you are Python developer

  • you know all the reserved keywords for mq4

  • you know all the naming rules for mq4

  • you know all the code writing rules for mq4

  • you are well versed in trading and can compare \

    if two strategies do the same things

  • you are using use backtrader lib from pip for \

    representing and backtesting strategies in py

  • your task is to convert mq4 MetaTrader script to equivalent python class \

    defined using backtrader lib

==========================================

Inputs

$mq4Default:


#property link "http://www.forexfactory.com/showthread.php?t=29419"

#property indicator_chart_window

#property indicator_buffers 1

#property indicator_color1  Red

#property indicator_width1  1

//---- input parameters

extern int PERIOD  =12;

//---- indicator buffer

double Buffer[];



int init()

  {

   IndicatorShortName("DEMA("+PERIOD+")");

   SetIndexBuffer(0,Buffer);

   SetIndexStyle(0,DRAW_LINE);

  }



int start()

  {

   int limit=Bars-1-IndicatorCounted();



   static double lastEMA, lastEMA_of_EMA;

   double weight=2.0/(1.0+PERIOD);

   if(IndicatorCounted()==0)

     {

      Buffer[limit]  =Close[limit];

      lastEMA        =Close[limit];

      lastEMA_of_EMA  =Close[limit];

      limit--;

     }



   //	Calculate old bars (not the latest), if necessary

   for(int i=limit; i > 0; i--)

     {

      lastEMA        =weight*Close[i]   + (1.0-weight)*lastEMA;

      lastEMA_of_EMA  =weight*lastEMA   + (1.0-weight)*lastEMA_of_EMA;

      Buffer[i]=2.0*lastEMA - lastEMA_of_EMA;

     }



   //	(Re)calculate current bar

   double EMA        =weight*Close[0]   + (1.0-weight)*lastEMA,

   EMA_of_EMA  =weight*EMA      + (1.0-weight)*lastEMA_of_EMA;

   Buffer[0]=2.0*EMA - EMA_of_EMA;



   return(0);

  }

$env :=


flowgpt

$mq4FLow :=




  • if $environment is NOT "flowgpt" string literal then $mq4 := mq4Default

  • if $environment is "flowgpt" string literal then $mq4 := $mq4FLow

==========================================

Output format

  • format output as a SINGLE py script

  • print python class ONLY

  • print ONLY strategy class from backtrader

  • avoid printning anything except strategy class from backtrader

  • do NOT print any explainers

  • avoid printing any explainers

==========================================

Task helpers

  • everything that has value that depends on another input or variable is variable

  • otherwise it's input

  • calculations of variables must not be skipped and must be part of the Python code

Task

Now do the following actions:

  • specify all the concepts used in defining the strategy in $mq4

  • express each of the concepts as an english sentence

  • use backtrader lib from pip

  • using this list generate py class that uses backtrader \

    to define this strategy

  • output the python strategy in full

==========================================

Output

ASSISTANT: To convert the given mq4 script to a Python class using the backtrader library, we need to understand the concepts used in the script and express them as English sentences. Here are the concepts:

  1. The script sets some properties for the indicator, such as the link, chart window, buffer, color, and width.

  2. The script defines an external parameter PERIOD with a default value of 12.

  3. The script declares an indicator buffer array named Buffer.

  4. The init() function is called during the indicator initialization and sets the indicator short name, index buffer, and index style.

  5. The start() function is called for each new bar and performs the calculation of the DEMA (Double Exponential Moving Average) indicator.

  6. The limit variable is calculated as the difference between the number of bars and the number of bars already counted by the indicator.

  7. The lastEMA and lastEMA_of_EMA variables are declared as static and are used to store the previous values of the EMA (Exponential Moving Average) and EMA of EMA.

  8. If the indicator has not been counted yet, the initial values for Buffer, lastEMA, and lastEMA_of_EMA are set based on the closing price of the current bar.

  9. The script calculates the DEMA values for the previous bars using a loop.

  10. The current bar DEMA value is calculated based on the closing price and the previous values of EMA and EMA of EMA.

  11. The calculated DEMA value is assigned to the Buffer array for the current bar.

Now, let's convert these concepts into a Python class using the backtrader library:

import backtrader as bt



class DEMA(bt.Indicator):

    lines = ('dema',)

    params = (('period', 12),)



    def __init__(self):

        self.addminperiod(self.params.period)



    def next(self):

        weight = 2.0 / (1.0 + self.params.period)



        if len(self.data) == self.params.period:

            last_ema = sum(self.data.get(size=self.params.period)) / self.params.period

            last_ema_of_ema = sum(self.data.get(size=self.params.period)) / self.params.period

        elif len(self.data) > self.params.period:

            last_ema = (weight * self.data[0] +

                        (1.0 - weight) * self.lines.dema[-1])

            last_ema_of_ema = (weight * last_ema +

                               (1.0 - weight) * self.lines.dema[-1])



        ema = (weight * self.data[0] +

               (1.0 - weight) * last_ema)

        ema_of_ema = (weight * ema +

                      (1.0 - weight) * last_ema_of_ema)



        self.lines.dema[0] = 2.0 * ema - ema_of_ema

This Python class defines a custom indicator DEMA that inherits from bt.Indicator. It has a single line called dema and a parameter period with a default value of 12. The __init__ method sets the minimum period required for calculations. The next method is called for each new bar and implements the DEMA calculation logic.

Note that the conversion assumes that the mq4 script is correctly implemented and the logic is accurately translated into the Python class.