Topics
Replies
alexander.n.fedorov
22 Oct 2018, 11:18
RE:
Panagiotis Charalampous said:
Hi Sasha,
You mean that your cBot should pick up on managing the order/position?
Best Regards,
Panagiotis
For Example
@alexander.n.fedorov
alexander.n.fedorov
22 Oct 2018, 10:41
I do not want it sorted, then I loose important information
Maybe I can circle them or use LINWQ?
I just do not know how
But I do not want to change the original structure of the list
@alexander.n.fedorov
alexander.n.fedorov
22 Oct 2018, 10:39
But then instance will be different (label)
I mean like user input, when ther rest of the management is the bot's
@alexander.n.fedorov
alexander.n.fedorov
18 Oct 2018, 16:10
( Updated at: 21 Dec 2023, 09:20 )
That is the latest piece of code
public override void Calculate(int indexi)
{
double high = MarketSeries.High.LastValue;
double low = MarketSeries.Low.LastValue;
for (int i = 0; i < LookBackDays; i++)
{
high = high > MarketSeries.High.Last(i) ? high : MarketSeries.High.Last(i);
low = low < MarketSeries.Low.Last(i) ? low : MarketSeries.Low.Last(i);
}
lowerHigh = high * (1 - ZoneWidth / zoneDivider / 100);
higherLow = low * (1 + ZoneWidth / zoneDivider / 100);
Chart.DrawRectangle("upper Zone", bar1, high, bar2, lowerHigh, Color.Aqua);
Chart.DrawRectangle("lower Zone", bar1, low, bar2, higherLow, Color.Crimson);
// Chart.RemoveAllObjects();
for (int i = 100; i >3 ; i--)
{
if (MarketSeries.High.Last(i) > MarketSeries.High.Last(i - 1) && MarketSeries.High.Last(i) > MarketSeries.High.Last(i - 2) && MarketSeries.High.Last(i) > MarketSeries.High.Last(i - 3) && MarketSeries.High.Last(i) > MarketSeries.High.Last(i + 1) && MarketSeries.High.Last(i) > MarketSeries.High.Last(i + 2))
{
Print("MarketSeries.High.Last(i)= {0}, i= {1}", MarketSeries.High.Last(i), i);
priceList.Add(MarketSeries.High.Last(i));
Print("PriceList Count ={0}", priceList.Count);
Chart.DrawTrendLine("ChartB" + i.ToString(), MarketSeries.OpenTime.Last(i+1 ), MarketSeries.High.Last(i) + Symbol.PipSize / zoneDivider * 5, MarketSeries.OpenTime.Last(i ), MarketSeries.High.Last(i) + Symbol.PipSize / zoneDivider * 5, Color.Aqua);
}
if (MarketSeries.Low.Last(i) < MarketSeries.Low.Last(i - 1) && MarketSeries.Low.Last(i) < MarketSeries.Low.Last(i - 2) && MarketSeries.Low.Last(i) < MarketSeries.Low.Last(i - 3) && MarketSeries.Low.Last(i) < MarketSeries.Low.Last(i + 1) && MarketSeries.Low.Last(i) < MarketSeries.Low.Last(i + 2))
{
priceList.Add(MarketSeries.Low.Last(i));
Print("PriceList Count ={0}", priceList.Count);
Chart.DrawTrendLine("ChartE" + i.ToString(), MarketSeries.OpenTime.Last(i+1 ), MarketSeries.Low.Last(i) - Symbol.PipSize / zoneDivider * 5, MarketSeries.OpenTime.Last(i ), MarketSeries.Low.Last(i) - Symbol.PipSize / zoneDivider * 5, Color.Crimson);
}
}
var midZone = (lowerHigh + higherLow) / 2;
Chart.DrawHorizontalLine("mid", midZone, Color.Chartreuse, 1, LineStyle.DotsVeryRare);
//Print("PriceList Count ={0}", priceList.Count);
//for (int k = 0; k < priceList.Count; k++)
//{
// if (priceList[k] <= midZone)
// {
// priceLower.Add(priceList[k]);
// }
//}
//var midline = priceLower.Count >0 ? priceLower.Average():0.0;
//Chart.DrawHorizontalLine("mid2", midline, Color.Cyan, 1, LineStyle.DotsVeryRare);
//Print(midline);
}
}
}
The latest screenshot
Regards,
Alexander
@alexander.n.fedorov
alexander.n.fedorov
18 Oct 2018, 16:05
Considering my English, it is a bit difficult to explain
on the chart you see the upper rectangle (supply zone?) and the lower rectangle (demand zone?), i may be confusing them
It is a daily chart
in between of them you see MarketSeries
If the Market Series represent a fractal it is marked
I am trying to draw the lines which will have the most of the fractals concentration around then. By drawing them I think I must have an idea about Support and Resistance
Regards.
P.S. The codes I tried different. they all do not make sense
@alexander.n.fedorov
alexander.n.fedorov
18 Oct 2018, 08:06
as you can see, the upper and lower bands are working perfectly, but the horizontal lines just would not draw. Can you explain?
Regards,
Sasha
@alexander.n.fedorov
alexander.n.fedorov
12 Oct 2018, 16:30
Hi, Panagiotis!
I think, I found some other solution, which is acceptable for my case.
Basically what it does, it takes the position, finds its entry time, by using "do while" loop finds the indicator value in a differet timeframe, and uses the original algorithm for the stops etc.
But you are right , the most conceptual approach would be to have the data stored not in file (files are different on different machines) but on some kind of SQL server in the database with and external address.
By doing that you become completely secure.
Regards, Alexander
@alexander.n.fedorov
alexander.n.fedorov
11 Oct 2018, 19:13
Or maybe, you could find the falues of a specific indicator (let us say Boulinger Bands), knowinng the position entry tirme?
@alexander.n.fedorov
alexander.n.fedorov
09 Oct 2018, 10:54
It does work
I would like to discuss this one in details with you
I'll clean it up send you.
Regards
Sasha
@alexander.n.fedorov
alexander.n.fedorov
09 Oct 2018, 10:29
Here it is:
"
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.RussianStandardTime, AccessRights = AccessRights.FullAccess)]
public class FAN32 : Robot
{
[Parameter("Source")]
public DataSeries Source { get; set; }
[Parameter("Minimum Equity", DefaultValue = 1500, MinValue = 500, Step = 100)]
public double MinEquity { get; set; }
[Parameter("BB Periods", DefaultValue = 40)]
public int BbPeriods { get; set; }
[Parameter("BB Deviations", DefaultValue = 2.0)]
public double BbDeviations { get; set; }
[Parameter("Grid Stiep in Pips", DefaultValue = 20)]
public double GridStepInPips { get; set; }
[Parameter("Max Martingales", DefaultValue = 6)]
public int MaxMartingales { get; set; }
[Parameter("Close on Drawdonw %", DefaultValue = 35, Step = 1, MinValue = 5, MaxValue = 50)]
public double DrawDownClose { get; set; }
[Parameter("Close on Profit %", DefaultValue = 1, Step = 1, MaxValue = 50)]
public double ProfitClose { get; set; }
[Parameter("Close GrigStep %", DefaultValue = 50, Step = 5, MaxValue = 100)]
public double CloseGridStepPips { get; set; }
[Parameter("Stop Trading Hour", DefaultValue = 23, Step = 1)]
public int StopHour { get; set; }
[Parameter("Stop Trading minutes", DefaultValue = 45, Step = 1)]
public int StopMinutes { get; set; }
[Parameter("Open Trades Hour", DefaultValue = 2, Step = 1, MinValue = 0)]
public int OpenTradesHour { get; set; }
private const int maSlowPeriods = 200;
private MovingAverage maSlow;
private const double minVolume = 1000;
private const int exponent = 2;
private string instance;
private BollingerBands bb;
private double volume, profit, equity;
private double gridStep, orderVolume;
private positons[] longPositions, shortPositions;
protected override void OnStart()
{
instance = ToString() + ", " + Symbol.Code + ", " + TimeFrame + ", " + Account.BrokerName + ", " + Account.Number;
bb = Indicators.BollingerBands(Source, BbPeriods, BbDeviations, MovingAverageType.Simple);
gridStep = GridStepInPips * Symbol.PipSize;
maSlow = Indicators.MovingAverage(Source, maSlowPeriods, MovingAverageType.Simple);
}
protected override void OnBar()
{
if ((MarketSeries.OpenTime.Last(0).Hour >= StopHour && MarketSeries.OpenTime.Last(0).Minute >= StopMinutes) || (MarketSeries.OpenTime.Last(0).Hour <= OpenTradesHour))
{
return;
}
TradeManagement();
if (volume == 0)
{
return;
}
var longPositions = Positions.FindAll(instance, Symbol, TradeType.Buy);
var shortPositions = Positions.FindAll(instance, Symbol, TradeType.Sell);
var allPositions = Positions.FindAll(instance);
if (shortPositions.Count() < MaxMartingales && longPositions.Count() < MaxMartingales)
{
#region Open Positions
#region short entries
if (MarketSeries.Close.Last(2) > bb.Top.Last(2))
{
if (MarketSeries.Close.Last(1) < bb.Top.Last(1) && MarketSeries.Low.Last(1) > bb.Main.Last(1))
{
if ((shortPositions.Count() == 0) || (shortPositions.Count() > 0 && MarketSeries.Close.Last(1) > ShortPrice() + gridStep))
{
orderVolume = volume * Math.Pow(exponent, shortPositions.Count());
ExecuteMarketOrder(TradeType.Sell, Symbol, orderVolume, instance);
}
}
}
#endregion
#region long entries
if (MarketSeries.Close.Last(2) < bb.Bottom.Last(2))
{
if (MarketSeries.Close.Last(1) > bb.Bottom.Last(1) && MarketSeries.High.Last(1) < bb.Main.Last(1))
{
if (longPositions.Count() == 0)
{
orderVolume = volume * Math.Pow(exponent, longPositions.Count());
ExecuteMarketOrder(TradeType.Buy, Symbol, orderVolume, instance);
}
if (longPositions.Count() > 0 && MarketSeries.Close.Last(1) < LongPrice() - gridStep)
{
orderVolume = volume * Math.Pow(exponent, longPositions.Count());
ExecuteMarketOrder(TradeType.Buy, Symbol, orderVolume, instance);
}
}
}
#endregion
#endregion
#region close on opposite side of the river if all the positions are same Trade Type.
longPositions = Positions.FindAll(instance, Symbol, TradeType.Buy);
shortPositions = Positions.FindAll(instance, Symbol, TradeType.Sell);
allPositions = Positions.FindAll(instance);
#region close shorts
if (longPositions.Count() == 0 && shortPositions.Count() > 0)
{
if (MarketSeries.Low.Last(1) <= bb.Bottom.Last(1))
{
foreach (var position in shortPositions)
{
if (position.NetProfit > 0)
{
ClosePosition(position);
}
}
}
}
#endregion
#region close longs
if (longPositions.Count() > 0 && shortPositions.Count() == 0)
{
if (MarketSeries.High.Last(1) >= bb.Top.Last(1))
{
foreach (var position in longPositions)
{
if (position.NetProfit > 0)
{
ClosePosition(position);
}
}
}
}
#endregion
#endregion
#region close two positevely locked positions
if (longPositions.Count() == 1 && shortPositions.Count() == 1)
{
profit = 0;
foreach (var position in Positions.FindAll(instance))
{
profit += position.NetProfit;
}
if (profit > 0)
{
foreach (var position in Positions.FindAll(instance))
{
ClosePosition(position);
}
}
}
#endregion
#region close bunch of same positons in Profit
longPositions = Positions.FindAll(instance, Symbol, TradeType.Buy);
shortPositions = Positions.FindAll(instance, Symbol, TradeType.Sell);
#region close shorts
if (shortPositions.Count() > 1 && longPositions.Count() == 0)
{
profit = 0;
foreach (var position in shortPositions)
{
profit += position.NetProfit;
}
if (profit >= 0)
{
foreach (var position in shortPositions)
{
ClosePosition(position);
}
}
}
#endregion
#region close longs
if (longPositions.Count() > 1 && shortPositions.Count() == 0)
{
profit = 0;
foreach (var position in longPositions)
{
profit += position.NetProfit;
}
if (profit >= 0)
{
foreach (var position in longPositions)
{
ClosePosition(position);
}
}
}
#endregion
#endregion
#region close various positions on profit
longPositions = Positions.FindAll(instance, Symbol, TradeType.Buy);
shortPositions = Positions.FindAll(instance, Symbol, TradeType.Sell);
if (longPositions.Count() > 0 & shortPositions.Count() > 0)
{
profit = 0;
foreach (var position in Positions.FindAll(instance))
{
profit += position.NetProfit;
}
if (profit > 0)
{
foreach (var position in Positions.FindAll(instance))
{
ClosePosition(position);
}
}
}
#endregion
#region Close single position in pips profit
allPositions = Positions.FindAll(instance);
if (allPositions.Count() == 1)
{
var position = (Positions.First(x => x.Label == instance));
if (position.Pips > GridStepInPips * CloseGridStepPips * 0.01)
{
ClosePosition(position);
}
}
#endregion
}
//longPositions = Positions.FindAll(instance, Symbol, TradeType.Buy);
//shortPositions = Positions.FindAll(instance, Symbol, TradeType.Sell);
//if (longPositions.Count() >= MaxMartingales)
//{
// if (bb.Main.Last(1) > maSlow.Result.Last(1))
// {
// if (MarketSeries.Close.Last(2) < bb.Bottom.Last(1))
// {
// if (MarketSeries.Close.Last(1) > bb.Bottom.Last(1) && MarketSeries.High.Last(1) < bb.Main.Last(1))
// {
// orderVolume = volume * Math.Pow(exponent, longPositions.Count());
// ExecuteMarketOrder(TradeType.Buy, Symbol, orderVolume, instance);
// }
// }
// }
//}
//if (shortPositions.Count() >= MaxMartingales)
//{
// if (bb.Main.Last(1) < maSlow.Result.Last(1))
// {
// if (MarketSeries.Close.Last(2) > bb.Top.Last(2))
// {
// if (MarketSeries.Close.Last(1) < bb.Top.Last(1) && MarketSeries.High.Last(1) > bb.Main.Last(1))
// {
// orderVolume = volume * Math.Pow(exponent, longPositions.Count());
// ExecuteMarketOrder(TradeType.Sell, Symbol, orderVolume, instance);
// }
// }
// }
//}
}
#region tradeManagement
private void TradeManagement()
{
var totalPositions = Positions.FindAll(instance);
if (totalPositions.Count() == 0)
{
equity = Account.Equity;
var newVolume = Math.Floor(equity / MinEquity) * minVolume;
volume = (volume > newVolume ? volume : newVolume);
return;
}
if (totalPositions.Count() > 0)
{
profit = 0;
foreach (var position in Positions.FindAll(instance))
{
profit = profit + position.NetProfit;
}
if (profit > equity * ProfitClose / 100 || profit < -equity * DrawDownClose / 100)
{
foreach (var position in Positions.FindAll(instance))
{
ClosePosition(position);
}
Print("closed all, Profit = {0}", profit);
equity = Account.Equity;
}
}
}
#endregion
#region LongPrice, ShortPrice
private double LongPrice()
{
var longPositions = Positions.FindAll(instance, Symbol, TradeType.Buy);
return longPositions.OrderByDescending(x => x.EntryPrice).Last().EntryPrice;
}
private double ShortPrice()
{
var shortPositions = Positions.FindAll(instance, Symbol, TradeType.Sell);
return shortPositions.OrderByDescending(x => x.EntryPrice).First().EntryPrice;
}
#endregion
private void FindPositions()
{
longPositions = Positions.FindAll(instance, Symbol, TradeType.Buy);
shortPositions = Positions.FindAll(instance, Symbol, TradeType.Sell);
}
}
}
"
@alexander.n.fedorov
alexander.n.fedorov
09 Oct 2018, 10:20
Hi, Panagiotis!
I guess it is :
private positions[] longPositions, shortPositions;
"
Should there be a different type?
Regards,
Sasha
@alexander.n.fedorov
alexander.n.fedorov
08 Oct 2018, 12:23
if (Positions.FindAll (Instance).Count()==1)
{
ClosePositions(?)
}
@alexander.n.fedorov
alexander.n.fedorov
08 Oct 2018, 12:22
if (Positions.FindAll (Instance).Count()=1)
{
ClosePositions(?)
}
@alexander.n.fedorov
alexander.n.fedorov
22 Oct 2018, 14:21
RE:
Panagiotis Charalampous said:
I was thinking of smth different . As my knowledge of c# could be improved drammatically, I do not know all the power of it
I was thinking, like eg. in VBA you could open a dialog box end enter some information
@alexander.n.fedorov