Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

76
LINES

< > BotCompany Repo | #1036449 // G22CandleBasedStrategy

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (30795L/194K).

sclass G22CandleBasedStrategy extends G22TradingStrategy {
  settableWithVar double granularity = 1; // candle granularity in minutes
  settableWithVar TradingCandleMaker candleMaker;
  
  // max number of candles to keep around (only used when first
  // creating candle maker)
  settableWithVar int maxCandles = 1000;
  
  // first (usually partial) candle that will be ignored
  //settableWithVar TradingCandle firstCandle;
  
  // Last completed candle
  settableWithVar TradingCandle completedCandle;
  
  // A candle has been completed
  event candleCompleted(TradingCandle candle);
  event newPrice(double price);
  
  // override me
  L<CandleBasedIndicator> indicators() { ret new L; }
  
  // For testing - feed candles directly
  void feed(Iterable<TradingCandle> candles) {
    fOr (candle : candles) try {
      currentTime(candle.endTime.toLong());
      gotCandle(candle);
    } finally {
      afterStep();
    }
  }
  
  void gotCandle(TradingCandle candle) {
    completedCandle(candle);
    fOr (indicator : indicators())
      indicator?.feed(candle);
    candleCompleted(candle);
  }
  
  // Normal operation from ticker
  void price(double price) {
    currentPrice = price;
    if (hasClosedItself()) ret;
    
    newPrice(price);
    
    //printVars("G22CandleBasedStrategy.price", +price);
    try {
      createCandleMaker();
        
      var completed = candleMaker.completedCandle();
      candleMaker.add(price, currentTime());
      /*var candle = candleMaker.currentCandle();
      /if (firstCandle == null)
        firstCandle(candle);
      else if (candle != firstCandle && ... */
      if (completed != completedCandle())
        gotCandle(completed);
    } finally {
      afterStep();
    }
  }
  
  S fieldsToReset() {
    ret lineCombine(super.fieldsToReset(), [[candleMaker firstCandle completedCandle]]);
  }
  
  L<TradingCandle> candles() { ret candleMaker().candles(); }
  
  // How many candles we need to see before the strategy can start making positions (override me)
  int candlesNeededBeforeOperational() { ret 0; }
  
  void createCandleMaker {
    if (candleMaker == null)
      candleMaker(new TradingCandleMaker().candleLength(granularity)).maxCandles(maxCandles);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036449
Snippet name: G22CandleBasedStrategy
Eternal ID of this version: #1036449/29
Text MD5: 6499173596bbc1853b91c490b94d0cbd
Transpilation MD5: a6154b2258b467cdf8a7404e53c303e3
Author: stefan
Category: javax / trading
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-03-28 19:12:38
Source code size: 2303 bytes / 76 lines
Pitched / IR pitched: No / No
Views / Downloads: 173 / 322
Version history: 28 change(s)
Referenced in: #1003674 - Standard Classes + Interfaces (LIVE continued in #1034167)
#1036462 - G22CandleBasedStrategyWithJuicer