Second position would not open
Second position would not open
02 Oct 2018, 14:35
Dear Panagiotis! Spent already more then 2 hours trying to find a mistake.
Please help
It is a gridstep martingale, and here is the piece of code:
"
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;
}
totalShortPositions = Positions.FindAll(Instance, Symbol, TradeType.Sell).Count();
totalLongPositions = Positions.FindAll(Instance, Symbol, TradeType.Buy).Count();
#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 (totalShortPositions == 0)
{
var orderVolume = volume * Math.Pow(GridsExponent, totalShortPositions);
var result = ExecuteMarketOrder(TradeType.Sell, Symbol, orderVolume, Instance);
if (result.IsSuccessful)
{
shortPrice = result.Position.EntryPrice;
totalShortPositions = totalShortPositions + 1;
shortList.Add(result.Position.Id);
Print("Short Price = {0}, gridStep = {1}, MarketSeries.Close.Last(1) = {2}", shortPrice, gridStep, MarketSeries.Close.Last(1));
}
}
if (totalShortPositions > 0)
{
if (MarketSeries.Close.Last(1) > shortPrice + gridStep)
{
var orderVolume = volume * Math.Pow(GridsExponent, totalShortPositions);
var result = ExecuteMarketOrder(TradeType.Sell, Symbol, orderVolume, Instance);
if (result.IsSuccessful)
{
shortPrice = result.Position.EntryPrice;
totalShortPositions = totalShortPositions + 1;
shortList.Add(result.Position.Id);
Print("Short Price = {0}, gridStep = {1}, MarketSeries.Close.Last(1) = {2}", shortPrice, gridStep, MarketSeries.Close.Last(1));
}
}
}
}
}
#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 (totalLongPositions == 0)
{
var orderVolume = volume * Math.Pow(GridsExponent, totalLongPositions);
var result = ExecuteMarketOrder(TradeType.Buy, Symbol, orderVolume, Instance);
if (result.IsSuccessful)
{
longPrice = result.Position.EntryPrice;
totalLongPositions = totalLongPositions + 1;
longList.Add(result.Position.Id);
Print("Long Price = {0}, gridStep = {1}, MarketSeries.Close.Last(1) = {2}, totalLongPositions = {3}", longPrice, gridStep, MarketSeries.Close.Last(1), totalLongPositions);
}
}
if (totalLongPositions > 0)
{
if (MarketSeries.Close.Last(1) < longPrice - gridStep)
{
var orderVolume = volume * Math.Pow(GridsExponent, totalLongPositions);
var result = ExecuteMarketOrder(TradeType.Buy, Symbol, orderVolume, Instance);
if (result.IsSuccessful)
{
longPrice = result.Position.EntryPrice;
totalLongPositions = totalLongPositions + 1;
longList.Add(result.Position.Id);
Print("Long Price = {0}, gridStep = {1}, MarketSeries.Close.Last(1) = {2}, totalLongPositions = {3}", longPrice, gridStep, MarketSeries.Close.Last(1), totalLongPositions);
}
}
}
}
}
"
Replies
PanagiotisCharalampous
02 Oct 2018, 15:48
Hi Sasha,
Can you please point me to the condition you expect to trigger the order?
Best Regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
02 Oct 2018, 16:01
In this case, the condition above does not seem to have been fulfilled
if (MarketSeries.Close.Last(1) > BB.Bottom.Last(1) && MarketSeries.High.Last(1) < BB.Main.Last(1))
That candlestick's last value is below BB Bottom.
Best Regards,
Panagiotis
@PanagiotisCharalampous
alexander.n.fedorov
02 Oct 2018, 16:10
( Updated at: 21 Dec 2023, 09:20 )
No, the candlestick I am pointing to is green color, it is bullish, it opens below and closes above BB bottom. Just try to increase the picture
@alexander.n.fedorov
PanagiotisCharalampous
02 Oct 2018, 16:18
Hi Sasha,
I will need the full cBot code so that I can investigate further. Can you please share?
Best Regards,
Panagiotis
@PanagiotisCharalampous
alexander.n.fedorov
02 Oct 2018, 16:19
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Collections.Generic;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.RussianStandardTime, AccessRights = AccessRights.None)]
public class FAN23 : Robot
{
#region Parameters public
[Parameter("Source")]
public DataSeries Source { get; set; }
[Parameter("Minimum Equity", DefaultValue = 1500, MinValue = 1000, Step = 100)]
public double MinEquity { get; set; }
[Parameter("Bollinger Bands Deviations", DefaultValue = 2.0, MinValue = 1.8, MaxValue = 2.4, Step = 0.1)]
public double Deviations { get; set; }
[Parameter("Bollinger Bands Periods", DefaultValue = 40, Step = 1)]
public int Periods { get; set; }
[Parameter("MA Periods", DefaultValue = 200, Step = 1)]
public int MaPeriods { get; set; }
[Parameter("GridStep in pips", MinValue = 20, MaxValue = 70, DefaultValue = 20, Step = 5)]
public double GridStep { get; set; }
[Parameter("Close on profit %", DefaultValue = 1, Step = 1, MinValue = 1, MaxValue = 10)]
public double ProfitClose { get; set; }
[Parameter("Tighten steps after ", DefaultValue = 8, MinValue = 3, Step = 1)]
public int TightenSteps { get; set; }
[Parameter("Close on Drawdonw %", DefaultValue = 35, Step = 1, MinValue = 5, MaxValue = 50)]
public double DrawDownClose { 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; }
#endregion
#region Parameters private
private const int GridsExponent = 2;
private string Instance;
private MovingAverage ma;
private MovingAverage BBma;
private double volume, minVolume, accountBalance, equity, profit, gridStep;
private BollingerBands BB;
private double shortPrice, longPrice;
private int totalLongPositions, totalShortPositions;
#endregion
#region cBot onStart
protected override void OnStart()
{
accountBalance = 0;
accountBalance = Account.Balance > accountBalance ? Account.Balance : accountBalance;
Instance = ToString() + ", " + Symbol.Code + ", " + TimeFrame.ToString() + ", " + Account.BrokerName + ", " + Account.Number.ToString();
BB = Indicators.BollingerBands(Source, Periods, Deviations, MovingAverageType.Simple);
ma = Indicators.MovingAverage(Source, MaPeriods, MovingAverageType.Simple);
BBma = Indicators.MovingAverage(Source, Periods, MovingAverageType.Triangular);
minVolume = 1000;
gridStep = GridStep * Symbol.PipSize;
equity = Account.Equity;
Print("Instance = {0}, Account Balanace = {1}, Account Equity = {2}, volume = {3}", Instance, Account.Balance, Account.Equity, volume);
}
#endregion
#region onBar
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;
}
totalShortPositions = Positions.FindAll(Instance, Symbol, TradeType.Sell).Count();
totalLongPositions = Positions.FindAll(Instance, Symbol, TradeType.Buy).Count();
#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 (totalShortPositions == 0)
{
var orderVolume = volume * Math.Pow(GridsExponent, totalShortPositions);
var result = ExecuteMarketOrder(TradeType.Sell, Symbol, orderVolume, Instance);
if (result.IsSuccessful)
{
shortPrice = result.Position.EntryPrice;
totalShortPositions = totalShortPositions + 1;
Print("Short Price = {0}, gridStep = {1}, MarketSeries.Close.Last(1) = {2}", shortPrice, gridStep, MarketSeries.Close.Last(1));
}
}
if (totalShortPositions > 0)
{
if (MarketSeries.Close.Last(1) > shortPrice + gridStep)
{
var orderVolume = volume * Math.Pow(GridsExponent, totalShortPositions);
var result = ExecuteMarketOrder(TradeType.Sell, Symbol, orderVolume, Instance);
if (result.IsSuccessful)
{
shortPrice = result.Position.EntryPrice;
totalShortPositions = totalShortPositions + 1;
Print("Short Price = {0}, gridStep = {1}, MarketSeries.Close.Last(1) = {2}", shortPrice, gridStep, MarketSeries.Close.Last(1));
}
}
}
}
}
#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 (totalLongPositions == 0)
{
var orderVolume = volume * Math.Pow(GridsExponent, totalLongPositions);
var result = ExecuteMarketOrder(TradeType.Buy, Symbol, orderVolume, Instance);
if (result.IsSuccessful)
{
longPrice = result.Position.EntryPrice;
totalLongPositions = totalLongPositions + 1;
Print("Long Price = {0}, gridStep = {1}, MarketSeries.Close.Last(1) = {2}, totalLongPositions = {3}", longPrice, gridStep, MarketSeries.Close.Last(1), totalLongPositions);
}
}
if (totalLongPositions > 0)
{
if (MarketSeries.Close.Last(1) < longPrice - gridStep)
{
var orderVolume = volume * Math.Pow(GridsExponent, totalLongPositions);
var result = ExecuteMarketOrder(TradeType.Buy, Symbol, orderVolume, Instance);
if (result.IsSuccessful)
{
longPrice = result.Position.EntryPrice;
totalLongPositions = totalLongPositions + 1;
Print("Long Price = {0}, gridStep = {1}, MarketSeries.Close.Last(1) = {2}, totalLongPositions = {3}", longPrice, gridStep, MarketSeries.Close.Last(1), totalLongPositions);
}
}
}
}
}
#endregion
#region hedge difference
#region hedge with short
if (totalLongPositions - totalShortPositions >= 4)
{
var orderVolume = volume * Math.Pow(GridsExponent, totalShortPositions + 1);
var result = ExecuteMarketOrder(TradeType.Sell, Symbol, orderVolume, Instance, null, null);
if (result.IsSuccessful)
{
totalShortPositions = totalShortPositions + 1;
}
}
#endregion
#region hedge with long
if (totalShortPositions - totalLongPositions >= 4)
{
var orderVolume = volume * Math.Pow(GridsExponent, totalLongPositions + 1);
var result = ExecuteMarketOrder(TradeType.Buy, Symbol, orderVolume, Instance, null, null);
if (result.IsSuccessful)
{
totalLongPositions = totalLongPositions + 1;
}
}
#endregion
#endregion
#region close multiple position on opposite
#region close long
if (MarketSeries.High.Last(1) >= BB.Top.Last(1))
{
if (totalLongPositions > 0 && totalShortPositions == 0)
{
var positions = Positions.FindAll(Instance, Symbol, TradeType.Buy);
foreach (var position in positions)
{
if (position.NetProfit > 0)
{
ClosePositionAsync(position);
totalLongPositions = totalLongPositions - 1;
}
}
if (totalLongPositions == 0)
{
longPrice = 0;
}
longPrice = positions.OrderByDescending(x => x.Id).Last().EntryPrice;
}
}
#endregion
#region close short
if (MarketSeries.Low.Last(1) <= BB.Bottom.Last(1))
{
if (totalLongPositions == 0 && totalShortPositions > 0)
{
var positions = Positions.FindAll(Instance, Symbol, TradeType.Sell);
foreach (var position in positions)
{
if (position.NetProfit > 0)
{
ClosePositionAsync(position);
totalShortPositions = totalShortPositions - 1;
}
}
if (totalShortPositions == 0)
{
shortPrice = 0;
}
shortPrice = positions.OrderByDescending(x => x.Id).Last().EntryPrice;
}
}
#endregion
#endregion
#region close bunch of same positions
#region close longs
if (totalLongPositions > 0 && totalShortPositions == 0)
{
var positions = Positions.FindAll(Instance, Symbol, TradeType.Buy);
profit = 0;
foreach (var position in positions)
{
profit = profit + position.NetProfit;
}
if (profit > 0)
{
foreach (var position in positions)
{
ClosePositionAsync(position);
}
}
longPrice = 0;
}
#endregion
#region close shorts
if (totalShortPositions > 0 && totalLongPositions == 0)
{
var positions = Positions.FindAll(Instance, Symbol, TradeType.Sell);
profit = 0;
foreach (var position in positions)
{
profit = profit + position.NetProfit;
}
if (profit > 0)
{
foreach (var position in positions)
{
ClosePositionAsync(position);
}
}
shortPrice = 0;
}
#endregion
}
#endregion
#endregion
#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 = 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))
{
ClosePositionAsync(position);
}
Print("closed all");
equity = Account.Equity;
shortPrice = 0;
longPrice = 0;
}
}
}
#endregion
}
}
@alexander.n.fedorov
PanagiotisCharalampous
02 Oct 2018, 16:40
( Updated at: 21 Dec 2023, 09:20 )
Hi Sasha,
I backtested on Beta and I get the trade. See below
Do you backtest on the same dates?
Best Regards,
Panagiotis
@PanagiotisCharalampous
alexander.n.fedorov
02 Oct 2018, 16:46
Yes, that is the trade
I did not have, even I restarted computer a few times
I showed it to my daughter, we looked together, could not understand
May be I shall reinstall the Fondex
By the way, when the beta is avail on other brokers, is the data going to as limited as well?
Regards
Sasha
P.S. Good news anyway, I started to worry about my abilities
@alexander.n.fedorov
PanagiotisCharalampous
02 Oct 2018, 16:51
Sasha,
For which dates do you backtest? I will try on Fondex as well. cTrader 3.3 should be available sometime in October. However, I cannot commit on that. The data of each broker is independent to cTrader releases.
Best Regards,
Panagiotis
@PanagiotisCharalampous
alexander.n.fedorov
02 Oct 2018, 16:54
I backtest from the first Fondex tick date, which is the 29 th of November
But I recognize the trade, the candles
With you it worked OK
I needed this release, as I could make some very important improvements and philosophy.
I hope it is going to be a bomb!
@alexander.n.fedorov
alexander.n.fedorov
02 Oct 2018, 14:35
Please, help! what do I do wrong?
Regards,
Sasha
@alexander.n.fedorov