The CalcTradePrice method should be called right after Trade object's initialization. Right now, it is being called before that, which leads to an exception:
System.DivideByZeroException: 'Attempted to divide by zero.'
var trade = new Trade
{
Pair = pair,
OpenRate = buyCandle.Close,
OpenDate = buyCandle.OpenTime,
Amount = 12345,
OpenFee = _backtestOptions.OpenFee,
CloseFee = _backtestOptions.CloseFee,
IsOpen = true
};
I know I can manually call the method after the initialization, just like that:
var trade = new Trade
{
Pair = pair
};
CalcTradePrice();
or to create a derived class which executes it, but I don't want to. Any recommendations?
public class Trade
{
public string Pair { get; set; }
public decimal OpenRate { get; set; }
public decimal CloseRate { get; set; }
public DateTime OpenDate { get; set; }
public DateTime CloseDate { get; set; }
public decimal OpenTradePrice { get; set; }
public decimal Amount { get; set; }
public decimal OpenFee { get; set; }
public decimal CloseFee { get; set; }
public SellType SellType { get; set; }
public Trade()
{
CalcTradePrice();
}
public void CalcTradePrice()
{
this.OpenTradePrice = ... // DivideByZeroException because all elements are null at that moment
}
}
You can create parameterized constructor. Assign all properties through parameters passed to constructor.Use these properties to calculate trade price in CalcTradePrice()
function.
public class Trade
{
...
//I just passed two properties as an example, you can pass all required properties.
public Trade(int currentBalance, int candleCloseValue)
{
this.CurrentBalance = currentBalance;
this.CandleCloseValue= candleCloseValue;
CalcTradePrice();
}
public void CalcTradePrice()
{
this.OpenTradePrice = this.CurrentBalance/this.CandleCloseValue;
}
}
While creating object of Trade class, pass values as a parameter like,
var currentBalance = 100;
var candleCloseValue = 20;
Trade trade = new Trade(currentBalance , candleCloseValue);
Note: I used above two properties just to show, how you can assign property values before calling CalcTradePrice()
function.
What about this:
public class Trade
{
public string Pair { get; set; }
public decimal OpenRate { get; set; }
public decimal CloseRate { get; set; }
public DateTime OpenDate { get; set; }
public DateTime CloseDate { get; set; }
public decimal OpenTradePrice { get; set; }
public decimal Amount { get; set; }
public decimal OpenFee { get; set; }
public decimal CloseFee { get; set; }
public SellType SellType { get; set; }
public Trade(buyCandle,_backtestOptions, ..and so on )
{
Pair = pair,
OpenRate = buyCandle.Close;
OpenDate = buyCandle.OpenTime;
Amount = currentBalance / buyCandle.Close;
OpenFee = _backtestOptions.OpenFee;
CloseFee = _backtestOptions.CloseFee;
IsOpen = true;
CalcTradePrice();
}
};
Your exception System.DivideByZeroException: 'Attempted to divide by zero.'
may be triggered by
Amount = currentBalance / buyCandle.Close
rather than your constructor call.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.