Gamestudio Links
Zorro Links
Newest Posts
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 840 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 3 of 3 1 2 3
Re: Zorro Trader GPT [Re: TipmyPip] #488158
04/01/24 12:06
04/01/24 12:06
Joined: Sep 2017
Posts: 82
T
TipmyPip Offline OP
Junior Member
TipmyPip  Offline OP
Junior Member
T

Joined: Sep 2017
Posts: 82
Market Volatility: Determine the market's volatility to adjust the sensitivity of our fuzzy logic conditions. High volatility might require more stringent conditions to enter or exit a trade to avoid false signals.

Trend Confirmation: Use a combination of risingF and fallingF functions to confirm the strength and direction of the market trend.

Entry Condition: Enter a trade based on a confirmed upward trend or after a significant market bottom (valleyF), adjusted for market volatility.

Exit Condition: Exit a trade based on a confirmed downward trend or after a significant market peak (peakF), also adjusted for market volatility.



Code
#include <contract.c>

vars PriceClose;
var Volatility;

void initFuzzyLogicSettings() {
  FuzzyRange = 0.05; // Adjust based on backtesting
}

var calculateVolatility(vars Data, int period) {
  var sumDelta = 0;
  for(int i = 1; i <= period; i++) {
    sumDelta += abs(Data[i] - Data[i-1]);
  }
  return sumDelta / period;
}

var adjustFuzzyForVolatility(var fuzzyValue) {
  var adjustmentFactor = 1 + Volatility * 10;
  return clamp(fuzzyValue / adjustmentFactor, 0, 1);
}

bool fuzzyEntryCondition(vars Data) {
  var entryFuzzy = max(risingF(Data), valleyF(Data));
  return adjustFuzzyForVolatility(entryFuzzy) > 0.5;
}

bool fuzzyExitCondition(vars Data) {
  var exitFuzzy = max(fallingF(Data), peakF(Data));
  return adjustFuzzyForVolatility(exitFuzzy) > 0.5;
}

void optionComboTrade() {
  CONTRACT* C1; // Define pointer for the first leg of the combo
  CONTRACT* C2; // Define pointer for the second leg of the combo
  
  // Dynamically adjust option strike based on current price and volatility
  var strikeCall = round(priceClose(0) + 10 + Volatility * 5); // Example for call strike
  var strikePut = round(priceClose(0) - 10 - Volatility * 5); // Example for put strike
  
  // Initialize contracts for a strangle combo
  C1 = contractFind(CALL, 30, strikeCall); // Find call option contract
  C2 = contractFind(PUT, 30, strikePut); // Find put option contract

  // Check if contracts are found and if entry condition is met
  if(C1 && C2 && fuzzyEntryCondition(PriceClose)) {
    combo(C1, 1, C2, 1, 0, 0, 0, 0); // Create a strangle combo
    enterLong(comboLeg(1)); // Enter long on both legs of the combo
    enterLong(comboLeg(2));
  }
}

void run() {
  BarPeriod = 60;
  LookBack = 100;
  StartDate = 2020;
  EndDate = 2024;
  assetList("AssetsIB");
  asset("SPY");

  PriceClose = series(priceClose());
  initFuzzyLogicSettings();

  Volatility = calculateVolatility(PriceClose, 20); // Calculate market volatility

  if (is(EXITRUN)) return;

  optionComboTrade(); // Execute the option combo trade based on fuzzy logic conditions
}

Last edited by TipmyPip; 04/01/24 21:14.

ZorroTraderGPT - https://bit.ly/3Gbsm4S
Re: Zorro Trader GPT [Re: TipmyPip] #488159
04/01/24 12:23
04/01/24 12:23
Joined: Sep 2017
Posts: 82
T
TipmyPip Offline OP
Junior Member
TipmyPip  Offline OP
Junior Member
T

Joined: Sep 2017
Posts: 82
The Following strategy calculates the EMA of ATR at the beginning of the run to establish a volatility baseline.
It then iterates through selected assets (SPY, AAPL, MSFT), applying its entry and exit logic to each based on the current market conditions as interpreted through its fuzzy logic framework.
Trades are made based on the composite fuzzy logic conditions, with the strategy entering long positions when the entry conditions are met and exiting positions when the exit conditions are met.


Code
#include <contract.c>

vars PriceClose, VolatilityEMA;

void init() {
  assetList("AssetsIB");
  BarPeriod = 60; // Setting bar period for the price series
  LookBack = 100; // Lookback period for technical indicators

  PriceClose = series(priceClose());
  initFuzzyLogicSettings();
  calculateEMAVolatility(20); // Initialize volatility calculation
}

void initFuzzyLogicSettings() {
  FuzzyRange = 0.05; // Adjust based on backtesting
}

