PH
Topics
12 Jun 2014, 09:27
5641
6
Replies
phajvaj
12 Jun 2014, 09:42
cBot Scalper sample
it running
// -------------------------------------------------------------------------------
//
// This is a Template used as a guideline to build your own Robot.
// Please use the “Feedback” tab to provide us with your suggestions about cAlgo’s API.
//
// -------------------------------------------------------------------------------
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using System.Collections.Generic;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class Scalper : Robot
{
[Parameter("BarCount", DefaultValue = 4)]
public int BarCount { get; set; }
[Parameter("MaxOrders", DefaultValue = 10)]
public int MaxOrders { get; set; }
[Parameter("TakeProfit", DefaultValue = 100)]
public int TakeProfit { get; set; }
[Parameter("StopLoss", DefaultValue = 100)]
public int StopLoss { get; set; }
[Parameter("Volume", DefaultValue = 10000)]
public int Volume { get; set; }
[Parameter("MaxDropDown", DefaultValue = 0)]
public double MaxDropDown { get; set; }
[Parameter("MaxProfit", DefaultValue = 0)]
public double MaxProfit { get; set; }
private int PosOpen = 0;
private int OpenIndex = 0;
private double StartBalanse;
private DateTime dt;
protected override void OnStart()
{
StartBalanse = Account.Balance;
dt = Server.Time;
}
protected override void OnTick()
{
if (Account.Balance <= 0)
Stop();
int last = MarketSeries.Close.Count - 1;
if (!(MarketSeries.Open[last] == MarketSeries.High[last] && MarketSeries.Open[last] == MarketSeries.Low[last]))
return;
if (dt.Date != Server.Time.Date)
{
StartBalanse = Account.Balance;
dt = Server.Time;
}
double bp = (StartBalanse - Account.Balance) / (StartBalanse / 100);
if (bp > 0 && bp >= MaxDropDown && MaxDropDown != 0)
return;
if (bp < 0 && Math.Abs(bp) >= MaxProfit && MaxProfit != 0)
return;
if (BarCount < 1)
{
Print("Мало баров для анализа тренда. BarCount должно быть больше или равно 1");
return;
}
if (PosOpen < MaxOrders)
{
if (OpenIndex == 0 || last - OpenIndex > BarCount)
{
if (IsBuy(last))
{
Trade.CreateBuyMarketOrder(Symbol, Volume);
PosOpen++;
OpenIndex = last;
}
if (IsSell(last))
{
Trade.CreateSellMarketOrder(Symbol, Volume);
PosOpen++;
OpenIndex = last;
}
}
}
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
protected override void OnPositionOpened(Position openedPosition)
{
if (openedPosition.TradeType == TradeType.Buy)
Trade.ModifyPosition(openedPosition, Symbol.Ask - StopLoss * Symbol.PointSize, Symbol.Ask + TakeProfit * Symbol.PointSize);
if (openedPosition.TradeType == TradeType.Sell)
Trade.ModifyPosition(openedPosition, Symbol.Bid + StopLoss * Symbol.PointSize, Symbol.Bid - TakeProfit * Symbol.PointSize);
}
protected override void OnPositionClosed(Position position)
{
PosOpen--;
if (PosOpen < 0)
PosOpen = 0;
}
private bool IsBuy(int last)
{
for (int i = BarCount; i > 0; i--)
{
if (MarketSeries.Open[last - i] < MarketSeries.Close[last - i])
return false;
if (i < 2)
continue;
if (MarketSeries.High[last - i] > MarketSeries.High[last - i - 1])
return false;
}
return true;
}
private bool IsSell(int last)
{
for (int i = BarCount; i > 0; i--)
{
if (MarketSeries.Open[last - i] > MarketSeries.Close[last - i])
return false;
if (i < 2)
continue;
if (MarketSeries.Low[last - i] < MarketSeries.Low[last - i - 1])
return false;
}
return true;
}
}
}
@phajvaj
phajvaj
12 Jun 2014, 09:49
I understand.
Thanks.
Help with code samples get histoey order in label and symbol
/* // A cBot IngDoi from Hmong Thai // Programming by. Phaj Vaj {sAcIw} // Create by. 08-06-14 // Modify by. 11-06-14 // Version 0.0.1 */ using System; using cAlgo.API; using cAlgo.API.Indicators; using System.Collections.Generic; namespace cAlgo.Robots { [Robot(AccessRights = AccessRights.None)] public class aCbotINgDOi : Robot { [Parameter("หมายเลขบอท :", DefaultValue = 1801, MinValue = 0)] public int magics { get; set; } [Parameter("จำนวนวอลุ่ม :", DefaultValue = 1000, MinValue = 1000)] public int InitialVolume { get; set; } [Parameter("จำนวนที่ต้องการคูณ :", DefaultValue = 2, MaxValue = 10, MinValue = 1)] public int multiply { get; set; } [Parameter("จำกัดวอลุ่ม :")] public bool useLimitValume { get; set; } [Parameter("วอลุ่มสูงสุด :", DefaultValue = 5000000, MinValue = 1)] public int LimitVolume { get; set; } [Parameter("SL(pips) ? :", DefaultValue = 35)] public int StopLoss { get; set; } [Parameter("TP(pips) ? :", DefaultValue = 80)] public int TakeProfit { get; set; } [Parameter("ใช้ Tralling :")] public bool useTralling { get; set; } [Parameter("ระยะห่างจากSL(pips) :", DefaultValue = 20.0)] public int Tralling { get; set; } [Parameter("เคลื่อนSL(pips) :", DefaultValue = 10.0)] public int init_StopLoss { get; set; } private Random random = new Random(); private const string botname = "bOt-iNgDOi_"; private const int BUY = 1; private const int SELL = -1; private const int HOLD = 0; private const int BLE = 2; public string cBotname; public bool entryTrade = false; public int countTrade = 0; protected override void OnStart() { cBotname = botname + magics; } protected override void OnTick() { if (Account.Balance <= 0) Stop(); if (Trade.IsExecuting) return; //multiply order //get signal to trade if (Positions.Count == 0 && !entryTrade) { var signal = get_signal(); if (signal == BUY) ExecuteOrder(InitialVolume, TradeType.Buy); if (signal == SELL) ExecuteOrder(InitialVolume, TradeType.Sell); } //Tralling Stop if (useTralling && Positions.Count > 0) get_TrallingStop(); } //End OnTick private void ExecuteOrder(long volume, TradeType tradeType) { var result = ExecuteMarketOrder(tradeType, Symbol, volume, cBotname, StopLoss, TakeProfit); if (result.Error == ErrorCode.NoMoney) { Print("ERROR : ", result.Error); Stop(); } if (result.IsSuccessful) { var position = result.Position; } } protected override void OnPositionClosed(Position position) { Print("Profit : {0}", position.GrossProfit); var volume = (long)position.Volume * multiply; if (position.Label != cBotname || position.SymbolCode != Symbol.Code) return; if (position.GrossProfit < 0) { if (useLimitValume && volume > LimitVolume) volume = LimitVolume; ExecuteOrder(volume, position.TradeType); entryTrade = true; } else entryTrade = false; } private int get_signal() { double close2 = MarketSeries.Close[2]; double close1 = MarketSeries.Close[1]; if (close2 > close1 && random.Next(2) != 0) return (SELL); if (close2 < close1 && random.Next(2) == 0) return BUY; return HOLD; } //CODE Tralling Stop private void get_TrallingStop() { foreach (var position in Positions) { if (Symbol.Code != position.SymbolCode || position.Label != botname) continue; //set initial sotploss if (position.TradeType == TradeType.Sell && position.StopLoss == null) ModifyPosition(position, position.EntryPrice + init_StopLoss * Symbol.PipSize, null); if (position.TradeType == TradeType.Buy && position.StopLoss == null) ModifyPosition(position, position.EntryPrice - init_StopLoss * Symbol.PipSize, null); //tralling stop if (position.GrossProfit <= 0) continue; if (position.TradeType == TradeType.Sell) { double profit1 = position.GrossProfit + position.Commissions - Tralling * 10 * position.Volume / 100000.0; if (profit1 > 0.0 && position.TradeType == TradeType.Sell) { double? TrallingPrice = Symbol.Bid + Tralling * Symbol.PipSize; if (Tralling != 0 && TrallingPrice < position.StopLoss && TrallingPrice < position.EntryPrice) { if (TrallingPrice - Symbol.Bid > 0) { ModifyPosition(position, TrallingPrice, position.TakeProfit); Print("Tralling Stop OK!"); } } } } else { double profit2 = position.GrossProfit + position.Commissions - Tralling * 10 * position.Volume / 100000.0; if (profit2 > 0.0 && position.TradeType == TradeType.Buy) { double? TrallingPrice = Symbol.Ask - Tralling * Symbol.PipSize; if (Tralling != 0 && TrallingPrice > position.StopLoss && TrallingPrice > position.EntryPrice) if (TrallingPrice - Symbol.Ask < 0) { ModifyPosition(position, TrallingPrice, position.TakeProfit); Print("Tralling Stop OK!"); } } } } } //END CODE Tralling Stop } }@phajvaj