Not logged in
Viewing 4 posts - 1 through 4 (of 4 total)
  • #114511|
    Customer
    484 Posts

    Plese copy code snippets here.

    #134359
    Customer
    484 Posts

    Money management: balance or equity needed to trade 1 whole lot:-

    
    package com.strategyquant.extend.MoneyManagementTypes;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import com.strategyquant.futurelib.backtester.SymbolInfo;
    import com.strategyquant.futurelib.backtester.SymbolsTable;
    import com.strategyquant.lib.computers.OrderPLComputer;
    import com.strategyquant.lib.language.L;
    import com.strategyquant.lib.results.SQOrder;
    import com.strategyquant.lib.results.SQOrderList;
    import com.strategyquant.lib.snippets.MoneyManagementSim.MoneyManagementType;
    import com.strategyquant.lib.utils.SQUtils;
    
    public class BalancePerLot extends MoneyManagementType {
    public static final Logger Log = LoggerFactory.getLogger("BalancePerLot");
    
    private double mmBalance = -1;
    private double mmMaxLots;
    
    private int mmLots;
    
    public BalancePerLot() {
    setName("Balance per lot");
    setNote(L.tHtml("Balance per lot"));
    
    addDoubleParameter("Balance", "Balance in $", 50000d, 1d, 10000d, 10d, L.t("How much balance the system/portfolio must have in order to trade 1 whole lot?"));
    addDoubleParameter("Lots", "Lots if no MM", 1d, 0.01d, 1000000d, 0.1d, L.t("How many lots should be traded if we disable or cannot use Money Management - for example when computed trade size is 0"));
    addDoubleParameter("MaxLots", "Maximum lots", 5d, 0.01d, 1000000d, 0.1d, L.t("The biggest lot size allowed"));
    
    setFormatedName("${Balance} per whole lot");
    }
    
    @Override
    public void init(SQOrderList originalOrderList, SymbolsTable symbolsTable) throws Exception {
    mmBalance = this.getDoubleParameterValue("Balance");
    mmLots = this.getIntParameterValue("Lots");
    mmMaxLots = this.getDoubleParameterValue("MaxLots");
    }
    
    @Override
    public double computeTradeSize(SQOrder order, double tickSize, double pointValue) throws Exception {
    if(mmBalance < 0) {
    throw new Exception("Money management wasn't properly initialized. Call init() method before computing trade size!");
    }
    
    double tradeSize = 0;
    double orderSL;
    
          tradeSize = equity / mmBalance ; 
    
    Log.info("trade size computed to: "+tradeSize);
    
    // round computed trade size to give decimal points and cap it by maximum lots
    tradeSize = SQUtils.roundDown(tradeSize, getDecimals());
    if(tradeSize  mmMaxLots) {
    tradeSize = mmMaxLots;
    }
    
    return tradeSize;
    }
    }
    
    
    #134480
    Customer
    358 Posts

     

    Money management: balance or equity needed to trade 1 whole lot:-

    
    package com.strategyquant.extend.MoneyManagementTypes;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import com.strategyquant.futurelib.backtester.SymbolInfo;
    import com.strategyquant.futurelib.backtester.SymbolsTable;
    import com.strategyquant.lib.computers.OrderPLComputer;
    import com.strategyquant.lib.language.L;
    import com.strategyquant.lib.results.SQOrder;
    import com.strategyquant.lib.results.SQOrderList;
    import com.strategyquant.lib.snippets.MoneyManagementSim.MoneyManagementType;
    import com.strategyquant.lib.utils.SQUtils;
    
    public class BalancePerLot extends MoneyManagementType {
    public static final Logger Log = LoggerFactory.getLogger("BalancePerLot");
    
    private double mmBalance = -1;
    private double mmMaxLots;
    
    private int mmLots;
    
    public BalancePerLot() {
    setName("Balance per lot");
    setNote(L.tHtml("Balance per lot"));
    
    addDoubleParameter("Balance", "Balance in $", 50000d, 1d, 10000d, 10d, L.t("How much balance the system/portfolio must have in order to trade 1 whole lot?"));
    addDoubleParameter("Lots", "Lots if no MM", 1d, 0.01d, 1000000d, 0.1d, L.t("How many lots should be traded if we disable or cannot use Money Management - for example when computed trade size is 0"));
    addDoubleParameter("MaxLots", "Maximum lots", 5d, 0.01d, 1000000d, 0.1d, L.t("The biggest lot size allowed"));
    
    setFormatedName("${Balance} per whole lot");
    }
    
    @Override
    public void init(SQOrderList originalOrderList, SymbolsTable symbolsTable) throws Exception {
    mmBalance = this.getDoubleParameterValue("Balance");
    mmLots = this.getIntParameterValue("Lots");
    mmMaxLots = this.getDoubleParameterValue("MaxLots");
    }
    
    @Override
    public double computeTradeSize(SQOrder order, double tickSize, double pointValue) throws Exception {
    if(mmBalance < 0) {
    throw new Exception("Money management wasn't properly initialized. Call init() method before computing trade size!");
    }
    
    double tradeSize = 0;
    double orderSL;
    
          tradeSize = equity / mmBalance ; 
    
    Log.info("trade size computed to: "+tradeSize);
    
    // round computed trade size to give decimal points and cap it by maximum lots
    tradeSize = SQUtils.roundDown(tradeSize, getDecimals());
    if(tradeSize  mmMaxLots) {
    tradeSize = mmMaxLots;
    }
    
    return tradeSize;
    }
    }
    
    

    And what is it good for? :huh:

    #134487
    Customer
    484 Posts

    And what is it good for? :huh:

     

    Money management: balance or equity needed to trade 1 whole lot.      :blink:   

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.