void calculateEMAVolatility(int period) {
  vars ATRValues = series(ATR(period));
  VolatilityEMA = series(EMA(ATRValues, period));
}

void tradeOptions() {
  // Assuming current asset is SPY and we're setting up a strangle
  if (NumOpenLong == 0 && fuzzyEntryCondition()) {
    // Find contracts for the call and put options
    CONTRACT* callContract = contractFind("Call", 30, priceClose(0) + 10); // Example: 30 days to expiration, strike 10 points above
    CONTRACT* putContract = contractFind("Put", 30, priceClose(0) - 10); // Example: 30 days to expiration, strike 10 points below

    if(callContract && putContract) {
      // Setup the combo - buying 1 call and 1 put
      combo(callContract, 1, putContract, 1, 0, 0, 0, 0);
      
      // Enter the combo trade
      enterLong(comboLeg(1)); // Enter long for the call option leg
      enterLong(comboLeg(2)); // Enter long for the put option leg
      
      printf("\nEntered a Strangle on SPY");
    }
  }
}

void run() {
  StartDate = 2020;
  EndDate = 2024;
  assetList("AssetsIB");
  asset("SPY");

  while(asset(loop("SPY","AAPL","MSFT"))) {
    if (is(EXITRUN)) continue;
    tradeOptions();
  }
}

Last edited by TipmyPip; 04/01/24 21:27.

ZorroTraderGPT - https://bit.ly/3Gbsm4S
Re: Zorro Trader GPT [Re: TipmyPip] #488160
04/01/24 21:39
04/01/24 21:39
Joined: Sep 2017
Posts: 82
T
TipmyPip Offline OP
Junior Member
TipmyPip  Offline OP
Junior Member
T

Joined: Sep 2017
Posts: 82
The Following Strategy will Adapt to changing market conditions using both trend analysis and volatility measurements to inform trade decisions.

Risk Management: Incorporates measures of market volatility into trade decision processes, although explicit risk management tactics (e.g., position sizing, stop-loss orders) are not detailed within the provided summary.

Theoretical Pricing: Utilizes the Black-Scholes model for option valuation, aiding in the identification of potentially mispriced options, though the direct application of this pricing within the trade execution process is implied rather than explicitly defined.

Code
#include <contract.h>

// Initialize global variables for storing price and volatility data
vars PriceClose, VolatilityEMA;

void init() {
  assetList("AssetsIB");
  BarPeriod = 60;
  LookBack = 100;
  StartDate = 2020;
  EndDate = 2024;
  
  PriceClose = series(priceClose());
  initFuzzyLogicSettings();
  calculateEMAVolatility(20); // Initialize volatility calculation
}

void initFuzzyLogicSettings() {
  FuzzyRange = 0.05; // Adjust based on backtesting
}

void calculateEMAVolatility(int period) {
  vars ATRValues = series(ATR(period));
  VolatilityEMA = series(EMA(ATRValues, period));
}

// Cumulative Normal Distribution Function for use in Black-Scholes Model
var CNDF(var x) {
  int neg = (x < 0.0) ? 1 : 0;
  if (neg) x = -x;
  
  var k = (1.0 / ( 1.0 + 0.2316419 * x));
  var y = (((1.330274429 * k - 1.821255978) * k + 1.781477937) *
           k - 0.356563782) * k + 0.319381530;
  y = 1.0 - 0.398942280401 * exp(-0.5 * x * x) * y;
  
  return (1.0 - neg) * y + neg * (1.0 - y);
}

// Black-Scholes Option Pricing Model
var BlackScholes(char *CallPutFlag, var S, var X, var T, var r, var v) {
  var d1, d2;
  d1 = (log(S / X) + (r + 0.5 * v * v) * T) / (v * sqrt(T));
  d2 = d1 - v * sqrt(T);
  
  if (CallPutFlag[0] == 'c' || CallPutFlag[0] == 'C') {
    return S * CNDF(d1) - X * exp(-r * T) * CNDF(d2);
  } else {
    return X * exp(-r * T) * CNDF(-d2) - S * CNDF(-d1);
  }
}

bool fuzzyEntryCondition() {
  return risingF(PriceClose) > 0.5 && VolatilityEMA[0] < 0.2;
}

bool fuzzyExitCondition() {
  return fallingF(PriceClose) > 0.5 || VolatilityEMA[0] > 0.3;
}

void tradeOptions() {
  CONTRACT* CallContract;
  CONTRACT* PutContract;
  var strikePrice = round(priceClose(0),100); // Example for rounding to nearest hundred
  var expirationDays = 30; // Targeting options 30 days to expiration

  if (fuzzyEntryCondition()) {
    // Prepare to trade a call option
    CallContract = contractFind("Call", strikePrice, wdate() + expirationDays * 86400);
    if(CallContract) {
      // Buy the call option
      combo(CallContract, 1, 0, 0, 0, 0, 0, 0);
      enterLong(comboLeg(1));
    }
  } else if (fuzzyExitCondition()) {
    // Prepare to trade a put option
    PutContract = contractFind("Put", strikePrice, wdate() + expirationDays * 86400);
    if(PutContract) {
      // Buy the put option
      combo(0, 0, PutContract, 1, 0, 0, 0, 0);
      enterLong(comboLeg(2));
    }
  }
}

