Thursday, May 3, 2018

extended script

using System;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;
using Community;

namespace Channels
{
class StrategyBasic : WealthScript
{
//Strategy Parameters - GLobal
StrategyParameter _periodFast;
StrategyParameter _periodSlow;

//Strategy Parameters - Constructor
public StrategyBasic()
{
_periodFast = CreateParameter("Period Fast", 50, 0, 100, 1);
_periodSlow = CreateParameter("Period SLow", 50, 0, 100, 1);
}

       
//Strategy Variables
bool enterLong;
bool enterShort;
bool exitLong;
bool exitShort;

//The Main Method
protected override void Execute()
{
//Technical Settings
ClearDebug();
HideVolume();
int firstValidValue = 0;

//Bringing in the Parameters
int periodFast = _periodFast.ValueInt;
int periodSlow = _periodSlow.ValueInt;

//Indicators
#region Indicators
DataSeries emaFast =EMA.Series(Close, periodFast, EMACalculation.Modern);
firstValidValue = Math.Max(firstValidValue, periodFast * 3);
PlotSeries(PricePane, emaFast, Color.Red, LineStyle.Solid, 2);

DataSeries emaSlow = EMA.Series(Close, periodSlow, EMACalculation.Modern);
firstValidValue = Math.Max(firstValidValue, periodSlow * 3);
PlotSeries(PricePane, emaSlow, Color.Blue, LineStyle.Solid, 2);

//Indicators on Other Panes
ChartPane rsiPane = new ChartPane();
rsiPane = CreatePane(20, true, true);
DataSeries rsi = RSI.Series(Close, 10);
PlotSeries(rsiPane, rsi, Color.Blue, LineStyle.Solid, 2);
DrawHorizontalBand(rsiPane, Color.Red, Color.Yellow, LineStyle.Solid, 2, 0, 25);
DrawHorzLine(rsiPane, 75, Color.Red, LineStyle.Solid, 2);

#endregion

for (int bar = firstValidValue; bar < Bars.Count; bar++)
{
//the rules
enterLong = CrossOver(bar, emaFast, emaSlow);
//extra rules added with &=
enterLong &= rsi[bar] < 70;

enterShort = CrossUnder(bar, emaFast, emaSlow);
enterShort &= rsi[bar] > 30;

exitLong = Bars.Close[bar] < emaFast[bar];
exitShort = Bars.Close[bar] > emaFast[bar];

//if we have a position
if (ActivePositions.Count>0)
{
//if the open position is LONG
if (LastActivePosition.PositionType==PositionType.Long)
{
if (exitLong)
{
ExitAtMarket(bar + 1, LastActivePosition, "Long Stop");
}
}

//if the open position is SHORT
else if (LastActivePosition.PositionType == PositionType.Short)
{
if (exitShort)
{
ExitAtMarket(bar + 1, LastActivePosition, "Short Stop");                         
}
}
}

//if we don't have the position
else if (ActivePositions.Count == 0)
{
double limitOrder = (Bars.High[bar]+Bars.Low[bar])/2;

if (enterLong)
{
//BuyAtMarket(bar + 1, "Long");
BuyAtLimit(bar + 1, limitOrder, "Long Limit");

DrawCircle(PricePane, 2, bar, limitOrder, Color.Blue, LineStyle.Solid, 3, false);

if (ActivePositions.Count==0)
{
DrawCircle(PricePane, 2, bar+1, limitOrder, Color.Blue, LineStyle.Solid, 3, false);
AnnotateBar("NO ENTRY", bar + 1, true, Color.Red, Color.Black);
}
}
if (enterShort)
{
//ShortAtMarket(bar + 1, "Short");
ShortAtLimit(bar + 1, limitOrder, "Short Limit");

DrawCircle(PricePane, 2, bar, limitOrder, Color.Blue, LineStyle.Solid, 3, false);

if (ActivePositions.Count == 0)
{
DrawCircle(PricePane, 2, bar + 1, limitOrder, Color.Blue, LineStyle.Solid, 3, false);
AnnotateBar("NO ENTRY", bar + 1, true, Color.Red, Color.Black);
}
}
}
}

}
}
}

No comments:

Post a Comment