Topics
Replies
amirus.stark
28 Jul 2022, 14:37
( Updated at: 21 Dec 2023, 09:22 )
RE: I managed to understand what you wanted to achieve, here is the code
paolo.panicali said:
Hi, several errors. Z was set as an integer while it is a double, when you divide it by 100 it goes to 0 if set as integer, while as double you get a real value.
Bar lastvalue is literally the last value, for an indicator you have to use bars.closeprices[index] for the current bar over time.
There are some LastValue left over you can correct them, the indicator works now.
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class RetracementBar : Indicator
{
[Parameter("Inventory Retracement Percentage %", DefaultValue = 45)]
public double z { get; set; }
[Output("Main")]
public IndicatorDataSeries Result { get; set; }
public override void Calculate(int index)
{
// Candle Range
double a = Math.Abs(Bars.HighPrices[index] - Bars.LowPrices[index]);
// Candle Body
double b = Math.Abs(Bars.ClosePrices[index] - Bars.OpenPrices[index]);
// Percent to Decimal
double c = z / 100;
var rv = def_rv(a, b, c);
var x = Bars.LowPrices[index] + (c * a);
var y = Bars.HighPrices[index] - (c * a);
var sl = def_sl(index, rv, y);
var ss = def_ss(rv, x);
var li = def_li(sl, y, ss, x);
Result[index] = li;
if (sl)
{
Chart.DrawIcon(Bars.OpenTimes.LastValue.ToString(), ChartIconType.DownArrow, Bars.OpenTimes.LastValue, Bars.HighPrices.LastValue, Color.Blue);
Print("sl");
}
if (ss)
{
Chart.DrawIcon(Bars.OpenTimes.LastValue.ToString(), ChartIconType.UpArrow, Bars.OpenTimes.LastValue, Bars.LowPrices.LastValue, Color.Red);
Print("ss");
}
}
public bool def_rv(double a, double b, double c)
{
// Print(a + " " + b + " " + c);
if (b < c * a)
{
return true;
}
else
{
return false;
}
}
public bool def_sl(int index, bool rv, double y)
{
if (rv)
{
//Print("rv true");
// Print("rv " + rv + " Y value passed: " + y + " high: " + Bars.HighPrices[index] + " open: " + Bars.OpenPrices[index] + " close: " + Bars.ClosePrices[index]);
}
//y = y - 1E-05;
if (rv && Bars.HighPrices[index] > y && Bars.ClosePrices[index] < y && Bars.OpenPrices[index] < y)
{
//Print("rv " + rv + " Y value passed: " + y + " high: " + Bars.HighPrices[index] + " open: " + Bars.OpenPrices[index] + " close: " + Bars.ClosePrices[index]);
return true;
}
else
{
//Print("rv " + rv + " Y value passed: " + y + " high: " + Bars.HighPrices[index] + " open: " + Bars.OpenPrices[index] + " close: " + Bars.ClosePrices[index]);
return false;
}
}
public bool def_ss(bool rv, double x)
{
if (rv && Bars.LowPrices.LastValue < x && Bars.ClosePrices.LastValue > x && Bars.OpenPrices.LastValue > x)
{
return true;
}
else
{
return false;
}
}
public double def_li(bool sl, double y, bool ss, double x)
{
if (sl)
{
return y;
}
if (ss && !sl)
{
return x;
}
else
{
return ((x + y) / 2);
}
}
}
}
REMEMBER TO REPLACE LASTVALUE WITH [INDEX] OTHERWISE YOU GET THE VALUES OF THE LAST BAR ALWAYS
Bye.
Thanks a lot for the precious insight man !
@amirus.stark
amirus.stark
21 Jul 2022, 14:03
RE:
PanagiotisCharalampous said:
Hi amirus.stark,
Please share the complete indicator code so that we can check.
Best Regards,
Panagiotis
Here's the algo
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class RetracementBar : Indicator
{
[Parameter("Inventory Retracement Percentage %", DefaultValue = 45)]
public int z { get; set; }
[Output("Main")]
public IndicatorDataSeries Result { get; set; }
public override void Calculate(int index)
{
// Candle Range
var a = Math.Abs(Bars.HighPrices.Last(0) - Bars.LowPrices.Last(0));
// Candle Body
var b = Math.Abs(Bars.ClosePrices.Last(0) - Bars.OpenPrices.Last(0));
// Percent to Decimal
var c = z / 100;
var rv = def_rv(a, b, c);
var x = Bars.LowPrices.LastValue + (c * a);
var y = Bars.HighPrices.LastValue - (c * a);
var sl = def_sl(rv, y);
var ss = def_ss(rv, x);
var li = def_li(sl, y, ss, x);
Result[index] = li;
if (sl)
{
Chart.DrawIcon(Bars.OpenTimes.LastValue.ToString(), ChartIconType.DownArrow, Bars.OpenTimes.LastValue, Bars.HighPrices.LastValue, Color.Blue);
Print("sl");
}
if (ss)
{
Chart.DrawIcon(Bars.OpenTimes.LastValue.ToString(), ChartIconType.UpArrow, Bars.OpenTimes.LastValue, Bars.LowPrices.LastValue, Color.Red);
Print("ss");
}
}
public bool def_rv(double a, double b, double c)
{
if (b < c * a)
{
return true;
}
else
{
return false;
}
}
public bool def_sl(bool rv, double y)
{
if (rv && Bars.HighPrices.LastValue > y && Bars.ClosePrices.LastValue < y && Bars.OpenPrices.LastValue < y)
{
return true;
}
else
{
return false;
}
}
public bool def_ss(bool rv, double x)
{
if (rv && Bars.LowPrices.LastValue < x && Bars.ClosePrices.LastValue > x && Bars.OpenPrices.LastValue > x)
{
return true;
}
else
{
return false;
}
}
public double def_li(bool sl, double y, bool ss, double x)
{
if (sl)
{
return y;
}
if (ss && !sl)
{
return x;
}
else
{
return ((x + y) / 2);
}
}
}
}
It's an adaptation of a tradingview algo, the main line works fine but apparently the bools sl and ss are always false for some reason
@amirus.stark
amirus.stark
02 Mar 2021, 11:55
RE: RE:
afhacker said:
Why you are not using text block? you are trying to mix chart objects with chart controls, Canvas is a chart control, and the Chart.DrawStaticText draws a chart object on the chart.
You have to use a chart control inside canvas not an object, try text block.
You can add multiple text blocks for each character so they will have different colors.
Well basically because in the same line some characters have different colors than others and in TextBlock you can pick a color for a whole string :/
@amirus.stark
amirus.stark
08 Aug 2020, 17:09
RE:
pankuolung0804 said:
Hi there,
I'm trying to do backtest
1. can backtest data result export to excel?
2. can backtest data be analyzed by the "tab" of "Analyze"?Thank you!
Mason
Hi Mason,
Wanted to export my backtest data too, didn't find a way to do it at first, so I coded an OnStop function that writes on a txt file the data I needed.
I can do this for you if you still need it :)
Amir
@amirus.stark
amirus.stark
08 May 2020, 05:07
RE:
for example :
protected override void OnBar()
{
var lastIndex = Bars.ClosePrices.Count - 1;
_isLongPositionOpen = _position != null && _position.TradeType == TradeType.Buy;
_isShortPositionOpen = _position != null && _position.TradeType == TradeType.Sell;
double close = Bars.ClosePrices[lastIndex];
double lastClose = Bars.ClosePrices[lastIndex];
double ema = _ema.Result[lastIndex];
double lastEma = _ema.Result[lastIndex];
foreach (var position in Positions)
{
if (close == _ema.Result.Last(0))
{
ClosePosition(position);
}
}
in this code the bars close above the ema but the position isn't being closed
@amirus.stark
amirus.stark
05 Feb 2023, 21:07
RE:
firemyst said:
Thanks I fixed it
btw I'm now drawing two lines (one should have a different color) as following :
kfilt[index] = error + velocityDict[index];
if (velocityDict[index]> 0 )
{
kfiltG[index] = kfilt[index];
kfiltR[index] = double.NaN;
Print("R = " + kfiltR[index]);
}
if (velocityDict[index] !> 0)
{
kfiltG[index] = double.NaN;
kfiltR[index] = kfilt[index];
Print("G = " + kfiltG[index]);
}
but when the values are NaN it draws a line that goes from the previous defined value to the last one
how to prevent this ?
@amirus.stark