void main() {
  init();
  while(asset(loop("Assets"))) {
    if (is(EXITRUN)) continue;
    tradeOptions(); // Execute the option trading logic based on market conditions
  }
}


ZorroTraderGPT - https://bit.ly/3Gbsm4S
Re: Zorro Trader GPT [Re: TipmyPip] #488164
04/05/24 04:51
04/05/24 04:51
Joined: Dec 2014
Posts: 206
Germany
Smon Offline
Member
Smon  Offline
Member

Joined: Dec 2014
Posts: 206
Germany
Hi!

GPT-4 seems not to be the best choice when it comes to coding any longer. I'm developing code with Claude-3 Sonnet (free version), and it's really great. I need to itereate less often compared to GPT-4. It has 200.000 tokens context length and at least the biggest model (Claude-3 Opus) is told to have almost perfect recall capabilities. I found a way to access Claude-3 Opus from a restricted country: https://poe.com/
You can even build your custom GPTs there. I wonder, how you fed ChatGPT the knowledge about Zorro. Did you preprocess the manual, aka summarize it?

More info about Claude-3: https://www.anthropic.com/news/claude-3-family

Last edited by Smon; 04/05/24 04:57.
Re: Zorro Trader GPT [Re: Smon] #488165
04/05/24 06:35
04/05/24 06:35
Joined: Sep 2017
Posts: 82
T
TipmyPip Offline OP
Junior Member
TipmyPip  Offline OP
Junior Member
T

Joined: Sep 2017
Posts: 82
Thank you for sharing the information, I suppose that you need more input in order that GPT4 will focus on your intentions and get the right results, but running a free version needs a serious graphics card, and getting 40 questions in 3 hours is quite hard without computing power. I have a number of ideas on how to feed ChatGPT, but I am quite sure it is improving.

Last edited by TipmyPip; 04/05/24 07:08.

ZorroTraderGPT - https://bit.ly/3Gbsm4S
Re: Zorro Trader GPT [Re: TipmyPip] #488166
04/06/24 05:12
04/06/24 05:12
Joined: Dec 2014
Posts: 206
Germany
Smon Offline
Member
Smon  Offline
Member

Joined: Dec 2014
Posts: 206
Germany
I believe there was a misunderstanding. The Claude3-Sonnet is hosted yet remains free of charge. However, it is not an open-source model. I was considering the possibility of integrating the entire manual into a custom GPT, but I wanted to consult with you first on how you managed to do it. Actually, I began exploring this concept several months ago and even developed a fine-tuned version of GPT-3.5 by converting the manual into question-answer pairs for the training dataset. It appears that this dataset was likely too small, leading me to abandon the fine-tuning process. It seems that retrieval-augmented generation might be the better approach.

Re: Zorro Trader GPT [Re: TipmyPip] #488167
04/06/24 08:11
04/06/24 08:11
Joined: Sep 2017
Posts: 82
T
TipmyPip Offline OP
Junior Member
TipmyPip  Offline OP
Junior Member
T

Joined: Sep 2017
Posts: 82
Wow, that is great free use of LLM without paying for the service, I am quite sure, Claude3 is much better. But Custom Agents API is already coming out. Which will increase the accuracy of the answers.

In addition, I use Mathematica, and Wolfram with OpenAI, so Claude3 will not have any advantage in my case, But Thank you for sharing the information.


ZorroTraderGPT - https://bit.ly/3Gbsm4S
Re: Zorro Trader GPT [Re: Smon] #488228
04/27/24 13:50
04/27/24 13:50
Joined: Sep 2017
Posts: 82
T
TipmyPip Offline OP
Junior Member
TipmyPip  Offline OP
Junior Member
T

Joined: Sep 2017
Posts: 82
"I believe there was a misunderstanding. The Claude3-Sonnet is hosted yet remains free of charge. However, it is not an open-source model. I was considering the possibility of integrating the entire manual into a custom GPT, but I wanted to consult with you first on how you managed to do it. Actually, I began exploring this concept several months ago and even developed a fine-tuned version of GPT-3.5 by converting the manual into question-answer pairs for the training dataset. It appears that this dataset was likely too small, leading me to abandon the fine-tuning process. It seems that retrieval-augmented generation might be the better approach.

well, now I believe you, and I have started making some changes.


ZorroTraderGPT - https://bit.ly/3Gbsm4S
Page 3 of 3 1 2 3

Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1