Help me plz, sl of Artificial Intelligence ROBOT dosnt work!
Help me plz, sl of Artificial Intelligence ROBOT dosnt work!
07 Oct 2013, 15:53
Hello
StopLoss of Artificial Intelligence ROBOT (/algos/robots/show/46) dos not work.
it has error.
could anybody repair it?!
thanks alot.
khorshidi.
Replies
Spotware
08 Oct 2013, 11:30
( Updated at: 21 Dec 2023, 09:20 )
RE:
hosseinkhorshidi said:
Mr Spotware or Mr algotrader or a programmer person,(I dont know all!), could edit above robot?
it is NECESSARY for me
thank you
Have you experienced the issue in backtesting?
What are the input parameters you have used?
Can you send a screenshot such as the following showing that the stop loss is not added properly on position modified, for the backtest?
@Spotware
khorshidi07
08 Oct 2013, 12:44
thank you Mr spotware...
when a position opened, Sl failed like below image.
@khorshidi07
khorshidi07
08 Oct 2013, 16:31
before modified, it has problem too.
i dont modified too much.
i modified a litle according to site of MQ4.
i send you last modified code.
using System; using cAlgo.API; using cAlgo.API.Indicators; namespace cAlgo.Robots { [Robot("ArtificialIntelligence")] public class ArtificialIntelligence : Robot { [Parameter(DefaultValue = "ArtificialIntelligence")] public string LabelName { get; set; } [Parameter("x1", DefaultValue = 120)] public int x1 { get; set; } [Parameter("x2", DefaultValue = 120)] public int x2 { get; set; } [Parameter("x3", DefaultValue = 19)] public int x3 { get; set; } [Parameter("x4", DefaultValue = 100)] public int x4 { get; set; } [Parameter("FastMA", DefaultValue = 12)] public int FastMA { get; set; } [Parameter("SlowMA", DefaultValue = 26)] public int SlowMA { get; set; } [Parameter("Step", DefaultValue = 9)] public int Step { get; set; } [Parameter("StopLoss", DefaultValue = 200)] public int StopLoss { get; set; } [Parameter("Volume", DefaultValue = 1)] public int Volume { get; set; } private MacdHistogram macd; private Position pos; private bool IsPosOpen = false; private double sl; protected override void OnStart() { macd = Indicators.MacdHistogram(SlowMA, FastMA, 9); } protected override void OnTick() { int last = MarketSeries.Close.Count - 1; if (!(MarketSeries.Open[last] == MarketSeries.High[last] && MarketSeries.Open[last] == MarketSeries.Low[last])) return; double per = percertron(); if (!IsPosOpen) { if (per > 0) { Trade.CreateBuyMarketOrder(Symbol, Volume); IsPosOpen = true; } if (per < 0) { Trade.CreateSellMarketOrder(Symbol, Volume); IsPosOpen = true; } } else { if (pos.TradeType == TradeType.Buy && per < 0) { Trade.Close(pos); Trade.CreateSellMarketOrder(Symbol, Volume); return; } else { if (Symbol.Ask > sl + StopLoss * 2 * Symbol.PointSize) Trade.ModifyPosition(pos, Symbol.Ask - StopLoss * Symbol.PointSize, 0); } if (pos.TradeType == TradeType.Sell && per > 0) { Trade.Close(pos); Trade.CreateBuyMarketOrder(Symbol, Volume); } else { if (Symbol.Bid < sl - StopLoss * 2 * Symbol.PointSize) Trade.ModifyPosition(pos, Symbol.Bid + StopLoss * Symbol.PointSize, 0); } } } protected override void OnStop() { // Put your deinitialization logic here } protected override void OnPositionOpened(Position openedPosition) { pos = openedPosition; if (openedPosition.TradeType == TradeType.Buy) { sl = Symbol.Ask - StopLoss * Symbol.PointSize; Trade.ModifyPosition(openedPosition, sl, 0); } if (openedPosition.TradeType == TradeType.Sell) { sl = Symbol.Bid + StopLoss * Symbol.PointSize; Trade.ModifyPosition(openedPosition, sl, 0); } } private double percertron() { int last = MarketSeries.Close.Count - 1; double w1 = x1 - 100; double w2 = x2 - 100; double w3 = x3 - 100; double w4 = x4 - 100; double a1 = macd.Histogram[last - 1]; double a2 = macd.Histogram[last - 1 - Step]; double a3 = macd.Histogram[last - 1 - Step * 2]; double a4 = macd.Histogram[last - 1 - Step * 3]; return w1 * a1 + w2 * a2 + w3 * a3 + w4 * a4; } } }
@khorshidi07
khorshidi07
08 Oct 2013, 16:36
code of EA MQ4:
http://www.earnforex.com/mt4-expert-advisors/ArtificialIntelligence.mq4
i think this EA is one of the best EA!
@khorshidi07
khorshidi07
09 Oct 2013, 11:12
UNFORTUNATELY my programing is very bad and i couldnt edit robot!
if i want, my position have takprofir and stoploss in above robot, i write :
[Parameter("TakeProfit", DefaultValue = 200)] public int TakeProfit { get; set; }
above the :
[Parameter("StopLoss", DefaultValue = 200)] public int StopLoss { get; set; }
and i must what do i do else?!
if it is possible, you edit all of robot and insert here, plz!
thanks alot Mr spotware
@khorshidi07
virtuesoft
09 Oct 2013, 17:34
RE:
Replace all of the code in OnPositionOpened with the code below...
protected override void OnPositionOpened(Position openedPosition) { pos = openedPosition; IsPosOpen = true; if (openedPosition.TradeType == TradeType.Buy) { sl = Symbol.Ask - StopLoss * Symbol.PointSize; Trade.ModifyPosition(openedPosition, sl, null); } if (openedPosition.TradeType == TradeType.Sell) { sl = Symbol.Bid + StopLoss * Symbol.PointSize; Trade.ModifyPosition(openedPosition, sl, null); } }
hosseinkhorshidi said:
UNFORTUNATELY my programing is very bad and i couldnt edit robot!
if i want, my position have takprofir and stoploss in above robot, i write :
[Parameter("TakeProfit", DefaultValue = 200)] public int TakeProfit { get; set; }above the :
[Parameter("StopLoss", DefaultValue = 200)] public int StopLoss { get; set; }and i must what do i do else?!
if it is possible, you edit all of robot and insert here, plz!
thanks alot Mr spotware
@virtuesoft
Spotware
09 Oct 2013, 17:53
If you need to set the take profit at the OnPositionOpened event:
protected override void OnPositionOpened(Position openedPosition) { pos = openedPosition; if (openedPosition.TradeType == TradeType.Buy) { sl = Symbol.Ask - StopLoss*Symbol.PointSize; var tp = Symbol.Ask + TakeProfit * Symbol.PointSize; Trade.ModifyPosition(openedPosition, sl, tp); } else { sl = Symbol.Bid + StopLoss*Symbol.PointSize; var tp = Symbol.Bid - TakeProfit * Symbol.PointSize; Trade.ModifyPosition(openedPosition, sl, tp); } }
If you also need to set the take profit in the OnTick event when the position is modified, please provide the logic.
@Spotware
khorshidi07
07 Oct 2013, 17:28
Mr Spotware or Mr algotrader or a programmer person,(I dont know all!), could edit above robot?
it is NECESSARY for me
thank you
@khorshidi07