Topics
Replies
tonymcgarry
01 Feb 2023, 11:20
Here you go:
I maybe have made a noob mistake you can see immediately?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class Heikenashi : Robot
{
// public HeikenAshiCandle ha;
public string previousRunDirection;
// private double _tradePercentage = 0.1; // 10% of account balance
//private TimeFrame _timeFrame = TimeFrame.Hour;
public Symbol symbol;
public TimeFrame tf;
public class HeikenAshiCandle
{
public double Open { get; set; }
public double High { get; set; }
public double Low { get; set; }
public double Close { get; set; }
public string Direction { get; set; }
}
public HeikenAshiCandle CalculateHa(Bar bar, Bar previousBar)
{
Print("starting ha calc");
HeikenAshiCandle ha = new HeikenAshiCandle();
ha.Close = (bar.Open + bar.High + bar.Low + bar.Close) / 4;
ha.Open = (previousBar.Open + previousBar.Close) / 2;
ha.High = Math.Max(bar.High, Math.Max(ha.Open, ha.Close));
ha.Low = Math.Min(bar.Low, Math.Min(ha.Open, ha.Close));
Print("done ha calc");
if (ha.Close >= ha.Open && ha.Low >= ha.Open)
{
ha.Direction = "Bullish";
return ha;
}
if (ha.Close <= ha.Open && ha.High <= ha.Open)
{
ha.Direction = "Bearish";
return ha;
}
if (ha.Close > ha.Open)
{
ha.Direction = "Green Indecision";
return ha;
}
if (ha.Close < ha.Open)
{
ha.Direction = "Red Indecision";
return ha;
}
else
ha.Direction = "indecision";
return ha;
}
private Double GetStopLossPrice(Bars bar, String dir)
{
HeikenAshiCandle ha;
double sl = 0;
if (dir == "Short")
{
for (int i = 2; i < 7; i++)
{
ha = CalculateHa(bar.ElementAt(Bars.Count - i), bar.ElementAt(bar.Count - (i + 1)));
if (ha.High > sl)//first one will be
{
sl = ha.High;
}
else
{
return sl;
}
//if the high is lower than the previous sl then use the previous stoploss
}
}
if (dir == "Long")
{
sl = 9999999999;
for (int i = 2; i < 7; i++)
{
ha = CalculateHa(bar.ElementAt(Bars.Count - i), bar.ElementAt(bar.Count - (i + 1)));
if (ha.Low < sl)//first one will be
{
sl = ha.Low;
}
else
{
return sl;
}
}
}
return sl;
}
private double GetStopLoss(double currentPrice, double desiredStopLossPrice, int digits)
{
// Calculate the difference between the current price and desired stop loss price
double difference = currentPrice - desiredStopLossPrice;
// Multiply the difference by the power of 10 to convert it into pips
return Math.Round(difference * Math.Pow(10, digits), 1);
}
protected override void OnStart()
{
Print("Onstart start");
//get current symbol bot is attached to
symbol = Chart.Symbol;
Print("Symbol is ", symbol.Name);
tf = Chart.TimeFrame;
//find last direction - use this only once to define the last direction - -2 is the previous bar
//Print("bars count = ", Bars.Count);
//for (int i = Bars.Count - 1; i >= 1; i--)
//{
// var Ha = CalculateHa(Bars.ElementAt(i), Bars.ElementAt(i - 1));
// if (Ha.Direction == "Bullish")
// {
// previousRunDirection = "Bullish";
// break;
// }
// else if (Ha.Direction == "Bearish")
// {
// previousRunDirection = "Bearish";
// break;
// }
//}
Print("Onstart start end");
}
private void WaitForLowSpread()
{
Print("Digits is:", Chart.Symbol.Digits);
Print("Spread is currently: " + Chart.Symbol.Spread);
while (Chart.Symbol.Spread >= 1)
{
}
}
private int GetNumPositions()
{
int openPositionFound = 0;
foreach (Position p in Positions)
{
if (p.SymbolName == Chart.SymbolName && p.Label == tf.ToString())
openPositionFound += 1;
}
return openPositionFound;
}
public string GetEmaDirection()
{
var emaSlow = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 30);
var emaFast = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 10);
var emaDirection = emaFast.Result.LastValue > emaSlow.Result.LastValue ? "Bullish" : "Bearish";
return emaDirection;
}
public Dictionary<TimeFrame, TimeFrame> HeikenTimeframes = new()
{
{TimeFrame.HeikinMinute, TimeFrame.HeikinMinute5},
{TimeFrame.HeikinMinute5,TimeFrame.HeikinMinute15},
{TimeFrame.HeikinMinute15, TimeFrame.HeikinHour},
{TimeFrame.HeikinHour, TimeFrame.HeikinHour4},
{TimeFrame.HeikinHour4,TimeFrame.HeikinHour8 },
};
protected override void OnBar()//on a new bar
{ Print ("New Bar");
Print("Chart.TimeFrame=", Chart.TimeFrame);
Print("Parent TimeFrame=", HeikenTimeframes[Chart.TimeFrame]);
Print("Parent TimeFrame=", HeikenTimeframes[Chart.TimeFrame]);
Bars bars = MarketData.GetBars(Chart.TimeFrame);
Bars parentBars = MarketData.GetBars(HeikenTimeframes [Chart.TimeFrame]);
// Print("parentBars - ", parentBars.ToString());
var ha = CalculateHa(bars.ElementAt(Bars.Count -2),bars.ElementAt(bars.Count -3));
// Print("starting haParent calc");
// Print("parentBars.Count= ", parentBars.Count);
// Print("parentBars.Count - 2= ",parentBars.Count - 2);
// Print("parentBars.Count - 1= ", parentBars.Count - 1);
var parentHa = CalculateHa(parentBars.ElementAt(parentBars.Count - 1), parentBars.ElementAt(parentBars.Count - 2));
// Print("done ha calc");
Print("Direction of last bar = ",ha.Direction);
Print("Direction of Parent = ", parentHa.Direction);
Print("Direction of EMA = ",GetEmaDirection());
//look to close trade
int openPositionFound = GetNumPositions();
Print("Positions count = ", openPositionFound);
//look to close trade
if (openPositionFound > 0)
foreach (Position p in Positions )
{
if (p.SymbolName == Chart.SymbolName && ha.Direction == "Bullish" && p.TradeType == TradeType.Sell && p.Label == tf.ToString() && p.NetProfit>0)
p.Close();
if (p.SymbolName == Chart.SymbolName && ha.Direction == "Bearish" && p.TradeType == TradeType.Buy && p.Label == tf.ToString() && p.NetProfit > 0)
p.Close();
openPositionFound = GetNumPositions();
}
//open new trade
//if no positions open...then see if previous bars' directiion warrants a new trade
if (openPositionFound == 0)
{
if ((parentHa.Direction == "Bullish" || parentHa.Direction == "Green Indecision") && ha.Direction == "Bullish" && GetEmaDirection()=="Bullish")
{
double sl = GetStopLossPrice(Bars,"Long");
double pipsSl = GetStopLoss(Symbol.Ask, sl, Symbol.Digits);
//open sell order
Print("Opening buy Order sl price/pips= ", sl.ToString(),"/",pipsSl.ToString());
ExecuteMarketOrder(TradeType.Buy, SymbolName, 50000, tf.ToString(), pipsSl,0);
}
if ((parentHa.Direction == "Bearish" || parentHa.Direction == "Red Indecision") && ha.Direction == "Bearish" && GetEmaDirection() == "Bearish")
{
double sl = GetStopLossPrice(Bars,"Short");
double pipsSl = GetStopLoss(Symbol.Ask, sl, Symbol.Digits);
Print("Opening buy Order sl/pips= ", sl.ToString(), "/", pipsSl.ToString());
ExecuteMarketOrder(TradeType.Sell, SymbolName, 50000, tf.ToString(), pipsSl, 0);
}
else
{
Print("no new signal detected");
}
}
}
protected override void OnTick()
{
// Handle price updates here
}
protected override void OnStop()
{
// Handle cBot stop here
}
}
}
@tonymcgarry
tonymcgarry
06 Feb 2023, 20:15
RE:
Spotware said:
Hi, I have done some more tests with another laptop (inferior I5, 4gb ram) and the application does not freeze with this.
My more powerful newer i7 with 32gb ram freezes every 20 seconds even without cbots running or even with the market open suggesting it's the application config or something I can't debug - is there anything I can do to diagnose this problem?
Thanks,
@tonymcgarry