Using ChatGPT to convert code.

Johnedoe

Well-known member
Joined
Feb 29, 2020
Posts
82
Likes
10
Has anyone tried using one of the AI's to write or convert other code to java?
Might be what we are looking for to simplify writing java strategies and studies.
 
Joined
Apr 11, 2023
Posts
9
Likes
2
In my experience as a software engineer, you'll only get so far. It’s great at generating functions or statement completions. Tying it all together and optimizing for runtime characteristics is where this method will fall short.

your mileage may vary but there is little room for error when money is involved.

Do you have something specific you’re trying to accomplish?
 

Khaled

Member
Joined
Jul 31, 2023
Posts
12
Likes
4
My question is more about how you convert a code like the following into a file you can import under MW Strategies module? Not sure where to put this in Eclipse or using another compilation software.

import java.util.ArrayList;
import java.util.List;

public class TradingStrategy {

// Function to calculate Exponential Moving Average (EMA)
private static double calculateEMA(List<Double> prices, int period) {
if (period <= 0 || period > prices.size()) {
throw new IllegalArgumentException("Invalid input data or period.");
}

double smoothingFactor = 2.0 / (period + 1);
double ema = prices.get(0);

for (int i = 1; i < prices.size(); i++) {
ema = (prices.get(i) - ema) * smoothingFactor + ema;
}

return ema;
}

// Function to calculate Relative Strength Index (RSI)
private static double calculateRSI(List<Double> prices, int period) {
if (period <= 0 || period > prices.size()) {
throw new IllegalArgumentException("Invalid input data or period.");
}

int startIndex = prices.size() - period;
double avgGain = 0.0;
double avgLoss = 0.0;

for (int i = startIndex; i < prices.size() - 1; i++) {
double priceDiff = prices.get(i + 1) - prices.get(i);
if (priceDiff >= 0) {
avgGain += priceDiff;
} else {
avgLoss += Math.abs(priceDiff);
}
}

avgGain /= period;
avgLoss /= period;

if (avgLoss == 0) {
return 100.0;
}

double rs = avgGain / avgLoss;
return 100.0 - (100.0 / (1 + rs));
}

// Main trading strategy function
public static String tradeSignal(List<Double> prices) {
int shortTermPeriod = 10;
int longTermPeriod = 50;
int rsiPeriod = 14;

if (prices.size() < longTermPeriod || prices.size() < rsiPeriod) {
return "Not enough data to generate a signal.";
}

double shortTermEMA = calculateEMA(prices, shortTermPeriod);
double longTermEMA = calculateEMA(prices, longTermPeriod);
double rsi = calculateRSI(prices, rsiPeriod);

double prevRsi = calculateRSI(prices.subList(0, prices.size() - rsiPeriod), rsiPeriod);

if (shortTermEMA > longTermEMA && rsi > prevRsi) {
return "Buy Long 1 Futures Contract";
} else if (shortTermEMA < longTermEMA && rsi < prevRsi) {
return "Sell Short 1 Futures Contract";
} else {
return "No trade signal.";
}
}

// Function to check exit signal conditions
public static boolean checkExitSignal(List<Double> prices, double entryPrice) {
double lastPrice = prices.get(prices.size() - 1);

double takeProfit = 20.0; // 20 points
double stopLoss = 15.0; // 15 points

double takeProfitPrice = entryPrice + takeProfit;
double stopLossPrice = entryPrice - stopLoss;

// Check if Take Profit or Stop Loss conditions are met
if (lastPrice >= takeProfitPrice || lastPrice <= stopLossPrice) {
return true;
} else {
return false;
}
}

public static void main(String[] args) {
// Sample data for prices (replace with real data)
List<Double> prices = new ArrayList<>();

// Add sample data (replace with real data)
prices.add(100.0);
prices.add(102.0);
prices.add(105.0);
prices.add(110.0);
prices.add(108.0);
prices.add(112.0);

// Generate the trade signal
String signal = tradeSignal(prices);
System.out.println("Trade Signal: " + signal);

// Assuming the signal is to "Buy Long 1 Futures Contract" and entry price is 110.0
double entryPrice = 110.0;

// Check exit signal
boolean exitSignal = checkExitSignal(prices, entryPrice);
System.out.println("Exit Signal: " + (exitSignal ? "Exit position" : "Continue holding"));
}
}
 

Johnedoe

Well-known member
Joined
Feb 29, 2020
Posts
82
Likes
10
In my experience as a software engineer, you'll only get so far. It’s great at generating functions or statement completions. Tying it all together and optimizing for runtime characteristics is where this method will fall short.

your mileage may vary but there is little room for error when money is involved.

Do you have something specific you’re trying to accomplish?

What I had in mind was to be able to convert trade station or ninja trader systems and strategies code into something I can use in MotiveWave....
 

Johnedoe

Well-known member
Joined
Feb 29, 2020
Posts
82
Likes
10
My question is more about how you convert a code like the following into a file you can import under MW Strategies module? Not sure where to put this in Eclipse or using another compilation software.

import java.util.ArrayList;
import java.util.List;

