Help referencing S/R indicator to buy / sell
Help referencing S/R indicator to buy / sell
06 Sep 2015, 17:51
Hi,
I am not a coding, but am learning bits as I go.
I'm struggling with the following...
Referencing "SinewaveSupportResistance" indicator to buy on or below support and sell on or above resistance..
Im fairly close, but was hoping someone would be able to help...
The output for the inidcator is "Support" and "Resistance" and another "Alpha"
Here is my hashed together code, would really appreciate a hand as I can hopefully use this test as a stepping stone to understanding more.
What I need;
Support.LastResult <= Market.Price = Buy
Resistance >= Market.Price = Sell
Thanks for any help!
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class ElConejo : Robot { [Parameter(DefaultValue = "HMAMACDRSI")] public string cBotLabel { get; set; } [Parameter("Quantity (Lots)", DefaultValue = 0.1, MinValue = 0.01, Step = 0.01)] public double Quantity { get; set; } [Parameter("Stop Loss (pips)", DefaultValue = 100)] public int StopLoss { get; set; } [Parameter("Take Profit (pips)", DefaultValue = 100)] public int TakeProfit { get; set; } [Parameter("Trigger (pips)", DefaultValue = 10)] public int Trigger { get; set; } [Parameter("Trailing Stop (pips)", DefaultValue = 10)] public int TrailingStop { get; set; } [Parameter("MinBalance", DefaultValue = 5000)] public double MinBalance { get; set; } [Parameter("MinLoss", DefaultValue = -200.0)] public double MinLoss { get; set; } [Parameter(DefaultValue = 3)] public int MaxPositions { get; set; } [Parameter("Source")] public DataSeries Source { get; set; } [Parameter(DefaultValue = false)] public bool EnableBreakEven { get; set; } [Parameter(DefaultValue = 10, MinValue = 0, Step = 1)] public double BreakEvenPips { get; set; } [Parameter(DefaultValue = 20, MinValue = 0, Step = 1)] public double BreakEvenGain { get; set; } private SinewaveSupportResistance _SSR; private const string label = "El Conjeco"; protected override void OnStart() { _SSR = Indicators.GetIndicator<SinewaveSupportResistance>(MarketSeries); Positions.Opened += PositionsOnOpened; Positions.Closed += PositionsOnClosed; } protected override void OnTick() { var cBotPositions = Positions.FindAll(cBotLabel); if (cBotPositions.Length > MaxPositions) return; var longPosition = Positions.Find(label, Symbol, TradeType.Buy); var shortPosition = Positions.Find(label, Symbol, TradeType.Sell); if (MarketSeries.Close.LastValue <= _SSR.Support.LastValue && longPosition == null) { if (shortPosition != null) ClosePosition(shortPosition); ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, label, StopLoss, TakeProfit); } else if (MarketSeries.Close.LastValue >= _SSR.Resistance.LastValue && shortPosition == null) { if (longPosition != null) ClosePosition(longPosition); ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label, StopLoss, TakeProfit); // Some condition to close all positions if (Account.Balance < MinBalance) foreach (var position in cBotPositions) ClosePosition(position); // Some condition to close one position foreach (var position in cBotPositions) if (position.GrossProfit < MinLoss) ClosePosition(position); // Trailing Stop for all positions SetTrailingStop(); } } private void PositionsOnOpened(PositionOpenedEventArgs obj) { Position openedPosition = obj.Position; if (openedPosition.Label != label) return; Print("position opened at {0}", openedPosition.EntryPrice); } private void PositionsOnClosed(PositionClosedEventArgs obj) { Position closedPosition = obj.Position; if (closedPosition.Label != label) return; Print("position closed with {0} gross profit", closedPosition.GrossProfit); } /// <summary> /// When the profit in pips is above or equal to Trigger the stop loss will start trailing the spot price. /// TrailingStop defines the number of pips the Stop Loss trails the spot price by. /// If Trigger is 0 trailing will begin immediately. /// </summary> private void SetTrailingStop() { var sellPositions = Positions.FindAll(cBotLabel, Symbol, TradeType.Sell); foreach (Position position in sellPositions) { double distance = position.EntryPrice - Symbol.Ask; if (distance < Trigger * Symbol.PipSize) continue; double newStopLossPrice = Symbol.Ask + TrailingStop * Symbol.PipSize; if (position.StopLoss == null || newStopLossPrice < position.StopLoss) ModifyPosition(position, newStopLossPrice, position.TakeProfit); } var buyPositions = Positions.FindAll(cBotLabel, Symbol, TradeType.Buy); foreach (Position position in buyPositions) { double distance = Symbol.Bid - position.EntryPrice; if (distance < Trigger * Symbol.PipSize) continue; double newStopLossPrice = Symbol.Bid - TrailingStop * Symbol.PipSize; if (position.StopLoss == null || newStopLossPrice > position.StopLoss) ModifyPosition(position, newStopLossPrice, position.TakeProfit); } } private long VolumeInUnits { get { return Symbol.QuantityToVolume(Quantity); } } } }
Replies
ColossusFX
06 Sep 2015, 20:08
RE:
Paul_Hayes said:
just a quick guess, but do you not need the current high and low price and not the closing price; have you tried:-
MarketSeries.High.LastValue MarketSeries.Low.LastValue
Thanks for the reply Paul,
As I said, I am not a coder, but from what I've read I need the implement marketseries somehow, and then reference Support.LastValue and Resistance.Lastvalue
If possible could you explain how to include this?
I am a new noob, so if its too much then dont worry :)
@ColossusFX
ClickAlgo
07 Sep 2015, 14:02
hi,
You are not referencing the custom indicator correctly, these are the user defined input parameters for the SinewaveSupportResistance Indicator:-
[Parameter(DefaultValue = 0.07)] public double Alpha { get; set; }
You are declaring your reference with no parameters; you just needed to include the Alpha value;
_SSR = Indicators.GetIndicator<SinewaveSupportResistance>(MarketSeries, 0.07);
Also try replacing the closing price with current high or low price:-
if (MarketSeries.Low.LastValue <= _SSR.Support.LastValue && longPosition == null)
maybe you want the closing price I don't know, but you are testing the conditions every tick instead of every closing bar price.
@ClickAlgo
ColossusFX
07 Sep 2015, 17:37
Awesome, will give it a go in a bit, thanks for the help Paul.
@ColossusFX
ColossusFX
07 Sep 2015, 17:45
Yep, all working now! Going to implement this with some other strategies now.
Thanks again.
@ColossusFX
ryanoia@gmail.com
15 Apr 2019, 19:59
buying at support and selling on resistance
Hello Mr. Lime,
I noticed that your code is similar to what I want to achieve, except I'll get the support/resistance levels from this indicator - https://ctrader.com/algos/indicators/show/1467
Would you know how to code the bot so that it references those levels in that indicator?
Your assistance would be great appreciated.
Thanks much.
@ryanoia@gmail.com
ClickAlgo
06 Sep 2015, 19:06
just a quick guess, but do you not need the current high and low price and not the closing price; have you tried:-
@ClickAlgo