Topics
Replies
fzlogic
04 Oct 2013, 10:08
RE:
t4fast said:
Hi !
thanks for your time and effort,
yes, code is working fine now , without any problems.
There is one thing that needs correction in the last line:
//md sl
if (position.TakeProfit == tp && position.StopLoss != sl)
if (!sl.Equals(0.0))
Trade.ModifyPosition(position, sl, position.TakeProfit);
else if (tp.Equals(0.0) && sl.Equals(0.0))
Trade.ModifyPosition(position, null, position.TakeProfit);
// change to this
//md sl
if (position.TakeProfit == tp && position.StopLoss != sl)
if (!sl.Equals(0.0))
Trade.ModifyPosition(position, sl, position.TakeProfit);
else
Trade.ModifyPosition(position, null, position.TakeProfit);
@fzlogic
fzlogic
03 Oct 2013, 12:54
Yeah, there seems to be something that is not exactly working. Even though positions opened in cTrader do get modified when I test your code, sometimes not all positions get modified.
Maybe too many requests to modify a position.
I tried modifying the code to this and it seems to be working better, give it a try and see it the same problem persists:
you also need to include the Linq namespace.
using System.Linq;
...
// modify
foreach (Position position in Account.Positions.Where(position => Symbol.Code == position.SymbolCode))
{
double sl = position.TradeType == TradeType.Buy ? _slB : _slS;
double tp = position.TradeType == TradeType.Buy ? _tpB : _tpS;
if (position.TakeProfit == tp && position.StopLoss == sl)
continue;
if (position.TakeProfit != tp && position.StopLoss != sl)
{
if (!tp.Equals(0.0) && !sl.Equals(0.0))
Trade.ModifyPosition(position, sl, tp);
else if (tp.Equals(0.0) && sl.Equals(0.0))
Trade.ModifyPosition(position, null, null);
else if (tp.Equals(0.0) && !sl.Equals(0.0))
Trade.ModifyPosition(position, sl, null);
else
Trade.ModifyPosition(position, null, tp);
continue;
}
// md tp
if (position.TakeProfit != tp && position.StopLoss == sl)
{
if (!tp.Equals(0.0))
Trade.ModifyPosition(position, position.StopLoss, tp);
else
Trade.ModifyPosition(position, position.StopLoss, null);
continue;
}
//md sl
if (position.TakeProfit == tp && position.StopLoss != sl)
if (!sl.Equals(0.0))
Trade.ModifyPosition(position, sl, position.TakeProfit);
else if (tp.Equals(0.0) && sl.Equals(0.0))
Trade.ModifyPosition(position, null, position.TakeProfit);
}
@fzlogic
fzlogic
12 Sep 2013, 16:05
( Updated at: 21 Dec 2023, 09:20 )
RE:
Cerunnos said:
Hi, I'm using in my robot the Heiken Ashi Indicator and would like to determine the color (blue or red) of the current candlestick. How can I do that? Thanks
protected override void OnBar()
{
// Put your core logic here
var index = heiken.closeha.Count - 1;
if (heiken.closeha[index] > heiken.openha[index])
Print("green");
else
Print("red");
}
I think that for the last bar it may not produce correct results. Notice in the screenshot how there is both colors in certain candles.

@fzlogic
fzlogic
24 Jul 2013, 17:09
RE: RE:
cAlgo_Fanatic said:What do you want OpenPrice to be set to? Most probably you are assigning a NaN value to it if the indexing is not right. This could result if lastIndex is smaller than backBars.
You may use Print statements throughout the code, to output the values assigned to variables that are used in the conditions that open/close trades, for instance. This way, you can identify what could be wrong.
Please be advised that the drawing of lines, has not been implemented for backtesting.
I would like to OpenPrice contain the value "Open" the first bar of the day (week).
Or simply how to get the value of the first bar of the Monday (Open High Low) when i start script on second day of week?I use Print but sometimes shows some strange messages.
protected override void OnBar()
{
// Get the open price of the FirstBar of the week
DateTime firstBar;
if (!isFirstBarInitialized && MarketSeries.OpenTime.LastValue.DayOfWeek == DayOfWeek.Monday)
{
firstBar = MarketSeries.OpenTime.LastValue.Date; // get the date only part which will default to midnight time => first bar of monday
if (MarketSeries.OpenTime.LastValue == firstBar)
{
OpenPrice = MarketSeries.Open.LastValue;
isFirstBarInitialized = true;
}
}
else if (MarketSeries.OpenTime.LastValue.DayOfWeek != DayOfWeek.Monday)
isFirstBarInitialized = false;
// the rest of your code
@fzlogic
fzlogic
05 Jul 2013, 14:24
RE: RE: RE:
fzlogic said:airlangga88 said:i download news robot on cTDN, but they dosnt work.. am i wrong in settings?
what settings do you use? Set News Hour to UTC time.
whether your trading news robot going well?
I'm still working on it on a demo account. Yours?
airlangga88, I don't know why yours doesn't work... maybe put a print statement to see the server time when you start the robot and troubleshoot it like that.
@fzlogic
fzlogic
02 Jul 2013, 15:36
I don't think angles are possible to be plotted via the API. There a similar post /forum/ctrader-support/929.
@fzlogic
fzlogic
25 Oct 2013, 16:56
RE:
Hyperloop said:
No, the method will crash:
Crashed in OnBar with ArgumentException: Symbol "CADUSD" does not exist
use try... catch block
try { Symbol USDCAD = MarketData.GetSymbol("CADUSD"); Print("USDCAD {0}", USDCAD); } catch (Exception ex) { Print("Invalid Symbol code. {0}", ex.Message); }@fzlogic