How use the properties of my indicator in my Cbot
How use the properties of my indicator in my Cbot
21 Nov 2017, 11:56
I have my own indicator, when signalBuy in this indicator is true , i want to execute order in my Cbot:
//indicator
bool signalBuy = false;
public override void Calculate(int index)
{
if (1 + 1 == 2)
signalBuy = true;
}
//Cbot
protected override void OnTick()
{
if(signalBuy == true)
{
ExecuteMarketOrder(TradeType.Buy, Symbol, 20, "lalbelmin", 120, 200);
}
}
how it is possible to do that?
Replies
itmfar
22 Nov 2017, 09:21
RE:
Panagiotis Charalampous said:
Hi itmfar,
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class NewcBot : Robot { [Parameter(DefaultValue = 0.0)] public double Parameter { get; set; } private NewIndicator newIndicator; protected override void OnStart() { newIndicator = Indicators.GetIndicator<NewIndicator>(); } protected override void OnTick() { var test = newIndicator.Test; protected override void OnStop() { // Put your deinitialization logic here } } }Let me know if this is what you need.
Best Regards,
Panagiotis
Thanks for your response but it did not work, I have modified my Indicator
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace cAlgo
{
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class NewIndicator : Indicator
{
[Output("Main")]
public IndicatorDataSeries Result { get; set; }
private MacdCrossOver _MACD;
public bool Test = false;
public int myIndex;
public bool myBuy;
protected override void Initialize()
{
_MACD = Indicators.MacdCrossOver(26, 12, 9);
myBuy = false;
}
public override void Calculate(int index)
{
if (_MACD.MACD[index] > _MACD.Signal[index])
{
myBuy = true;
}
myIndex = index;
if (index % 2 == 1)
Test = true;
}
}
}
and it is my Cbot
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class NewcBot : Robot
{
[Parameter(DefaultValue = 0.0)]
public double Parameter { get; set; }
private NewIndicator newIndicator;
protected override void OnStart()
{
newIndicator = Indicators.GetIndicator<NewIndicator>();
}
protected override void OnTick()
{
var test = newIndicator.Test;
Print("test " + test);
var myindex = newIndicator.myIndex;
Print("my index " + myindex);
var mybuy = newIndicator.myBuy;
Print("buy " + mybuy);
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
}
}
now for each tick output is test false- index 0 - buy false.
@itmfar
whis.gg
22 Nov 2017, 12:59
The calculation method in the refenreced indicator won't be invoked unless you read its output value or force it to calculate.
Add one of these lines into OnTick method inside your cBot.
newIndicator.Calculate(MarketSeries.Open.Count - 1);
var unused = newIndicator.Result.LastValue;
Both samples will make the indicator to calculate at current index.
@whis.gg
itmfar
23 Nov 2017, 13:55
RE:
tmc. said:
The calculation method in the refenreced indicator won't be invoked unless you read its output value or force it to calculate.
Add one of these lines into OnTick method inside your cBot.
newIndicator.Calculate(MarketSeries.Open.Count - 1);var unused = newIndicator.Result.LastValue;Both samples will make the indicator to calculate at current index.
Great, It saved my day.
@itmfar
itmfar
23 Nov 2017, 13:57
RE:
tmc. said:
By the way, if you need historical data to be calculated as well, add this into OnStart method.
for (var index = 0; index < MarketSeries.Open.Count; index++) { newIndicator.Calculate(index); }
After that how i can use historical data in OnTick() method?
@itmfar
PanagiotisCharalampous
21 Nov 2017, 17:07
Hi itmfar,
You can create a property inside the indicator as follows
and read it it in your cBot like the example below
Let me know if this is what you need.
Best Regards,
Panagiotis
@PanagiotisCharalampous