Second run, works different
23 Mar 2017, 06:54
Hey, this is not just an specific code. It's a lot of code that I build and they work different in the second backtesting or the second time that I start a cBot.
The most common thing is with the Print, after the first run, there is not a single Print (unless I call it from the main class).
Check this out..
while (!(start <= price && price < end))
{
start = Math.Round(start + directed, Symbol.Digits);
end = Math.Round(start + Step, Symbol.Digits);
}
After a while... start instead of having a normal value, ends up being 114.10000000001 instead of 114.1
And god knows why, (i have this problem since yesterday) yesterday.. the Print didn't show all those 0 and the 1 at the end...
So "price < end", wasn't working properly... and now... I have to use Round all the time.
Maybe is because I'm using a .dll... I don't know this is really weird..
Here is the entire code..
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.API.Requests;
using cAlgo.Library.Extensions;
using cAlgo.Library.Variables;
using System;
using System.Collections.Generic;
using System.Linq;
namespace cAlgo.Library.Business
{
public class Grid
{
private string label = cBot.Label;
private string symbolCode = cBot.Symbol.Code;
private Symbol Symbol = cBot.Symbol;
private Positions Positions = cBot.Positions;
private PendingOrders PendingOrders = cBot.PendingOrders;
private double Step = 2;
private double InitialAsk = cBot.Symbol.Ask;
private double InitialBid = cBot.Symbol.Bid;
public bool FreeAsk
{
get
{
double[] range = GetRange(Symbol.Ask, ref InitialAsk);
return CheckRange(range[0], range[1]);
}
}
public bool FreeBid
{
get
{
double[] range = GetRange(Symbol.Bid, ref InitialBid);
return CheckRange(range[0], range[1]);
}
}
public Grid(string label, string symbolCode)
{
this.label = label;
this.symbolCode = symbolCode;
Symbol = symbolCode.ToSymbol();
}
public void StepPips(double stepPips)
{
Step = Math.Round(stepPips * Symbol.PipSize, Symbol.Digits);
InitialAsk = Math.Round(Symbol.Ask - (Symbol.Ask % Step), Symbol.Digits);
InitialBid = Math.Round(Symbol.Bid - (Symbol.Bid % Step), Symbol.Digits);
}
private double[] GetRange(double price, ref double initial)
{
double start = initial;
double end = start + Step;
double directed = (price >= end) ? Step : -Step;
while (!(start <= price && price < end))
{
start = Math.Round(start + directed, Symbol.Digits);
end = Math.Round(start + Step, Symbol.Digits);
}
initial = start;
return new double[] { start, end };
}
private bool CheckRange(double min, double max)
{
min = Math.Round(min, Symbol.Digits);
max = Math.Round(max, Symbol.Digits);
var positions = Positions.Where(p => p.Label == label && p.SymbolCode == symbolCode);
var pendingOrders = PendingOrders.Where(p => p.Label == label && p.SymbolCode == symbolCode);
positions = positions.Where(p => min <= p.EntryPrice && p.EntryPrice < max);
pendingOrders = pendingOrders.Where(p => min <= p.TargetPrice && p.TargetPrice < max);
return (positions.Count() + pendingOrders.Count() == 0);
}
}
}
The thing is......... that when I make a second backtesting, without any change.. the results change, the Grid is not respected anymore.
Thanks for your time and patience.

fermjy
23 Mar 2017, 19:47
Solved
Ok.. after........ a while... ripping out my code... I found the problem
I still can't believe it...
I start all my bots with..
namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class NameOfTheBot: MyRobot {MyRobot.cs
namespace cAlgo.Library { public class MyRobot : Robot { private string label = "MyRobot"; private Grid grid; private MarketAction marketAction; private MarketStatus marketStatus; public string Label { get { return label; } set { label = value; } } public Grid Grid { get { return grid; } } public MarketAction MarketAction { get { return marketAction; } } public MarketStatus MarketStatus { get { return marketStatus; } } public MyRobot() { // Put your initialization logic here cBot.setInstance(this); // so far.. so good }cBot.cs
namespace cAlgo.Library { public static class cBot { private static MyRobot robot = null; public static MyRobot Robot { get { return robot; } } .... public static void setInstance(MyRobot robot) { //if (cBot.robot == null) <---- * cBot.robot = robot; } }* Well.. i putted that condition, just in case in the future the DLL gets bigger... so.. I just tested without that condition.... all is working fine now....................
I have no idea why that condition gives issues when I execute the code after the first time... If some one is so kind to explain it, I would appreciate it. Because It might happen again.
:S
Thanks for your time and patience.
@fermjy