Tuesday, May 22, 2018

candle stick

using System;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;
using System.Collections.Generic;

namespace ChannelSystems
{
public class TurnAroundPatternA:WealthScript
{

StrategyParameter _tailSizeCoef;
StrategyParameter _profitCoef;

public TurnAroundPatternA()
{
_tailSizeCoef = CreateParameter("Tail Coef", 2, 1.5, 3, 0.1);
_profitCoef = CreateParameter("Profit Coef", 1, 2, 3, 0.5);
}

protected override void Execute()
{

ClearDebug();
PlotStops();

double tailSizeCoef = _tailSizeCoef.Value;
double profitCoef = _profitCoef.Value;

bool enterLong;
bool enterShort;
double stop=0;
double profit=0;

//double bodySize;
//double tailLowSize;
//double tailHighSize;

//bool bullCandle;
//bool bearCandle;


for (int bar = 1; bar < Bars.Count; bar++)
{
//bullCandle = Bars.Close[bar] > Bars.Open[bar];
//bearCandle= Bars.Close[bar] < Bars.Open[bar];

//if (bullCandle)
//{
//    bodySize = Bars.Close[bar] - Bars.Open[bar];
//    tailLowSize = Bars.Open[bar] - Bars.Low[bar];
//    tailHighSize= Bars.High[bar] - Bars.Close[bar];
//}

//if (bearCandle)
//{
//    bodySize = Bars.Open[bar] - Bars.Close[bar];
//    tailLowSize = Bars.Close[bar] - Bars.Low[bar];
//    tailHighSize = Bars.High[bar] - Bars.Open[bar];
//}

//long
enterLong = Bars.Open[bar-1] - Bars.Low[bar-1] > (Bars.Close[bar-1] - Bars.Open[bar-1]) * tailSizeCoef; //tail size low
enterLong &= Bars.High[bar - 1] - Bars.Close[bar - 1] < Bars.Close[bar-1] - Bars.Open[bar-1]; //tail size high
enterLong &= Bars.Close[bar] > Bars.Open[bar]; //has to be bullish
enterLong &= Bars.Close[bar] > Bars.High[bar-1]; //has to close higher than the previous high

//short
enterShort = Bars.High[bar-1] - Bars.Open[bar-1] > (Bars.Open[bar-1] - Bars.Close[bar-1]) * tailSizeCoef; //tail high
enterShort &= Bars.Close[bar - 1] - Bars.Low[bar - 1] < Bars.Open[bar-1] - Bars.Close[bar-1]; // tail low
enterShort &= Bars.Close[bar] < Bars.Open[bar];
enterShort &= Bars.Close[bar] < Bars.Low[bar-1];

if (ActivePositions.Count>0)
{
if (LastActivePosition.PositionType==PositionType.Long)
{
if (bar == LastActivePosition.EntryBar)
{
stop = Bars.Low[bar - 2];
profit = LastActivePosition.EntryPrice + (LastActivePosition.EntryPrice-stop) * profitCoef;
}

if (!ExitAtStop(bar+1,LastActivePosition,stop,"Stop Long"))
{
ExitAtLimit(bar + 1, LastActivePosition, profit, "Profit Long");
}

}
else if (LastActivePosition.PositionType == PositionType.Short)
{
if (bar == LastActivePosition.EntryBar)
{
stop = Bars.High[bar - 2];
profit = LastActivePosition.EntryPrice - (stop- LastActivePosition.EntryPrice) * profitCoef;
}

if (!ExitAtStop(bar + 1, LastActivePosition, stop, "Stop Short"))
{
ExitAtLimit(bar + 1, LastActivePosition, profit, "Profit Short");
}
}
}

else if (ActivePositions.Count==0)
{
if (enterLong)
{
BuyAtLimit(bar + 1, Bars.Close[bar], "Long");
AnnotateBar("bar1", bar - 1, true, Color.Red, Color.Black);
}
if (enterShort)
{
ShortAtLimit(bar + 1, Bars.Close[bar], "Short");
AnnotateBar("bar1", bar - 1, true, Color.Red, Color.Black);
}
}


}
}
}
}

No comments:

Post a Comment