public class TradingStrategy {

// Function to calculate Exponential Moving Average (EMA)
private static double calculateEMA(List<Double> prices, int period) {
if (period <= 0 || period > prices.size()) {
throw new IllegalArgumentException("Invalid input data or period.");
}

double smoothingFactor = 2.0 / (period + 1);
double ema = prices.get(0);

for (int i = 1; i < prices.size(); i++) {
ema = (prices.get(i) - ema) * smoothingFactor + ema;
}

return ema;
}

// Function to calculate Relative Strength Index (RSI)
private static double calculateRSI(List<Double> prices, int period) {
if (period <= 0 || period > prices.size()) {
throw new IllegalArgumentException("Invalid input data or period.");
}

int startIndex = prices.size() - period;
double avgGain = 0.0;
double avgLoss = 0.0;

for (int i = startIndex; i < prices.size() - 1; i++) {
double priceDiff = prices.get(i + 1) - prices.get(i);
if (priceDiff >= 0) {
avgGain += priceDiff;
} else {
avgLoss += Math.abs(priceDiff);
}
}

avgGain /= period;
avgLoss /= period;

if (avgLoss == 0) {
return 100.0;
}

double rs = avgGain / avgLoss;
return 100.0 - (100.0 / (1 + rs));
}

// Main trading strategy function
public static String tradeSignal(List<Double> prices) {
int shortTermPeriod = 10;
int longTermPeriod = 50;
int rsiPeriod = 14;

if (prices.size() < longTermPeriod || prices.size() < rsiPeriod) {
return "Not enough data to generate a signal.";
}

double shortTermEMA = calculateEMA(prices, shortTermPeriod);
double longTermEMA = calculateEMA(prices, longTermPeriod);
double rsi = calculateRSI(prices, rsiPeriod);

double prevRsi = calculateRSI(prices.subList(0, prices.size() - rsiPeriod), rsiPeriod);

if (shortTermEMA > longTermEMA && rsi > prevRsi) {
return "Buy Long 1 Futures Contract";
} else if (shortTermEMA < longTermEMA && rsi < prevRsi) {
return "Sell Short 1 Futures Contract";
} else {
return "No trade signal.";
}
}

// Function to check exit signal conditions
public static boolean checkExitSignal(List<Double> prices, double entryPrice) {
double lastPrice = prices.get(prices.size() - 1);

double takeProfit = 20.0; // 20 points
double stopLoss = 15.0; // 15 points

double takeProfitPrice = entryPrice + takeProfit;
double stopLossPrice = entryPrice - stopLoss;

// Check if Take Profit or Stop Loss conditions are met
if (lastPrice >= takeProfitPrice || lastPrice <= stopLossPrice) {
return true;
} else {
return false;
}
}

public static void main(String[] args) {
// Sample data for prices (replace with real data)
List<Double> prices = new ArrayList<>();

// Add sample data (replace with real data)
prices.add(100.0);
prices.add(102.0);
prices.add(105.0);
prices.add(110.0);
prices.add(108.0);
prices.add(112.0);

// Generate the trade signal
String signal = tradeSignal(prices);
System.out.println("Trade Signal: " + signal);

// Assuming the signal is to "Buy Long 1 Futures Contract" and entry price is 110.0
double entryPrice = 110.0;

// Check exit signal
boolean exitSignal = checkExitSignal(prices, entryPrice);
System.out.println("Exit Signal: " + (exitSignal ? "Exit position" : "Continue holding"));
}
}

That is all very greek to me... I have tried to learn some coding but my brain just doesn't seem to be wired that way.....
 

Aionda

Member
Joined
Dec 26, 2022
Posts
17
Likes
1
Has anyone tried using one of the AI's to write or convert other code to java?
Might be what we are looking for to simplify writing java strategies and studies.
It works fine for small part of codes, like creating a method which detects an inverse hammer candle etc...
 

q0paz

Active member
Joined
Aug 19, 2020
Posts
27
Likes
7
What I had in mind was to be able to convert trade station or ninja trader systems and strategies code into something I can use in MotiveWave....
i have used labs.perplexity.ai to convert C# to java. I would say a basic understanding of the whole java/maven/compiling process is needed.
In general I am quite amazed at how it answers my questions but it never shows what libraries it's referencing to produce the code and quite often it's just plain wrong. But it usually gets you on your way.
As an aside, it really depends on your programming knowledge to start with. I gave up using java and the eclipse/Ant setup found on these forums and have now migrated to using kotlin and gradle, which is not still stuck in 2015 like java. Kotlin is a far superior language. Almost as good as C#. ;)
 

Aionda

Member
Joined
Dec 26, 2022
Posts
17
Likes
1
i have used labs.perplexity.ai to convert C# to java. I would say a basic understanding of the whole java/maven/compiling process is needed.
In general I am quite amazed at how it answers my questions but it never shows what libraries it's referencing to produce the code and quite often it's just plain wrong. But it usually gets you on your way.
As an aside, it really depends on your programming knowledge to start with. I gave up using java and the eclipse/Ant setup found on these forums and have now migrated to using kotlin and gradle, which is not still stuck in 2015 like java. Kotlin is a far superior language. Almost as good as C#. ;)
We have huge discussions about Gradle vs Maven in the company where I work.
 

q0paz

Active member
Joined
Aug 19, 2020
Posts
27
Likes
7
We have huge discussions about Gradle vs Maven in the company where I work.
xml was developed in 1998. It was everywhere, especially in Microsoft, and .NET. The fact that even they have "dropped" it and embraced json tells us something. Maven xml hurts my eyes. The fact that Gradle can be built using kotlin itself for me is a huge plus. I have seen metrics showing teams that code with kotlin and build with gradle are far more productive...probably not massively relevant for this forum but interesting nonetheless...
 

Aionda

Member
Joined
Dec 26, 2022
Posts
17
Likes
1
Yes, you are right. However Java is easier to learn for most people, as it has less keywords and Code generators.
 
Top