How to call custom indicator into bot
How to call custom indicator into bot
12 Jul 2021, 13:13
Good day,
I am having trouble calling a custom indicator and its parameters to a bot.
Since cTrader doesn't a built-in VWAP, I have found a basic one to implement.
I tried following the API guide, but can't seem to come right.
Here is the current code for the VWAP indicator:
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = true, AutoRescale = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class SampleVWAP : Indicator
{
[Parameter("Source")]
public DataSeries Source { get; set; }
[Output("Main", LineColor = "Orange", Thickness = 2)]
public IndicatorDataSeries Result { get; set; }
private TypicalPrice typ;
public IndicatorDataSeries tpv;
private SampleVWAP _SampleVWAP;
public double CTPV, CV;
protected override void Initialize()
{
_SampleVWAP = Indicators.GetIndicator<SampleVWAP>();
CTPV = 0;
CV = 0;
typ = Indicators.TypicalPrice();
tpv = CreateDataSeries();
}
public override void Calculate(int index)
{
tpv[index] = typ.Result[index] * Bars.TickVolumes[index];
CTPV = 0;
CV = 0;
int per = 0;
int day = Bars.OpenTimes[index].Day;
while (Bars.OpenTimes[index - per].Day == day)
{
per++;
CTPV += tpv[index - per];
CV += MarketSeries.TickVolume[index - per];
}
Result[index] = CTPV / CV;
}
}
}
I am trying to call the indicator into this reference bot:
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace Reference_VWAP
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
public class cAlgo : Robot
{
private double _volumeInUnits;
[Parameter("Label", DefaultValue = "Bagmaker")]
public string Label { get; set; }
[Parameter("Starting Volume (Lots)", DefaultValue = 1.0, MinValue = 0.01)]
public double StartingVolumeInLots { get; set; }
[Parameter(DefaultValue = 10, MinValue = 1)]
public int StopLoss { get; set; }
[Parameter(DefaultValue = 15, MinValue = 1)]
public int TakeProfit { get; set; }
private bool VWAP_Bias_Buy;
private bool VWAP_Bias_Sell;
protected override void OnStart()
{
_volumeInUnits = Symbol.QuantityToVolumeInUnits(StartingVolumeInLots);
VWAP_Bias_Buy = false;
VWAP_Bias_Sell = false;
}
protected override void OnBar()
{
var ActiveBuy = Positions.Find(Label, SymbolName, TradeType.Buy);
var ActiveSell = Positions.Find(Label, SymbolName, TradeType.Sell);
// VWAP Last Result Logic
//
//Open Buy
if (VWAP_Bias_Buy && ActiveBuy == null)
{
if (ActiveSell != null)
{
ClosePosition(ActiveSell);
}
ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLoss, TakeProfit);
}
//Open Sell
if (VWAP_Bias_Sell && ActiveSell == null)
{
if (ActiveBuy != null)
{
ClosePosition(ActiveBuy);
}
ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, StopLoss, TakeProfit);
}
}
}
}
I would like to use the last result of the VWAP similarly to that of a moving average.
Replies
wmclennan77
14 Jul 2021, 10:22
RE:
akbarlotfi15168 said:
Hello
You should first write the indicator then call it in your robot with
Your IndicatorNameInRobot = Indicators.GetIndicator<YourIndicatorName>(YourIndicatorFirstParameter,YourIndicatorSecondParameter);
Then Below the Ontick()
double YourIndicatorFirstNeededOutput = IndicatorNameInRobot.YourIndicatorOutputName.Last();
Your mistake is you use getindicator in your indicator with the it's namespace in itself.
Hi there,
Thank you for your help so far, if you don't mind there is still an issue.
When calling in the bot as you said, I get an error described as "Error CS0246: The type or namespace name 'SampleVWAP' could not be found (are you missing a using directive or an assembly reference?)"
I have added the reference to the indicator in the bot via Manage References.
@wmclennan77
akbarlotfi15168
13 Jul 2021, 09:43
Hello
You should first write the indicator then call it in your robot with
Your IndicatorNameInRobot = Indicators.GetIndicator<YourIndicatorName>(YourIndicatorFirstParameter,YourIndicatorSecondParameter);
Then Below the Ontick()
double YourIndicatorFirstNeededOutput = IndicatorNameInRobot.YourIndicatorOutputName.Last();
Your mistake is you use getindicator in your indicator with the it's namespace in itself.
@akbarlotfi15168