Topics
Replies
fang0092
04 Sep 2020, 01:49
RE:
firemyst said:
In Visual Studio:
Right Click on your project and select 'Add reference'
Select 'Assemblies->Framework' in 'Reference Manager' window.
Add select
Microsoft.CSharp.dll
and click on add
You might also try this link:
- I got the path of Microsoft.CSharp.dll from the objects manager of Visual Studio
- I right click my code in ctrader, selected add reference, and pasted the path
Seems to be working. Thank you.
@fang0092
fang0092
06 Aug 2020, 10:42
( Updated at: 21 Dec 2023, 09:22 )
RE:
PanagiotisCharalampous said:
Hi fang0092,
Do you ask how to determine the color of the rectangle that will be drawn using DrawRectangle()?
Best Regards,
Panagiotis
Im wondering if I can link the color I use for the objects (below) to the color argument DrawingRectangle() is asking for.
@fang0092
fang0092
04 Aug 2020, 12:14
RE:
PanagiotisCharalampous said:
Hi fang0092,
. If I load the m1 bars ahead of time (before using the indicator) will it significantly reduce the loading time?
It will take the same time to load no matter where you call the function
Do the m1 bars loaded remain until I close ctrader?
They will stay in memory as long as you use the indicator.
Best Regards,
Panagiotis
Join us on Telegram
Ok thank you very much
@fang0092
fang0092
04 Aug 2020, 11:31
RE:
PanagiotisCharalampous said:
Hi fang0092,
There is not enough data loaded in your m1 bars. You need to load more history to obtain the data. See below
while (m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1) < 0) m1Bars.LoadMoreHistory(); var PRICE = m1Bars.HighPrices[m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1) + i]; Print("for statement: price_tobreak is " + price_tobreak + " while high for this time is" + PRICE);
while (m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1) < 0) m1Bars.LoadMoreHistory(); var PRICE = m1Bars.LowPrices[m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1) + i]; Print("for: price_tobreak is " + price_tobreak + " while low for this time is" + PRICE);
Best Regards,
Panagiotis
Join us on Telegram
It works, but it take a while to load. If I load the m1 bars ahead of time (before using the indicator) will it significantly reduce the loading time? Do the m1 bars loaded remain until I close ctrader?
@fang0092
fang0092
04 Aug 2020, 10:53
RE:
PanagiotisCharalampous said:
Hi fang0092,
Please provide the complete code so that we can reproduce the behavior.
Best Regards,
Panagiotis
- Run the code.
- Go to 4hour timeframe of WTI.
- Click on the button on the upper left.
- Drag green line to 40.676.
- Drag red line to 39.275.
- Hold ctrl and click on the chart to draw a rectangle with time 1 at July 9, 2020 17:00 to July 13,2020 05:00. User time is UTC+8.
- last print to log-> for: price_tobreak is 40.676 while low for this time isNaN
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class NewIndicator : Indicator
{
[Parameter(DefaultValue = 0.0)]
public double Parameter { get; set; }
[Parameter(DefaultValue = "2F68D0F7")]
public string SelectedColor { get; set; }
[Output("Main")]
public IndicatorDataSeries Result { get; set; }
ChartHorizontalLine RFinishLine;
ChartHorizontalLine GStartLine;
ChartRectangle Rect1;
ControlBase DrawingDialog;
protected override void Initialize()
{
Chart.MouseDown += OnChartMouseDown;
Chart.MouseMove += OnChartMouseMove;
Chart.MouseUp += OnChartMouseUp;
Chart.ObjectUpdated += OnChartObjectUpdated;
TheChoices();
}
void OnHor_LineClick(ButtonClickEventArgs obj)
{
RFinishLine = DrawRHL1();
GStartLine = DrawBHL2();
RFinishLine.IsInteractive = true;
GStartLine.IsInteractive = true;
Print(RFinishLine.Name + " was created at price " + RFinishLine.Y);
Print(GStartLine.Name + " was created at price " + GStartLine.Y);
}
void OnChartMouseDown(ChartMouseEventArgs obj)
{
if (obj.CtrlKey == false)
{
return;
}
if (obj.CtrlKey == true)
{
Rect1 = DrawRect1(obj.TimeValue);
Chart.IsScrollingEnabled = false;
}
}
void OnChartMouseMove(ChartMouseEventArgs obj)
{
if (Rect1 == null)
{
return;
}
Rect1.Time2 = obj.TimeValue;
}
void OnChartObjectUpdated(ChartObjectUpdatedEventArgs obj)
{
if (obj.ChartObject.Name == RFinishLine.Name)
{
Print(RFinishLine.Name + " was moved to price " + RFinishLine.Y);
}
if (obj.ChartObject.Name == GStartLine.Name)
{
Print(GStartLine.Name + " was moved to price " + GStartLine.Y);
}
}
private void DETm1bar_thatbreaksGStartLine_pos()
{
double priceTG_toreach = RFinishLine.Y;
double price_tobreak = GStartLine.Y;
// ONLY gets the bars of one minute timeframe
var m1Bars = MarketData.GetBars(TimeFrame.Minute);
if (RFinishLine.Y > GStartLine.Y)
{
Print("first if statement works");
for (int i = 0; m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1) + i <= m1Bars.OpenTimes.GetIndexByTime(Rect1.Time2); i++)
{
var PRICE = m1Bars.HighPrices[m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1) + i];
Print("for statement: price_tobreak is " + price_tobreak + " while high for this time is" + PRICE);
if (m1Bars.HighPrices[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1)) + i] > price_tobreak)
{
Print("2nd IF");
DateTime m1bar_thatbreaksGStartLine_pos = m1Bars.OpenTimes[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1)) + i];
Print("The position is at " + m1bar_thatbreaksGStartLine_pos + " with the price of " + m1Bars.HighPrices[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1)) + i]);
ChartVerticalLine LineOfm1bar_thatbreaksStartLinepos = Chart.DrawVerticalLine("LineThatIndicates m1bar_thatbreaksGStartLine_pos" + m1bar_thatbreaksGStartLine_pos, m1bar_thatbreaksGStartLine_pos, Color.PaleGreen);
LineOfm1bar_thatbreaksStartLinepos.IsInteractive = true;
LineOfm1bar_thatbreaksStartLinepos.Comment = "high is at " + m1Bars.HighPrices[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1)) + i];
break;
}
}
}
if (RFinishLine.Y < GStartLine.Y)
{
Print("first if statement works");
for (int i = 0; m1Bars.OpenTimes[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1)) + i] <= m1Bars.OpenTimes[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time2))]; i++)
{
var PRICE = m1Bars.LowPrices[m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1) + i];
Print("for: price_tobreak is " + price_tobreak + " while low for this time is" + PRICE);
if (m1Bars.LowPrices[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1)) + i] < price_tobreak)
{
DateTime m1bar_thatbreaksGStartLine_pos = m1Bars.OpenTimes[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1)) + i];
Print("The position is at " + m1bar_thatbreaksGStartLine_pos + " with the price of " + m1Bars.LowPrices[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1)) + i]);
ChartVerticalLine LineOfm1bar_thatbreaksStartLinepos = Chart.DrawVerticalLine("LineThatIndicates m1bar_thatbreaksGStartLine_pos" + m1bar_thatbreaksGStartLine_pos, m1bar_thatbreaksGStartLine_pos, Color.PaleGreen);
LineOfm1bar_thatbreaksStartLinepos.IsInteractive = true;
LineOfm1bar_thatbreaksStartLinepos.Comment = "high is at " + m1Bars.LowPrices[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1)) + i];
break;
}
}
}
}
void OnChartMouseUp(ChartMouseEventArgs obj)
{
Chart.IsScrollingEnabled = true;
if (Rect1 != null)
{
if (RFinishLine != null)
{
if (GStartLine != null)
{
DETm1bar_thatbreaksGStartLine_pos();
//DETm1bar_that_reachesTG();
Chart.RemoveObject(RFinishLine.Name);
Chart.RemoveObject(GStartLine.Name);
Chart.RemoveObject(Rect1.Name);
}
}
}
Rect1 = null;
return;
}
private void TheChoices()
{
var therealbox = new StackPanel
{
VerticalAlignment = VerticalAlignment.Top,
HorizontalAlignment = HorizontalAlignment.Left,
Orientation = Orientation.Vertical,
Width = 160,
BackgroundColor = "Red"
};
// when clicked, these cause an event. Event handler attached to it, not in it,will run.
var HorLineButton = new Button
{
Text = "#1 HLs",
HorizontalContentAlignment = HorizontalAlignment.Center
};
therealbox.AddChild(HorLineButton);
// therealbox.AddChild(RealizeButton);
HorLineButton.Click += OnHor_LineClick;
DrawingDialog = therealbox;
Chart.AddControl(DrawingDialog);
// Chart.AddControl(therealbox);
}
//method to draw a horizontal line HL1
private ChartHorizontalLine DrawRHL1()
{
var RHL1 = Chart.DrawHorizontalLine("End Line", ((Chart.TopY - Chart.BottomY) * 2 / 3) + Chart.BottomY, Color.Red);
return RHL1;
}
//method to draw a horizontal line HL2
private ChartHorizontalLine DrawBHL2()
{
var BHL2 = Chart.DrawHorizontalLine("Start Line", ((Chart.TopY - Chart.BottomY) * 1 / 3) + Chart.BottomY, Color.Green);
return BHL2;
}
//method to draw a rectangle Rect1
private ChartRectangle DrawRect1(DateTime Timefrommouse)
{
var Rect1 = Chart.DrawRectangle("Rect1", Timefrommouse, Chart.BottomY, Timefrommouse, Chart.TopY, SelectedColor);
Rect1.IsFilled = true;
return Rect1;
}
public override void Calculate(int index)
{
// Calculate value at specified index
// Result[index] = ...
}
}
}
@fang0092
fang0092
08 Jul 2020, 09:52
RE:
PanagiotisCharalampous said:
Hi francisang92,
You need to have a unique name for each object. Else when the DrawText() is called using the name of an existing object, the object will be replaced with the new one.
Best Regards,
Panagiotis
So code on line xyz creates a text, but since it has the same object ID, it keeps getting overwritten. Text only appears at the last bar, because nothing else is replacing.
Ok. Thank you.
@fang0092
fang0092
12 Sep 2020, 09:49
RE:
fang0092 said:
I made a few changes and I got this.
Crashed in ChartArea.MouseUp with RuntimeBinderException: Cannot convert type 'System.Collections.Generic.List<object>' to 'int'
It seems like it was because I supplied variables of the wrong data type(var) to the DrawRectangle method.
@fang0092