0

私は取引システムを実装していますが、ライブ取引とバックテストの両方に同じコードを使用したいと思います。ライブ取引を行うときは、発生するすべてを永続化する必要がありますが、バックテストするときは、同じクラスとロジックを使用したいのですが、永続化しないでください(速度と保存する理由がないため)。

それらに対してsave()を呼び出さないだけでこれを実行できると思いましたが、問題が発生しました。私は、PositionオブジェクトのhasManyコレクションを含むAccountクラスを持っています。問題は、アカウントで正しい位置を見つける必要があることですが、保存されていないため、見つけられないようです。hasManyのPositionsコレクションを反復処理しようとしましたが、Positionオブジェクトを作成し、addToPositions()を使用してアカウントに追加したにもかかわらず、そこには何もありません。私が避けようとしているsave()の呼び出しが終わるまで、これは意味がないと思います。

永続的な目的と一時的な目的の両方で同じクラスを再利用する方法はありますか、それとも不自然な行為をしようとしていますか?両方のシナリオでこれらのクラスを再利用する方法についての提案を歓迎します。

更新(コードサンプルの追加):

Account.groovy:

class Account {
    String name
    static belongsTo = [user:S2User]
    static hasMany = [positions:Position, transactions:Transaction]
}

SimAccount.groovy:

class SimAccount extends Account {
}

Position.groovy:

class Position implements Serializable {
    String symbol
    static belongsTo = [account:Account]
}

BacktestController.groovy:

        // add Account
        def acct = new SimAccount(name:"backtest")
        // add position
        def pos = new Position(symbol:'SIRI')
        acct.addToPositions(pos)

Strategy.groovy:

    println "apply($accounts, $quotes) [$name]"
    accounts.each{ a ->
        println "  acct $a (${a?.name})"
        a?.positions?.each{ p ->
            println "    pos: $p"
        }
    }

    def siri = Position.findByAccountAndSymbol(accounts[0], 'SIRI')
    println "siri pos: $siri"

オブジェクトが保存されたときのStrategy.groovyからの出力は次のとおりです。

apply([com.lossless.account.SimAccount : 1, null], [:]) [Live SIRI 50k]
  acct com.lossless.account.SimAccount : 1 (sim1)
    pos: com.lossless.account.Position : 1
  acct null (null)
siri pos: com.lossless.account.Position : 1

オブジェクトが保存されていない場合の出力は次のとおりです。

apply([com.lossless.account.SimAccount : null, null], [bid:1.74, ask:1.74]) [Backtest SIRI 50k]
  acct com.lossless.account.SimAccount : null (backtest)
    pos: com.lossless.account.Position : null
  acct null (null)
| Error 2013-01-21 03:11:11,984 [http-bio-8080-exec-2] ERROR errors.GrailsExceptionResolver  - TransientObjectException occurred when processing request: [POST] /lossless/backtest/index - parameters:
object references an unsaved transient instance - save the transient instance before flushing: com.lossless.account.Account. Stacktrace follows:
Message: object references an unsaved transient instance - save the transient instance before flushing: com.lossless.account.Account
   Line | Method
->> 105 | doCall         in org.grails.datastore.gorm.GormStaticApi$_methodMissing_closure2
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    66 | apply          in com.lossless.Strategy

Strategy.groovyの66行目はへの呼び出しPosition.findByAccountAndSymbol()です。

4

0 に答える 0