Simple MACD
Simple MACD
20 Sep 2014, 15:00
Hi There, i trying to build a simple cbot based on MACD. Basically, the cbot should open a buy position when the macd goes up through the signal line and vice versa. Proble is looks like the values of the MACD.Histogram i get are not the correct ones.
I have the following but it does open and close positions each time the macd goes through the signal line.
Thanks for ur help.
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.API.Requests;
using cAlgo.Indicators;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.FileSystem)]
public class MACD4 : Robot
{
private MacdHistogram _macd;
private Position _position;
[Parameter(DefaultValue = 10000, MinValue = 0)]
public int Volume { get; set; }
[Parameter("Period", DefaultValue = 7)]
public int Period { get; set; }
[Parameter("Long Cycle", DefaultValue = 26)]
public int LongCycle { get; set; }
[Parameter("Short Cycle", DefaultValue = 12)]
public int ShortCycle { get; set; }
protected override void OnStart()
{
_macd = Indicators.MacdHistogram(LongCycle, ShortCycle, Period);
}
protected override void OnBar()
{
if (Trade.IsExecuting)
{
return;
}
bool isLongPositionOpen = _position != null && _position.TradeType == TradeType.Buy;
bool isShortPositionOpen = _position != null && _position.TradeType == TradeType.Sell;
Print(_macd.Histogram.LastValue);
if (_macd.Histogram.LastValue > 0 && !isLongPositionOpen)
{
ClosePosition();
Buy();
}
if (_macd.Histogram.LastValue < 0 && !isShortPositionOpen)
{
ClosePosition();
Sell();
}
}
private void ClosePosition()
{
if (_position != null)
{
_position = null;
}
}
private void Buy()
{
Trade.CreateBuyMarketOrder(Symbol, Volume);
}
private void Sell()
{
Trade.CreateSellMarketOrder(Symbol, Volume);
}
}
}
Ymer
23 Sep 2014, 22:13
Hi there, is Spotware actually checking this thread? Hope i can get an answer to the above! Thanks
@Ymer