Volume without calculating commissions :/
Volume without calculating commissions :/
18 Oct 2022, 19:12
Hi everyone, I'm new to programming with calgo, so I ask for your help, thanks in advance!
I found the code to enter a direct market order, but it seems that the code lacks the way to take into account the commissions when calculating the lottery, which is done precisely without taking into account the commissions. Anyone help me?
Here is the code:
Creator: ajinori
using
cAlgo.API;
using
cAlgo.API.Internals;
using
System;
//===========================================================================================================================================
// ClickEntry
//===========================================================================================================================================
namespace
cAlgo.Robots {
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public
class
ClickEntry : Robot {
// --- min lots
[Parameter(
"Min Lots"
, DefaultValue = 0.01, MinValue = 0.01, Step = 0.1)]
public
double
MinLots {
get
;
set
; }
// --- max lots
[Parameter(
"Max Lots"
, DefaultValue = 100, MinValue = 0.01, Step = 0.1)]
public
double
MaxLots {
get
;
set
; }
// --- risk%
[Parameter(
"Risk Percent"
, DefaultValue = 0.5, MinValue = 0.001, MaxValue = 20, Step = 0.1)]
public
double
RiskPercent {
get
;
set
; }
// --- risk amount
[Parameter(
"Risk Amount"
, DefaultValue = 1000, MinValue = 1, Step = 0.5)]
public
double
RiskAmount {
get
;
set
; }
// --- RRR
[Parameter(
"Risk Reward Ratio"
, DefaultValue = 2, MinValue = 0.01)]
public
double
RiskRewardRatio {
get
;
set
; }
// --- commissions
[Parameter(
"Commissions"
, DefaultValue = 5, MinValue = 1, Step = 0.01)]
public
double Commissions
{
get
;
set
; }
//---------
// OnStart
protected
override
void
OnStart() {
var sltpSetter =
new
ClickEntrySupport(Chart, RiskRewardRatio);
sltpSetter.Fixed += (obj, args) => Order(args.StopLoss, args.TakeProfit);
}
//--------
// order
private
void
Order(
double
stopPrice,
double
limitPrice) {
TradeType tt = (stopPrice < limitPrice ? TradeType.Buy : TradeType.Sell);
var price = (tt == TradeType.Buy ? Ask : Bid);
var digit = (
int
)(Math.Log10(Symbol.PipSize) - Math.Log10(Symbol.TickSize));
var stopPips = Math.Round(Math.Abs(price - stopPrice) / Symbol.PipSize, digit);
var limitPips = Math.Round(Math.Abs(price - limitPrice) / Symbol.PipSize, digit);
// --- calc volume
var permitableLoss = Math.Min((Account.Balance) * RiskPercent / 100, RiskAmount);
var volume = permitableLoss / stopPips / Symbol.PipValue;
volume = Symbol.NormalizeVolumeInUnits(volume);
if
(volume < Symbol.QuantityToVolumeInUnits(MinLots)) volume = Symbol.QuantityToVolumeInUnits(MinLots);
if
(volume > Symbol.QuantityToVolumeInUnits(MaxLots)) volume = Symbol.QuantityToVolumeInUnits(MaxLots);
// --- order
var res = ExecuteMarketOrder(tt, SymbolName, volume, ToString(), stopPips, limitPips);
if
(!res.IsSuccessful) {
Print(res.Error.ToString());
}
// --- stop
Stop();
}
}
//==================================================
// ClickEntrySupport
// for decide SL and TP at intaractive
//==================================================
internal
class
ClickEntrySupport :IDisposable {
public
event
EventHandler<PriceFixecEventArgs> Fixed;
public
double
RiskRewardRatio {
get
;
set
; }
private
Point _mouseDown =
new
Point(0, 0);
private
ChartHorizontalLine _stopLine;
private
ChartHorizontalLine _limitLine;
private
const
string
STOP_LINE_NAME =
"EasyEntryStopLine"
;
private
const
string
LIMIT_LINE_NAME =
"EasyEntryLimitLine"
;
private
Chart _chart;
public
ClickEntrySupport(Chart chart,
double
riskRewardRatio) {
RiskRewardRatio = riskRewardRatio;
_chart = chart;
_stopLine = _chart.DrawHorizontalLine(STOP_LINE_NAME, 0, Color.Red, 1, LineStyle.Dots);
_limitLine = _chart.DrawHorizontalLine(LIMIT_LINE_NAME, 0, Color.Green, 1, LineStyle.Dots);
_chart.MouseMove += OnMove;
_chart.MouseDown += OnDown;
_chart.MouseUp += OnUp;
}
//----------
// Dispose
public
void
Dispose() {
_chart.RemoveObject(_stopLine.Name);
_chart.RemoveObject(_limitLine.Name);
_chart.MouseUp -= OnUp;
_chart.MouseDown -= OnDown;
_chart.MouseMove -= OnMove;
}
//-----------
// for event
private
void
OnUp(ChartMouseEventArgs args) {
if
(_mouseDown ==
new
Point(args.MouseX, args.MouseY)) {
Fixed(
this
,
new
PriceFixecEventArgs(_stopLine.Y, _limitLine.Y));
Dispose();
}
}
private
void
OnDown(ChartMouseEventArgs args) {
_mouseDown =
new
Point(args.MouseX, args.MouseY);
}
private
void
OnMove(ChartMouseEventArgs args) {
_stopLine.Y = args.YValue;
var bid = _chart.Bars.LastBar.Close;
_limitLine.Y = bid + (bid - args.YValue) * RiskRewardRatio;
}
}
//============
// EventArgs
//============
internal
class
PriceFixecEventArgs : EventArgs {
public
double
StopLoss, TakeProfit;
public
PriceFixecEventArgs(
double
sl,
double
tp) { StopLoss = sl; TakeProfit = tp; }
}
}