Information
Username: | Sendgate |
Member since: | 27 Sep 2020 |
Last login: | 06 Oct 2024 |
Status: | Active |
Activity
Where | Created | Comments |
---|---|---|
Algorithms | 0 | 2 |
Forum Topics | 0 | 3 |
Jobs | 0 | 0 |
Last Algorithm Comments
This is a start:
Cheers,
--
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.FullAccess)]
public class NewcBot : Robot
{
[Parameter("Volume (Lots)", DefaultValue = 0.01)]
public double VolumeInLots { get; set; }
public ExponentialMovingAverage slow_ema;
public ExponentialMovingAverage standart_ema;
public ExponentialMovingAverage fast_ema;
protected override void OnStart()
{
slow_ema = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 200);
standart_ema = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 14);
fast_ema = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 5);
}
protected override void OnBar()
{
bool buy_case = fast_ema.Result.LastValue > slow_ema.Result.LastValue;
bool sell_case = fast_ema.Result.LastValue < slow_ema.Result.LastValue;
bool buy_order = fast_ema.Result.LastValue > standart_ema.Result.LastValue;
bool sell_order = fast_ema.Result.LastValue < standart_ema.Result.LastValue;
if (buy_case && buy_order)
{
if (!IsBuyPositionOpen(TradeType.Buy))
ExecuteMarketOrder(TradeType.Buy, SymbolName, 1000, "order_buy");
}
else if (sell_case && sell_order)
{
if (!IsSellPositionOpen(TradeType.Sell))
ExecuteMarketOrder(TradeType.Sell, SymbolName, 1000, "order_sell");
}
bool close_buy = standart_ema.Result.LastValue > fast_ema.Result.LastValue;
bool close_sell = standart_ema.Result.LastValue < fast_ema.Result.LastValue;
var buy_position = Positions.Find("order_buy", SymbolName, TradeType.Buy);
var sell_position = Positions.Find("order_sell", SymbolName, TradeType.Sell);
if (close_buy)
ClosePosition(buy_position);
else if (close_sell)
ClosePosition(sell_position);
}
private bool IsBuyPositionOpen(TradeType type)
{
return Positions.FindAll("order_buy", SymbolName, type).Length > 0;
}
private bool IsSellPositionOpen(TradeType type)
{
return Positions.FindAll("order_sell", SymbolName, type).Length > 0;
}
}
}
Hi,
Well it worked for me, opened and closed a position. Backtesting it would not do any good cause it only shows you how to open and close a position.
Use the code I gave you and try it.
If you want to see what it does you need to run it in Visual Studio.
You can read about the steps here:
https://ctrader.info/d/442-how-to-debug-a-cbot-using-visual-studio-2022
Cheers